<?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.salaryacct.SalaryAcctRecordMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO">
        <result column="id" property="id"/>
        <result column="salary_month" property="salaryMonth"/>
        <result column="tax_cycle" property="taxCycle"/>
        <result column="salary_sob_id" property="salarySobId"/>
        <result column="status" property="status"/>
        <result column="acct_times" property="acctTimes"/>
        <result column="description" property="description"/>
        <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"/>
        <result column="back_calc_status" property="backCalcStatus"/>
        <result column="lock_salary_item_ids" property="lockSalaryItemIds" typeHandler="com.engine.salary.handle.SalaryListTypeHandler"/>
    </resultMap>

    <resultMap id="BaseResultMap2" type="com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO">
        <result column="id" property="id"/>
        <result column="salary_month" property="salaryMonth"/>
        <result column="tax_cycle" property="taxCycle"/>
        <result column="salary_sob_id" property="salarySobId"/>
        <result column="status" property="status"/>
        <result column="acct_times" property="acctTimes"/>
        <result column="description" property="description"/>
        <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"/>
        <result column="back_calc_status" property="backCalcStatus"/>
        <result column="lock_salary_item_ids" property="lockSalaryItemIds" typeHandler="com.engine.salary.handle.SalaryListTypeHandler"/>
        <result column="tax_agent_id" property="taxAgentId"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t
        .
        id
        , t.salary_month
        , t.tax_cycle
        , t.salary_sob_id
        , t.status
        , t.acct_times
        , t.description
        , t.creator
        , t.create_time
        , t.update_time
        , t.delete_type
        , t.tenant_key
        , t.back_calc_status
        , t.lock_salary_item_ids
        , t.approval_status
        , t.approval_id
    </sql>

    <!-- 查询全部 -->
    <select id="listAll" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_salary_acct_record t
        WHERE delete_type = 0
    </select>

    <!-- 根据主键获取单条记录 -->
    <select id="getById" resultMap="BaseResultMap" parameterType="Long">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_salary_acct_record t
        WHERE id = #{id} AND delete_type = 0
    </select>

    <!-- 条件查询 -->
    <select id="listSome" resultMap="BaseResultMap2"
            parameterType="com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO">
        SELECT
        <include refid="baseColumns"/>
        ,sob.tax_agent_id
        FROM hrsa_salary_acct_record t
        left join hrsa_salary_sob sob on t.salary_sob_id = sob.id
        WHERE t.delete_type = 0
        <if test="id != null">
            AND t.id = #{id}
        </if>
        <if test="salaryMonth != null">
            AND t.salary_month = #{salaryMonth}
        </if>
        <if test="taxCycle != null">
            AND t.tax_cycle = #{taxCycle}
        </if>
        <if test="salarySobId != null">
            AND t.salary_sob_id = #{salarySobId}
        </if>
        <if test="status != null">
            AND t.status = #{status}
        </if>
        <if test="acctTimes != null">
            AND t.acct_times = #{acctTimes}
        </if>
        <if test="description != null">
            AND t.description = #{description}
        </if>
        <if test="creator != null">
            AND t.creator = #{creator}
        </if>
        <if test="createTime != null">
            AND t.create_time = #{createTime}
        </if>
        <if test="updateTime != null">
            AND t.update_time = #{updateTime}
        </if>
        <if test="deleteType != null">
            AND t.delete_type = #{deleteType}
        </if>
        <if test="tenantKey != null">
            AND t.tenant_key = #{tenantKey}
        </if>
        <if test="ids != null and ids.size()>0">
            AND t.id IN
            <foreach collection="ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="salarySobIds != null and salarySobIds.size()>0">
            AND t.salary_sob_id IN
            <foreach collection="salarySobIds" open="(" item="salarySobId" separator="," close=")">
                #{salarySobId}
            </foreach>
        </if>
        <if test="salaryMonths != null and salaryMonths.fromDate != null">
            AND t.salary_month <![CDATA[ >= ]]> #{salaryMonths.fromDate}
        </if>
        <if test="salaryMonths != null and salaryMonths.endDate != null">
            AND t.salary_month <![CDATA[ <= ]]> #{salaryMonths.endDate}
        </if>
        <if test="taxCycleDateRange != null and taxCycleDateRange.fromDate != null">
            AND t.tax_cycle <![CDATA[ >= ]]> #{taxCycleDateRange.fromDate}
        </if>
        <if test="taxCycleDateRange != null and taxCycleDateRange.endDate != null">
            AND t.tax_cycle <![CDATA[ <= ]]> #{taxCycleDateRange.endDate}
        </if>
        <if test="taxAgentId != null">
            AND sob.tax_agent_id = #{taxAgentId}
        </if>
        ORDER BY t.id DESC
    </select>
    <select id="listByCreateDate" resultType="com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_salary_acct_record t
        WHERE delete_type = 0
        <if test="salarySobIds != null and salarySobIds.size()>0">
            AND salary_sob_id IN
            <foreach collection="salarySobIds" open="(" item="salarySobId" separator="," close=")">
                #{salarySobId}
            </foreach>
        </if>
        <if test="createRange != null and createRange.fromDate != null">
            AND create_time <![CDATA[ >= ]]> #{createRange.fromDate}
        </if>
        <if test="createRange != null and createRange.endDate != null">
            AND create_time <![CDATA[ <= ]]> #{createRange.endDate}
        </if>
        ORDER BY id DESC
    </select>

    <select id="getBackCalcRecordByIds" resultType="com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO">
        select
        <include refid="baseColumns"/>
        FROM hrsa_salary_acct_record t
        where delete_type = 0
          AND back_calc_status = 1
        <if test="collection != null and collection.size()>0">
            AND id IN
            <foreach collection="collection" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>


    <!-- 插入不为NULL的字段 -->
    <insert id="insertIgnoreNull" parameterType="com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO"
            keyProperty="id" keyColumn="id" useGeneratedKeys="true"
    >
        INSERT INTO hrsa_salary_acct_record
        <trim prefix="(" suffix=")" suffixOverrides=",">

            <if test="id != null">
                id,
            </if>
            <if test="salaryMonth != null">
                salary_month,
            </if>
            <if test="taxCycle != null">
                tax_cycle,
            </if>
            <if test="salarySobId != null">
                salary_sob_id,
            </if>
            <if test="status != null">
                status,
            </if>
            <if test="acctTimes != null">
                acct_times,
            </if>
            <if test="description != null">
                description,
            </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="taxCycle != null">
                #{taxCycle},
            </if>
            <if test="salarySobId != null">
                #{salarySobId},
            </if>
            <if test="status != null">
                #{status},
            </if>
            <if test="acctTimes != null">
                #{acctTimes},
            </if>
            <if test="description != null">
                #{description},
            </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>

    <insert id="insertIgnoreNull" parameterType="com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO"
            databaseId="oracle"
    >
        <selectKey keyProperty="id" resultType="long" order="AFTER">
            select hrsa_salary_acct_record_id.currval from dual
        </selectKey>
        INSERT INTO hrsa_salary_acct_record
        <trim prefix="(" suffix=")" suffixOverrides=",">

            <if test="id != null">
                id,
            </if>
            <if test="salaryMonth != null">
                salary_month,
            </if>
            <if test="taxCycle != null">
                tax_cycle,
            </if>
            <if test="salarySobId != null">
                salary_sob_id,
            </if>
            <if test="status != null">
                status,
            </if>
            <if test="acctTimes != null">
                acct_times,
            </if>
            <if test="description != null">
                description,
            </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="taxCycle != null">
                #{taxCycle},
            </if>
            <if test="salarySobId != null">
                #{salarySobId},
            </if>
            <if test="status != null">
                #{status},
            </if>
            <if test="acctTimes != null">
                #{acctTimes},
            </if>
            <if test="description != null">
                #{description},
            </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>

    <!-- 更新,更新全部字段 -->
    <update id="update" parameterType="com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO">
        UPDATE hrsa_salary_acct_record
        <set>
            salary_month=#{salaryMonth},
            tax_cycle=#{taxCycle},
            salary_sob_id=#{salarySobId},
            status=#{status},
            acct_times=#{acctTimes},
            description=#{description},
            creator=#{creator},
            create_time=#{createTime},
            update_time=#{updateTime},
            delete_type=#{deleteType},
            tenant_key=#{tenantKey},
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


    <!-- 更新不为NULL的字段 -->
    <update id="updateIgnoreNull" parameterType="com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO">
        UPDATE hrsa_salary_acct_record
        <set>
            <if test="salaryMonth != null">
                salary_month=#{salaryMonth},
            </if>
            <if test="taxCycle != null">
                tax_cycle=#{taxCycle},
            </if>
            <if test="salarySobId != null">
                salary_sob_id=#{salarySobId},
            </if>
            <if test="status != null">
                status=#{status},
            </if>
            <if test="acctTimes != null">
                acct_times=#{acctTimes},
            </if>
            <if test="description != null">
                description=#{description},
            </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="backCalcStatus != null">
                back_calc_status=#{backCalcStatus},
            </if>
            <if test="approvalId != null">
                approval_id=#{approvalId},
            </if>
            <if test="approvalStatus != null">
                approval_status=#{approvalStatus},
            </if>
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


    <!-- 根据主键删除记录 -->
    <delete id="delete" parameterType="com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO">
        UPDATE hrsa_salary_acct_record
        SET delete_type=1
        WHERE id = #{id}
          AND delete_type = 0
    </delete>

    <update id="deleteByIds">
        UPDATE hrsa_salary_acct_record
        SET delete_type = 1
        WHERE delete_type = 0
        AND id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </update>


    <!-- 更新不为NULL的字段 -->
    <update id="updateLockSalaryItemIds" parameterType="com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO">
        UPDATE hrsa_salary_acct_record
        <set>
            lock_salary_item_ids =
            #{lockSalaryItemIds, jdbcType=ARRAY, typeHandler=com.engine.salary.handle.SalaryListTypeHandler},
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>

    <update id="updateDate">
        UPDATE hrsa_salary_acct_record
        SET update_time=#{updateTime}
        WHERE delete_type = 0
        AND id = #{id}
    </update>

</mapper>