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

  <resultMap id="tStatisticsBonusMap" type="com.yifu.cloud.plus.v1.yifu.salary.entity.TStatisticsBonus">
    <id property="id" column="ID"/>
    <result property="declareUnit" column="DECLARE_UNIT"/>
    <result property="declareMonth" column="DECLARE_MONTH"/>
    <result property="empName" column="EMP_NAME"/>
    <result property="empIdcard" column="EMP_IDCARD"/>
    <result property="annualBonus" column="ANNUAL_BONUS"/>
    <result property="bonusTax" column="BONUS_TAX"/>
    <result property="deptId" column="DEPT_ID"/>
    <result property="deptNo" column="DEPT_NO"/>
    <result property="deptName" column="DEPT_NAME"/>
    <result property="unitId" column="UNIT_ID"/>
    <result property="unitNo" column="UNIT_NO"/>
  </resultMap>
    <sql id="Base_Column_List">
                a.ID,
                a.DECLARE_UNIT,
                a.DECLARE_MONTH,
                a.EMP_NAME,
                a.EMP_IDCARD,
                a.ANNUAL_BONUS,
                a.BONUS_TAX,
                a.DEPT_ID,
                a.DEPT_NO,
                a.DEPT_NAME,
                a.UNIT_ID,
                a.UNIT_NO
    </sql>
    <sql id="tStatisticsBonus_where">
        <if test="tStatisticsBonus != null">
                    <if test="tStatisticsBonus.id != null and tStatisticsBonus.id.trim() != ''">
                        AND a.ID = #{tStatisticsBonus.id}
                    </if>
                    <if test="tStatisticsBonus.declareUnit != null and tStatisticsBonus.declareUnit.trim() != ''">
                        AND a.DECLARE_UNIT = #{tStatisticsBonus.declareUnit}
                    </if>
                    <if test="tStatisticsBonus.declareMonth != null and tStatisticsBonus.declareMonth.trim() != ''">
                        AND a.DECLARE_MONTH = #{tStatisticsBonus.declareMonth}
                    </if>
                    <if test="tStatisticsBonus.empName != null and tStatisticsBonus.empName.trim() != ''">
                        AND a.EMP_NAME = #{tStatisticsBonus.empName}
                    </if>
                    <if test="tStatisticsBonus.empIdcard != null and tStatisticsBonus.empIdcard.trim() != ''">
                        AND a.EMP_IDCARD = #{tStatisticsBonus.empIdcard}
                    </if>
                    <if test="tStatisticsBonus.annualBonus != null">
                        AND a.ANNUAL_BONUS = #{tStatisticsBonus.annualBonus}
                    </if>
                    <if test="tStatisticsBonus.bonusTax != null">
                        AND a.BONUS_TAX = #{tStatisticsBonus.bonusTax}
                    </if>
                    <if test="tStatisticsBonus.deptId != null and tStatisticsBonus.deptId.trim() != ''">
                        AND a.DEPT_ID = #{tStatisticsBonus.deptId}
                    </if>
                    <if test="tStatisticsBonus.deptNo != null and tStatisticsBonus.deptNo.trim() != ''">
                        AND a.DEPT_NO = #{tStatisticsBonus.deptNo}
                    </if>
                    <if test="tStatisticsBonus.deptName != null and tStatisticsBonus.deptName.trim() != ''">
                        AND a.DEPT_NAME = #{tStatisticsBonus.deptName}
                    </if>
                    <if test="tStatisticsBonus.unitId != null and tStatisticsBonus.unitId.trim() != ''">
                        AND a.UNIT_ID = #{tStatisticsBonus.unitId}
                    </if>
                    <if test="tStatisticsBonus.unitNo != null and tStatisticsBonus.unitNo.trim() != ''">
                        AND a.UNIT_NO = #{tStatisticsBonus.unitNo}
                    </if>
        </if>
    </sql>
    <!--tStatisticsBonus简单分页查询-->
    <select id="getTStatisticsBonusPage" resultMap="tStatisticsBonusMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_statistics_bonus a
        <where>
            1=1
            <include refid="tStatisticsBonus_where"/>
        </where>
		order by a.DECLARE_MONTH desc
    </select>

	<!--统计-->
	<select id="doStatisticsBonus" resultMap="tStatisticsBonusMap">
		select
			#{nowMonth} DECLARE_MONTH,a.EMP_NAME,a.EMP_IDCARD,a.INVOICE_TITLE DECLARE_UNIT,
			sum(annualbonus.SALARY_MONEY) ANNUAL_BONUS,sum(tax.SALARY_MONEY) BONUS_TAX
		from t_salary_account a
				 left join t_salary_account_item annualbonus on annualbonus.SALARY_ACCOUNT_ID = a.id and annualbonus.CN_NAME='年终奖'
				 left join t_salary_account_item tax on tax.SALARY_ACCOUNT_ID = a.id and tax.CN_NAME='年终奖单独扣税'
		where a.DELETE_FLAG = 0 and annualbonus.SALARY_MONEY > 0 and tax.SALARY_MONEY > 0 and a.SETTLEMENT_MONTH like '${lastYear}%'
		GROUP BY a.EMP_IDCARD,a.EMP_NAME
	</select>
</mapper>
