<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.engine.salary.mapper.salarybill.SalarySendMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.salaryBill.po.SalarySendPO">
        <result column="id" property="id"/>
        <result column="salary_month" property="salaryMonth"/>
        <result column="salary_accounting_id" property="salaryAccountingId"/>
        <result column="salary_sob_id" property="salarySobId"/>
        <result column="send_num" property="sendNum"/>
        <result column="send_total" property="sendTotal"/>
        <result column="last_send_time" property="lastSendTime"/>
        <result column="creator" property="creator"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="delete_type" property="deleteType"/>
        <result column="tenant_key" property="tenantKey"/>
    </resultMap>

    <sql id="salarySendPOColumn">
        id,
        salary_month,
        salary_accounting_id,
        salary_sob_id,
        send_num,
        send_total,
        last_send_time,
        send_status,
        salary_acct_type,
        creator,
        create_time,
        update_time,
        delete_type,
        tenant_key
    </sql>
    <sql id="salarySendColumn">
        t1.id,
        t1.salary_accounting_id AS salaryAcctId,
        t1.send_status AS sendStatus,
        t1.salary_acct_type AS salaryAcctType,
        t2.salary_month AS salaryYearMonth,
        t3.name AS salarySob,
        t3.id AS salarySobId,
        t1.send_num,
        t1.send_total,
        t1.last_send_time,
        t2.acct_times,
        t2.back_calc_status AS salaryAcctRecordType
    </sql>

    <sql id="paramSql">
        <!-- 薪资所属月 -->
        <if test="param.salaryMonthDate != null">
            <if test="param.salaryMonthDate.size() == 1">
                AND t2.salary_month = #{param.salaryMonthDate[0]}
            </if>
            <if test="param.salaryMonthDate.size() == 2">
                AND (t2.salary_month BETWEEN #{param.salaryMonthDate[0]} AND #{param.salaryMonthDate[1]})
            </if>
        </if>
        <if test="param.salarySobIds != null and param.salarySobIds.size() > 0">
            AND t3.id IN
            <foreach collection="param.salarySobIds" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </sql>
    <update id="deleteBySalaryAcctRecordIds">
        update hrsa_salary_send
        set delete_type = 1
        where salary_accounting_id in
        <foreach collection="salaryAcctRecordId" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="list" resultType="com.engine.salary.entity.salaryBill.dto.SalarySendListDTO">
        SELECT
        <include refid="salarySendColumn"/>
        FROM
        hrsa_salary_send t1
        LEFT JOIN hrsa_salary_acct_record t2 ON t2.id = t1.salary_accounting_id
        LEFT JOIN hrsa_salary_sob t3 ON t3.id = t2.salary_sob_id
        WHERE
         t1.delete_type = 0
         AND t2.delete_type = 0
            <include refid="paramSql"/>
        ORDER BY t1.id DESC
    </select>

    <select id="getById" resultType="com.engine.salary.entity.salaryBill.po.SalarySendPO">
        SELECT
            id,
            salary_month,
            salary_accounting_id,
            salary_sob_id,
            send_num,
            send_total,
            last_send_time,
            send_status,
            salary_acct_type,
            creator
        FROM hrsa_salary_send
        WHERE delete_type = 0
        AND id = #{id}
    </select>

    <!-- 更新不为NULL的字段 -->
    <update id="updateIgnoreNull" parameterType="com.engine.salary.entity.salaryBill.po.SalarySendPO">
        UPDATE hrsa_salary_send
        <set>
            <if test="salaryMonth != null">
                salary_month=#{salaryMonth},
            </if>
            <if test="salaryAccountingId != null">
                salary_accounting_id=#{salaryAccountingId},
            </if>
            <if test="salarySobId != null">
                salary_sob_id=#{salarySobId},
            </if>
            <if test="sendNum != null">
                send_num=#{sendNum},
            </if>
            <if test="sendTotal != null">
                send_total=#{sendTotal},
            </if>
            <if test="lastSendTime != null">
                last_send_time=#{lastSendTime},
            </if>
            <if test="creator != null">
                creator=#{creator},
            </if>
            <if test="createTime != null">
                create_time=#{createTime},
            </if>
            <if test="updateTime != null">
                update_time=#{updateTime},
            </if>
            <if test="deleteType != null">
                delete_type=#{deleteType},
            </if>
            <if test="tenantKey != null">
                tenant_key=#{tenantKey},
            </if>
            <if test="sendStatus != null">
                send_status=#{sendStatus},
            </if>
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>

    <update id="batchHandleSendStatusHistory">
        update hrsa_salary_send
        set send_status = 0
        where send_status is NULL
    </update>


    <update id="batchHandleSalaryAcctTypeHistory">
        update hrsa_salary_send
        set salary_acct_type = 0
        where salary_acct_type is NULL
    </update>


    <select id="listSomeWithCondition" resultMap="BaseResultMap">
        SELECT
        <include refid="salarySendPOColumn"/>
        FROM
        hrsa_salary_send t1
        WHERE
            t1.delete_type = 0
            <if test="param.sendNum != null and param.sendNum != ''">
                AND t1.send_num = #{param.sendNum}
            </if>

            <if test="param.salaryAccountingIds != null and param.salaryAccountingIds.size() != 0">
                AND t1.salary_accounting_id IN
                <foreach collection="param.salaryAccountingIds" open="(" item="id" separator="," close=")">
                    #{id}
                </foreach>
            </if>

        ORDER BY t1.id DESC
    </select>

    <!-- 条件查询 -->
    <select id="listSome" resultMap="BaseResultMap" parameterType="com.engine.salary.entity.salaryBill.po.SalarySendPO">
        SELECT
        <include refid="salarySendPOColumn" />
        FROM hrsa_salary_send t
        WHERE delete_type = 0

        <if test="id != null and id != ''">
            AND id = #{id}
        </if>
        <if test="salaryMonth != null and salaryMonth != ''">
            AND salary_month = #{salaryMonth}
        </if>
        <if test="salaryAccountingId != null and salaryAccountingId != ''">
            AND salary_accounting_id = #{salaryAccountingId}
        </if>
<!--        <if test="salaryAcctRecordId != null and salaryAcctRecordId != ''">-->
<!--            AND salary_acct_record_id = #{salaryAcctRecordId}-->
<!--        </if>-->
        <if test="salarySobId != null and salarySobId != ''">
            AND salary_sob_id = #{salarySobId}
        </if>
        <if test="sendNum != null and sendNum != ''">
            AND send_num = #{sendNum}
        </if>
        <if test="sendTotal != null and sendTotal != ''">
            AND send_total = #{sendTotal}
        </if>
        <if test="lastSendTime != null and lastSendTime != ''">
            AND last_send_time = #{lastSendTime}
        </if>
        <if test="sendStatus != null and sendStatus != ''">
            AND send_status = #{sendStatus}
        </if>
        <if test="salaryAcctType != null and salaryAcctType != ''">
            AND salary_acct_type = #{salaryAcctTypec}
        </if>
        <if test="creator != null and creator != ''">
            AND creator = #{creator}
        </if>
        <if test="createTime != null and createTime != ''">
            AND createTime = #{createTime}
        </if>
        <if test="updateTime != null and updateTime != ''">
            AND update_time = #{updateTime}
        </if>
        <if test="deleteType != null and deleteType != ''">
            AND delete_type = #{deleteType}
        </if>
        <if test="tenantKey != null and tenantKey != ''">
            AND tenant_key = #{tenantKey}
        </if>
        ORDER BY id DESC
    </select>
    <select id="getNeedSendListBySalarySobIds"
            resultType="com.engine.salary.entity.salaryBill.po.SalarySendPO">
        select
        <include refid="salarySendPOColumn" />
        from hrsa_salary_send
        where
        delete_type=0 and send_total !=  send_num
        and (salary_acct_type = 0 or salary_acct_type is null )
        and (send_status = 0 or send_status is null)
        <if test="salarySobIds != null and salarySobIds.size() > 0">
            AND salary_sob_id IN
            <foreach collection="salarySobIds" open="(" item="salarySobId" separator="," close=")">
                #{salarySobId}
            </foreach>
        </if>
    </select>
    <select id="getByIds" resultType="com.engine.salary.entity.salaryBill.po.SalarySendPO">
        SELECT
        <include refid="salarySendPOColumn" />
        FROM hrsa_salary_send t
        WHERE delete_type = 0
        <if test="ids != null and ids.size() > 0">
            AND id IN
            <foreach collection="ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>


    <!-- 插入不为NULL的字段 -->
    <insert id="insertIgnoreNull" parameterType="com.engine.salary.entity.salaryBill.po.SalarySendPO">
        INSERT INTO hrsa_salary_send
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="salaryMonth != null">
                salary_month,
            </if>
            <if test="salaryAccountingId != null">
                salary_accounting_id,
            </if>
            <if test="salarySobId != null">
                salary_sob_id,
            </if>
            <if test="sendNum != null">
                send_num,
            </if>
            <if test="sendTotal != null">
                send_total,
            </if>
            <if test="sendStatus != null">
                send_status,
            </if>
            <if test="salaryAcctType != null">
                salary_acct_type,
            </if>
            <if test="lastSendTime != null">
                last_send_time,
            </if>
            <if test="creator != null">
                creator,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="deleteType != null">
                delete_type,
            </if>
            <if test="tenantKey != null">
                tenant_key,
            </if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">

            <if test="id != null">
                #{id},
            </if>
            <if test="salaryMonth != null">
                #{salaryMonth},
            </if>
            <if test="salaryAccountingId != null">
                #{salaryAccountingId},
            </if>
            <if test="salarySobId != null">
                #{salarySobId},
            </if>
            <if test="sendNum != null">
                #{sendNum},
            </if>
            <if test="sendTotal != null">
                #{sendTotal},
            </if>
            <if test="sendStatus != null">
                #{sendStatus},
            </if>
            <if test="salaryAcctType != null">
                #{salaryAcctType},
            </if>
            <if test="lastSendTime != null">
                #{lastSendTime},
            </if>
            <if test="creator != null">
                #{creator},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
            <if test="deleteType != null">
                #{deleteType},
            </if>
            <if test="tenantKey != null">
                #{tenantKey},
            </if>
        </trim>
    </insert>








</mapper>