<?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.SalaryAcctResultMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.salaryacct.po.SalaryAcctResultPO">
        <result column="id" property="id"/>
        <result column="salary_sob_id" property="salarySobId"/>
        <result column="salary_acct_emp_id" property="salaryAcctEmpId"/>
        <result column="salary_acct_record_id" property="salaryAcctRecordId"/>
        <result column="employee_id" property="employeeId"/>
        <result column="tax_agent_id" property="taxAgentId"/>
        <result column="salary_item_id" property="salaryItemId"/>
        <result column="result_value" property="resultValue"/>
        <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="baseColumns">
        t
        .
        id
        , t.salary_sob_id
        , t.salary_acct_emp_id
        , t.salary_acct_record_id
        , t.employee_id
        , t.tax_agent_id
        , t.salary_item_id
        , t.result_value
        , t.origin_result_value
        , t.creator
        , t.create_time
        , t.update_time
        , t.delete_type
        , t.tenant_key
    </sql>

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

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

    <!-- 条件查询 -->
    <select id="listSome" resultMap="BaseResultMap"
            parameterType="com.engine.salary.entity.salaryacct.po.SalaryAcctResultPO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_salary_acct_result t
        WHERE delete_type = 0
        <if test="id != null">
            AND id = #{id}
        </if>
        <if test="salarySobId != null">
            AND salary_sob_id = #{salarySobId}
        </if>
        <if test="salaryAcctEmpId != null">
            AND salary_acct_emp_id = #{salaryAcctEmpId}
        </if>
        <if test="salaryAcctRecordId != null">
            AND salary_acct_record_id = #{salaryAcctRecordId}
        </if>
        <if test="employeeId != null">
            AND employee_id = #{employeeId}
        </if>
        <if test="taxAgentId != null">
            AND tax_agent_id = #{taxAgentId}
        </if>
        <if test="salaryItemId != null">
            AND salary_item_id = #{salaryItemId}
        </if>
        <if test="resultValue != null">
            AND result_value = #{resultValue}
        </if>
        <if test="creator != null">
            AND creator = #{creator}
        </if>
        <if test="createTime != null">
            AND create_time = #{createTime}
        </if>
        <if test="updateTime != null">
            AND update_time = #{updateTime}
        </if>
        <if test="deleteType != null">
            AND delete_type = #{deleteType}
        </if>
        <if test="tenantKey != null">
            AND tenant_key = #{tenantKey}
        </if>
        <if test="ids != null and ids.size()>0">
            AND id IN
            <foreach collection="ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="salaryAcctRecordIds != null and salaryAcctRecordIds.size()>0">
            AND salary_acct_record_id IN
            <foreach collection="salaryAcctRecordIds" open="(" item="salaryAcctRecordId" separator="," close=")">
                #{salaryAcctRecordId}
            </foreach>
        </if>
        <if test="salaryAcctEmpIds != null and salaryAcctEmpIds.size()>0">
            AND salary_acct_emp_id IN
            <foreach collection="salaryAcctEmpIds" open="(" item="salaryAcctEmpId" separator="," close=")">
                #{salaryAcctEmpId}
            </foreach>
        </if>
        <if test="employeeIds != null and employeeIds.size()>0">
            AND employee_id IN
            <foreach collection="employeeIds" open="(" item="employeeId" separator="," close=")">
                #{employeeId}
            </foreach>
        </if>
        <if test="taxAgentIds != null and taxAgentIds.size()>0">
            AND tax_agent_id IN
            <foreach collection="taxAgentIds" open="(" item="taxAgentId" separator="," close=")">
                #{taxAgentId}
            </foreach>
        </if>
        <if test="salaryItemIds != null and salaryItemIds.size()>0">
            AND salary_item_id IN
            <foreach collection="salaryItemIds" open="(" item="salaryItemId" separator="," close=")">
                #{salaryItemId}
            </foreach>
        </if>
        ORDER BY id DESC
    </select>


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

            <if test="id != null">
                id,
            </if>
            <if test="salarySobId != null">
                salary_sob_id,
            </if>
            <if test="salaryAcctEmpId != null">
                salary_acct_emp_id,
            </if>
            <if test="salaryAcctRecordId != null">
                salary_acct_record_id,
            </if>
            <if test="employeeId != null">
                employee_id,
            </if>
            <if test="taxAgentId != null">
                tax_agent_id,
            </if>
            <if test="salaryItemId != null">
                salary_item_id,
            </if>
            <if test="resultValue != null">
                result_value,
            </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="salarySobId != null">
                #{salarySobId},
            </if>
            <if test="salaryAcctEmpId != null">
                #{salaryAcctEmpId},
            </if>
            <if test="salaryAcctRecordId != null">
                #{salaryAcctRecordId},
            </if>
            <if test="employeeId != null">
                #{employeeId},
            </if>
            <if test="taxAgentId != null">
                #{taxAgentId},
            </if>
            <if test="salaryItemId != null">
                #{salaryItemId},
            </if>
            <if test="resultValue != null">
                #{resultValue},
            </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.SalaryAcctResultPO" databaseId="oracle"
    >
        <selectKey  keyProperty="id" resultType="long" order="AFTER">
            select hrsa_salary_acct_result_id.currval from dual
        </selectKey>
        INSERT INTO hrsa_salary_acct_result
        <trim prefix="(" suffix=")" suffixOverrides=",">

            <if test="id != null">
                id,
            </if>
            <if test="salarySobId != null">
                salary_sob_id,
            </if>
            <if test="salaryAcctEmpId != null">
                salary_acct_emp_id,
            </if>
            <if test="salaryAcctRecordId != null">
                salary_acct_record_id,
            </if>
            <if test="employeeId != null">
                employee_id,
            </if>
            <if test="taxAgentId != null">
                tax_agent_id,
            </if>
            <if test="salaryItemId != null">
                salary_item_id,
            </if>
            <if test="resultValue != null">
                result_value,
            </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="salarySobId != null">
                #{salarySobId},
            </if>
            <if test="salaryAcctEmpId != null">
                #{salaryAcctEmpId},
            </if>
            <if test="salaryAcctRecordId != null">
                #{salaryAcctRecordId},
            </if>
            <if test="employeeId != null">
                #{employeeId},
            </if>
            <if test="taxAgentId != null">
                #{taxAgentId},
            </if>
            <if test="salaryItemId != null">
                #{salaryItemId},
            </if>
            <if test="resultValue != null">
                #{resultValue},
            </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.SalaryAcctResultPO">
        UPDATE hrsa_salary_acct_result
        <set>
            salary_sob_id=#{salarySobId},
            salary_acct_emp_id=#{salaryAcctEmpId},
            salary_acct_record_id=#{salaryAcctRecordId},
            employee_id=#{employeeId},
            tax_agent_id=#{taxAgentId},
            salary_item_id=#{salaryItemId},
            result_value=#{resultValue},
            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.SalaryAcctResultPO">
        UPDATE hrsa_salary_acct_result
        <set>
            <if test="salarySobId != null">
                salary_sob_id=#{salarySobId},
            </if>
            <if test="salaryAcctEmpId != null">
                salary_acct_emp_id=#{salaryAcctEmpId},
            </if>
            <if test="salaryAcctRecordId != null">
                salary_acct_record_id=#{salaryAcctRecordId},
            </if>
            <if test="employeeId != null">
                employee_id=#{employeeId},
            </if>
            <if test="taxAgentId != null">
                tax_agent_id=#{taxAgentId},
            </if>
            <if test="salaryItemId != null">
                salary_item_id=#{salaryItemId},
            </if>
            <if test="resultValue != null">
                result_value=#{resultValue},
            </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>
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


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


    <resultMap id="SalaryAcctResultMap" type="com.engine.salary.entity.salaryacct.po.SalaryAcctResultPO">
        <id column="id" property="id"/>
        <result column="salary_sob_id" property="salarySobId"/>
        <result column="salary_acct_emp_id" property="salaryAcctEmpId"/>
        <result column="salary_acct_record_id" property="salaryAcctRecordId"/>
        <result column="employee_id" property="employeeId"/>
        <result column="tax_agent_id" property="taxAgentId"/>
        <result column="salary_item_id" property="salaryItemId"/>
        <result column="result_value" property="resultValue"/>
        <result column="creator" property="creator"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="tenant_key" property="tenantKey"/>
    </resultMap>

    <sql id="SalaryAcctResultColumn">
        id
        , salary_sob_id, salary_acct_emp_id, salary_acct_record_id, employee_id, tax_agent_id,
        salary_item_id, result_value, creator, create_time, update_time,
        delete_type, tenant_key
    </sql>

    <select id="listSameSalaryMonthResult" resultMap="SalaryAcctResultMap">
        SELECT
        <include refid="SalaryAcctResultColumn"/>
        FROM hrsa_salary_acct_result
        WHERE  delete_type = 0
        AND salary_acct_record_id IN
        <foreach collection="salaryAcctRecordIds" open="(" item="salaryAcctRecordId" separator="," close=")">
            #{salaryAcctRecordId}
        </foreach>
        AND salary_acct_emp_id IN
        (
        SELECT id FROM hrsa_salary_acct_emp emp
        WHERE  emp.delete_type = 0
        AND emp.salary_acct_record_id IN
        <foreach collection="salaryAcctRecordIds" open="(" item="salaryAcctRecordId" separator="," close=")">
            #{salaryAcctRecordId}
        </foreach>
        )
    </select>
    <select id="getAcctEmpIsExist" resultType="java.lang.Long">
        select distinct salary_acct_emp_id
        from hrsa_salary_acct_result
        where 1=1
        <if test="empIds != null and empIds.size()>0">
            AND salary_acct_emp_id IN
            <foreach collection="empIds" open="(" item="salaryAcctEmpId" separator="," close=")">
                #{salaryAcctEmpId}
            </foreach>
        </if>
    </select>

    <insert id="batchInsert">
        INSERT INTO hrsa_salary_acct_result( salary_sob_id, salary_acct_emp_id, salary_acct_record_id, employee_id,
        tax_agent_id, salary_item_id, result_value, origin_result_value, creator, create_time, update_time, delete_type, tenant_key)
        VALUES
        <foreach collection="collection" item="item" separator=",">
            (
            #{item.salarySobId},
            #{item.salaryAcctEmpId},
            #{item.salaryAcctRecordId},
            #{item.employeeId},
            #{item.taxAgentId},
            #{item.salaryItemId},
            #{item.resultValue},
            #{item.originResultValue},
            #{item.creator},
            #{item.createTime},
            #{item.updateTime},
            #{item.deleteType},
            #{item.tenantKey}
            )
        </foreach>
    </insert>
    <insert id="batchInsert" databaseId="oracle">
        INSERT INTO hrsa_salary_acct_result( salary_sob_id, salary_acct_emp_id, salary_acct_record_id, employee_id,
        tax_agent_id, salary_item_id, result_value, origin_result_value, creator, create_time, update_time, delete_type, tenant_key)

        <foreach collection="collection" item="item" separator="union all">
            select
            #{item.salarySobId,jdbcType=DOUBLE},
            #{item.salaryAcctEmpId,jdbcType=DOUBLE},
            #{item.salaryAcctRecordId,jdbcType=DOUBLE},
            #{item.employeeId,jdbcType=DOUBLE},
            #{item.taxAgentId,jdbcType=DOUBLE},
            #{item.salaryItemId,jdbcType=DOUBLE},
            #{item.resultValue,jdbcType=VARCHAR},
            #{item.originResultValue,jdbcType=VARCHAR},
            #{item.creator,jdbcType=DOUBLE},
            #{item.createTime,jdbcType=DATE},
            #{item.updateTime,jdbcType=DATE},
            #{item.deleteType,jdbcType=INTEGER},
            #{item.tenantKey,jdbcType=VARCHAR}
            from dual
        </foreach>
    </insert>
    <insert id="batchInsert" databaseId="sqlserver">
        <foreach collection="collection" item="item" separator=";">
        INSERT INTO hrsa_salary_acct_result(salary_sob_id, salary_acct_emp_id, salary_acct_record_id, employee_id,
        tax_agent_id, salary_item_id, result_value, origin_result_value, creator, create_time, update_time, delete_type, tenant_key)
        VALUES
            (
            #{item.salarySobId},
            #{item.salaryAcctEmpId},
            #{item.salaryAcctRecordId},
            #{item.employeeId},
            #{item.taxAgentId},
            #{item.salaryItemId},
            #{item.resultValue},
            #{item.originResultValue},
            #{item.creator},
            #{item.createTime},
            #{item.updateTime},
            #{item.deleteType},
            #{item.tenantKey}
            )
        </foreach>
    </insert>

    <delete id="deleteBySalaryAcctRecordIds">
        DELETE FROM hrsa_salary_acct_result
        WHERE  delete_type = 0
        AND salary_acct_record_id IN
        <foreach collection="salaryAcctRecordIds" open="(" item="salaryAcctRecordId" separator="," close=")">
            #{salaryAcctRecordId}
        </foreach>
    </delete>

    <delete id="deleteBySalaryAcctEmpIds">
        DELETE FROM hrsa_salary_acct_result
        WHERE  delete_type = 0
        AND salary_acct_emp_id IN
        <foreach collection="salaryAcctEmpIds" open="(" item="salaryAcctEmpId" separator="," close=")">
            #{salaryAcctEmpId}
        </foreach>
    </delete>

    <delete id="deleteByAcctEmpIdsAndSalaryItemIds">
        DELETE FROM hrsa_salary_acct_result
        WHERE  delete_type = 0
        AND salary_acct_emp_id IN
        <foreach collection="salaryAcctEmpIds" open="(" item="salaryAcctEmpId" separator="," close=")">
            #{salaryAcctEmpId}
        </foreach>
        AND salary_item_id IN
        <foreach collection="salaryItemIds" open="(" item="salaryItemId" separator="," close=")">
            #{salaryItemId}
        </foreach>
    </delete>

    <delete id="deleteBySalaryAcctRecordIdAndSalaryItemIds">
        DELETE FROM hrsa_salary_acct_result
        WHERE  delete_type = 0
        AND salary_acct_record_id =#{salaryAcctRecordId}
        AND salary_item_id IN
        <foreach collection="salaryItemIds" open="(" item="salaryItemId" separator="," close=")">
            #{salaryItemId}
        </foreach>
    </delete>

    <delete id="deleteByIds">
        DELETE FROM hrsa_salary_acct_result
        WHERE  delete_type = 0
        AND id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </delete>


    <update id="batchUpdate" parameterType="java.util.List">
        update hrsa_salary_acct_result
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="result_value =case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.resultValue!=null">
                        when id=#{item.id} then #{item.resultValue}
                    </if>
                </foreach>
            </trim>

            <trim prefix="origin_result_value =case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.originResultValue!=null">
                        when id=#{item.id} then #{item.originResultValue}
                    </if>
                </foreach>
            </trim>

            <trim prefix="delete_type =case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.deleteType!=null">
                        when id=#{item.id} then #{item.deleteType}
                    </if>
                </foreach>
            </trim>
        </trim>
        where
        id in
        <foreach collection="collection" item="item" index="index" separator="," open="(" close=")">
            #{item.id}
        </foreach>
    </update>

    <update id="batchUpdateOriginResultValue">
        update hrsa_salary_acct_result
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="origin_result_value = case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.originResultValue!=null">
                        when id=#{item.id} then #{item.originResultValue}
                    </if>
                </foreach>
            </trim>
        </trim>
        where
        id in
        <foreach collection="collection" item="item" index="index" separator="," open="(" close=")">
            #{item.id}
        </foreach>
    </update>

</mapper>