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

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

	<!-- 本期申报 -->
	<insert id="insertStatisticsCurrentReport">
		insert into t_statistics_current_report (
			select
				concat(s.id,#{declareMonth}) id,
				s.INVOICE_TITLE,
				s.SETTLE_DEPART_ID,
				s.SETTLE_DEPART_NO,
				s.SETTLE_DEPART_NAME,
				s.DECLARE_MONTH,
				s.EMP_ID,
				if(s.EMP_NAME REGEXP '[0-9]',REPLACE(s.EMP_NAME,right(s.EMP_NAME,4),''),s.EMP_NAME) EMP_NAME,
				s.EMP_IDCARD,
				s.cost_reduction,
				s.child_eduInfo_money,
				s.housing_rentalInfo_money,
				s.housing_loan_Interest_expense_money,
				s.support_elderly_expense_money,
				s.continuing_education_expense_money,
				s.REAL_SALARY - ifnull(m.all_income,0) REAL_SALARY,
				s.PERSONAL_PENSION_MONEY - ifnull(m.PERSONAL_PENSION_MONEY,0) PERSONAL_PENSION_MONEY,
				s.PERSONAL_MEDICAL_MONEY - ifnull(m.PERSONAL_MEDICAL_MONEY,0) PERSONAL_MEDICAL_MONEY,
				s.PERSONAL_UNEMPLOYMENT_MONEY - ifnull(m.PERSONAL_UNEMPLOYMENT_MONEY,0) PERSONAL_UNEMPLOYMENT_MONEY,
				s.PERSONAL_SOCIAL,
				s.PERSONAL_FUND - ifnull(m.PERSONAL_PROVIDENT_FEE,0) PERSONAL_PROVIDENT_FEE,
				s.enterprise_annuity - ifnull(m.enterprise_annuity,0) enterprise_annuity,
				s.taxable,
				s.tax_fee,
				s.QUICK_DEDUCATION,
				s.salary_tax - ifnull(m.all_tax_prepaid,0) - ifnull(m.all_tax_payable,0) salary_tax,
				s.UNIT_ID,
				s.UNIT_NO,
				s.UNIT_NAME,
				s.SPECIAL_DEDU_MONEY,
				s.sum_baby_money,
				s.SUM_PRIVATE_PENSION,
				if (#{lastMonth} = s.CREATE_MONTH,1,0) IS_NEW_EMPLOYEE,
				s.CREATE_MONTH,
				s.EMP_PHONE
			from t_statistics_tax_salary s
					 left join
				 (
					 select m.INVOICE_TITLE,m.EMP_IDCARD,
							sum(ifnull(m.PERSONAL_PENSION_MONEY,0)) PERSONAL_PENSION_MONEY,
							sum(ifnull(m.PERSONAL_MEDICAL_MONEY,0)) PERSONAL_MEDICAL_MONEY,
							sum(ifnull(m.PERSONAL_UNEMPLOYMENT_MONEY,0)) PERSONAL_UNEMPLOYMENT_MONEY,
							sum(ifnull(m.PERSONAL_PROVIDENT_FEE,0)) PERSONAL_PROVIDENT_FEE,
							sum(ifnull(m.enterprise_annuity,0)) enterprise_annuity,
							sum(if(m.declare_MONTH = #{lastTwoMonth},all_income,0)) all_income,
							sum(if(m.declare_MONTH = #{lastTwoMonth},all_tax_prepaid,0)) all_tax_prepaid,
							sum(if(m.declare_MONTH = #{lastTwoMonth},all_tax_payable,0)) all_tax_payable
					 from t_statistics_current_report_market m
					 where m.declare_MONTH <![CDATA[<=]]> #{lastTwoMonth} and m.declare_MONTH like '${lastTwoYear}%'
					 GROUP BY m.EMP_IDCARD,m.INVOICE_TITLE
				 ) m on m.EMP_IDCARD=s.EMP_IDCARD and m.INVOICE_TITLE = s.INVOICE_TITLE
			 left join t_statistics_declarer d on d.EMP_IDCARD=s.EMP_IDCARD and d.DECLARE_UNIT=s.INVOICE_TITLE
			                                          and d.OCCUPATION_TYPE='0' and d.DECLARE_MONTH=#{declareMonth}
			where s.DECLARE_MONTH = #{declareMonth} and (d.id is null or d.IS_DECLARE = '0')
		)
	</insert>

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

	<!--导出-->
	<select id="getCurrentReportExport" resultMap="tStatisticsCurrentReportMap" parameterType="com.yifu.cloud.plus.v1.yifu.salary.vo.TStatisticsCurrentReportSearchVo">
		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_PROVIDENT_FEE,
			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.SUM_PRIVATE_PENSION,
			if(a.IS_NEW_EMPLOYEE ='0','否','是') IS_NEW_EMPLOYEE,
			a.CREATE_MONTH,
			a.EMP_PHONE
		FROM t_statistics_current_report a
		<where>
			1=1
			<include refid="tStatisticsCurrentReport_where"/>
		</where>
		order by a.DECLARE_MONTH desc
		<if test="tStatisticsCurrentReport != null">
			<if test="tStatisticsCurrentReport.limitStart != null">
				limit #{tStatisticsCurrentReport.limitStart},#{tStatisticsCurrentReport.limitEnd}
			</if>
		</if>
	</select>

</mapper>