<?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.SalaryMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.SalaryEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="unicode" property="unicode"/>
        <result column="year_and_month" property="yearAndMonth"/>
        <result column="average_monthly_salary" property="averageMonthlySalary"/>
        <result column="average_monthly_salary_edit" property="averageMonthlySalaryEdit"/>
        <result column="pension_insurance" property="pensionInsurance"/>
        <result column="pension_insurance_edit" property="pensionInsuranceEdit"/>
        <result column="medical_insurance" property="medicalInsurance"/>
        <result column="medical_insurance_edit" property="medicalInsuranceEdit"/>
        <result column="unemployment_insurance" property="unemploymentInsurance"/>
        <result column="unemployment_insurance_edit" property="unemploymentInsuranceEdit"/>
        <result column="injury_insurance" property="injuryInsurance"/>
        <result column="injury_insurance_edit" property="injuryInsuranceEdit"/>
        <result column="maternity_insurance" property="maternityInsurance"/>
        <result column="maternity_insurance_edit" property="maternityInsuranceEdit"/>
        <result column="provident_fund" property="providentFund"/>
        <result column="provident_fund_edit" property="providentFundEdit"/>
        <result column="bonus" property="bonus"/>
        <result column="bonus_edit" property="bonusEdit"/>
        <result column="benefit" property="benefit"/>
        <result column="benefit_edit" property="benefitEdit"/>
        <result column="pension_insurance_bc" property="pensionInsuranceBc"/>
        <result column="pension_insurance_bc_edit" property="pensionInsuranceBcEdit"/>
        <result column="medical_insurance_bc" property="medicalInsuranceBc"/>
        <result column="medical_insurance_bc_edit" property="medicalInsuranceBcEdit"/>
        <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.SalaryVO" extends="ResultMap">
        <result column="name" property="name"/>
        <result column="number" property="number"/>
        <result column="id_card" property="idCard"/>
        <result column="department" property="department"/>
        <result column="personnel_type" property="personnelType"/>
    </resultMap>

    <sql id="basicColumns">
        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
    </sql>

    <update id="updateSalary" parameterType="org.sky.scientific.pojo.entity.SalaryEntity">
        update kd_salary
        <trim prefix="set" suffixOverrides=",">
            <if test="averageMonthlySalary !=null">average_monthly_salary = #{averageMonthlySalary},average_monthly_salary_edit = 1,</if>
            <if test="pensionInsurance!=null">pension_insurance = #{pensionInsurance}, pension_insurance_edit = 1,</if>
            <if test="medicalInsurance!=null">medical_insurance = #{medicalInsurance}, medical_insurance_edit = 1,</if>
            <if test="unemploymentInsurance!=null">unemployment_insurance = #{unemploymentInsurance},unemployment_insurance_edit = 1,</if>
            <if test="injuryInsurance!=null">injury_insurance = #{injuryInsurance}, injury_insurance_edit = 1,</if>
            <if test="maternityInsurance!=null">maternity_insurance = #{maternityInsurance}, maternity_insurance_edit = 1,</if>
            <if test="providentFund!=null">provident_fund = #{providentFund}, provident_fund_edit = 1,</if>
            <if test="bonus!=null">bonus = #{bonus}, bonus_edit = 1,</if>
            <if test="benefit!=null">benefit = #{benefit}, benefit_edit = 1,</if>
            <if test="pensionInsuranceBc!=null">pension_insurance_bc = #{pensionInsuranceBc}, pension_insurance_bc_edit = 1,</if>
            <if test="medicalInsuranceBc!=null">medical_insurance_bc = #{medicalInsuranceBc}, medical_insurance_bc_edit = 1,</if>
        </trim>
        where unicode = #{unicode} and year_and_month = #{yearAndMonth}
    </update>

    <update id="clearSalary">
        update kd_salary
        set average_monthly_salary = null,
            pension_insurance      = null,
            medical_insurance      =null,
            unemployment_insurance = null,
            injury_insurance       = null,
            maternity_insurance    = null,
            provident_fund         = null,
            bonus                  =null,
            benefit                = null,
            pension_insurance_bc   =null,
            medical_insurance_bc   = null
        where unicode = #{unicode}
          and year_and_month = #{yearAndMonth}
    </update>

    <select id="selectSalaryPage" resultMap="ResultVoMap">
        SELECT
        s.year_and_month,
        s.unicode,
        t.name,
        t.number,
        t.id_card,
        t.department,
        t.personnel_type,
        s.average_monthly_salary,
        s.average_monthly_salary_edit,
        s.pension_insurance,
        s.pension_insurance_edit,
        s.medical_insurance,
        s.medical_insurance_edit,
        s.unemployment_insurance,
        s.unemployment_insurance_edit,
        s.injury_insurance,
        s.injury_insurance_edit,
        s.maternity_insurance,
        s.maternity_insurance_edit,
        s.provident_fund,
        s.provident_fund_edit,
        s.bonus,
        s.bonus_edit,
        s.benefit,
        s.benefit_edit,
        s.pension_insurance_bc,
        s.pension_insurance_bc_edit,
        s.medical_insurance_bc,
        s.medical_insurance_bc_edit
        FROM kd_salary s
        LEFT JOIN kd_technician t on t.unicode = s.unicode and t.year_and_month = #{salary.yearAndMonth}
        WHERE s.is_deleted = 0 and s.year_and_month = #{salary.yearAndMonth}
        <if test="salary.name != null">
            and t.name like concat('%',#{salary.name},'%')
        </if>
        <if test="salary.number != null">
            and t.number like concat('%',#{salary.number},'%')
        </if>
        <if test="salary.idCard != null">
            and t.id_card like concat('%',#{salary.idCard},'%')
        </if>
        <if test="salary.department != null">
            and t.department like concat('%',#{salary.department},'%')
        </if>
        <if test="salary.personnelType != null">
            and t.personnel_type = #{salary.personnelType}
        </if>
        <if test="salary.xmId!=null and salary.xmId>0">
            and s.unicode in (select distinct unicode from kd_xm_technician where xm_id = #{salary.xmId} and year_and_month = #{salary.yearAndMonth})

        </if>
    </select>

    <delete id="deleteSalary">
        delete
        from kd_salary
        where unicode = #{unicode} and year_and_month = #{yearAndMonth}
    </delete>

    <select id="fetchLastYearData" resultMap="ResultMap">
        SELECT *
        FROM kd_salary
        WHERE year_and_month = #{lastYear} and unicode in
        (
            SELECT t.unicode FROM kd_technician t
            WHERE LEFT(t.year_and_month, 4) = #{currentYear}
        )
    </select>

</mapper>
