<?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.FtbPersonnelsTurnoverAnalysisMapper">
    <sql id="queryTheUser">
        <foreach collection="userIdListByOrganizeIds" open="(" close=")" separator="," item="userId">
            #{userId}
        </foreach>
    </sql>

    <select id="getLeavePeopleMonth" resultType="Integer">
        SELECT
        COUNT(0)
        FROM
        ftb_personnels_turnover_management
        WHERE
        F_ResignationDate BETWEEN #{startTime} AND #{endTime}
        AND F_WorkingCondition ='305'
        <if test="userIdListByOrganizeIds != null and userIdListByOrganizeIds.size() != 0 ">
            and F_UserId in <include refid="queryTheUser"/>
        </if>
    </select>

    <select id="getLeavePeopleSexMonth" resultType="java.lang.Integer">
        SELECT
        count(0)
        FROM
        ftb_personnels_turnover_management
        WHERE
        F_ResignationDate BETWEEN #{startTime} AND #{endTime}
        AND F_WorkingCondition ='305'
        <if test="userIdListByOrganizeIds != null and userIdListByOrganizeIds.size() != 0 ">
            and F_UserId  in <include refid="queryTheUser"/>
        </if>
    </select>

    <select id="getFirstMonthLeaveRate" resultType="java.lang.Integer">
        SELECT
        COUNT(0)
        FROM
        ftb_personnels_turnover_management
        WHERE
        F_ResignationDate BETWEEN #{startTime} AND #{endTime}
        AND F_WorkingCondition ='305'
        <if test="userIdListByOrganizeIds != null and userIdListByOrganizeIds.size() != 0 ">
            and F_UserId in <include refid="queryTheUser"/>
        </if>
        <if test="firstMonth != null ">
            and DATEDIFF(F_ResignationDate, F_DateOfEntry) &lt;= #{firstMonth}
        </if>

    </select>

    <select id="getLeaveReasonDistribution" resultType="java.util.Map">
        SELECT
        ma.F_UserId as userId,
        ma.F_ReasonForResignation as reason
        FROM
        ftb_personnels_turnover_management ma
        WHERE
        F_ResignationDate BETWEEN #{startTime} AND #{endTime}
        AND F_WorkingCondition ='305'
        AND ma.F_ReasonForResignation IS NOT NULL
        <if test="userIdListByOrganizeIds != null and userIdListByOrganizeIds.size() != 0 ">
            and F_UserId in <include refid="queryTheUser"/>
        </if>
        GROUP BY
        F_UserId
    </select>

    <select id="getLeavePeopleAgeDistribution" resultType="java.util.Map">
        SELECT
        ma.F_UserId AS userId,
        DATEDIFF( NOW(), ros.F_ActualStartDate )	AS deff
        FROM
        ftb_personnels_turnover_management AS ma
        LEFT JOIN ftb_personnels_staff_roster AS ros ON ros.F_UserId = ma.F_UserId
        WHERE
        ma.F_WorkingCondition = '305'
        AND ma.F_ResignationDate BETWEEN #{startTime} AND #{endTime}
        <if test="userIdListByOrganizeIds != null and userIdListByOrganizeIds.size() != 0 ">
            and ma.F_UserId in <include refid="queryTheUser"/>
        </if>
    </select>

    <select id="getLeavePeoplePostRatio" resultType="java.util.Map">
        SELECT
        ma.F_UserId as userId,
        ma.F_OrganizationInfo as organizeInfo
        FROM
        ftb_personnels_turnover_management AS ma
        WHERE
        ma.F_WorkingCondition = '305'
        AND ma.F_ResignationDate BETWEEN #{startTime} AND #{endTime}
        and ma.F_OrganizationInfo is not null
        <if test="userIdListByOrganizeIds != null and userIdListByOrganizeIds.size() != 0 ">
            and ma.F_UserId in <include refid="queryTheUser"/>
        </if>
    </select>

    <select id="getLeavePeopleAgeLogRatio" resultType="java.util.Map">
        SELECT
        ma.F_UserId as userId,
        ma.F_UserName as name,
        ma.F_PhoneNumber as phone,
        ma.F_SystemWokerId as employeeId,
        ma.F_OrganizationInfo as organizationInfo,
        ma.F_DateOfEntry as entryDate,
        ma.F_ResignationDate as leaveDate,
        da.F_Value  as `data`
        FROM
        ftb_personnels_turnover_management AS ma
        LEFT JOIN ftb_personnels_staff_registration_form_data as da  ON  da.F_Phone = ma.F_PhoneNumber  and da.F_FormFieldId = #{formId}
        WHERE
        ma.F_WorkingCondition = '305'
        AND ma.F_ResignationDate BETWEEN #{startTime} AND #{endTime}
        <if test="userIdListByOrganizeIds != null and userIdListByOrganizeIds.size() != 0 ">
            and ma.F_UserId in <include refid="queryTheUser"/>
        </if>
        group by
        ma.F_UserId
    </select>

    <select id="numberOfOnboardedEmployees" resultType="java.lang.Integer">
        SELECT
        COUNT( 0 )
        FROM
        ftb_personnels_staff_roster
        <where>
            F_ActualStartDate BETWEEN #{ringStartTime} AND #{ringEndTime}
            <if test="userIdListByOrganizeIds != null and userIdListByOrganizeIds.size() != 0 ">
                and F_UserId in <include refid="queryTheUser"/>
            </if>

        </where>

    </select>

    <select id="getLeaveRateDetail" resultType="java.util.Map">
        SELECT
        ma.F_UserId as userId,
        ma.F_UserName as name,
        ma.F_PhoneNumber as phone,
        ma.F_SystemWokerId as employeeId,
        ma.F_OrganizationInfo as organizationInfo,
        ma.F_DateOfEntry as entryDate,
        ma.F_ResignationDate as leaveDate,
        ma.F_ReasonForResignation as reason
        FROM
        ftb_personnels_turnover_management ma
        WHERE
        F_ResignationDate BETWEEN #{startTime} AND #{endTime}
        AND F_WorkingCondition ='305'
        AND ma.F_ReasonForResignation IS NOT NULL
        <if test="userIdListByOrganizeIds != null and userIdListByOrganizeIds.size() != 0 ">
            and F_UserId in <include refid="queryTheUser"/>
        </if>
        GROUP BY
        F_UserId
    </select>
</mapper>