<?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.salarysob.SalarySobItemMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.salarysob.po.SalarySobItemPO">
        <result column="create_time" property="createTime"/>
        <result column="creator" property="creator"/>
        <result column="delete_type" property="deleteType"/>
        <result column="description" property="description"/>
        <result column="formula_id" property="formulaId"/>
        <result column="id" property="id"/>
        <result column="salary_item_id" property="salaryItemId"/>
        <result column="salary_sob_id" property="salarySobId"/>
        <result column="salary_sob_item_group_id" property="salarySobItemGroupId"/>
        <result column="sorted_index" property="sortedIndex"/>
        <result column="tenant_key" property="tenantKey"/>
        <result column="update_time" property="updateTime"/>
        <result column="can_delete" property="canDelete"/>
        <result column="item_hide" property="itemHide"/>
        <result column="default_value" property="defaultValue"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t
        .
        create_time
        , t.creator
        , t.delete_type
        , t.description
        , t.formula_id
        , t.id
        , t.salary_item_id
        , t.salary_sob_id
        , t.salary_sob_item_group_id
        , t.sorted_index
        , t.tenant_key
        , t.update_time
        , t.can_delete
        , t.rounding_mode
        , t.pattern
        , t.value_type
        , t.default_value
        , t.item_hide
    </sql>

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

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

    <!-- 条件查询 -->
    <select id="listSome" resultMap="BaseResultMap" parameterType="com.engine.salary.entity.salarysob.po.SalarySobItemPO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_salary_sob_item t
        WHERE delete_type = 0
        <if test="createTime != null and createTime != ''">
            AND create_time = #{createTime}
        </if>
        <if test="creator != null">
            AND creator = #{creator}
        </if>
        <if test="deleteType != null">
            AND delete_type = #{deleteType}
        </if>
        <if test="description != null and description != ''">
            AND description = #{description}
        </if>
        <if test="formulaId != null">
            AND formula_id = #{formulaId}
        </if>
        <if test="id != null and id != ''">
            AND id = #{id}
        </if>
        <if test="salaryItemId != null">
            AND salary_item_id = #{salaryItemId}
        </if>
        <if test="salarySobId != null ">
            AND salary_sob_id = #{salarySobId}
        </if>
        <if test="salarySobItemGroupId != null ">
            AND salary_sob_item_group_id = #{salarySobItemGroupId}
        </if>
        <if test="sortedIndex != null ">
            AND sorted_index = #{sortedIndex}
        </if>
        <if test="tenantKey != null ">
            AND tenant_key = #{tenantKey}
        </if>
        <if test="updateTime != null ">
            AND update_time = #{updateTime}
        </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="salarySobIds != null and salarySobIds.size()>0">
            AND salary_sob_id IN
            <foreach collection="salarySobIds" open="(" item="salarySobId" separator="," close=")">
                #{salarySobId}
            </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>
        <if test="notInSalaryItemIds != null and notInSalaryItemIds.size()>0">
            AND salary_item_id NOT IN
            <foreach collection="notInSalaryItemIds" open="(" item="salaryItemId" separator="," close=")">
                #{salaryItemId}
            </foreach>
        </if>
        ORDER BY id DESC
    </select>

    <select id="listBySalarySobIdAndGroupId"
            resultType="com.engine.salary.entity.salarysob.po.SalarySobItemPO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_salary_sob_item t
        WHERE delete_type = 0
        <if test="salarySobId != null ">
            AND salary_sob_id = #{salarySobId}
        </if>
        <if test="salarySobItemGroupIds != null and salarySobItemGroupIds.size()>0">
            AND salary_sob_item_group_id IN
            <foreach collection="salarySobItemGroupIds" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>

        <if test="salarySobItemGroupIds == null or salarySobItemGroupIds.size() == 0">
            AND salary_sob_item_group_id IN (0)
        </if>

    </select>


    <!-- 更新,更新全部字段 -->
    <update id="update" parameterType="com.engine.salary.entity.salarysob.po.SalarySobItemPO">
        UPDATE hrsa_salary_sob_item
        <set>
            create_time=#{createTime},
            creator=#{creator},
            delete_type=#{deleteType},
            description=#{description},
            formula_id=#{formulaId},
            salary_item_id=#{salaryItemId},
            salary_sob_id=#{salarySobId},
            salary_sob_item_group_id=#{salarySobItemGroupId},
            sorted_index=#{sortedIndex},
            tenant_key=#{tenantKey},
            update_time=#{updateTime},
            can_delete=#{canDelete},
            rounding_mode=#{roundingMode},
            pattern=#{pattern},
            value_type=#{valueType},
            item_hide=#{itemHide},
            default_value=#{defaultValue},
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


    <!-- 更新不为NULL的字段 -->
    <update id="updateIgnoreNull" parameterType="com.engine.salary.entity.salarysob.po.SalarySobItemPO">
        UPDATE hrsa_salary_sob_item
        <set>
            <if test="createTime != null">
                create_time=#{createTime},
            </if>
            <if test="creator != null">
                creator=#{creator},
            </if>
            <if test="deleteType != null">
                delete_type=#{deleteType},
            </if>
            <if test="description != null">
                description=#{description},
            </if>
            <if test="formulaId != null">
                formula_id=#{formulaId},
            </if>
            <if test="salaryItemId != null">
                salary_item_id=#{salaryItemId},
            </if>
            <if test="salarySobId != null">
                salary_sob_id=#{salarySobId},
            </if>
            <if test="salarySobItemGroupId != null">
                salary_sob_item_group_id=#{salarySobItemGroupId},
            </if>
            <if test="sortedIndex != null">
                sorted_index=#{sortedIndex},
            </if>
            <if test="tenantKey != null">
                tenant_key=#{tenantKey},
            </if>
            <if test="updateTime != null">
                update_time=#{updateTime},
            </if>
            <if test="canDelete != null">
                can_delete=#{canDelete},
            </if>
            <if test="roundingMode != null">
                rounding_mode=#{roundingMode},
            </if>
            <if test="pattern != null">
                pattern=#{pattern},
            </if>
            <if test="valueType != null">
                value_type=#{valueType},
            </if>
            <if test="defaultValue != null">
                default_value=#{defaultValue},
            </if>
            <if test="itemHide != null">
                item_hide=#{itemHide},
            </if>
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


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

    <!-- 根据薪资账套ID删除记录 -->
    <delete id="deleteItemShowBySalarySobId">
        DELETE FROM hrsa_salary_item_hide
        WHERE delete_type = 0
        AND salary_sob_id IN
        <foreach collection="salarySobIds" open="(" item="salarySobId" separator="," close=")">
            #{salarySobId}
        </foreach>
    </delete>


    <update id="deleteBySalarySobIds">
        UPDATE hrsa_salary_sob_item
        SET delete_type = 1
        WHERE delete_type = 0
        AND salary_sob_id IN
        <foreach collection="salarySobIds" open="(" item="salarySobId" separator="," close=")">
            #{salarySobId}
        </foreach>
    </update>

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

    <insert id="batchInsert">
        INSERT INTO hrsa_salary_sob_item(salary_sob_id, salary_item_id, salary_sob_item_group_id, formula_id,
        sorted_index, description, create_time, update_time, creator, delete_type, tenant_key,can_delete,rounding_mode,pattern,value_type,item_hide,default_value)
        VALUES
        <foreach collection="collection" item="item" separator=",">
            (
            #{item.salarySobId},
            #{item.salaryItemId},
            #{item.salarySobItemGroupId},
            #{item.formulaId},
            #{item.sortedIndex},
            #{item.description},
            #{item.createTime},
            #{item.updateTime},
            #{item.creator},
            #{item.deleteType},
            #{item.tenantKey},
            #{item.canDelete},
            #{item.roundingMode},
            #{item.pattern},
            #{item.valueType},
            #{item.itemHide},
            #{item.defaultValue}
            )
        </foreach>
    </insert>
    <insert id="batchInsert" databaseId="oracle">
        INSERT INTO hrsa_salary_sob_item( salary_sob_id, salary_item_id, salary_sob_item_group_id, formula_id,
        sorted_index, description, create_time, update_time, creator, delete_type, tenant_key,can_delete,rounding_mode,pattern,value_type,item_hide,default_value)

        <foreach collection="collection" item="item" separator="union all">
            select
            #{item.salarySobId,jdbcType=DOUBLE},
            #{item.salaryItemId,jdbcType=DOUBLE},
            #{item.salarySobItemGroupId,jdbcType=DOUBLE},
            #{item.formulaId,jdbcType=DOUBLE},
            #{item.sortedIndex,jdbcType=INTEGER},
            #{item.description,jdbcType=VARCHAR},
            #{item.createTime,jdbcType=DATE},
            #{item.updateTime,jdbcType=DATE},
            #{item.creator,jdbcType=DOUBLE},
            #{item.deleteType,jdbcType=INTEGER},
            #{item.tenantKey,jdbcType=VARCHAR},
            #{item.canDelete,jdbcType=INTEGER},
            #{item.roundingMode,jdbcType=INTEGER},
            #{item.pattern,jdbcType=INTEGER},
            #{item.valueType,jdbcType=INTEGER},
            #{item.itemHide,jdbcType=DOUBLE},
            #{item.defaultValue,jdbcType=VARCHAR}
            from dual
        </foreach>
    </insert>
    <insert id="batchInsert" databaseId="sqlserver">
        <foreach collection="collection" item="item" separator=";">
        INSERT INTO hrsa_salary_sob_item( salary_sob_id, salary_item_id, salary_sob_item_group_id, formula_id,
        sorted_index, description, create_time, update_time, creator, delete_type, tenant_key,can_delete,rounding_mode,pattern,value_type,item_hide,default_value)
        VALUES
            (
            #{item.salarySobId},
            #{item.salaryItemId},
            #{item.salarySobItemGroupId},
            #{item.formulaId},
            #{item.sortedIndex},
            #{item.description},
            #{item.createTime},
            #{item.updateTime},
            #{item.creator},
            #{item.deleteType},
            #{item.tenantKey},
            #{item.canDelete},
            #{item.roundingMode},
            #{item.pattern},
            #{item.valueType},
            #{item.itemHide},
            #{item.defaultValue}
            )
        </foreach>
    </insert>


    <update id="deleteByGroupIds">
        UPDATE hrsa_salary_sob_item
        SET delete_type = 1
        WHERE delete_type = 0
        AND salary_sob_item_group_id IN
        <foreach collection="groupIds" open="(" item="groupId" separator="," close=")">
            #{groupId}
        </foreach>
    </update>




</mapper>