<?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.SalaryBillItemNameMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.salaryBill.po.SalaryBillItemNamePO">
        <result column="id" property="id" />
        <result column="salary_item_id" property="salaryItemId" />
        <result column="salary_item_show_name" property="salaryItemShowName" />
        <result column="salary_template_id" property="salaryTemplateId" />
        <result column="delete_type" property="deleteType" />
        <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="baseColumns">
        t.id
        , t.salary_item_id
        , t.salary_item_show_name
        , t.salary_template_id
        , t.salary_bill_type
        , t.delete_type
        , t.creator
        , t.create_time
        , t.update_time
        , t.tenant_key
    </sql>

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

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

    <!-- 条件查询 -->
    <select id="listSome" resultMap="BaseResultMap" parameterType="com.engine.salary.entity.salaryBill.po.SalaryBillItemNamePO">
        SELECT
        <include refid="baseColumns" />
        FROM hrsa_salary_bill_item_name t
        WHERE delete_type = 0
        <if test="id != null">
            AND id = #{id}
        </if>
        <if test="salaryItemId != null">
            AND salary_item_id = #{salaryItemId}
        </if>
        <if test="salaryItemShowName != null">
            AND salary_item_show_name = #{salaryItemShowName}
        </if>
        <if test="salaryTemplateId != null">
            AND salary_template_id = #{salaryTemplateId}
        </if>
        <if test="salaryBillType != null">
            AND salary_bill_type = #{salaryBillType}
        </if>
        <if test="ids != null and ids.size()>0">
            AND id IN
            <foreach collection="ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        ORDER BY id DESC
    </select>


    <!-- 插入不为NULL的字段 -->
    <insert id="insertIgnoreNull" parameterType="com.engine.salary.entity.salaryBill.po.SalaryBillItemNamePO">
        INSERT INTO hrsa_salary_bill_item_name
        <trim prefix="(" suffix=")" suffixOverrides=",">

            <if test="id != null">
                id,
            </if>
            <if test="salaryItemId != null">
                salary_item_id,
            </if>
            <if test="salaryItemShowName != null">
                salary_item_show_name,
            </if>
            <if test="salaryTemplateId != null">
                salary_template_id,
            </if>
            <if test="salaryBillType != null">
                salary_bill_type,
            </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="salaryItemId != null" >
                #{salaryItemId},
            </if>
            <if test="salaryItemShowName != null" >
                #{salaryItemShowName},
            </if>
            <if test="salaryTemplateId != null" >
                #{salaryTemplateId},
            </if>
            <if test="salaryBillType != null" >
                #{salaryBillType},
            </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="batchInsert">
        INSERT INTO hrsa_salary_bill_item_name (
        id,
        salary_template_id,
        salary_item_id,
        salary_item_show_name,
        salary_bill_type,
        creator,
        create_time,
        update_time,
        delete_type,
        tenant_key
        )
        VALUES
        <foreach collection="collection" item="item" separator=",">
            (
            #{item.id},
            #{item.salaryTemplateId},
            #{item.salaryItemId},
            #{item.salaryItemShowName},
            #{item.salaryBillType},
            #{item.creator},
            #{item.createTime},
            #{item.updateTime},
            #{item.deleteType},
            #{item.tenantKey}
            )
        </foreach>
    </insert>
    <insert id="batchInsert" databaseId="oracle">
        INSERT INTO hrsa_salary_bill_item_name (
        id,
        salary_template_id,
        salary_item_id,
        salary_item_show_name,
        salary_bill_type,
        creator,
        create_time,
        update_time,
        delete_type,
        tenant_key
        )
        <foreach collection="collection" item="item" separator="union all">
            select
            #{item.id,jdbcType=DOUBLE},
            #{item.salaryTemplateId,jdbcType=DOUBLE},
            #{item.salaryItemId,jdbcType=DOUBLE},
            #{item.salaryItemShowName,jdbcType=VARCHAR},
            #{item.salaryBillType,jdbcType=INTEGER},
            #{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_bill_item_name (
            id,
            salary_template_id,
            salary_item_id,
            salary_item_show_name,
            salary_bill_type,
            creator,
            create_time,
            update_time,
            delete_type,
            tenant_key
            )
            VALUES
            (
            #{item.id},
            #{item.salaryTemplateId},
            #{item.salaryItemId},
            #{item.salaryItemShowName},
            #{item.salaryBillType},
            #{item.creator},
            #{item.createTime},
            #{item.updateTime},
            #{item.deleteType},
            #{item.tenantKey}
            )
        </foreach>
    </insert>

    <!-- 更新,更新全部字段 -->
    <update id="update" parameterType="com.engine.salary.entity.salaryBill.po.SalaryBillItemNamePO">
        UPDATE hrsa_salary_bill_item_name
        <set>
            salary_item_id=#{salaryItemId},
            salary_item_show_name=#{salaryItemShowName},
            salary_template_id=#{salaryTemplateId},
            salary_bill_type=#{salaryBillType},
            creator=#{creator},
            create_time={createTime},
            update_time=#{updateTime},
            tenant_key=#{tenantKey}
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


    <!-- 更新不为NULL的字段 -->
    <update id="updateIgnoreNull" parameterType="com.engine.salary.entity.salaryBill.po.SalaryBillItemNamePO">
        UPDATE hrsa_salary_bill_item_name
        <set>
            <if test="salaryItemId != null" >
                salary_item_id=#{salaryItemId},
            </if>
            <if test="salaryItemShowName != null" >
                salary_item_show_name=#{salaryItemShowName},
            </if>
            <if test="salaryTemplateId != null" >
                salary_template_id=#{salaryTemplateId},
            </if>
            <if test="salaryBillType != null" >
                salary_bill_type=#{salaryBillType},
            </if>
            <if test="creator != null" >
                creator=#{creator},
            </if>
            <if test="create_time != null" >
                create_time=#{createTime},
            </if>
            <if test="updateTime != null" >
                update_time=#{updateTime},
            </if>
            <if test="tenantKey != null" >
                tenant_key=#{tenantKey},
            </if>
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>

    <update id="batchUpdate">
        UPDATE hrsa_salary_bill_item_name
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="salary_item_show_name = case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    WHEN id = #{item.id} THEN #{item.salaryItemShowName}
                </foreach>
            </trim>
            <trim prefix="update_time = case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    WHEN id = #{item.id} THEN #{item.updateTime}
                </foreach>
            </trim>
        </trim>
        WHERE delete_type = 0
        AND id IN
        <foreach collection="collection" item="item" open="(" close=")" separator=",">
            #{item.id}
        </foreach>
    </update>


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

    <delete id="deleteByItemIdsAndTemplateId">
        UPDATE hrsa_salary_bill_item_name
        SET delete_type=1
        WHERE delete_type = 0
        AND salary_template_id = #{salaryTemplateId}
        AND salary_bill_type = #{salaryBillType}
        AND salary_item_id IN
        <foreach collection="salaryItemIds" item="itemId" open="(" close=")" separator=",">
            #{itemId}
        </foreach>
    </delete>

    <delete id="deleteByIds">
        UPDATE hrsa_salary_bill_item_name
        SET delete_type=1
        WHERE delete_type = 0
        AND id IN
        <foreach collection="collection" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteByTemplateIds">
        UPDATE hrsa_salary_bill_item_name
        SET delete_type=1
        WHERE delete_type = 0
        AND salary_template_id IN
        <foreach collection="collection" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>


</mapper>