<?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="isDeclare" column="IS_DECLARE"/>
    <result property="undeclareReason" column="UNDECLARE_REASON"/>
  </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_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.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>
    <!--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>

	<!--统计-->
	<select id="doStatisticsTaxDeclarer" resultMap="tStatisticsDeclarerMap">
		select
			#{nowMonth} DECLARE_MONTH,a.EMP_NAME,a.EMP_IDCARD,a.EMP_PHONE,a.INVOICE_TITLE DECLARE_UNIT,
			if(a.FORM_TYPE=0,'雇员',if(a.FORM_TYPE=3 or a.FORM_TYPE=4,'其他','-')) OCCUPATION_TYPE
		from t_salary_account a
	    left join t_salary_employee b on b.id = a.EMP_ID
		where a.DELETE_FLAG = '0' and a.SETTLEMENT_MONTH like '${lastYear}%'
		GROUP BY a.EMP_IDCARD,OCCUPATION_TYPE,b.UNIT_ID,a.SETTLEMENT_MONTH
	</select>
</mapper>
