<?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.social.mapper.TIncomeMapper">

	<resultMap id="tIncomeMap" type="com.yifu.cloud.plus.v1.yifu.social.entity.TIncome">
		<id property="id" column="ID"/>
		<result property="empName" column="EMP_NAME"/>
		<result property="empIdcard" column="EMP_IDCARD"/>
		<result property="unitId" column="UNIT_ID"/>
		<result property="unitName" column="UNIT_NAME"/>
		<result property="unitNo" column="UNIT_NO"/>
		<result property="deptId" column="DEPT_ID"/>
		<result property="deptName" column="DEPT_NAME"/>
		<result property="deptNo" column="DEPT_NO"/>
		<result property="feeType" column="FEE_TYPE"/>
		<result property="feeMode" column="FEE_MODE"/>
		<result property="charges" column="CHARGES"/>
		<result property="sourceType" column="SOURCE_TYPE"/>
		<result property="money" column="MONEY"/>
		<result property="createMonth" column="CREATE_MONTH"/>
		<result property="payMonth" column="PAY_MONTH"/>
		<result property="createTime" column="CREATE_TIME"/>
		<result property="dataCreateMonth" column="DATA_CREATE_MONTH"/>
		<result property="sendStatus" column="SEND_STATUS"/>
		<result property="redData" column="RED_DATA"/>
		<result property="sendStatus" column="SEND_STATUS"/>
		<result property="sendTime" column="SEND_TIME"/>
		<result property="sendMonth" column="SEND_MONTH"/>
		<result property="ekpId" column="EKP_ID"/>
		<result property="applyNo" column="APPLY_NO"/>
		<result property="incomeSettleFlag" column="INCOME_SETTLE_FLAG"/>
		<result property="incomeCollectFlag" column="INCOME_COLLECT_FLAG"/>
	</resultMap>
	<resultMap id="incomeExportMap" type="com.yifu.cloud.plus.v1.yifu.social.vo.IncomeExportVo">
		<result property="empName" column="EMP_NAME"/>
		<result property="empIdcard" column="EMP_IDCARD"/>
		<result property="unitName" column="UNIT_NAME"/>
		<result property="deptName" column="DEPT_NAME"/>
		<result property="deptNo" column="DEPT_NO"/>
		<result property="feeType" column="FEE_TYPE"/>
		<result property="feeMode" column="FEE_MODE"/>
		<result property="charges" column="CHARGES"/>
		<result property="sourceType" column="SOURCE_TYPE"/>
		<result property="money" column="MONEY"/>
		<result property="createMonth" column="CREATE_MONTH"/>
		<result property="payMonth" column="PAY_MONTH"/>
		<result property="createTime" column="CREATE_TIME"/>
		<result property="sendStatus" column="SEND_STATUS"/>
	</resultMap>
	<sql id="Base_Column_List">
		a.ID,
		a.EMP_NAME,
		a.EMP_IDCARD,
		a.UNIT_ID,
		a.UNIT_NAME,
		a.UNIT_NO,
		a.DEPT_ID,
		a.DEPT_NAME,
		a.DEPT_NO,
		a.FEE_TYPE,
		a.FEE_MODE,
		a.CHARGES,
		a.SOURCE_TYPE,
		a.MONEY,
		a.CREATE_MONTH,
		a.PAY_MONTH,
		a.CREATE_TIME,
		a.DATA_CREATE_MONTH,
		a.RED_DATA,
		a.SEND_STATUS,
		a.SEND_TIME,
		a.SEND_MONTH,
		a.APPLY_NO
	</sql>
	<sql id="tIncome_where">
		<if test="tIncome != null">
			<if test="tIncome.id != null and tIncome.id.trim() != ''">
				AND a.ID = #{tIncome.id}
			</if>
			<if test="tIncome.empName != null and tIncome.empName.trim() != ''">
				AND a.EMP_NAME = #{tIncome.empName}
			</if>
			<if test="tIncome.empIdcard != null and tIncome.empIdcard.trim() != ''">
				AND a.EMP_IDCARD = #{tIncome.empIdcard}
			</if>
			<if test="tIncome.unitId != null and tIncome.unitId.trim() != ''">
				AND a.UNIT_ID = #{tIncome.unitId}
			</if>
			<if test="tIncome.unitName != null and tIncome.unitName.trim() != ''">
				AND a.UNIT_NAME = #{tIncome.unitName}
			</if>
			<if test="tIncome.unitNo != null and tIncome.unitNo.trim() != ''">
				AND a.UNIT_NO = #{tIncome.unitNo}
			</if>
			<if test="tIncome.deptId != null and tIncome.deptId.trim() != ''">
				AND a.DEPT_ID = #{tIncome.deptId}
			</if>
			<if test="tIncome.deptName != null and tIncome.deptName.trim() != ''">
				AND a.DEPT_NAME = #{tIncome.deptName}
			</if>
			<if test="tIncome.deptNo != null and tIncome.deptNo.trim() != ''">
				AND a.DEPT_NO = #{tIncome.deptNo}
			</if>
			<if test="tIncome.feeType != null and tIncome.feeType.trim() != ''">
				AND a.FEE_TYPE = #{tIncome.feeType}
			</if>
			<if test="tIncome.feeMode != null and tIncome.feeMode.trim() != ''">
				AND a.FEE_MODE = #{tIncome.feeMode}
			</if>
			<if test="tIncome.charges != null and tIncome.charges.trim() != ''">
				AND a.CHARGES = #{tIncome.charges}
			</if>
			<if test="tIncome.sourceType != null and tIncome.sourceType.trim() != ''">
				AND a.SOURCE_TYPE = #{tIncome.sourceType}
			</if>
			<if test="tIncome.money != null">
				AND a.MONEY = #{tIncome.money}
			</if>
			<if test="tIncome.createMonth != null and tIncome.createMonth.trim() != ''">
				AND a.CREATE_MONTH = #{tIncome.createMonth}
			</if>
			<if test="tIncome.payMonth != null and tIncome.payMonth.trim() != ''">
				AND a.PAY_MONTH = #{tIncome.payMonth}
			</if>
			<if test="tIncome.dataCreateMonth != null and tIncome.dataCreateMonth.trim() != ''">
				AND a.DATA_CREATE_MONTH = #{tIncome.dataCreateMonth}
			</if>
			<if test="tIncome.sendStatus != null and tIncome.sendStatus.trim() != ''">
				AND a.SEND_STATUS = #{tIncome.sendStatus}
			</if>
			<if test="tIncome.redData != null and tIncome.redData.trim() != ''">
				AND a.RED_DATA = #{tIncome.redData}
			</if>
			<if test="tIncome.applyNo != null and tIncome.applyNo.trim() != ''">
				AND a.APPLY_NO = #{tIncome.applyNo}
			</if>
			<if test="tIncome.authSql != null and tIncome.authSql.trim() != ''">
				${tIncome.authSql}
			</if>
		</if>
	</sql>
	<!--tIncome简单分页查询-->
	<select id="getTIncomePage" resultMap="tIncomeMap">
		SELECT
		<include refid="Base_Column_List"/>
		FROM t_income a
		<where>
			1=1
			<include refid="tIncome_where"/>
			order by a.CREATE_TIME DESC
		</where>
	</select>

	<!--noPageDiy查询-->
	<select id="noPageDiy" resultMap="incomeExportMap">
		SELECT
			a.ID,
			a.EMP_NAME,
			a.EMP_IDCARD,
			a.UNIT_NAME,
			a.UNIT_NO,
			a.DEPT_NAME,
			a.DEPT_NO,
			if(a.FEE_TYPE=1,'管理费','风险金') FEE_TYPE,
			if(a.FEE_MODE=1,'按比例',if(a.FEE_MODE=2,'金额-人数','金额-人次')) FEE_MODE,
			a.CHARGES,
			case a.SOURCE_TYPE when 1 then '社保'  when 2 then '公积金'  when 3 then '商险' else '薪资' end SOURCE_TYPE,
			a.MONEY,
			a.CREATE_MONTH,
			a.PAY_MONTH,
			a.CREATE_TIME,
			a.DATA_CREATE_MONTH,
			if(a.SEND_STATUS=0,'未推送','已推送') SEND_STATUS,
			a.SEND_TIME,
			a.SEND_MONTH,
			a.APPLY_NO
		FROM t_income a
		<where>
			1=1
			<include refid="export_sql"/>
			order by a.CREATE_TIME DESC
			<if test="tIncome != null">
				<if test="tIncome.limitStart != null">
					limit #{tIncome.limitStart},#{tIncome.limitEnd}
				</if>
			</if>
		</where>
	</select>

	<!--noPageCountDiy查询-->
	<select id="noPageCountDiy" resultType="java.lang.Integer">
		SELECT
		count(1) from(
		SELECT
		a.id
		FROM t_income a
		<where>
			1=1
			<include refid="export_sql"/>
			group by a.id
		</where>
		) b
	</select>

	<!-- 列表 -->
	<select id="getTIncomeList" resultMap="tIncomeMap">
		SELECT
		<include refid="Base_Column_List"/>
		FROM t_income a
		<where>
			1=1
			<include refid="tIncome_where"/>
		</where>
	</select>
	<sql id="export_sql">
		<include refid="tIncome_where"/>
		<if test="idsStr != null and idsStr.size > 0">
			AND a.ID in
			<foreach item="items" index="index" collection="idsStr" open="(" separator="," close=")">
				#{items}
			</foreach>
		</if>
	</sql>

	<update id="updateIncomeById" parameterType="java.util.List">
		update t_income
		<trim prefix="set" suffixOverrides=",">
			<trim prefix="SEND_STATUS =case" suffix="end,">
				<foreach collection="list" item="i" index="index">
					<if test="i.sendStatus!=null">
						when ID=#{i.id} then #{i.sendStatus}
					</if>
				</foreach>
			</trim>
			<trim prefix="SEND_TIME =case" suffix="end,">
				<foreach collection="list" item="i" index="index">
					<if test="i.sendTime!=null">
						when ID=#{i.id} then #{i.sendTime}
					</if>
				</foreach>
			</trim>
			<trim prefix="SEND_MONTH =case" suffix="end,">
				<foreach collection="list" item="i" index="index">
					<if test="i.sendMonth!=null">
						when ID=#{i.id} then #{i.sendMonth}
					</if>
				</foreach>
			</trim>
			<trim prefix="EKP_ID =case" suffix="end,">
				<foreach collection="list" item="i" index="index">
					<if test="i.ekpId!=null">
						when ID=#{i.id} then #{i.ekpId}
					</if>
				</foreach>
			</trim>
		</trim>
		where
		<foreach collection="list" separator="or" item="i" index="index" >
			ID=#{i.id}
		</foreach>
	</update>

	<delete id="deleteIncomeByApplyNo">
		delete from t_income_detail where APPLY_NO = #{applyNo}
	</delete>
	<delete id="deleteIncomeDetailByApplyNo">
		delete from t_income where APPLY_NO = #{applyNo}
	</delete>

	<!-- ekp费用划转-收入 -->
	<update id="changeIncomeDept">
		update t_income p
		set p.DEPT_ID = #{dept.id}
		,p.DEPT_NAME = #{dept.departName}
		,p.DEPT_NO = #{dept.departNo}
		,p.UNIT_ID = #{dept.customerId}
		,p.UNIT_NAME = #{dept.customerName}
		,p.UNIT_NO = #{dept.customerCode}
		where p.id in
		<foreach item="item" index="index" collection="idSet" open="(" separator="," close=")">
			#{item}
		</foreach>
	</update>

	<!-- ekp费用划转-明细 -->
	<update id="changeIncomeDetailDept">
		update t_income_detail p
		set p.DEPT_ID = #{dept.id}
		,p.DEPT_NAME = #{dept.departName}
		,p.DEPT_NO = #{dept.departNo}
		,p.UNIT_ID = #{dept.customerId}
		,p.UNIT_NAME = #{dept.customerName}
		,p.UNIT_NO = #{dept.customerCode}
		where p.id in
		<foreach item="item" index="index" collection="idSet" open="(" separator="," close=")">
			#{item}
		</foreach>
	</update>


	<!-- ekp收入更新 -->
	<update id="updateIncomeByIdList">
		update t_income p
		set p.SEND_STATUS = '1'
		,p.SEND_MONTH = DATE_FORMAT(CURRENT_DATE(),'%Y%m')
		,p.SEND_TIME = now()
		where p.id in
		<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
			#{item}
		</foreach>
	</update>
</mapper>