<?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="vip.xiaonuo.hr.modular.baseemployeeinfo.mapper.HrBaseEmployeeInfoMapper">

    <select id="listByEoucation" resultType="vip.xiaonuo.hr.modular.baseemployeeinfo.entity.vo.EoucationVo">
        SELECT b.NAME departmentName ,COUNT(a.ID) countNum FROM `hr_base_employee_info` a JOIN `hr_base_department` b ON
        a.DEPARTMENT_ID = b.ID
        where
        <if test="orgId!=null and orgId!=''">
            a.ORG_ID = #{orgId} AND
        </if>
        a.USER_TYPE =0 AND a.EDUCATION >2 GROUP BY a.DEPARTMENT_ID
        order by count(a.ID)
    </select>
    <select id="listByDepart" resultType="vip.xiaonuo.hr.modular.baseemployeeinfo.entity.vo.EoucationVo">
        SELECT
        b.NAME AS departmentName,
        COUNT(a.ID) AS countNum
        FROM
        `hr_base_department` b
        LEFT JOIN
        `hr_base_employee_info` a
        ON
        b.ID = a.DEPARTMENT_ID
        <if test="orgId!=null and orgId!=''">
            AND a.ORG_ID = #{orgId}
        </if>
        AND a.USER_TYPE = 0
        GROUP BY
        b.NAME;
    </select>
    <select id="listByDepartGender" resultType="vip.xiaonuo.hr.modular.baseemployeeinfo.entity.vo.EoucationVo">
        SELECT
        b.NAME AS departmentName ,SUM(CASE WHEN a.GENDER = 0 AND a.USER_TYPE = 0 THEN 1 ELSE 0 END) AS maleNum,
        SUM(CASE WHEN a.GENDER = 1 AND a.USER_TYPE = 0 THEN 1 ELSE 0 END) AS femaleNum
        FROM
        `hr_base_department` b
        LEFT JOIN
        `hr_base_employee_info` a ON a.DEPARTMENT_ID = b.ID
        <if test="orgId!=null and orgId!=''">
         WHERE   a.ORG_ID = #{orgId}
        </if>
        GROUP BY a.DEPARTMENT_ID
    </select>
    <select id="listByDepartGenderEducationDis"
            resultType="vip.xiaonuo.hr.modular.baseemployeeinfo.entity.vo.EoucationVo">
        SELECT
        b.NAME AS departmentName ,
        SUM(CASE WHEN a.EDUCATION = 0 THEN 1 ELSE 0 END) AS priNum,
        SUM(CASE WHEN a.EDUCATION = 1 THEN 1 ELSE 0 END) AS midNum,
        SUM(CASE WHEN a.EDUCATION = 2 THEN 1 ELSE 0 END) AS highNum,
        SUM(CASE WHEN a.EDUCATION = 3 THEN 1 ELSE 0 END) AS underNum,
        SUM(CASE WHEN a.EDUCATION = 4 THEN 1 ELSE 0 END) AS postNum,
        SUM(CASE WHEN a.EDUCATION = 5 THEN 1 ELSE 0 END) AS phdNum
        FROM
        `hr_base_department` b
        LEFT JOIN
        `hr_base_employee_info` a
        ON
        b.ID = a.DEPARTMENT_ID
        <if test="orgId!=null and orgId!=''">
            AND a.ORG_ID = #{orgId}
        </if>
        AND a.USER_TYPE = 0
        GROUP BY
        b.NAME;

    </select>


    <select id="birthDis" resultType="vip.xiaonuo.hr.modular.baseemployeeinfo.entity.vo.PerNum">
        SELECT
        CONCAT(months.month_num,"月") AS `name`,
        COUNT(b.BIRTHDAY) AS `value`
        FROM
        (SELECT 1 AS month_num UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL
        SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION
        ALL SELECT 12) AS months
        LEFT JOIN
        `hr_base_employee_info` b ON DATE_FORMAT(b.BIRTHDAY, '%m') = months.month_num
        <if test="orgId!=null and orgId!=''">
            AND b.ORG_ID = #{orgId}
        </if>
        AND b.USER_TYPE = 0
        GROUP BY
        months.month_num
        ORDER BY
        months.month_num;
    </select>
    <select id="siLingDis" resultType="vip.xiaonuo.hr.modular.baseemployeeinfo.entity.vo.PerNum">
        SELECT
        IFNULL(SUM(CASE WHEN a.SI_LING BETWEEN 0 AND 3 THEN 1 ELSE 0 END),'0') AS 'one',
        IFNULL(SUM(CASE WHEN a.SI_LING BETWEEN 4 AND 5 THEN 1 ELSE 0 END),'0') AS 'five',
        IFNULL(SUM(CASE WHEN a.SI_LING BETWEEN 6 AND 10 THEN 1 ELSE 0 END),'0') AS 'ten',
        IFNULL(SUM(CASE WHEN a.SI_LING BETWEEN 11 AND 15 THEN 1 ELSE 0 END),'0') AS 'oneFive',
        IFNULL(SUM(CASE WHEN a.SI_LING BETWEEN 16 AND 20 THEN 1 ELSE 0 END),'0') AS 'twoZ',
        IFNULL(SUM(CASE WHEN a.SI_LING BETWEEN 21 AND 25 THEN 1 ELSE 0 END),'0') AS 'twoFive',
        IFNULL(SUM(CASE WHEN a.SI_LING > 26 THEN 1 ELSE 0 END),'0') AS 'threeZ'
        FROM
        `hr_base_employee_info` a
        WHERE
        <if test="orgId!=null and orgId!=''">
            a.ORG_ID = #{orgId} AND
        </if>
        a.USER_TYPE = 0
    </select>
    <select id="gongLingDis" resultType="vip.xiaonuo.hr.modular.baseemployeeinfo.entity.vo.PerNum">
        select
        IFNULL(SUM(CASE WHEN SENIORITY BETWEEN 0 AND 1 THEN 1 ELSE 0 END),'0') AS '1年',
        IFNULL(SUM(CASE WHEN SENIORITY BETWEEN 2 AND 5 THEN 1 ELSE 0 END),'0') AS '5年',
        IFNULL(SUM(CASE WHEN SENIORITY BETWEEN 6 AND 10 THEN 1 ELSE 0 END),'0') AS '10年',
        IFNULL(SUM(CASE WHEN SENIORITY BETWEEN 11 AND 15 THEN 1 ELSE 0 END),'0') AS '15年',
        IFNULL(SUM(CASE WHEN SENIORITY BETWEEN 16 AND 20 THEN 1 ELSE 0 END),'0') AS '20年',
        IFNULL(SUM(CASE WHEN SENIORITY BETWEEN 21 AND 25 THEN 1 ELSE 0 END),'0') AS '25年',
        IFNULL(SUM(CASE WHEN SENIORITY BETWEEN 26 AND 30 THEN 1 ELSE 0 END),'0') AS '30年'
        FROM
        `hr_base_employee_info`
        WHERE
        <if test="orgId!=null and orgId!=''">
            ORG_ID = #{orgId} AND
        </if>
        USER_TYPE = 0
    </select>
    <select id="zhiBiao" resultType="vip.xiaonuo.hr.modular.baseemployeeinfo.entity.vo.DongShiCountNum">
              SELECT MAX(HR_NUM) jobNum ,
              ROUND(IFNULL(SUM(PAY), 0)/10000 , 2)   pay  ,
              ROUND(IFNULL(AVG(OUTPUT_VALUE), 0)/10000 , 2)  outputValue FROM `hr_count`   ${ew.customSqlSegment}

    </select>
    <select id="yuanGongNumMonth" resultType="vip.xiaonuo.hr.modular.baseemployeeinfo.entity.vo.DongShiCountNum">
     SELECT MAX(HR_NUM) jobNum ,DATE_FORMAT(CREATE_TIME , '%Y-%m') ymonth      FROM `hr_count`  ${ew.customSqlSegment}

    </select>
    <select id="yuanGongNumYear" resultType="vip.xiaonuo.hr.modular.baseemployeeinfo.entity.vo.DongShiCountNum">
            SELECT MAX(HR_NUM)  jobNum ,DATE_FORMAT(CREATE_TIME , '%Y') ymonth      FROM `hr_count` ${ew.customSqlSegment}
    </select>
</mapper>