<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.timer.api.dao.yggl.YgglMainEmpMapper">

    <resultMap id="BaseResultMap" type="cn.timer.api.bean.yggl.YgglMainEmp" >
        	<id column="id" property="id" />
                <result column="emp_num" property="empNum" />
                <result column="phone" property="phone" />
                <result column="password" property="password" />
                <result column="head_url" property="headUrl" />
                <result column="name" property="name" />
                <result column="english_name" property="englishName" />
                <result column="sex" property="sex" />
                <result column="zj_type" property="zjType" />
                <result column="zj_num" property="zjNum" />
                <result column="sfzyx_time" property="sfzyxTime" />
                <result column="birthday" property="birthday" />
                <result column="age" property="age" />
                <result column="is_married" property="isMarried" />
                <result column="is_pregnant" property="isPregnant" />
                <result column="area" property="area" />
                <result column="mz" property="mz" />
                <result column="zzmm" property="zzmm" />
                <result column="jg" property="jg" />
                <result column="city" property="city" />
                <result column="hk_type" property="hkType" />
                <result column="hk_address" property="hkAddress" />
                <result column="qq" property="qq" />
                <result column="wechat" property="wechat" />
                <result column="email" property="email" />
                <result column="blood_type" property="bloodType" />
                <result column="language" property="language" />
                <result column="edu" property="edu" />
                <result column="zy" property="zy" />
                <result column="zz_remark" property="zzRemark" />
                <result column="job_type" property="jobType" />
                <result column="job_status" property="jobStatus" />
                <result column="rz_time" property="rzTime" />
                <result column="syq" property="syq" />
                <result column="zz_time" property="zzTime" />
                <result column="sjzz_time" property="sjzzTime" />
                <result column="is_jrkq" property="isJrkq" />
                <result column="job_num" property="jobNum" />
                <result column="work_address" property="workAddress" />
                <result column="work_phone" property="workPhone" />
                <result column="work_email" property="workEmail" />
                <result column="zpqd" property="zpqd" />
                <result column="bmgw_id" property="bmgwId" />
                <result column="bmgw_name" property="bmgwName" />
                <result column="custom_num" property="customNum" />

                <result column="org_code" property="orgCode" />
                <result column="unionid" property="unionid" />
                <result column="openid" property="openid" />
                <result column="mpopenid" property="mpopenid" />
                <result column="appopenid" property="appopenid" />
				<result column="is_insure" property="isInsure"/>
    </resultMap>

   <resultMap id="Bintu" type="cn.timer.api.dto.yggl.YgbintuDto" >
   		<id column="id" property="id" />
   			<result column="age" property="age" />
   			<result column="bmgwId" property="bmgwId" />
   			<result column="gw" property="gw" />
   			<result column="bm" property="bm" />
   			<result column="proname" property="proname" />
   			<result column="eduname" property="eduname" />
   			<result column="jobStatus" property="jobStatus" />
   			<result column="workage" property="workage" />
   </resultMap>

   <resultMap id="DepartmentStaffMap" type="cn.timer.api.dto.zzgl.DepartmentStaffDto" >
   			<id column="id" property="id" />
   			<result column="empNum" property="empNum" />
   			<result column="name" property="name" />
   			<result column="phone" property="phone" />
   			<result column="bmgwname" property="bmgwname" />
   </resultMap>

    <sql id="Base_Column_List">
                id,
                emp_num,
                phone,
                password,
                head_url,
                name,
                english_name,
                sex,
                zj_type,
                zj_num,
                sfzyx_time,
                birthday,
                age,
                is_married,
                is_pregnant,
                area,
                mz,
                zzmm,
                jg,
                city,
                hk_type,
                hk_address,
                qq,
                wechat,
                email,
                blood_type,
                language,
                edu,
                zy,
                zz_remark,
                job_type,
                job_status,
                rz_time,
                syq,
                zz_time,
                sjzz_time,
                is_jrkq,
                job_num,
                work_address,
                work_phone,
                work_email,
                zpqd,
                bmgw_id,
                org_code,
                unionid,
                openid,
                mpopenid,
                appopenid,
                custom_num,
				is_insure
    </sql>

    <sql id="Base_Column_List_a">
                a.id,
                a.emp_num,
                a.phone,
                a.head_url,
                a.name,
                a.english_name,
                a.sex,
                a.birthday,
                a.jg,
                a.job_type,
                a.job_status,
                a.rz_time,
                a.bmgw_id,
                a.org_code,
                a.zj_type,
                a.zj_num,
                a.syq,
                a.zz_time,
                a.custom_num,
				a.is_insure

    </sql>

     <!-- rsybp人事仪表盘 -->
    <select id="rsybp" resultMap="Bintu">
    	SELECT e.id AS id,IFNULL(e.age,0) AS age,IFNULL(e.bmgw_id,0) AS bmgwId,IFNULL(b.`name`,'null') AS gw,
    	IFNULL(bb.`name`,'null') AS bm,IFNULL( p.`name`,'null') AS proname,IFNULL(d.`name` ,'null')AS eduname,
    	IFNULL(e.job_status,0) AS jobStatus,IFNULL(datediff(CURDATE(),e.rz_time),0)AS workage
		FROM yggl_main_emp e
		LEFT JOIN zzgl_bmgw_m b ON b.id = e.bmgw_id
		LEFT JOIN zzgl_bmgw_m bb ON bb.id = b.up_id
		LEFT JOIN province_class p ON  left (e.`jg`,2) = p.province
		LEFT JOIN education_class d ON d.number = e.edu
		WHERE e.org_code = #{orgCode}
    </select>

    <select id="DepartmentStaff" resultMap="DepartmentStaffMap">
    	select em.emp_num as empNum,
    		   em.`name` as empname,
    		   bm.`name` as bmgwname,
    		   em.phone
			from yggl_main_emp em
			LEFT JOIN zzgl_bmgw_m as bm on bm.id = em.bmgw_id
			where em.org_code = #{orgcode}
			and em.bmgw_id in (select bm.id from zzgl_bmgw_m bm
			WHERE bm.up_id = #{upid})
    </select>

    <select id="selectME" resultMap="BaseResultMap">
        SELECT
        	<include refid="Base_Column_List_a" />,
        	(SELECT name FROM zzgl_bmgw_m WHERE id = (SELECT up_id FROM zzgl_bmgw_m WHERE id = a.bmgw_id limit 1) limit 1) as bmgw_name
        FROM yggl_main_emp a
        <where>
        	AND a.org_code = #{param.orgCode}
        	AND a.job_status != 3
        	<if test="param.jobType != null">
        		AND a.job_type = #{param.jobType}
        	</if>
        	<if test="param.jobStatus != null">
        		AND a.job_status = #{param.jobStatus}
        	</if>
        	<if test="param.query != null and param.query != ''">
        		AND (
        			a.name like CONCAT('%',#{param.query},'%') or
        			a.phone like CONCAT('%',#{param.query},'%')
        		)
        	</if>
        	<if test="param.empNums != null and param.empNums.size() > 0">
        		<choose>
					<when test="param.empNums.size() > 1">
						AND a.emp_num IN
						(
						<foreach item="it" index="index" collection="param.empNums" separator="," close="" >
				        	#{it}
						</foreach>
						)
					</when>
					<otherwise>
						AND a.emp_num = #{param.empNums[0]}
					</otherwise>
				</choose>
        	</if>
        </where>
    </select>

    <!-- benyueLz本月离职人数 -->
    <select id="benyueLz" resultType="Integer">
		SELECT COUNT(datediff(CURDATE(),l.sjlz_time)) FROM yggl_main_lzb l WHERE datediff(CURDATE(),l.sjlz_time) <![CDATA[ <= ]]> 30 AND org_code = #{orgCode};
    </select>

     <!-- benyueZz本月转正人数 -->
    <select id="benyueZz" resultType="Integer">
    	SELECT COUNT(datediff(CURDATE(),e.sjzz_time)) FROM yggl_main_emp e WHERE datediff(CURDATE(),e.sjzz_time) <![CDATA[ <= ]]> 30 AND org_code = #{orgCode};
    </select>

    <!-- 查询员工信息 搜索 分页 -->
	<select id="queryEmpMessage" resultType="cn.timer.api.dto.yggl.YgQueryDto">
    	SELECT
    	    a.id,
			a.name empName,
			a.emp_num empNum,
			b.name deptName,
			a.rz_time rzTime,
			a.job_type jobType,
			a.phone phone,
			a.job_status jobStatus,
			a.head_url headUrl,
			a.sex sex,
    	    a.is_insure isInsure,
    	    a.zj_num zjNum
		FROM
			yggl_main_emp a
		LEFT JOIN zzgl_bmgw_m b ON a.bmgw_id = b.id
		WHERE
			a.org_code = #{orgCode}
		AND
			a.job_status in (0,1,2)
    	<if test="isInsure!=null and isInsure != ''">
    	AND	a.is_insure in (1,2)
		</if>
		ORDER BY
			emp_num DESC
    </select>

	<!-- 查询在职员工(试用员工人数+正式员工人数) -->
	<select id="queryInservice" resultType="cn.timer.api.dto.yggl.YgglCartogramDto">
    	SELECT
			SUM(job_status=1) AS probation,
			SUM(job_status=2) AS regular
		FROM
			yggl_main_emp
		WHERE
			org_code=#{orgCode}
    </select>

    <!-- 查询当月入职人数 -->
    <select id="queryInduction" resultType="cn.timer.api.dto.yggl.YgglCartogramDto">
    	SELECT
			EXTRACT(MONTH FROM rz_time) AS month,
			COUNT(emp_num) induction
		FROM
			yggl_main_emp
		WHERE
			org_code=#{orgCode}
    </select>


    <!-- 查询本月转正人数  -->
    <select id="queryPositive" resultType="cn.timer.api.dto.yggl.YgglCartogramDto">
		SELECT
			EXTRACT(MONTH FROM sjzz_time) AS month,
			COUNT(emp_num) positive
		FROM
			yggl_main_emp
		WHERE
			org_code=#{orgCode}
	</select>

	<!-- 查询员工平均年龄  -->
    <select id="queryAvgAge" resultType="cn.timer.api.dto.yggl.YgglCartogramDto">
		SELECT
			avg(age) avgAge
		FROM
			yggl_main_emp
		WHERE
			org_code=#{orgCode}
	</select>

	<!-- 查询岗位上级部门id及名称及人数  -->
    <select id="queryDepartmentNum" resultType="cn.timer.api.dto.yggl.YgglCartogramDto">
		SELECT
			IFNULL(c.name,'null') updeptname,
			IFNULL(b.up_id,0) updeptid,
			count(1) updeptnum
		FROM
			yggl_main_emp y
		LEFT JOIN zzgl_bmgw_m b ON y.bmgw_id = b.id
		LEFT JOIN zzgl_bmgw_m c ON c.id = b.up_id
		WHERE
			y.org_code = #{orgCode}
		GROUP BY
			c.name,
			b.up_id
	</select>

	<!-- 查询岗位名称及人数 -->
	<select id="queryJobsNum" resultType="cn.timer.api.dto.yggl.YgglCartogramDto">
		SELECT
			IFNULL(b.name,'null') jobname,
			IFNULL(y.bmgw_id,0) jobid,
			count(1) jobnum
		FROM
			yggl_main_emp y
		LEFT JOIN zzgl_bmgw_m b ON y.bmgw_id = b.id
		WHERE
			y.org_code = #{orgCode}
		GROUP BY
			y.bmgw_id,
			b.name;
	</select>

	<!-- 学历及人数查询 -->
	<select id="queryEducation" resultType="cn.timer.api.dto.yggl.YgglCartogramDto">
		SELECT
			IFNULL(e.name,'null') education,count(y.edu) educationNum
		FROM
			yggl_main_emp y
		LEFT JOIN
			education_class e
		ON
			y.edu = e.id
		WHERE
			org_code = #{orgCode}
		GROUP BY
			e.name
		ORDER BY
			e.id ASC
	</select>

	<!-- 员工年龄分布查询 -->
	<select id="queryEmpAge" resultType="cn.timer.api.dto.yggl.YgglCartogramDto">
		<![CDATA[
		SELECT
			SUM((CASE WHEN age < 0.5 THEN 1 ELSE 0 END)) AS lowerEighteen,
			SUM((CASE WHEN age >= 18 AND age < 25 THEN 1 ELSE 0 END)) AS eighteenToTwentyfive,
			SUM((CASE WHEN age >= 25 AND age < 32 THEN 1 ELSE 0 END)) AS twentyfiveToThirtytwo,
			SUM((CASE WHEN age >= 32 AND age < 38 THEN 1 ELSE 0 END)) AS thirtytwoToThirtyeight,
			SUM((CASE WHEN age >= 38 AND age < 45 THEN 1 ELSE 0 END)) AS thirtyeightToFortyfive,
			SUM((CASE WHEN age >=45 THEN 1 ELSE 0 END)) AS higherFortyfive
		FROM
			yggl_main_emp
		WHERE
			org_code = #{orgCode}
			]]>
	</select>

	<!-- 籍贯统计(省份)查询 -->
	<select id="queryNativeplace" resultType="cn.timer.api.dto.yggl.YgglCartogramDto">
		SELECT
			IFNULL(c.city_name,'null') province,count(y.jg_id) provinceNum
		FROM
			yggl_main_emp y
		LEFT JOIN
			common_area c
		ON
			y.jg_id = c.code_id
		WHERE
			org_code = #{orgCode}
		GROUP BY
			c.city_name
	</select>

	<!-- 员工工龄分布查询 -->
	<select id="queryWorkingAge" resultType="cn.timer.api.dto.yggl.YgglCartogramDto">
	<![CDATA[
		SELECT
			SUM((CASE WHEN TIMESTAMPDIFF(day, rz_time,now())/365 < 0.5 THEN 1 ELSE 0 END)) AS lowerHalfyear,
			SUM((CASE WHEN TIMESTAMPDIFF(day, rz_time,now())/365 >= 0.5 AND TIMESTAMPDIFF(day, rz_time,now())/365 < 1 THEN 1 ELSE 0 END)) AS halfYear,
			SUM((CASE WHEN TIMESTAMPDIFF(day, rz_time,now())/365 >= 1 AND TIMESTAMPDIFF(day, rz_time,now())/365 < 1.5 THEN 1 ELSE 0 END)) AS oneYear,
			SUM((CASE WHEN TIMESTAMPDIFF(day, rz_time,now())/365 >= 1.5 AND TIMESTAMPDIFF(day, rz_time,now())/365 < 2 THEN 1 ELSE 0 END)) AS onehalfYear,
			SUM((CASE WHEN TIMESTAMPDIFF(day, rz_time,now())/365 >= 2 AND TIMESTAMPDIFF(day, rz_time,now())/365 < 2.5 THEN 1 ELSE 0 END)) AS twoYear,
			SUM((CASE WHEN TIMESTAMPDIFF(day, rz_time,now())/365 >=2.5 THEN 1 ELSE 0 END)) AS higherTwohalfYear
		FROM
			yggl_main_emp
		WHERE
			org_code = #{orgCode}
			]]>
	</select>

	<!--根据id数组查询员工-->
	<select id="selectListByIds" resultType="cn.timer.api.bean.yggl.YgglMainEmp">
		SELECT
			<include refid="Base_Column_List"/>
		FROM yggl_main_emp
		WHERE id IN
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</select>

	<select id="selectListByIZjNum" resultType="cn.timer.api.bean.yggl.YgglMainEmp">
		SELECT
		<include refid="Base_Column_List"/>
		FROM yggl_main_emp
		WHERE org_code = #{orgCode} and zj_num IN
		<foreach item="zjnum" collection="array" open="(" separator="," close=")">
			#{zjnum}
		</foreach>
	</select>
</mapper>