<?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"/>
	<result property="sumPrivatePension" column="SUM_PRIVATE_PENSION"/>
  </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,
                a.SUM_PRIVATE_PENSION
    </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 test="tSalaryAccount2022.sumPrivatePension != null">
						AND a.SUM_PRIVATE_PENSION = #{tSalaryAccount2022.sumPrivatePension}
					</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>

	<!-- 自动创建年度报账表 -->
	<update id="createAccountYear" statementType="STATEMENT">
		CREATE TABLE IF NOT EXISTS ${tableName} (
		`ID` varchar(32) NOT NULL COMMENT '主键',
		`INVOICE_TITLE` varchar(50) DEFAULT NULL COMMENT '封面抬头',
		`EMP_ID` varchar(32) DEFAULT NULL COMMENT '员工 ID',
		`EMP_NAME` varchar(50) DEFAULT NULL COMMENT '员工姓名',
		`EMP_IDCARD` varchar(25) DEFAULT NULL COMMENT '员工身份证号',
		`SETTLE_DEPART_ID` varchar(32) DEFAULT NULL COMMENT '结算部门id',
		`SETTLE_DEPART_NO` varchar(50) DEFAULT NULL COMMENT '结算部门no',
		`SETTLE_DEPART_NAME` varchar(50) DEFAULT NULL COMMENT '结算部门name',
		`SETTLEMENT_MONTH` varchar(20) DEFAULT NULL COMMENT '结算月',
		`TAX_MONTH` varchar(6) DEFAULT NULL COMMENT '计税月份',
		`REAL_SALARY` decimal(12,2) DEFAULT '0.00' COMMENT '应发',
		`cost_reduction` decimal(12,2) DEFAULT '0.00' COMMENT '减除',
		`SPECIAL_DEDU_MONEY` decimal(12,2) DEFAULT '0.00' COMMENT '专项',
		`child_eduInfo_money` decimal(12,2) DEFAULT '0.00' COMMENT '累计子女教育',
		`housing_loan_Interest_expense_money` decimal(12,2) DEFAULT '0.00' COMMENT '累计住房贷款利息',
		`housing_rentalInfo_money` decimal(12,2) DEFAULT '0.00' COMMENT '累计住房租金',
		`continuing_education_expense_money` decimal(12,2) DEFAULT '0.00' COMMENT '累计继续教育',
		`support_elderly_expense_money` decimal(12,2) DEFAULT '0.00' COMMENT '累计赡养老人',
		`PERSONAL_SOCIAL` decimal(12,2) DEFAULT '0.00' COMMENT '个人社保',
		`PERSONAL_FUND` decimal(12,2) DEFAULT '0.00' COMMENT '个人公积金',
		`withholiding_person_social` decimal(12,2) DEFAULT '0.00' COMMENT '代扣个人社保',
		`withholiding_person_fund` decimal(12,2) DEFAULT '0.00' COMMENT '代扣个人公积金',
		`enterprise_annuity` decimal(12,2) DEFAULT '0.00' COMMENT '企业年金',
		`salary_tax` decimal(12,2) DEFAULT '0.00' COMMENT '个税',
		`actual_salary_sum` decimal(12,2) DEFAULT '0.00' COMMENT '实发',
		`annual_bonus` decimal(12,2) DEFAULT '0.00' COMMENT '年终奖(单独扣税的部分)',
		`annual_bonus_tax` decimal(12,2) DEFAULT '0.00' COMMENT '年终奖单独扣税',
		`exemption_persion_tax` decimal(12,2) DEFAULT '0.00' COMMENT '免个税个人代扣',
		`unit_id` varchar(32) DEFAULT NULL COMMENT '单位id',
		`unit_no` varchar(50) DEFAULT NULL COMMENT '单位no',
		`unit_name` varchar(50) DEFAULT NULL COMMENT '单位name',
		`sum_baby_money` decimal(12,2) DEFAULT '0.00' COMMENT '累计婴幼儿照护费用',
		`SUM_PRIVATE_PENSION` decimal(12,2) DEFAULT '0.00' COMMENT '累计个人养老金',
		`IS_NEW_EMPLOYEE` varchar(1) DEFAULT NULL COMMENT '是否当月新增人员（0否1是）',
		`CREATE_MONTH` varchar(6) DEFAULT NULL COMMENT '入职年月',
		`EMP_PHONE` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '手机号码',
		PRIMARY KEY (`ID`) USING BTREE,
		UNIQUE KEY `SY_ACCOUNT_ID` (`ID`) USING BTREE,
		KEY `SY_ACCOUNT_SETTLEMENT_MONTH` (`SETTLEMENT_MONTH`) USING BTREE,
		KEY `SY_ACCOUNT_SETTLE_DEPART_ID` (`SETTLE_DEPART_ID`) USING BTREE,
		KEY `SY_ACCOUNT_EMP_IDCARD` (`EMP_IDCARD`) USING BTREE,
		KEY `SY_ACCOUNT_EMP_NAME` (`EMP_NAME`) USING BTREE,
		KEY `SY_ACCOUNT_INVOICETITLE` (`INVOICE_TITLE`) USING BTREE
		) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='工资报账年度表'
	</update>

	<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,
				ifnull(max(sumPrivatePension.SALARY_MONEY),0) SUM_PRIVATE_PENSION,
				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 sumPrivatePension on sumPrivatePension.SALARY_ACCOUNT_ID = a.id and sumPrivatePension.JAVA_FIED_NAME='sumPrivatePension'
					 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 a.FORM_TYPE != '4' and s.DELETE_FLAG = 0
			GROUP BY a.EMP_IDCARD,a.INVOICE_TITLE,a.SETTLEMENT_MONTH)
	</insert>

</mapper>
