<?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="jnpf.personnels.mapper.FtbPersonnelsOverviewAnalysisMapper">

    <select id="employeesEmployedInTheMonth" resultType="java.math.BigDecimal">
        SELECT
        COUNT(*)
        FROM
        ftb_personnels_staff_roster a
        WHERE
        <include refid="commonCondition"/>
    </select>

    <sql id="commonCondition">
        a.F_WorkerStatus IN
        <foreach collection="workStatus" open="(" close=")" separator="," item="statuss">
            #{statuss}
        </foreach>
        <if test="userIds != null and userIds.size() != 0">
            and a.F_UserId IN
            <foreach collection="userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        AND a.F_EnabledMark = 0
        AND DATE_FORMAT(a.F_ActualStartDate, '%Y-%m') &lt;= #{month}
    </sql>

    <select id="numberOfDaysOfAge" resultType="java.math.BigDecimal">
        SELECT
        IFNULL(SUM(DATEDIFF(CURDATE(),F_ActualStartDate)),0)
        FROM
        ftb_personnels_staff_roster a WHERE a.F_ActualStartDate IS NOT NULL
        and
        <include refid="commonCondition"/>
    </select>

    <select id="employeeAge" resultType="java.lang.String">
        SELECT
        da.F_Value
        FROM
        ftb_personnels_staff_roster AS a
        JOIN ftb_personnels_staff_registration_form_data AS da ON a.F_Id = da.F_RosterId
        WHERE
        da.F_FormFieldId = 'birthday'
        and
        <include refid="commonCondition"/>
        GROUP BY a.F_UserId
    </select>

    <select id="genderDistribution" resultType="java.math.BigDecimal">
        SELECT
        COUNT(*)
        FROM
        ftb_personnels_staff_roster AS a
        JOIN ftb_personnels_staff_registration_form_data AS da ON a.F_Id = da.F_RosterId
        WHERE
        da.F_Value = #{sex}
        AND da.F_FormFieldId = 'workerSex'
        AND <include refid="commonCondition"/>
    </select>

    <select id="employeeStatusProportion" resultType="jnpf.model.personnels.vo.analysis.PersonnelsOverviewProportionVO">
        SELECT
        COUNT(*) as `data`,
        a.F_WorkerStatus as dataName
        FROM
        ftb_personnels_staff_roster a
        WHERE
        a.F_WorkerStatus not in ('301','305')
        and a.F_UserId IN
        <foreach collection="userIds" open="(" close=")" separator="," item="userId">
            #{userId}
        </foreach>
        AND a.F_EnabledMark = 0
        AND DATE_FORMAT(a.F_ActualStartDate, '%Y-%m') &lt;= #{month}
        GROUP BY
        a.F_WorkerStatus
    </select>

    <select id="proportionOfEmploymentTypes"
            resultType="jnpf.model.personnels.vo.analysis.PersonnelsOverviewProportionVO">
        SELECT
        COUNT(*) as `data`,
        a.F_WorkerType as dataName
        FROM
        ftb_personnels_staff_roster a
        WHERE
        <include refid="commonCondition"/>
        GROUP BY
        a.F_WorkerType
    </select>

    <select id="educationalBackground" resultType="jnpf.model.personnels.vo.analysis.PersonnelsOverviewProportionVO">
        SELECT
        COUNT(*) AS `data`,
        da.F_Value AS dataName
        FROM
        ftb_personnels_staff_roster AS a
        JOIN ftb_personnels_staff_registration_form_data AS da ON a.F_Id = da.F_RosterId
        WHERE
        da.F_FormFieldId = 'education' AND da.F_Value != ''
        AND <include refid="commonCondition"/>
        GROUP BY
        da.F_Value
    </select>

    <select id="ageDistribution" resultType="jnpf.model.personnels.vo.analysis.PersonnelsOverviewProportionVO">
        SELECT
        DATEDIFF(CURDATE(),F_ActualStartDate ) as `data`
        FROM
        ftb_personnels_staff_roster a
        WHERE
        a.F_ActualStartDate IS NOT NULL
        AND <include refid = "commonCondition" />
    </select>

    <select id="ageDistributionProportion"
            resultType="jnpf.model.personnels.vo.analysis.PersonnelsOverviewProportionVO">
        SELECT
        da.F_Value as dataName
        FROM
        ftb_personnels_staff_roster AS a
        JOIN ftb_personnels_staff_registration_form_data AS da ON a.F_Id = da.F_RosterId
        WHERE
        da.F_FormFieldId = 'birthday'
        and
        <include refid="commonCondition"/>

        GROUP BY a.F_Id
    </select>

    <select id="departmentHeadcountRatio" resultType="java.lang.String">
        SELECT
        F_UserId
        FROM
        ftb_personnels_staff_roster a
        WHERE
        <include refid="commonCondition"/>
    </select>

    <select id="seniorityDistribution" resultType="java.math.BigDecimal">
        SELECT
        ROUND(( DATEDIFF( NOW(), da.F_Value ) / 365 ),2) AS dayDiff
        FROM
        ftb_personnels_staff_roster AS a
        INNER JOIN `ftb_personnels_staff_registration_form_data` AS da ON a.F_Id = da.F_RosterId
        <where>
            da.`F_FormFieldId` = 'firstWorkDate'
            AND da.F_Value != ''
            AND  <include refid="commonCondition"/>
        </where>
        GROUP BY
        a.F_Id
    </select>

    <select id="queryTheNumberOfNewcomers" resultType="java.math.BigDecimal">
        SELECT
        COUNT(*)
        FROM
        (
        SELECT
        DATEDIFF( NOW(), F_ActualStartDate ) AS dayDiff
        FROM
        ftb_personnels_staff_roster AS a
        <where>
            <if test="userIds != null and userIds.size() != 0">
                and a.F_UserId IN
                <foreach collection="userIds" open="(" close=")" separator="," item="userId">
                    #{userId}
                </foreach>
            </if>
            AND a.F_EnabledMark = 0
            AND DATE_FORMAT(a.F_ActualStartDate, '%Y-%m') &lt;= #{endMonth}
        </where>
        ) a
        WHERE
        a.dayDiff &lt;= #{timeRule}
    </select>
    <select id="queryTheNumberOfNewcomersList" resultType="jnpf.model.personnels.po.FtbPersonnelsStaffEmploymentApply">
        SELECT
        distinct  a.userId AS userId,
        a.actualStartDate AS actualStartDate
        FROM
        (
        SELECT
        DATEDIFF( NOW(), F_ActualStartDate ) AS dayDiff,
        a.F_UserId AS userId,
        a.F_ActualStartDate as actualStartDate
        FROM
        ftb_personnels_staff_roster AS a
        <where>
            <if test="userIds != null and userIds.size() != 0">
                and a.F_UserId IN
                <foreach collection="userIds" open="(" close=")" separator="," item="userId">
                    #{userId}
                </foreach>
            </if>
            AND a.F_EnabledMark = 0
            AND DATE_FORMAT(a.F_ActualStartDate, '%Y-%m') &lt;= #{endMonth}
        </where>
        ) a
        WHERE
        a.dayDiff &lt;= #{timeRule}
    </select>
    <select id="insurancePurchaseAnalysis"
            resultType="jnpf.model.personnels.vo.analysis.PersonnelsOverviewProportionVO">
        SELECT
        COUNT(*) AS `data`,
        da.F_Value AS dataName
        FROM
        ftb_personnels_staff_roster AS a
        JOIN ftb_personnels_staff_registration_form_data AS da ON a.F_Id = da.F_RosterId
        WHERE
        da.F_FormFieldId = #{formFieldId} AND da.F_Value != ''
        AND <include refid="commonCondition"/>
        GROUP BY
        da.F_Value
    </select>

    <select id="queryTheNewcomerID" resultType="java.lang.String">
        SELECT
        a.userId
        FROM
        (
        SELECT
        DATEDIFF( NOW(), a.F_ActualStartDate ) AS dayDiff,
        a.F_UserId as userId
        FROM
        ftb_personnels_staff_roster AS a
        WHERE
        <if test="userIds != null and userIds.size() != 0">
             a.F_UserId IN
            <foreach collection="userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        AND a.F_EnabledMark = 0
        AND DATE_FORMAT(a.F_ActualStartDate, '%Y-%m') &lt;= #{endMonth}
        ) a
        WHERE
        a.dayDiff  &lt;= #{timeRule}
    </select>

    <select id="genderDistributionList"
            resultType="jnpf.model.personnels.vo.analysis.PersonnelsOverviewProportionDetailsVO">
        SELECT
        da.F_Value as gender,
        a.F_SystemWokerId as employeeId,
        a.F_UserId as userId
        FROM
        ftb_personnels_staff_roster AS a
        JOIN ftb_personnels_staff_registration_form_data AS da ON a.F_Id = da.F_RosterId
        WHERE da.F_FormFieldId = 'workerSex' and da.F_Value != '' and da.F_Value in ('1','2')
        AND <include refid="commonCondition"/>
        group by a.F_Id
    </select>

    <select id="employeeStatusProportionList"
            resultType="jnpf.model.personnels.vo.analysis.PersonnelsOverviewSituationDetailsVO">
        SELECT
        a.F_UserId as userId,
        a.F_WorkerStatus as employeeStatus,
        a.F_SystemWokerId as employeeId
        FROM
        ftb_personnels_staff_roster a
        WHERE
        a.F_WorkerStatus not in ('301','305')
        and a.F_UserId IN
        <foreach collection="userIds" open="(" close=")" separator="," item="userId">
            #{userId}
        </foreach>
        AND a.F_EnabledMark = 0
        AND DATE_FORMAT(a.F_ActualStartDate, '%Y-%m') &lt;= #{month}
    </select>

    <select id="proportionOfEmploymentTypesList"
            resultType="jnpf.model.personnels.vo.analysis.PersonnelsOverviewTypeDetailsVO">
        SELECT
        a.F_UserId as userId,
        a.F_WorkerType as employeeType,
        a.F_SystemWokerId as employeeId
        FROM
        ftb_personnels_staff_roster a
        WHERE
        <include refid="commonCondition"/>
    </select>

    <select id="insurancePurchaseAnalysisList"
            resultType="jnpf.model.personnels.vo.analysis.PersonnelsOverviewInsuranceDetailsVO">
        SELECT
        a.F_UserId as userId,
        da.F_Value as insurancePurchase,
        a.F_SystemWokerId as employeeId
        FROM
        ftb_personnels_staff_roster AS a
        JOIN ftb_personnels_staff_registration_form_data AS da ON a.F_Id = da.F_RosterId
        WHERE
        da.F_FormFieldId = #{formFieldId} AND da.F_Value != ''
        AND <include refid="commonCondition"/>
    </select>

    <select id="educationalBackgroundList"
            resultType="jnpf.model.personnels.vo.analysis.PersonnelsOverviewEducationDetailsVO">
        SELECT
        a.F_UserId as userId,
        da.F_Value as education,
        a.F_SystemWokerId as employeeId
        FROM
        ftb_personnels_staff_roster AS a
        JOIN ftb_personnels_staff_registration_form_data AS da ON a.F_Id = da.F_RosterId
        WHERE
        da.F_FormFieldId = 'education' AND da.F_Value != ''
        AND <include refid="commonCondition"/>
    </select>

    <select id="seniorityDistributionList"
            resultType="jnpf.model.personnels.vo.analysis.PersonnelsOverviewSeniorityDetailsVO">
        SELECT
        a.F_UserId as userId,
        da.F_Value AS seniority,
        a.F_SystemWokerId as employeeId
        FROM
        ftb_personnels_staff_roster AS a
        INNER JOIN `ftb_personnels_staff_registration_form_data` AS da ON a.F_Id = da.F_RosterId
        WHERE
        da.`F_FormFieldId` = 'firstWorkDate'
        AND da.F_Value != ''
        AND <include refid="commonCondition"/>
        GROUP BY
        a.F_Id
    </select>

    <select id="ageDistributionProportionList"
            resultType="jnpf.model.personnels.vo.analysis.PersonnelsOverviewAgeDetailsVO">
        SELECT
        a.F_UserId as userId,
        da.F_Value as age,
        a.F_SystemWokerId as employeeId
        FROM
        ftb_personnels_staff_roster AS a
        left JOIN ftb_personnels_staff_registration_form_data AS da ON a.F_Id = da.F_RosterId
        WHERE
        da.F_FormFieldId = 'birthday' AND da.F_Value != ''
        AND <include refid="commonCondition"/>
        GROUP BY a.F_Id
    </select>

    <select id="companyDistributionList"
            resultType="jnpf.model.personnels.vo.analysis.PersonnelsOverviewCompanyDetailsVO">
        SELECT
        a.F_UserId as userId,
        DATE_FORMAT(a.F_ActualStartDate, '%Y-%m-%d') as companyAge,
        a.F_SystemWokerId as employeeId
        FROM
        ftb_personnels_staff_roster AS a
        WHERE
        a.F_ActualStartDate IS NOT NULL
        AND <include refid="commonCondition"/>
    </select>

    <select id="employeesEmployedInTheMonthList"
            resultType="jnpf.model.personnels.vo.analysis.PersonnelsOverviewEmployeesDetailsVO">
        SELECT
        a.F_UserId as userId,
        a.F_SystemWokerId as employeeId,
        DATE_FORMAT(a.F_ActualStartDate, '%Y-%m-%d') as entryDate,
        a.F_WorkerType as employeeType
        FROM
        ftb_personnels_staff_roster AS a
        WHERE
        <include refid="commonCondition"/>
    </select>
</mapper>