<?xml version="1.0" encoding="UTF-8"?>

<!--
  ~
  ~      Copyright (c) 2018-2025, lengleng All rights reserved.
  ~
  ~  Redistribution and use in source and binary forms, with or without
  ~  modification, are permitted provided that the following conditions are met:
  ~
  ~ Redistributions of source code must retain the above copyright notice,
  ~  this list of conditions and the following disclaimer.
  ~  Redistributions in binary form must reproduce the above copyright
  ~  notice, this list of conditions and the following disclaimer in the
  ~  documentation and/or other materials provided with the distribution.
  ~  Neither the name of the yifu4cloud.com developer nor the names of its
  ~  contributors may be used to endorse or promote products derived from
  ~  this software without specific prior written permission.
  ~  Author: lengleng (wangiegie@gmail.com)
  ~
  -->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.yifu.cloud.plus.v1.yifu.salary.mapper.TSalaryAccount2022Mapper">

  <resultMap id="tSalaryAccount2022Map" type="com.yifu.cloud.plus.v1.yifu.salary.entity.TSalaryAccount2022">
    <id property="id" column="ID"/>
    <result property="invoiceTitle" column="INVOICE_TITLE"/>
    <result property="empId" column="EMP_ID"/>
    <result property="empName" column="EMP_NAME"/>
    <result property="empIdcard" column="EMP_IDCARD"/>
    <result property="settleDepartId" column="SETTLE_DEPART_ID"/>
    <result property="settleDepartNo" column="SETTLE_DEPART_NO"/>
    <result property="settleDepartName" column="SETTLE_DEPART_NAME"/>
    <result property="settlementMonth" column="SETTLEMENT_MONTH"/>
    <result property="taxMonth" column="TAX_MONTH"/>
    <result property="realSalary" column="REAL_SALARY"/>
    <result property="costReduction" column="cost_reduction"/>
    <result property="specialDeduMoney" column="SPECIAL_DEDU_MONEY"/>
    <result property="childEduinfoMoney" column="child_eduInfo_money"/>
    <result property="housingLoanInterestExpenseMoney" column="housing_loan_Interest_expense_money"/>
    <result property="housingRentalinfoMoney" column="housing_rentalInfo_money"/>
    <result property="continuingEducationExpenseMoney" column="continuing_education_expense_money"/>
    <result property="supportElderlyExpenseMoney" column="support_elderly_expense_money"/>
    <result property="personalSocial" column="PERSONAL_SOCIAL"/>
    <result property="personalFund" column="PERSONAL_FUND"/>
    <result property="withholidingPersonSocial" column="withholiding_person_social"/>
    <result property="withholidingPersonFund" column="withholiding_person_fund"/>
    <result property="enterpriseAnnuity" column="enterprise_annuity"/>
    <result property="salaryTax" column="salary_tax"/>
    <result property="actualSalarySum" column="actual_salary_sum"/>
    <result property="annualBonus" column="annual_bonus"/>
    <result property="annualBonusTax" column="annual_bonus_tax"/>
    <result property="exemptionPersionTax" column="exemption_persion_tax"/>
    <result property="unitId" column="unit_id"/>
    <result property="unitNo" column="unit_no"/>
    <result property="unitName" column="unit_name"/>
    <result property="sumBabyMoney" column="sum_baby_money"/>
	<result property="isNewEmployee" column="IS_NEW_EMPLOYEE"/>
	<result property="createMonth" column="CREATE_MONTH"/>
	<result property="empPhone" column="EMP_PHONE"/>
  </resultMap>
    <sql id="Base_Column_List">
                a.ID,
                a.INVOICE_TITLE,
                a.EMP_ID,
                a.EMP_NAME,
                a.EMP_IDCARD,
                a.SETTLE_DEPART_ID,
                a.SETTLE_DEPART_NO,
                a.SETTLE_DEPART_NAME,
                a.SETTLEMENT_MONTH,
                a.TAX_MONTH,
                a.REAL_SALARY,
                a.cost_reduction,
                a.SPECIAL_DEDU_MONEY,
                a.child_eduInfo_money,
                a.housing_loan_Interest_expense_money,
                a.housing_rentalInfo_money,
                a.continuing_education_expense_money,
                a.support_elderly_expense_money,
                a.PERSONAL_SOCIAL,
                a.PERSONAL_FUND,
                a.withholiding_person_social,
                a.withholiding_person_fund,
                a.enterprise_annuity,
                a.salary_tax,
                a.actual_salary_sum,
                a.annual_bonus,
                a.annual_bonus_tax,
                a.exemption_persion_tax,
                a.unit_id,
                a.unit_no,
                a.unit_name,
                a.sum_baby_money,
                a.IS_NEW_EMPLOYEE,
                a.CREATE_MONTH,
                a.EMP_PHONE
    </sql>
    <sql id="tSalaryAccount2022_where">
        <if test="tSalaryAccount2022 != null">
                    <if test="tSalaryAccount2022.id != null and tSalaryAccount2022.id.trim() != ''">
                        AND a.ID = #{tSalaryAccount2022.id}
                    </if>
                    <if test="tSalaryAccount2022.invoiceTitle != null and tSalaryAccount2022.invoiceTitle.trim() != ''">
                        AND a.INVOICE_TITLE = #{tSalaryAccount2022.invoiceTitle}
                    </if>
                    <if test="tSalaryAccount2022.empId != null and tSalaryAccount2022.empId.trim() != ''">
                        AND a.EMP_ID = #{tSalaryAccount2022.empId}
                    </if>
                    <if test="tSalaryAccount2022.empName != null and tSalaryAccount2022.empName.trim() != ''">
                        AND a.EMP_NAME = #{tSalaryAccount2022.empName}
                    </if>
                    <if test="tSalaryAccount2022.empIdcard != null and tSalaryAccount2022.empIdcard.trim() != ''">
                        AND a.EMP_IDCARD = #{tSalaryAccount2022.empIdcard}
                    </if>
                    <if test="tSalaryAccount2022.settleDepartId != null and tSalaryAccount2022.settleDepartId.trim() != ''">
                        AND a.SETTLE_DEPART_ID = #{tSalaryAccount2022.settleDepartId}
                    </if>
                    <if test="tSalaryAccount2022.settleDepartNo != null and tSalaryAccount2022.settleDepartNo.trim() != ''">
                        AND a.SETTLE_DEPART_NO = #{tSalaryAccount2022.settleDepartNo}
                    </if>
                    <if test="tSalaryAccount2022.settleDepartName != null and tSalaryAccount2022.settleDepartName.trim() != ''">
                        AND a.SETTLE_DEPART_NAME = #{tSalaryAccount2022.settleDepartName}
                    </if>
                    <if test="tSalaryAccount2022.settlementMonth != null and tSalaryAccount2022.settlementMonth.trim() != ''">
                        AND a.SETTLEMENT_MONTH = #{tSalaryAccount2022.settlementMonth}
                    </if>
                    <if test="tSalaryAccount2022.taxMonth != null and tSalaryAccount2022.taxMonth.trim() != ''">
                        AND a.TAX_MONTH = #{tSalaryAccount2022.taxMonth}
                    </if>
                    <if test="tSalaryAccount2022.realSalary != null">
                        AND a.REAL_SALARY = #{tSalaryAccount2022.realSalary}
                    </if>
                    <if test="tSalaryAccount2022.costReduction != null">
                        AND a.cost_reduction = #{tSalaryAccount2022.costReduction}
                    </if>
                    <if test="tSalaryAccount2022.specialDeduMoney != null">
                        AND a.SPECIAL_DEDU_MONEY = #{tSalaryAccount2022.specialDeduMoney}
                    </if>
                    <if test="tSalaryAccount2022.childEduinfoMoney != null">
                        AND a.child_eduInfo_money = #{tSalaryAccount2022.childEduinfoMoney}
                    </if>
                    <if test="tSalaryAccount2022.housingLoanInterestExpenseMoney != null">
                        AND a.housing_loan_Interest_expense_money = #{tSalaryAccount2022.housingLoanInterestExpenseMoney}
                    </if>
                    <if test="tSalaryAccount2022.housingRentalinfoMoney != null">
                        AND a.housing_rentalInfo_money = #{tSalaryAccount2022.housingRentalinfoMoney}
                    </if>
                    <if test="tSalaryAccount2022.continuingEducationExpenseMoney != null">
                        AND a.continuing_education_expense_money = #{tSalaryAccount2022.continuingEducationExpenseMoney}
                    </if>
                    <if test="tSalaryAccount2022.supportElderlyExpenseMoney != null">
                        AND a.support_elderly_expense_money = #{tSalaryAccount2022.supportElderlyExpenseMoney}
                    </if>
                    <if test="tSalaryAccount2022.personalSocial != null">
                        AND a.PERSONAL_SOCIAL = #{tSalaryAccount2022.personalSocial}
                    </if>
                    <if test="tSalaryAccount2022.personalFund != null">
                        AND a.PERSONAL_FUND = #{tSalaryAccount2022.personalFund}
                    </if>
                    <if test="tSalaryAccount2022.withholidingPersonSocial != null">
                        AND a.withholiding_person_social = #{tSalaryAccount2022.withholidingPersonSocial}
                    </if>
                    <if test="tSalaryAccount2022.withholidingPersonFund != null">
                        AND a.withholiding_person_fund = #{tSalaryAccount2022.withholidingPersonFund}
                    </if>
                    <if test="tSalaryAccount2022.enterpriseAnnuity != null">
                        AND a.enterprise_annuity = #{tSalaryAccount2022.enterpriseAnnuity}
                    </if>
                    <if test="tSalaryAccount2022.salaryTax != null">
                        AND a.salary_tax = #{tSalaryAccount2022.salaryTax}
                    </if>
                    <if test="tSalaryAccount2022.actualSalarySum != null">
                        AND a.actual_salary_sum = #{tSalaryAccount2022.actualSalarySum}
                    </if>
                    <if test="tSalaryAccount2022.annualBonus != null">
                        AND a.annual_bonus = #{tSalaryAccount2022.annualBonus}
                    </if>
                    <if test="tSalaryAccount2022.annualBonusTax != null">
                        AND a.annual_bonus_tax = #{tSalaryAccount2022.annualBonusTax}
                    </if>
                    <if test="tSalaryAccount2022.exemptionPersionTax != null">
                        AND a.exemption_persion_tax = #{tSalaryAccount2022.exemptionPersionTax}
                    </if>
                    <if test="tSalaryAccount2022.unitId != null and tSalaryAccount2022.unitId.trim() != ''">
                        AND a.unit_id = #{tSalaryAccount2022.unitId}
                    </if>
                    <if test="tSalaryAccount2022.unitNo != null and tSalaryAccount2022.unitNo.trim() != ''">
                        AND a.unit_no = #{tSalaryAccount2022.unitNo}
                    </if>
                    <if test="tSalaryAccount2022.unitName != null and tSalaryAccount2022.unitName.trim() != ''">
                        AND a.unit_name = #{tSalaryAccount2022.unitName}
                    </if>
                    <if test="tSalaryAccount2022.sumBabyMoney != null">
                        AND a.sum_baby_money = #{tSalaryAccount2022.sumBabyMoney}
                    </if>
        </if>
    </sql>
    <!--tSalaryAccount2022简单分页查询-->
    <select id="getTSalaryAccount2022Page" resultMap="tSalaryAccount2022Map">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_salary_account_2022 a
        <where>
            1=1
            <include refid="tSalaryAccount2022_where"/>
        </where>
    </select>

	<delete id="deleteSalaryAccountYearByMonth" >
		delete from ${tableName} where SETTLEMENT_MONTH = #{lastMonth};
	</delete>

	<!-- 插入2021表 -->
	<insert id="insertIntoSalaryAccountYear">
		insert into ${tableName}  (
			select
				a.id,a.INVOICE_TITLE,a.EMP_ID,a.EMP_NAME,a.EMP_IDCARD,
				a.DEPT_ID SETTLE_DEPART_ID,a.DEPT_NO SETTLE_DEPART_NO,a.DEPT_NAME SETTLE_DEPART_NAME,a.SETTLEMENT_MONTH,a.TAX_MONTH,
				ifnull(sum(relaySalary.SALARY_MONEY),0) REAL_SALARY,
				ifnull(max(costReduction.SALARY_MONEY),0) cost_reduction,
				ifnull(max(specialDeduMoney.SALARY_MONEY),0) SPECIAL_DEDU_MONEY,
				ifnull(max(sumChildEduMoney.SALARY_MONEY),0) child_eduInfo_money,
				ifnull(max(sumHousingLoanMoney.SALARY_MONEY),0) housing_loan_Interest_expense_money,
				ifnull(max(sumHousingRentMoney.SALARY_MONEY),0) housing_rentalInfo_money,
				ifnull(max(sumContinuingEducationMoney.SALARY_MONEY),0) continuing_education_expense_money,
				ifnull(max(sumSupportElderlyMoney.SALARY_MONEY),0) support_elderly_expense_money,
				ifnull(sum(personalSocial.SALARY_MONEY),0) PERSONAL_SOCIAL,
				ifnull(sum(personalFund.SALARY_MONEY),0) PERSONAL_FUND,
				ifnull(sum(withholidingPersonSocial.SALARY_MONEY),0) withholiding_person_social,
				ifnull(sum(withholidingPersonFund.SALARY_MONEY),0) withholiding_person_fund,
				ifnull(sum(enterpriseAnnuity.SALARY_MONEY),0) enterprise_annuity,
				ifnull(sum(salaryTax.SALARY_MONEY),0) salary_tax,
				ifnull(sum(actualSalarySum.SALARY_MONEY),0) actual_salary_sum,
				ifnull(sum(if(annualBonusTax.SALARY_MONEY > 0,annualBonus.SALARY_MONEY,0)),0) annual_bonus,
				ifnull(sum(annualBonusTax.SALARY_MONEY),0) annual_bonus_tax,
				ifnull(sum(exemptionPersionTax.SALARY_MONEY),0) exemption_persion_tax,
				s.unit_id,s.unit_no,s.unit_name,
				ifnull(max(sumBabyMoney.SALARY_MONEY),0) sum_baby_money,
				if(#{lastMonth} = DATE_FORMAT(e.CREATE_TIME,'%Y%m'),1,0) IS_NEW_EMPLOYEE,
				DATE_FORMAT(e.CREATE_TIME,'%Y%m') CREATE_MONTH,
				e.EMP_PHONE
			from t_salary_account a
					 left join t_salary_standard s on s.id = a.SALARY_FORM_ID
					 left join t_salary_employee e on e.id = a.EMP_ID
					 left join t_salary_account_item relaySalary on relaySalary.SALARY_ACCOUNT_ID = a.id and relaySalary.JAVA_FIED_NAME='relaySalary'
					 left join t_salary_account_item exemptionPersionTax on exemptionPersionTax.SALARY_ACCOUNT_ID = a.id and exemptionPersionTax.JAVA_FIED_NAME='exemptionPersionTax'
					 left join t_salary_account_item costReduction on costReduction.SALARY_ACCOUNT_ID = a.id and costReduction.JAVA_FIED_NAME='costReduction'
					 left join t_salary_account_item sumChildEduMoney on sumChildEduMoney.SALARY_ACCOUNT_ID = a.id and sumChildEduMoney.JAVA_FIED_NAME='sumChildEduMoney'
					 left join t_salary_account_item sumHousingLoanMoney on sumHousingLoanMoney.SALARY_ACCOUNT_ID = a.id and sumHousingLoanMoney.JAVA_FIED_NAME='sumHousingLoanMoney'
					 left join t_salary_account_item sumHousingRentMoney on sumHousingRentMoney.SALARY_ACCOUNT_ID = a.id and sumHousingRentMoney.JAVA_FIED_NAME='sumHousingRentMoney'
					 left join t_salary_account_item sumSupportElderlyMoney on sumSupportElderlyMoney.SALARY_ACCOUNT_ID = a.id and sumSupportElderlyMoney.JAVA_FIED_NAME='sumSupportElderlyMoney'
					 left join t_salary_account_item sumContinuingEducationMoney on sumContinuingEducationMoney.SALARY_ACCOUNT_ID = a.id and sumContinuingEducationMoney.JAVA_FIED_NAME='sumContinuingEducationMoney'
					 left join t_salary_account_item sumBabyMoney on sumBabyMoney.SALARY_ACCOUNT_ID = a.id and sumBabyMoney.JAVA_FIED_NAME='sumBabyMoney'
					 left join t_salary_account_item personalSocial on personalSocial.SALARY_ACCOUNT_ID = a.id and personalSocial.JAVA_FIED_NAME='personalSocial'
					 left join t_salary_account_item personalFund on personalFund.SALARY_ACCOUNT_ID = a.id and personalFund.JAVA_FIED_NAME='personalFund'
					 left join t_salary_account_item withholidingPersonSocial on withholidingPersonSocial.SALARY_ACCOUNT_ID = a.id and withholidingPersonSocial.JAVA_FIED_NAME='withholidingPersonSocial'
					 left join t_salary_account_item withholidingPersonFund on withholidingPersonFund.SALARY_ACCOUNT_ID = a.id and withholidingPersonFund.JAVA_FIED_NAME='withholidingPersonFund'
					 left join t_salary_account_item actualSalarySum on actualSalarySum.SALARY_ACCOUNT_ID = a.id and actualSalarySum.JAVA_FIED_NAME='actualSalarySum'
					 left join t_salary_account_item salaryTax on salaryTax.SALARY_ACCOUNT_ID = a.id and salaryTax.JAVA_FIED_NAME='salaryTax'
					 left join t_salary_account_item specialDeduMoney on specialDeduMoney.SALARY_ACCOUNT_ID = a.id and specialDeduMoney.JAVA_FIED_NAME='specialDeduMoney'
					 left join t_salary_account_item enterpriseAnnuity on enterpriseAnnuity.SALARY_ACCOUNT_ID = a.id and enterpriseAnnuity.JAVA_FIED_NAME='enterpriseAnnuity'
					 left join t_salary_account_item annualBonusTax on annualBonusTax.SALARY_ACCOUNT_ID = a.id and annualBonusTax.JAVA_FIED_NAME='annualBonusTax'
					 left join t_salary_account_item annualBonus on annualBonus.SALARY_ACCOUNT_ID = a.id and annualBonus.JAVA_FIED_NAME='annualBonus'
			where
				a.SETTLEMENT_MONTH = #{lastMonth} and a.DELETE_FLAG = 0 and a.FORM_TYPE != '7' and a.FORM_TYPE != '3' and s.DELETE_FLAG = 0
			GROUP BY a.EMP_IDCARD,a.INVOICE_TITLE,a.SETTLEMENT_MONTH)
	</insert>

</mapper>
