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

  <resultMap id="tStatisticsTaxSalaryMap" type="com.yifu.cloud.plus.v1.yifu.salary.entity.TStatisticsTaxSalary">
    <id property="id" column="id"/>
    <result property="invoiceTitle" column="INVOICE_TITLE"/>
    <result property="settleDepartId" column="SETTLE_DEPART_ID"/>
    <result property="settleDepartNo" column="SETTLE_DEPART_NO"/>
    <result property="settleDepartName" column="SETTLE_DEPART_NAME"/>
    <result property="declareMonth" column="DECLARE_MONTH"/>
    <result property="empId" column="EMP_ID"/>
    <result property="empName" column="EMP_NAME"/>
    <result property="empIdcard" column="EMP_IDCARD"/>
    <result property="costReduction" column="cost_reduction"/>
    <result property="childEduinfoMoney" column="child_eduInfo_money"/>
    <result property="housingRentalinfoMoney" column="housing_rentalInfo_money"/>
    <result property="housingLoanInterestExpenseMoney" column="housing_loan_Interest_expense_money"/>
    <result property="supportElderlyExpenseMoney" column="support_elderly_expense_money"/>
    <result property="continuingEducationExpenseMoney" column="continuing_education_expense_money"/>
    <result property="realSalary" column="REAL_SALARY"/>
    <result property="personalPensionMoney" column="PERSONAL_PENSION_MONEY"/>
    <result property="personalMedicalMoney" column="PERSONAL_MEDICAL_MONEY"/>
    <result property="personalUnemploymentMoney" column="PERSONAL_UNEMPLOYMENT_MONEY"/>
    <result property="personalSocial" column="PERSONAL_SOCIAL"/>
    <result property="personalFund" column="PERSONAL_FUND"/>
    <result property="enterpriseAnnuity" column="enterprise_annuity"/>
    <result property="taxable" column="taxable"/>
    <result property="taxFee" column="tax_fee"/>
    <result property="quickDeducation" column="QUICK_DEDUCATION"/>
    <result property="salaryTax" column="salary_tax"/>
    <result property="unitId" column="UNIT_ID"/>
    <result property="unitNo" column="UNIT_NO"/>
    <result property="unitName" column="UNIT_NAME"/>
    <result property="specialDeduMoney" column="SPECIAL_DEDU_MONEY"/>
    <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.SETTLE_DEPART_ID,
                a.SETTLE_DEPART_NO,
                a.SETTLE_DEPART_NAME,
                a.DECLARE_MONTH,
                a.EMP_ID,
                a.EMP_NAME,
                a.EMP_IDCARD,
                a.cost_reduction,
                a.child_eduInfo_money,
                a.housing_rentalInfo_money,
                a.housing_loan_Interest_expense_money,
                a.support_elderly_expense_money,
                a.continuing_education_expense_money,
                a.REAL_SALARY,
                a.PERSONAL_PENSION_MONEY,
                a.PERSONAL_MEDICAL_MONEY,
                a.PERSONAL_UNEMPLOYMENT_MONEY,
                a.PERSONAL_SOCIAL,
                a.PERSONAL_FUND,
                a.enterprise_annuity,
                a.taxable,
                a.tax_fee,
                a.QUICK_DEDUCATION,
                a.salary_tax,
                a.UNIT_ID,
                a.UNIT_NO,
                a.UNIT_NAME,
                a.SPECIAL_DEDU_MONEY,
                a.sum_baby_money,
                a.IS_NEW_EMPLOYEE,
                a.CREATE_MONTH,
                a.EMP_PHONE,
                a.SUM_PRIVATE_PENSION
    </sql>
    <sql id="tStatisticsTaxSalary_where">
        <if test="tStatisticsTaxSalary != null">
                    <if test="tStatisticsTaxSalary.id != null and tStatisticsTaxSalary.id.trim() != ''">
                        AND a.id = #{tStatisticsTaxSalary.id}
                    </if>
                    <if test="tStatisticsTaxSalary.invoiceTitle != null and tStatisticsTaxSalary.invoiceTitle.trim() != ''">
                        AND a.INVOICE_TITLE = #{tStatisticsTaxSalary.invoiceTitle}
                    </if>
                    <if test="tStatisticsTaxSalary.settleDepartId != null and tStatisticsTaxSalary.settleDepartId.trim() != ''">
                        AND a.SETTLE_DEPART_ID = #{tStatisticsTaxSalary.settleDepartId}
                    </if>
                    <if test="tStatisticsTaxSalary.settleDepartNo != null and tStatisticsTaxSalary.settleDepartNo.trim() != ''">
                        AND a.SETTLE_DEPART_NO = #{tStatisticsTaxSalary.settleDepartNo}
                    </if>
                    <if test="tStatisticsTaxSalary.settleDepartName != null and tStatisticsTaxSalary.settleDepartName.trim() != ''">
                        AND a.SETTLE_DEPART_NAME = #{tStatisticsTaxSalary.settleDepartName}
                    </if>
                    <if test="tStatisticsTaxSalary.declareMonth != null and tStatisticsTaxSalary.declareMonth.trim() != ''">
                        AND a.DECLARE_MONTH = #{tStatisticsTaxSalary.declareMonth}
                    </if>
                    <if test="tStatisticsTaxSalary.empId != null and tStatisticsTaxSalary.empId.trim() != ''">
                        AND a.EMP_ID = #{tStatisticsTaxSalary.empId}
                    </if>
                    <if test="tStatisticsTaxSalary.empName != null and tStatisticsTaxSalary.empName.trim() != ''">
                        AND a.EMP_NAME = #{tStatisticsTaxSalary.empName}
                    </if>
                    <if test="tStatisticsTaxSalary.empIdcard != null and tStatisticsTaxSalary.empIdcard.trim() != ''">
                        AND a.EMP_IDCARD = #{tStatisticsTaxSalary.empIdcard}
                    </if>
                    <if test="tStatisticsTaxSalary.costReduction != null">
                        AND a.cost_reduction = #{tStatisticsTaxSalary.costReduction}
                    </if>
                    <if test="tStatisticsTaxSalary.childEduinfoMoney != null">
                        AND a.child_eduInfo_money = #{tStatisticsTaxSalary.childEduinfoMoney}
                    </if>
                    <if test="tStatisticsTaxSalary.housingRentalinfoMoney != null">
                        AND a.housing_rentalInfo_money = #{tStatisticsTaxSalary.housingRentalinfoMoney}
                    </if>
                    <if test="tStatisticsTaxSalary.housingLoanInterestExpenseMoney != null">
                        AND a.housing_loan_Interest_expense_money = #{tStatisticsTaxSalary.housingLoanInterestExpenseMoney}
                    </if>
                    <if test="tStatisticsTaxSalary.supportElderlyExpenseMoney != null">
                        AND a.support_elderly_expense_money = #{tStatisticsTaxSalary.supportElderlyExpenseMoney}
                    </if>
                    <if test="tStatisticsTaxSalary.continuingEducationExpenseMoney != null">
                        AND a.continuing_education_expense_money = #{tStatisticsTaxSalary.continuingEducationExpenseMoney}
                    </if>
                    <if test="tStatisticsTaxSalary.realSalary != null">
                        AND a.REAL_SALARY = #{tStatisticsTaxSalary.realSalary}
                    </if>
                    <if test="tStatisticsTaxSalary.personalPensionMoney != null">
                        AND a.PERSONAL_PENSION_MONEY = #{tStatisticsTaxSalary.personalPensionMoney}
                    </if>
                    <if test="tStatisticsTaxSalary.personalMedicalMoney != null">
                        AND a.PERSONAL_MEDICAL_MONEY = #{tStatisticsTaxSalary.personalMedicalMoney}
                    </if>
                    <if test="tStatisticsTaxSalary.personalUnemploymentMoney != null">
                        AND a.PERSONAL_UNEMPLOYMENT_MONEY = #{tStatisticsTaxSalary.personalUnemploymentMoney}
                    </if>
                    <if test="tStatisticsTaxSalary.personalSocial != null">
                        AND a.PERSONAL_SOCIAL = #{tStatisticsTaxSalary.personalSocial}
                    </if>
                    <if test="tStatisticsTaxSalary.personalFund != null">
                        AND a.PERSONAL_FUND = #{tStatisticsTaxSalary.personalFund}
                    </if>
                    <if test="tStatisticsTaxSalary.enterpriseAnnuity != null">
                        AND a.enterprise_annuity = #{tStatisticsTaxSalary.enterpriseAnnuity}
                    </if>
                    <if test="tStatisticsTaxSalary.taxable != null">
                        AND a.taxable = #{tStatisticsTaxSalary.taxable}
                    </if>
                    <if test="tStatisticsTaxSalary.taxFee != null">
                        AND a.tax_fee = #{tStatisticsTaxSalary.taxFee}
                    </if>
                    <if test="tStatisticsTaxSalary.quickDeducation != null">
                        AND a.QUICK_DEDUCATION = #{tStatisticsTaxSalary.quickDeducation}
                    </if>
                    <if test="tStatisticsTaxSalary.salaryTax != null">
                        AND a.salary_tax = #{tStatisticsTaxSalary.salaryTax}
                    </if>
                    <if test="tStatisticsTaxSalary.unitId != null and tStatisticsTaxSalary.unitId.trim() != ''">
                        AND a.UNIT_ID = #{tStatisticsTaxSalary.unitId}
                    </if>
                    <if test="tStatisticsTaxSalary.unitNo != null and tStatisticsTaxSalary.unitNo.trim() != ''">
                        AND a.UNIT_NO = #{tStatisticsTaxSalary.unitNo}
                    </if>
                    <if test="tStatisticsTaxSalary.unitName != null and tStatisticsTaxSalary.unitName.trim() != ''">
                        AND a.UNIT_NAME = #{tStatisticsTaxSalary.unitName}
                    </if>
                    <if test="tStatisticsTaxSalary.specialDeduMoney != null">
                        AND a.SPECIAL_DEDU_MONEY = #{tStatisticsTaxSalary.specialDeduMoney}
                    </if>
                    <if test="tStatisticsTaxSalary.sumBabyMoney != null">
                        AND a.sum_baby_money = #{tStatisticsTaxSalary.sumBabyMoney}
                    </if>
					<if test="tStatisticsTaxSalary.sumPrivatePension != null">
						AND a.SUM_PRIVATE_PENSION = #{tStatisticsTaxSalary.sumPrivatePension}
					</if>
        </if>
    </sql>
    <!--tStatisticsTaxSalary简单分页查询-->
    <select id="getTStatisticsTaxSalaryPage" resultMap="tStatisticsTaxSalaryMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_statistics_tax_salary a
        <where>
            1=1
            <include refid="tStatisticsTaxSalary_where"/>
        </where>
    </select>

	<insert id="insertStatisticsTaxSalaryByTableName">
		insert into t_statistics_tax_salary (
			select
				a.id,a.INVOICE_TITLE,a.SETTLE_DEPART_ID,a.SETTLE_DEPART_NO,a.SETTLE_DEPART_NAME
				 ,a.DECLARE_MONTH,a.EMP_ID,a.EMP_NAME,a.EMP_IDCARD,a.cost_reduction
				 ,a.child_eduInfo_money,a.housing_rentalInfo_money,a.housing_loan_Interest_expense_money
				 ,a.support_elderly_expense_money,a.continuing_education_expense_money
				 ,a.REAL_SALARY,a.PERSONAL_PENSION_MONEY,a.PERSONAL_MEDICAL_MONEY,a.PERSONAL_UNEMPLOYMENT_MONEY
				 ,a.PERSONAL_SOCIAL,a.PERSONAL_FUND,a.enterprise_annuity,a.taxable,ifnull(c.WITHHOLDING_RATE,0) tax_fee
				 ,ifnull(c.QUICK_DEDUCATION,0) QUICK_DEDUCATION,a.salary_tax,a.unit_id,a.unit_no,a.unit_name,a.SPECIAL_DEDU_MONEY
				 ,a.sum_baby_money,a.SUM_PRIVATE_PENSION,
				a.IS_NEW_EMPLOYEE,
				a.CREATE_MONTH,
				a.EMP_PHONE
			from (
					 SELECT
						 concat(a.id,#{declareMonth}) id,
						 #{declareMonth} DECLARE_MONTH,
						 a.INVOICE_TITLE,
						 a.EMP_ID,
						 a.EMP_NAME,
						 a.EMP_IDCARD,
						 a.SETTLE_DEPART_ID,
						 a.SETTLE_DEPART_NO,
						 a.SETTLE_DEPART_NAME,
						 sum(a.REAL_SALARY) - sum(a.exemption_persion_tax) - sum(if(a.annual_bonus_tax > 0,a.annual_bonus,0)) REAL_SALARY,
						 max(a.cost_reduction) cost_reduction,
						 max(a.SPECIAL_DEDU_MONEY) SPECIAL_DEDU_MONEY,
						 max(a.child_eduInfo_money) child_eduInfo_money,
						 max(a.housing_loan_Interest_expense_money) housing_loan_Interest_expense_money,
						 max(a.housing_rentalInfo_money) housing_rentalInfo_money,
						 max(a.continuing_education_expense_money) continuing_education_expense_money,
						 max(a.support_elderly_expense_money) support_elderly_expense_money,
						 max(a.sum_baby_money) sum_baby_money,
						 max(a.SUM_PRIVATE_PENSION) SUM_PRIVATE_PENSION,
						 sum(a.PERSONAL_SOCIAL) + sum(a.withholiding_person_social) PERSONAL_SOCIAL,
						 round(((sum(a.PERSONAL_SOCIAL) + sum(a.withholiding_person_social)) * 0.75),2) PERSONAL_PENSION_MONEY,
						 round(((sum(a.PERSONAL_SOCIAL) + sum(a.withholiding_person_social)) * 0.2),2) PERSONAL_MEDICAL_MONEY,
						 round(((sum(a.PERSONAL_SOCIAL) + sum(a.withholiding_person_social)) * 0.05),2) PERSONAL_UNEMPLOYMENT_MONEY,
						 sum(a.PERSONAL_FUND) + sum(a.withholiding_person_fund) PERSONAL_FUND,
						 sum(a.REAL_SALARY) - sum(a.exemption_persion_tax)
							 - max(a.SPECIAL_DEDU_MONEY)
							 - sum(a.PERSONAL_SOCIAL) - sum(a.withholiding_person_social)
							 - sum(a.PERSONAL_FUND) - sum(a.withholiding_person_fund)
							 - max(a.cost_reduction)
							 - sum(a.enterprise_annuity)
							 - sum(if(a.annual_bonus_tax > 0,a.annual_bonus,0)) taxable,
						 sum(a.salary_tax) salary_tax,
						 sum(a.enterprise_annuity) enterprise_annuity,
						 a.unit_id,
						 a.unit_no,
						 a.unit_name,
						 a.IS_NEW_EMPLOYEE,
						 a.CREATE_MONTH,
						 a.EMP_PHONE
					 FROM ${tableName} a
					 where a.SETTLEMENT_MONTH <![CDATA[ <= ]]>  #{lastMonth} and a.SETTLEMENT_MONTH like concat(#{lastYear},'%')
					 GROUP BY a.EMP_IDCARD,a.INVOICE_TITLE ORDER BY a.settle_depart_id asc
				 ) a
					 left join t_salary_tax_config c on c.MIN_INCOME <![CDATA[ < ]]> a.taxable and c.MAX_INCOME >= a.taxable and c.TYPE=0
		)
	</insert>

	<!-- 删除-按申报月份 -->
	<delete id="deleteByYearMonth">
		DELETE FROM t_statistics_tax_salary WHERE DECLARE_MONTH = #{yearMonth}
	</delete>

</mapper>