<?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="org.sky.scientific.mapper.XmTechnicianMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.XmTechnicianEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="year_and_month" property="yearAndMonth"/>
        <result column="xm_id" property="xmId"/>
        <result column="XMBH" property="xmbh"/>
        <result column="XMMC" property="xmmc"/>
        <result column="unicode" property="unicode"/>
        <result column="name" property="name"/>
        <result column="number" property="number"/>
        <result column="id_card" property="idCard"/>
        <result column="department_id" property="departmentId"/>
        <result column="department" property="department"/>
        <result column="nature" property="nature"/>
        <result column="situation" property="situation"/>
        <result column="personnel_type" property="personnelType"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>

    <resultMap id="ResultVoMap" type="org.sky.scientific.pojo.vo.XmTechnicianVO">
        <result column="unicode" property="unicode"/>
        <result column="name" property="name"/>
        <result column="number" property="number"/>
        <result column="id_card" property="idCard"/>
        <result column="department_id" property="departmentId"/>
        <result column="department" property="department"/>
        <result column="post_location" property="postLocation"/>
        <result column="post" property="post"/>
        <result column="nature" property="nature"/>
        <result column="situation" property="situation"/>
        <result column="personnel_type" property="personnelType"/>
        <collection property="xmList" ofType="org.sky.scientific.pojo.entity.XmEntity">
            <result column="xmId" property="id"/>
            <result column="xmmc" property="xmmc"/>
            <result column="xmbh" property="xmbh"/>
        </collection>
        <collection property="hoursList" ofType="org.sky.scientific.pojo.vo.DailyAttendance">
            <result column="workDate" property="workDate"/>
            <result column="workHours" property="workHours"/>
         </collection>
    </resultMap>

    <delete id="physicalDeleteXmTechnicianMonthlyById">
        DELETE FROM kd_xm_technician
        WHERE id = #{id}
    </delete>

    <select id="meiyueshijicanyuanrenyuanmingdan"
            resultMap="ResultMap"
            parameterType="org.sky.scientific.pojo.dto.XmTechnicianDto">
        SELECT
        xt.id,xt.year_and_month,
        xm.*,
        t.name, xt.unicode, t.number,t.id_card,t.department,t.nature,t.situation,t.personnel_type
        FROM kd_xm_technician xt
        LEFT JOIN kd_technician t on xt.unicode = t.unicode and t.year_and_month = #{ew.yearAndMonth}
        LEFT JOIN (
            SELECT temp.id as xm_id, XMBH,
                    COALESCE(get_xm_info(temp.id,'XMMC', #{ew.yearAndMonth}), temp.XMMC ) AS XMMC
            FROM kd_xm temp
        ) xm ON xt.xm_id = xm.xm_id
        WHERE xt.year_and_month = #{ew.yearAndMonth}
        <if test="ew.xmmc!=null and ew.xmmc!=''">and xm.XMMC like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.name!=null and ew.name!=''">and t.name like concat('%',#{ew.name},'%')</if>
        <if test="ew.number!=null and ew.number!=''">and t.number like concat('%',#{ew.number},'%')</if>
        <if test="ew.idCard!=null and ew.idCard!=''">and t.id_card like concat('%',#{ew.idCard},'%')</if>
        <if test="ew.department!=null and ew.department!=''">and t.department like concat('%',#{ew.department},'%')</if>
        <if test="ew.nature!=null and ew.nature!=''">and t.nature = #{ew.nature}</if>
        <if test="ew.situation!=null and ew.situation!=''">and t.situation = #{ew.situation}</if>
    </select>

    <resultMap id="ResultXmTechnicianSummary" type="org.sky.scientific.pojo.vo.XmTechnicianSummary">
        <result column="xmbh" property="xmbh"/>
        <result column="XMMC" property="xmmc"/>
        <collection property="technicianList" ofType="org.sky.scientific.pojo.entity.XmTechnicianEntity">
            <result column="year_and_month" property="yearAndMonth"/>
            <result column="unicode" property="unicode"/>
            <result column="name" property="name"/>
            <result column="number" property="number"/>
            <result column="id_card" property="idCard"/>
            <result column="department_id" property="departmentId"/>
            <result column="department" property="department"/>
            <result column="post_location" property="postLocation"/>
            <result column="post" property="post"/>
            <result column="nature" property="nature"/>
            <result column="situation" property="situation"/>
            <result column="personnel_type" property="personnelType"/>
        </collection>
    </resultMap>

    <select id="xmTechnicianSummaryByMonthly"
            resultMap="ResultXmTechnicianSummary"
            parameterType="org.sky.scientific.pojo.entity.XmTechnicianEntity">
        SELECT
            xm.xmbh,xm.XMMC,
            m.unicode,t.name, t.number,t.id_card,t.department_id,t.department,t.post,t.post_location,t.nature,t.situation,t.personnel_type,t.year_and_month
        FROM kd_xm_technician m
        LEFT JOIN kd_technician t on t.unicode = m.unicode and t.year_and_month = #{ew.yearAndMonth}
        LEFT JOIN (
            SELECT temp.id as xm_id, XMBH,
                COALESCE(get_xm_info(temp.id,'XMMC', #{ew.yearAndMonth}),temp.XMMC ) AS XMMC
            FROM kd_xm temp
        ) xm ON m.xm_id = xm.xm_id
        WHERE m.year_and_month = #{ew.yearAndMonth}
        <if test="ew.xmId!=null">and m.xm_id = #{ew.xmId}</if>
        <if test="ew.xmmc!=null and ew.xmmc!=''">and xm.XMMC like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.name!=null and ew.name!=''">and t.name like concat('%',#{ew.name},'%')</if>
        <if test="ew.number!=null and ew.number!=''">and t.number like concat('%',#{ew.number},'%')</if>
        <if test="ew.idCard!=null and ew.idCard!=''">and t.id_card like concat('%',#{ew.idCard},'%')</if>
        <if test="ew.department!=null and ew.department!=''">and t.department like concat('%',#{ew.department},'%')</if>
        <if test="ew.post!=null and ew.post!=''">and t.post like concat('%',#{ew.post},'%')</if>
        <if test="ew.nature!=null and ew.nature!=''">and t.nature = #{ew.nature}</if>
        <if test="ew.situation!=null and ew.situation!=''">and t.situation = #{ew.situation}</if>
        <if test="ew.personnelType!=null and ew.personnelType!=''">and t.personnel_type = #{ew.personnelType}</if>
        GROUP BY xm.XMMC,m.id
        order by xm.XMMC asc
    </select>


    <select id="xmTechnicianSummaryByYear"
            resultMap="ResultXmTechnicianSummary"
            parameterType="org.sky.scientific.pojo.entity.XmTechnicianEntity">
        SELECT
            xm.xmbh,xm.XMMC,
            m.unicode,t.number,t.id_card, t.name,t.department_id,t.department,t.post,t.post_location,t.nature,t.situation,t.personnel_type
        FROM kd_xm_technician m
        LEFT JOIN kd_technician t on t.unicode = m.unicode and left(t.year_and_month,4) = #{ew.yearAndMonth}
        LEFT JOIN (
            SELECT temp.id as xm_id, XMBH,
                COALESCE(get_xm_info(temp.id,'XMMC', #{ew.yearAndMonth}), temp.XMMC ) AS XMMC
            FROM kd_xm temp
        ) xm ON m.xm_id = xm.xm_id
        WHERE LEFT(m.year_and_month,4) = #{ew.yearAndMonth}
        <if test="ew.xmmc!=null and ew.xmmc!=''">and xm.XMMC like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.name!=null and ew.name!=''">and t.name like concat('%',#{ew.name},'%')</if>
        <if test="ew.number!=null and ew.number!=''">and t.number like concat('%',#{ew.number},'%')</if>
        <if test="ew.idCard!=null and ew.idCard!=''">and t.id_card like concat('%',#{ew.idCard},'%')</if>
        <if test="ew.department!=null and ew.department!=''">and t.department like concat('%',#{ew.department},'%')</if>
        <if test="ew.post!=null and ew.post!=''">and t.post like concat('%',#{ew.post},'%')</if>
        <if test="ew.nature!=null and ew.nature!=''">and t.nature = #{ew.nature}</if>
        <if test="ew.situation!=null and ew.situation!=''">and t.situation = #{ew.situation}</if>
        <if test="ew.personnelType!=null and ew.personnelType!=''">and t.personnel_type = #{ew.personnelType}</if>
        <if test="ew.xmIdList!=null and ew.xmIdList.size()>0">
          and m.xm_id in <foreach collection="ew.xmIdList" item="xmId" open="(" separator="," close=")">#{xmId}</foreach>
        </if>
        GROUP BY m.xm_id,m.unicode
        order by xm.XMMC asc
    </select>

    <select id="canyanrenyuanhuamingce" resultMap="ResultVoMap">
        SELECT
        xt.unicode,
        COUNT(DISTINCT xm_id) as proCount,
        t.`name`,t.number, t.id_card, t.department_id,t.department, t.post, post_location, t.nature, t.situation , t.personnel_type
        FROM kd_xm_technician xt
        left join
        (
            select
                unicode, NAME, number, id_card, department_id , department, post, post_location, nature, situation , personnel_type
            from kd_technician temp
            where temp.unicode = unicode and temp.year_and_month = ( select max(year_and_month) from kd_technician where unicode = temp.unicode and LEFT(year_and_month, 4) = #{ew.yearAndMonth} group by unicode )
        ) t on xt.unicode = t.unicode
        WHERE LEFT(xt.year_and_month, 4) = #{ew.yearAndMonth}
        <if test="ew.name!=null and ew.name!=''">and t.name like concat('%', #{ew.name},'%')</if>
        <if test="ew.number!=null and ew.number!=''">and t.number like concat('%',#{ew.number}, '%')</if>
        <if test="ew.idCard!=null and ew.idCard!=''">and t.id_card like concat('%', #{ew.idCard}, '%')</if>
        <if test="ew.department!=null and ew.department!=''">and t.department like concat('%',#{ew.department}, '%')</if>
        <if test="ew.post!=null and ew.post!=''">and t.post like concat('%', #{ew.post},'%')</if>
        <if test="ew.nature!=null and ew.nature!=''">and t.nature = #{ew.nature}</if>
        <if test="ew.personnelType!=null and ew.personnelType!=''">and t.personnel_type = #{ew.personnelType}</if>
        <if test="ew.situation !=null and ew.situation!=''">and t.situation = #{ew.situation}</if>
        GROUP BY xt.unicode
        <if test="ew.situationCountMin !=null and ew.situationCountMax!=''">
            HAVING proCount between #{ew.situationCountMin} and #{ew.situationCountMax}
        </if>
    </select>

    <select id="selectXmListByUnicodeAndYear" resultType="org.sky.scientific.pojo.entity.XmEntity">
        SELECT
            xt.xm_id as id,
            xm.xmbh,
            xm.xmmc
        FROM kd_xm_technician xt
        LEFT JOIN (
            SELECT
                id,XMBH,
                COALESCE(get_xm_info(temp.id,'XMMC', #{year}),temp.XMMC ) AS XMMC
            FROM kd_xm temp
        ) xm ON xt.xm_id = xm.id
        WHERE xt.unicode = #{unicode} and LEFT(xt.year_and_month, 4) = #{year}
        GROUP BY xt.xm_id
    </select>

    <resultMap id="XmTechnicianSalaryVOMap" type="org.sky.scientific.pojo.vo.XmTechnicianSalaryVO">
        <result column="year_and_month" property="yearAndMonth"/>
        <result column="xm_id" property="xmId"/>
        <result column="xmbh" property="xmbh"/>
        <result column="xmmc" property="xmmc"/>
        <result column="unicode" property="unicode"/>
        <result column="number" property="number"/>
        <result column="id_card" property="idCard"/>
        <result column="name" property="name"/>
        <result column="personnel_type" property="personnelType"/>
        <result column="attendanceHours" property="attendanceHours"/>
        <result column="workHours" property="workHours"/>
        <result column="average_monthly_salary" property="averageMonthlySalary"/>
        <result column="pension_insurance" property="pensionInsurance"/>
        <result column="medical_insurance" property="medicalInsurance"/>
        <result column="unemployment_insurance" property="unemploymentInsurance"/>
        <result column="injury_insurance" property="injuryInsurance"/>
        <result column="maternity_insurance" property="maternityInsurance"/>
        <result column="provident_fund" property="providentFund"/>
        <result column="bonus" property="bonus"/>
        <result column="benefit" property="benefit"/>
        <result column="pension_insurance_bc" property="pensionInsuranceBc"/>
        <result column="medical_insurance_bc" property="medicalInsuranceBc"/>
    </resultMap>
    <select id="meiyueyanfarengongfeiyongfeifenpeibiao" resultMap="XmTechnicianSalaryVOMap" parameterType="org.sky.scientific.pojo.dto.XmTechnicianDto">
        SELECT
            xm.id as xm_id,xm.XMBH,xm.XMMC,
            t.unicode,t.name, t.number,t.id_card, t.personnel_type,t.year_and_month,
            (SELECT IFNULL(SUM(work_hours),0) FROM kd_attendance_records WHERE unicode = m.unicode AND LEFT(work_date,6)= #{ew.yearAndMonth}) AS attendanceHours,
            (SELECT IFNULL(SUM(work_hours),0) FROM kd_xm_technician_hours WHERE xm_id = m.xm_id and unicode = m.unicode AND LEFT(work_date,6)=#{ew.yearAndMonth}) AS workHours,
            s.average_monthly_salary,
            s.pension_insurance,
            s.medical_insurance,
            s.unemployment_insurance,
            s.injury_insurance,
            s.maternity_insurance,
            s.provident_fund,
            s.bonus,
            s.benefit,
            s.pension_insurance_bc,
            s.medical_insurance_bc
        FROM kd_xm_technician m
        LEFT JOIN kd_technician t on t.unicode = m.unicode and t.year_and_month = #{ew.yearAndMonth}
        LEFT JOIN kd_salary s on s.unicode = m.unicode and s.year_and_month = #{ew.yearAndMonth}
        LEFT JOIN (
            SELECT temp.id, XMBH,
                COALESCE(get_xm_info(temp.id, 'XMMC', #{ew.yearAndMonth}), temp.XMMC ) AS XMMC
            FROM kd_xm as  temp
        ) xm ON m.xm_id = xm.id
        WHERE m.tenant_id = #{ew.tenantId} and m.year_and_month = #{ew.yearAndMonth}
        <if test="ew.xmId!=null">and xm.id = #{ew.xmId}</if>
        <if test="ew.xmmc!=null and ew.xmmc!=''">and xm.XMMC like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.name!=null and ew.name!=''">and t.name like concat('%', #{ew.name},'%')</if>
        <if test="ew.number!=null and ew.number!=''">and t.number like concat('%',#{ew.number}, '%')</if>
        <if test="ew.idCard!=null and ew.idCard!=''">and t.id_card like concat('%', #{ew.idCard}, '%')</if>
        <if test="ew.personnelType!=null and ew.personnelType!=''">and t.personnel_type = #{ew.personnelType}</if>
        order by m.xm_id, m.unicode
    </select>

    <select id="meiyueyanfarengongfeiyongfeifenpeibiaoYear"
            resultMap="XmTechnicianSalaryVOMap">
        SELECT
            m.xm_id,xm.XMBH,xm.XMMC,
            m.unicode,t.name, t.number,t.id_card, t.personnel_type,t.year_and_month,
            (SELECT IFNULL(SUM(work_hours),0) FROM kd_attendance_records kar WHERE kar.unicode= m.unicode AND LEFT ( work_date, 4 ) = LEFT(m.year_and_month,4)) AS attendanceHours,
            (SELECT IFNULL(SUM(work_hours),0) FROM kd_xm_technician_hours kxth WHERE kxth.xm_id = kxth.xm_id AND kxth.unicode = m.unicode AND LEFT ( work_date, 4 ) = LEFT(m.year_and_month,4)) AS workHours,
            (SELECT IFNULL( sum( s.average_monthly_salary ), 0 ) FROM kd_salary s WHERE s.unicode = m.unicode AND LEFT ( s.year_and_month, 4 ) = LEFT(m.year_and_month,4)) AS average_monthly_salary,
            (SELECT IFNULL( sum( s.pension_insurance ), 0 ) FROM kd_salary s WHERE s.unicode = m.unicode AND LEFT ( s.year_and_month, 4 ) = LEFT(m.year_and_month,4)) AS pension_insurance,
            (SELECT IFNULL( sum( s.medical_insurance ), 0 ) FROM kd_salary s WHERE s.unicode = m.unicode AND LEFT ( s.year_and_month, 4 ) = LEFT(m.year_and_month,4)) AS medical_insurance,
            (SELECT IFNULL( sum( s.unemployment_insurance ), 0 ) FROM kd_salary s WHERE s.unicode = m.unicode AND LEFT ( s.year_and_month, 4 ) = LEFT(m.year_and_month,4)) AS unemployment_insurance,
            (SELECT IFNULL( sum( s.injury_insurance ), 0 ) FROM kd_salary s WHERE s.unicode = m.unicode AND LEFT ( s.year_and_month, 4 ) = LEFT(m.year_and_month,4)) AS injury_insurance,
            (SELECT IFNULL( sum( s.maternity_insurance ), 0 ) FROM kd_salary s WHERE s.unicode = m.unicode AND LEFT ( s.year_and_month, 4 ) = LEFT(m.year_and_month,4)) AS maternity_insurance,
            (SELECT IFNULL( sum( s.provident_fund ), 0 ) FROM kd_salary s WHERE s.unicode = m.unicode AND LEFT ( s.year_and_month, 4 ) = LEFT(m.year_and_month,4)) AS provident_fund,
            (SELECT IFNULL( sum( s.bonus ), 0 ) FROM kd_salary s WHERE s.unicode = m.unicode AND LEFT ( s.year_and_month, 4 ) = LEFT(m.year_and_month,4)) AS bonus,
            (SELECT IFNULL( sum( s.benefit ), 0 ) FROM kd_salary s WHERE s.unicode = m.unicode AND LEFT ( s.year_and_month, 4 ) = LEFT(m.year_and_month,4)) AS benefit,
            (SELECT IFNULL( sum( s.pension_insurance_bc ), 0 ) FROM kd_salary s WHERE s.unicode = m.unicode AND LEFT ( s.year_and_month, 4 ) = LEFT(m.year_and_month,4)) AS pension_insurance_bc,
            (SELECT IFNULL( sum( s.medical_insurance_bc ), 0 ) FROM kd_salary s WHERE s.unicode = m.unicode AND LEFT ( s.year_and_month, 4 ) = LEFT(m.year_and_month,4)) AS medical_insurance_bc
        FROM kd_xm_technician m
        LEFT JOIN
        (
            select unicode,name,number,id_card,personnel_type,year_and_month
            from kd_technician temp
            where temp.unicode = unicode and year_and_month = (select max(year_and_month) from kd_technician where unicode = temp.unicode and left(year_and_month,4) = #{ew.yearAndMonth} order by year_and_month desc limit 1)
        ) as t on t.unicode = m.unicode
        LEFT JOIN (
            SELECT temp.id, XMBH,
                COALESCE(get_xm_info(temp.id, 'XMMC', #{ew.yearAndMonth}), temp.XMMC ) AS XMMC
            FROM kd_xm temp
        ) xm ON m.xm_id = xm.id
        WHERE m.tenant_id = #{ew.tenantId} and LEFT(m.year_and_month,4) = #{ew.yearAndMonth}
        <if test="ew.xmmc!=null and ew.xmmc!=''">and xm.XMMC like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.name!=null and ew.name!=''">and t.name like concat('%', #{ew.name},'%')</if>
        <if test="ew.number!=null and ew.number!=''">and t.number like concat('%',#{ew.number}, '%')</if>
        <if test="ew.idCard!=null and ew.idCard!=''">and t.id_card like concat('%', #{ew.idCard}, '%')</if>
        <if test="ew.personnelType!=null and ew.personnelType!=''">and t.personnel_type = #{ew.personnelType}</if>
        GROUP BY m.xm_id, m.unicode
        order by m.xm_id, m.unicode
    </select>

<!--    <select id="meiyueyanfaxiangmurengongfenpeihuizongbiao" resultType="org.sky.scientific.pojo.vo.XmSalaryVO">-->
<!--        SELECT-->
<!--            t.xm_id,-->
<!--            m.xmbh,-->
<!--            m.xmmc,-->
<!--            &#45;&#45; 工资薪金总和（每个人的工资×研发占比）-->
<!--            ROUND(SUM(COALESCE(s.average_monthly_salary, 0) * COALESCE(rd_ratio.ratio, 0)), 2) AS averageMonthlySalary,-->
<!--            &#45;&#45; 社保总和（每个人的社保×研发占比）-->
<!--            ROUND(SUM((COALESCE(s.pension_insurance, 0) + COALESCE(s.medical_insurance, 0) + COALESCE(s.unemployment_insurance, 0) + COALESCE(s.injury_insurance, 0) + COALESCE(s.maternity_insurance, 0)) * COALESCE(rd_ratio.ratio, 0)), 2) AS totalInsurance,-->
<!--            &#45;&#45; 公积金总和（每个人的公积金×研发占比）-->
<!--            ROUND(SUM(COALESCE(s.provident_fund, 0) * COALESCE(rd_ratio.ratio, 0)), 2) AS providentFund,-->
<!--            &#45;&#45; 奖金总和（每个人的公积金×研发占比）-->
<!--            ROUND(SUM(COALESCE(s.bonus, 0) * COALESCE(rd_ratio.ratio, 0)), 2) AS bonus,-->
<!--            &#45;&#45; 福利费总和（每个人的福利费×研发占比）-->
<!--            ROUND(SUM(COALESCE(s.benefit, 0) * COALESCE(rd_ratio.ratio, 0)), 2) AS benefit,-->
<!--            &#45;&#45; 补充养老保险总和（每个人的补充养老保险×研发占比）-->
<!--            ROUND(SUM(COALESCE(s.pension_insurance_bc, 0) * COALESCE(rd_ratio.ratio, 0)), 2) AS pensionInsuranceBc,-->
<!--            &#45;&#45; 补充医疗保险总和（每个人的补充医疗保险×研发占比）-->
<!--            ROUND(SUM(COALESCE(s.medical_insurance_bc, 0) * COALESCE(rd_ratio.ratio, 0)), 2) AS medicalInsuranceBc-->
<!--        FROM kd_xm_technician t-->
<!--        &#45;&#45; 计算研发占比的子查询-->
<!--        INNER JOIN (-->
<!--            SELECT-->
<!--                rd.tenant_id,rd.unicode,rd.work_year_month,rd.xm_id,-->
<!--            &#45;&#45; 研发占比 = 研发工时 / 考勤工时-->
<!--                CASE WHEN COALESCE(att.attendance_hours, 0) > 0 THEN LEAST(rd.rd_hours / att.attendance_hours, 1.0) ELSE 0 END AS ratio,-->
<!--                rd.rd_hours,att.attendance_hours-->
<!--            FROM (-->
<!--                &#45;&#45; 计算每个人的研发工时-->
<!--                SELECT-->
<!--                    tenant_id,unicode,LEFT(work_date, 6) AS work_year_month, xm_id,SUM(work_hours) AS rd_hours-->
<!--                FROM kd_xm_technician_hours-->
<!--                GROUP BY tenant_id, unicode, LEFT(work_date, 6), xm_id-->
<!--            ) rd-->
<!--            LEFT JOIN (-->
<!--                &#45;&#45; 计算每个人的考勤工时-->
<!--                SELECT-->
<!--                    tenant_id, unicode,LEFT(work_date, 6) AS work_year_month, SUM(work_hours) AS attendance_hours-->
<!--                FROM kd_attendance_records-->
<!--                GROUP BY tenant_id, unicode, LEFT(work_date, 6)-->
<!--            ) att ON rd.tenant_id = att.tenant_id AND rd.unicode = att.unicode AND rd.work_year_month = att.work_year_month-->
<!--        ) rd_ratio ON t.tenant_id = rd_ratio.tenant_id AND t.unicode = rd_ratio.unicode AND t.year_and_month = rd_ratio.work_year_month AND t.xm_id = rd_ratio.xm_id-->

<!--        &#45;&#45; 关联工资数据-->
<!--        LEFT JOIN kd_salary s ON t.tenant_id = s.tenant_id AND t.unicode = s.unicode AND t.year_and_month = s.year_and_month-->

<!--        left join-->
<!--        (-->
<!--            select-->
<!--                temp.id, XMBH ,-->
<!--                COALESCE(get_xm_info(temp.id, 'XMMC', #{ew.yearAndMonth}), temp.XMMC) as XMMC from kd_xm temp-->
<!--        ) m on m.id = t.xm_id-->
<!--        WHERE t.tenant_id = #{ew.tenantId} and t.year_and_month = #{ew.yearAndMonth}-->
<!--        <if test="ew.xmmc!=null and ew.xmmc!=''">and m.XMMC like concat('%',#{ew.xmmc},'%')</if>-->
<!--        GROUP BY t.xm_id-->
<!--    </select>-->



<!--    <select id="meiyueyanfaxiangmurengongfenpeihuizongbiaoByYear" resultType="org.sky.scientific.pojo.vo.XmSalaryVO">-->
<!--        SELECT-->
<!--        xm.xm_id,m.XMBH,m.XMMC,-->
<!--        xm.year_and_month,-->
<!--        (SELECT IFNULL(SUM(work_hours),0) FROM kd_attendance_records r WHERE r.unicode in (select distinct (tmp.unicode) from kd_xm_technician tmp WHERE tmp.xm_id = xm.xm_id and LEFT(tmp.year_and_month,4) = #{ew.yearAndMonth}) and LEFT(work_date,4)= #{ew.yearAndMonth}) AS attendanceHours,-->
<!--        (SELECT IFNULL(SUM(work_hours),0) FROM kd_xm_technician_hours kxth WHERE kxth.xm_id = xm.xm_id AND LEFT(work_date,4)= #{ew.yearAndMonth})AS workHours,-->
<!--        (SELECT IFNULL(sum(s.average_monthly_salary),0) FROM kd_salary s WHERE s.unicode in (select distinct (tmp.unicode) from kd_xm_technician tmp WHERE tmp.xm_id = xm.xm_id and left(tmp.year_and_month,4) = #{ew.yearAndMonth}) AND left(s.year_and_month,4) = #{ew.yearAndMonth}) AS average_monthly_salary,-->
<!--        (SELECT IFNULL(sum(s.pension_insurance),0) FROM kd_salary s WHERE s.unicode in (select distinct (tmp.unicode) from kd_xm_technician tmp WHERE tmp.xm_id = xm.xm_id and left(tmp.year_and_month,4) = #{ew.yearAndMonth}) AND left(s.year_and_month,4) = #{ew.yearAndMonth}) AS pension_insurance,-->
<!--        (SELECT IFNULL(sum(s.medical_insurance),0) FROM kd_salary s WHERE s.unicode in (select distinct (tmp.unicode) from kd_xm_technician tmp WHERE tmp.xm_id = xm.xm_id and left(tmp.year_and_month,4) = #{ew.yearAndMonth}) AND left(s.year_and_month,4) = #{ew.yearAndMonth}) AS medical_insurance,-->

<!--        (SELECT IFNULL(sum(s.unemployment_insurance),0) FROM kd_salary s WHERE s.unicode in (select distinct (tmp.unicode) from kd_xm_technician tmp WHERE tmp.xm_id = xm.xm_id and left(tmp.year_and_month,4) = #{ew.yearAndMonth}) AND left(s.year_and_month,4) = #{ew.yearAndMonth}) AS unemployment_insurance,-->

<!--        (SELECT IFNULL(sum(s.injury_insurance),0) FROM kd_salary s WHERE s.unicode in (select distinct (tmp.unicode) from kd_xm_technician tmp WHERE tmp.xm_id = xm.xm_id and left(tmp.year_and_month,4) = #{ew.yearAndMonth}) AND left(s.year_and_month,4) = #{ew.yearAndMonth}) AS injury_insurance,-->

<!--        (SELECT IFNULL(sum(s.maternity_insurance),0) FROM kd_salary s WHERE s.unicode in (select distinct (tmp.unicode) from kd_xm_technician tmp WHERE tmp.xm_id = xm.xm_id and left(tmp.year_and_month,4) = #{ew.yearAndMonth}) AND left( s.year_and_month,4) = #{ew.yearAndMonth}) AS maternity_insurance,-->

<!--        (SELECT IFNULL(sum(s.provident_fund),0) FROM kd_salary s WHERE s.unicode in (select distinct (tmp.unicode) from kd_xm_technician tmp WHERE tmp.xm_id = xm.xm_id and left(tmp.year_and_month,4) = #{ew.yearAndMonth}) AND left(s.year_and_month,4) = #{ew.yearAndMonth}) AS provident_fund,-->
<!--        (SELECT IFNULL(sum(s.bonus),0) FROM kd_salary s WHERE s.unicode in (select distinct (tmp.unicode) from kd_xm_technician tmp WHERE tmp.xm_id = xm.xm_id and left(tmp.year_and_month,4) = #{ew.yearAndMonth}) AND left(s.year_and_month,4) = #{ew.yearAndMonth}) AS bonus,-->
<!--        (SELECT IFNULL(sum(s.benefit),0) FROM kd_salary s WHERE s.unicode in (select distinct (tmp.unicode) from kd_xm_technician tmp WHERE tmp.xm_id = xm.xm_id and left(tmp.year_and_month,4) = #{ew.yearAndMonth}) AND left(s.year_and_month,4) = #{ew.yearAndMonth}) AS benefit,-->
<!--        (SELECT IFNULL(sum(s.pension_insurance_bc),0) FROM kd_salary s WHERE s.unicode in (select distinct (tmp.unicode) from kd_xm_technician tmp WHERE tmp.xm_id = xm.xm_id and left(tmp.year_and_month,4) = #{ew.yearAndMonth}) AND left(s.year_and_month,4) = #{ew.yearAndMonth}) AS pension_insurance_bc,-->
<!--        (SELECT IFNULL(sum(s.medical_insurance_bc),0) FROM kd_salary s WHERE s.unicode in (select distinct (tmp.unicode) from kd_xm_technician tmp WHERE tmp.xm_id = xm.xm_id and left(tmp.year_and_month,4) = #{ew.yearAndMonth}) AND left(s.year_and_month,4) = #{ew.yearAndMonth}) AS medical_insurance_bc-->
<!--        FROM kd_xm_technician xm-->
<!--        LEFT JOIN (-->
<!--            SELECT-->
<!--                temp.id, XMBH,-->
<!--                COALESCE(get_xm_info(temp.id, 'XMMC', #{ew.yearAndMonth}), temp.XMMC ) AS XMMC-->
<!--            FROM kd_xm as  temp-->
<!--        ) m ON m.id = xm.xm_id-->
<!--        WHERE xm.tenant_id = #{ew.tenantId} and LEFT(xm.year_and_month,4) = #{ew.yearAndMonth}-->
<!--        <if test="ew.xmmc!=null and ew.xmmc!=''">and m.XMMC like concat('%',#{ew.xmmc},'%')</if>-->
<!--        GROUP BY xm.xm_id-->
<!--    </select>-->

    <select id="meiyueyanfaxiangmurengongfenpeihuizongbiao" resultType="org.sky.scientific.pojo.vo.XmSalaryVO">
        SELECT
            v.xm_id,m.XMBH,m.XMMC,v.year_and_month,
            sum(averageMonthlySalary) as averageMonthlySalary,
            sum(totalInsurance) as totalInsurance,
            sum(providentFund) as providentFund,
            sum(bonus) as bonus,
            sum(benefit) as benefit,
            sum(pensionInsuranceBc) as pensionInsuranceBc,
            sum(medicalInsuranceBc) as medicalInsuranceBc
        FROM ryrgfyhz_view v
        LEFT JOIN (
            SELECT
            temp.id, XMBH,
            COALESCE(get_xm_info(temp.id, 'XMMC', #{ew.yearAndMonth}), temp.XMMC ) AS XMMC
            FROM kd_xm as  temp
        ) m ON m.id = v.xm_id
        WHERE v.tenant_id = #{ew.tenantId} and LEFT(v.year_and_month,6) = #{ew.yearAndMonth}
        <if test="ew.xmmc!=null and ew.xmmc!=''">and m.XMMC like concat('%',#{ew.xmmc},'%')</if>
        GROUP BY v.xm_id
    </select>

    <select id="meiyueyanfaxiangmurengongfenpeihuizongbiaoByYear" resultType="org.sky.scientific.pojo.vo.XmSalaryVO">
        SELECT
            v.xm_id,m.XMBH,m.XMMC,v.year_and_month,
            sum(averageMonthlySalary) as averageMonthlySalary,
            sum(totalInsurance) as totalInsurance,
            sum(providentFund) as providentFund,
            sum(bonus) as bonus,
            sum(benefit) as benefit,
            sum(pensionInsuranceBc) as pensionInsuranceBc,
            sum(medicalInsuranceBc) as medicalInsuranceBc
        FROM ryrgfyhz_view v
        LEFT JOIN (
            SELECT
                temp.id, XMBH,
                COALESCE(get_xm_info(temp.id, 'XMMC', #{ew.yearAndMonth}), temp.XMMC ) AS XMMC
            FROM kd_xm as  temp
        ) m ON m.id = v.xm_id
        WHERE v.tenant_id = #{ew.tenantId} and LEFT(v.year_and_month,4) = #{ew.yearAndMonth}
        <if test="ew.xmmc!=null and ew.xmmc!=''">and m.XMMC like concat('%',#{ew.xmmc},'%')</if>
        GROUP BY v.xm_id
        order by v.xm_id asc
    </select>

    <select id="statisticsWorkHoursByXmAndUnicodeAndMonth" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            m.xm_id as xmId,
            m.unicode as unicode,
            m.year_and_month as yearAndMonth,
            (SELECT sum(r.work_hours) FROM kd_attendance_records r WHERE r.unicode = m.unicode and DATE_FORMAT(r.work_date,'%Y%m') = m.year_and_month) as attendanceHours,
            (SELECT sum(h.work_hours) FROM kd_xm_technician_hours h WHERE h.xm_id = m.xm_id and h.unicode = m.unicode and DATE_FORMAT(h.work_date,'%Y%m') = m.year_and_month) as workHours
        FROM
            kd_xm_technician m
        WHERE
            m.tenant_id = #{tenantId} and LEFT(m.year_and_month,4)= #{year}
        GROUP BY m.xm_id, m.unicode,m.year_and_month
        order by m.xm_id asc
    </select>
</mapper>
