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

  <resultMap id="tStatisticsDeclarerMap" type="com.yifu.cloud.plus.v1.yifu.salary.entity.TStatisticsDeclarer">
    <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="empPhone" column="EMP_PHONE"/>
    <result property="occupationType" column="OCCUPATION_TYPE"/>
    <result property="occupationMonth" column="OCCUPATION_MONTH"/>
    <result property="isZeroDeclare" column="IS_ZERO_DECLARE"/>
    <result property="isDeclare" column="IS_DECLARE"/>
    <result property="undeclareReason" column="UNDECLARE_REASON"/>
	  <result property="isZeroFlag" column="IS_ZERO_FLAG"/>
  </resultMap>
    <sql id="Base_Column_List">
                a.ID,
                a.DECLARE_UNIT,
                a.DECLARE_MONTH,
                a.EMP_NAME,
                a.EMP_IDCARD,
                a.EMP_PHONE,
                a.OCCUPATION_TYPE,
                a.OCCUPATION_MONTH,
                a.IS_ZERO_DECLARE,
                a.IS_DECLARE,
                a.UNDECLARE_REASON
    </sql>
    <sql id="tStatisticsDeclarer_where">
        <if test="tStatisticsDeclarer != null">
                    <if test="tStatisticsDeclarer.id != null and tStatisticsDeclarer.id.trim() != ''">
                        AND a.ID = #{tStatisticsDeclarer.id}
                    </if>
                    <if test="tStatisticsDeclarer.declareUnit != null and tStatisticsDeclarer.declareUnit.trim() != ''">
                        AND a.DECLARE_UNIT like concat(#{tStatisticsDeclarer.declareUnit},'%')
                    </if>
                    <if test="tStatisticsDeclarer.declareMonth != null and tStatisticsDeclarer.declareMonth.trim() != ''">
                        AND a.DECLARE_MONTH = #{tStatisticsDeclarer.declareMonth}
                    </if>
                    <if test="tStatisticsDeclarer.empName != null and tStatisticsDeclarer.empName.trim() != ''">
                        AND a.EMP_NAME = #{tStatisticsDeclarer.empName}
                    </if>
                    <if test="tStatisticsDeclarer.empIdcard != null and tStatisticsDeclarer.empIdcard.trim() != ''">
                        AND a.EMP_IDCARD = #{tStatisticsDeclarer.empIdcard}
                    </if>
                    <if test="tStatisticsDeclarer.empPhone != null and tStatisticsDeclarer.empPhone.trim() != ''">
                        AND a.EMP_PHONE = #{tStatisticsDeclarer.empPhone}
                    </if>
                    <if test="tStatisticsDeclarer.occupationType != null and tStatisticsDeclarer.occupationType.trim() != ''">
                        AND a.OCCUPATION_TYPE = #{tStatisticsDeclarer.occupationType}
                    </if>
                    <if test="tStatisticsDeclarer.occupationMonth != null and tStatisticsDeclarer.occupationMonth.trim() != ''">
                        AND a.OCCUPATION_MONTH = #{tStatisticsDeclarer.occupationMonth}
                    </if>
                    <if test="tStatisticsDeclarer.isZeroDeclare != null and tStatisticsDeclarer.isZeroDeclare.trim() != ''">
                        AND a.IS_ZERO_DECLARE = #{tStatisticsDeclarer.isZeroDeclare}
                    </if>
                    <if test="tStatisticsDeclarer.isDeclare != null and tStatisticsDeclarer.isDeclare.trim() != ''">
						AND a.IS_DECLARE = #{tStatisticsDeclarer.isDeclare}
                    </if>
                    <if test="tStatisticsDeclarer.undeclareReason != null and tStatisticsDeclarer.undeclareReason.trim() != ''">
                        AND a.UNDECLARE_REASON = #{tStatisticsDeclarer.undeclareReason}
                    </if>
        </if>
    </sql>

	<sql id="tStatisticsDeclarer_export_where">
		<if test="tStatisticsDeclarer != null">
			<if test="tStatisticsDeclarer.idList != null">
				AND a.ID in
				<foreach item="idStr" index="index" collection="tStatisticsDeclarer.idList" open="(" separator="," close=")">
					#{idStr}
				</foreach>
			</if>
			<if test="tStatisticsDeclarer.id != null and tStatisticsDeclarer.id.trim() != ''">
				AND a.ID = #{tStatisticsDeclarer.id}
			</if>
			<if test="tStatisticsDeclarer.declareUnit != null and tStatisticsDeclarer.declareUnit.trim() != ''">
				AND a.DECLARE_UNIT like concat(#{tStatisticsDeclarer.declareUnit},'%')
			</if>
			<if test="tStatisticsDeclarer.declareMonth != null and tStatisticsDeclarer.declareMonth.trim() != ''">
				AND a.DECLARE_MONTH = #{tStatisticsDeclarer.declareMonth}
			</if>
			<if test="tStatisticsDeclarer.empName != null and tStatisticsDeclarer.empName.trim() != ''">
				AND a.EMP_NAME = #{tStatisticsDeclarer.empName}
			</if>
			<if test="tStatisticsDeclarer.empIdcard != null and tStatisticsDeclarer.empIdcard.trim() != ''">
				AND a.EMP_IDCARD = #{tStatisticsDeclarer.empIdcard}
			</if>
			<if test="tStatisticsDeclarer.isZeroDeclare != null and tStatisticsDeclarer.isZeroDeclare.trim() != ''">
				AND a.IS_ZERO_DECLARE = #{tStatisticsDeclarer.isZeroDeclare}
			</if>
		</if>
	</sql>
    <!--tStatisticsDeclarer简单分页查询-->
    <select id="getTStatisticsDeclarerPage" resultMap="tStatisticsDeclarerMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_statistics_declarer a
        <where>
            1=1
            <include refid="tStatisticsDeclarer_where"/>
        </where>
		order by a.DECLARE_MONTH desc
    </select>

	<!--tStatisticsDeclarer简单分页查询-->
	<select id="exportDeclarer" resultMap="tStatisticsDeclarerMap">
		SELECT
		a.DECLARE_UNIT,
		a.DECLARE_MONTH,
		a.EMP_NAME,
		a.EMP_IDCARD,
		a.EMP_PHONE,
		CASE WHEN a.OCCUPATION_TYPE=0 THEN "雇员"
		WHEN a.OCCUPATION_TYPE =1 THEN "其他"
		ELSE a.OCCUPATION_TYPE END as "OCCUPATION_TYPE",
		CASE WHEN a.IS_ZERO_DECLARE=0 THEN "是"
		WHEN a.IS_ZERO_DECLARE =1 THEN "否"
		ELSE a.IS_ZERO_DECLARE END as "IS_ZERO_DECLARE",
		CASE WHEN a.IS_DECLARE=0 THEN "是"
		WHEN a.IS_DECLARE =1 THEN "否"
		ELSE a.IS_DECLARE END as "IS_DECLARE",
		a.UNDECLARE_REASON
		FROM t_statistics_declarer a
		<where>
			1=1
			<include refid="tStatisticsDeclarer_export_where"/>
		</where>
		order by a.DECLARE_MONTH desc
		<if test="tStatisticsDeclarer.limitStart != null">
			limit #{tStatisticsDeclarer.limitStart},#{tStatisticsDeclarer.limitEnd}
		</if>
	</select>

	<!--统计-->
	<select id="doStatisticsTaxDeclarer" resultMap="tStatisticsDeclarerMap">
		SELECT
		#{nowMonth} DECLARE_MONTH,c.EMP_NAME,c.EMP_IDCARD,c.EMP_PHONE,c.INVOICE_TITLE DECLARE_UNIT,
		if(c.FORM_TYPE=0,'0',if(c.FORM_TYPE=3 or c.FORM_TYPE=4,'1','-')) OCCUPATION_TYPE,
		'1' IS_ZERO_DECLARE,
		'0' IS_ZERO_FLAG
		FROM
		(SELECT s.* FROM t_salary_account s,
		(SELECT a.EMP_NAME,a.EMP_IDCARD,a.EMP_PHONE,a.INVOICE_TITLE,a.FORM_TYPE,MAX(CREATE_TIME) max_day,a.SETTLEMENT_MONTH,
			if(a.FORM_TYPE=0,'0',if(a.FORM_TYPE=3 or a.FORM_TYPE=4,'1','-')) CUPATION_TYPE
			FROM t_salary_account a WHERE a.DELETE_FLAG = '0' AND a.SETTLEMENT_MONTH = #{lastMonth}
		 GROUP BY a.EMP_IDCARD,a.INVOICE_TITLE,CUPATION_TYPE,a.SETTLEMENT_MONTH) b
		 WHERE s.DELETE_FLAG = '0' AND b.max_day = s.CREATE_TIME
		   AND s.EMP_IDCARD = b.EMP_IDCARD AND s.INVOICE_TITLE = b.INVOICE_TITLE
		   AND s.SETTLEMENT_MONTH = b.SETTLEMENT_MONTH) c
		GROUP BY c.EMP_IDCARD,c.INVOICE_TITLE,c.SETTLEMENT_MONTH,OCCUPATION_TYPE
	</select>
	<select id="doStatisticsTaxDeclarerYear" resultMap="tStatisticsDeclarerMap">
		select 	b.DECLARE_MONTH,b.EMP_NAME,b.EMP_IDCARD,b.EMP_PHONE,b.DECLARE_UNIT,
				  b.OCCUPATION_TYPE,
				  b.IS_ZERO_DECLARE,
				  b.IS_ZERO_FLAG from (
				SELECT
				#{nowMonth} DECLARE_MONTH,c.EMP_NAME,c.EMP_IDCARD,c.EMP_PHONE,c.INVOICE_TITLE DECLARE_UNIT,
				if(c.FORM_TYPE=0 or c.FORM_TYPE=1 or c.FORM_TYPE=2,'0',if(c.FORM_TYPE=3 or c.FORM_TYPE=4,'1','-')) OCCUPATION_TYPE,
				'2' IS_ZERO_DECLARE,
				if(DATE_FORMAT(max(c.CREATE_TIME),"%Y%m")>=DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL 3 MONTH),"%Y%m"),'1','0') IS_ZERO_FLAG
				FROM t_salary_account c WHERE c.DELETE_FLAG = '0' and c.SETTLEMENT_MONTH like concat(DATE_FORMAT(NOW(), '%Y'), '%')
				GROUP BY c.EMP_IDCARD
				having  DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL 1 MONTH),"%Y%m")
				>DATE_FORMAT(max(c.CREATE_TIME),"%Y%m")) b
				group by b.EMP_IDCARD,b.DECLARE_UNIT,b.OCCUPATION_TYPE
	</select>
</mapper>
