<?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.salaryitem.SalaryItemMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.salaryitem.po.SalaryItemPO">
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="code" property="code"/>
        <result column="system_type" property="systemType"/>
        <result column="sys_salary_item_id" property="sysSalaryItemId"/>
        <result column="use_default" property="useDefault"/>
        <result column="use_in_employee_salary" property="useInEmployeeSalary"/>
        <result column="rounding_mode" property="roundingMode"/>
        <result column="pattern" property="pattern"/>
        <result column="value_type" property="valueType"/>
        <result column="formula_id" property="formulaId"/>
        <result column="description" property="description"/>
        <result column="can_edit" property="canEdit"/>
        <result column="creator" property="creator"/>
        <result column="delete_type" property="deleteType"/>
        <result column="tenant_key" property="tenantKey"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="data_type" property="dataType"/>
        <result column="shared_type" property="sharedType"/>
        <result column="tax_agent_ids" property="taxAgentIds"/>
        <result column="sorted_index" property="sortedIndex"/>
        <result column="hide_default" property="hideDefault"/>
        <result column="width" property="width"/>
        <result column="default_value" property="defaultValue"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t
        .
        id
        ,
        t.name,
        t.code,
        t.system_type,
        t.sys_salary_item_id,
        t.use_default,
        t.use_in_employee_salary,
        t.rounding_mode,
        t.pattern,
        t.value_type,
        t.formula_id,
        t.description,
        t.can_edit,
        t.can_delete,
        t.creator,
        t.delete_type,
        t.tenant_key,
        t.create_time,
        t.update_time,
        t.data_type,
        t.shared_type,
        t.tax_agent_ids,
        t.sorted_index,
        t.hide_default,
        t.width,
        t.default_value
    </sql>

    <!-- 查询全部 -->
    <select id="listAll" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_salary_item t
        WHERE delete_type = 0
        ORDER BY sorted_index DESC, id DESC
    </select>

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

    <!-- 条件查询 -->
    <select id="listSome" resultMap="BaseResultMap" parameterType="com.engine.salary.entity.salaryitem.po.SalaryItemPO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_salary_item t
        WHERE delete_type = 0
        <if test="canEdit != null and canEdit != ''">
            AND can_edit = #{canEdit}
        </if>
        <if test="code != null and code != ''">
            AND code = #{code}
        </if>
        <if test="createTime != null and createTime != ''">
            AND create_time = #{createTime}
        </if>
        <if test="creator != null and creator != ''">
            AND creator = #{creator}
        </if>
        <if test="deleteType != null and deleteType != ''">
            AND delete_type = #{deleteType}
        </if>
        <if test="description != null and description != ''">
            AND description = #{description}
        </if>
        <if test="formulaId != null and formulaId != ''">
            AND formula_id = #{formulaId}
        </if>
        <if test="name != null and name != ''">
            AND name = #{name}
        </if>
        <if test="pattern != null and pattern != ''">
            AND pattern = #{pattern}
        </if>
        <if test="roundingMode != null and roundingMode != ''">
            AND rounding_mode = #{roundingMode}
        </if>
        <if test="sysSalaryItemId != null and sysSalaryItemId != ''">
            AND sys_salary_item_id = #{sysSalaryItemId}
        </if>
        <if test="systemType != null">
            AND system_type = #{systemType}
        </if>
        <if test="tenantKey != null and tenantKey != ''">
            AND tenant_key = #{tenantKey}
        </if>
        <if test="updateTime != null and updateTime != ''">
            AND update_time = #{updateTime}
        </if>
        <if test="useDefault != null and useDefault != ''">
            AND use_default = #{useDefault}
        </if>
        <if test="useInEmployeeSalary != null and useInEmployeeSalary != ''">
            AND use_in_employee_salary = #{useInEmployeeSalary}
        </if>
        <if test="valueType != null and valueType != ''">
            AND value_type = #{valueType}
        </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="sysSalaryItemIds != null and sysSalaryItemIds.size()>0">
            AND sys_salary_item_id IN
            <foreach collection="sysSalaryItemIds" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        ORDER BY sorted_index DESC, id DESC
    </select>

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

            <if test="id != null">
                id,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="code != null">
                code,
            </if>
            <if test="systemType != null">
                system_type,
            </if>
            <if test="sysSalaryItemId != null">
                sys_salary_item_id,
            </if>
            <if test="useDefault != null">
                use_default,
            </if>
            <if test="useInEmployeeSalary != null">
                use_in_employee_salary,
            </if>
            <if test="hideDefault != null">
                hide_default,
            </if>
            <if test="roundingMode != null">
                rounding_mode,
            </if>
            <if test="pattern != null">
                pattern,
            </if>
            <if test="valueType != null">
                value_type,
            </if>
            <if test="formulaId != null">
                formula_id,
            </if>
            <if test="description != null">
                description,
            </if>
            <if test="canEdit != null">
                can_edit,
            </if>
            <if test="creator != null">
                creator,
            </if>
            <if test="deleteType != null">
                delete_type,
            </if>
            <if test="tenantKey != null">
                tenant_key,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="dataType != null">
                data_type,
            </if>
            <if test="sharedType != null">
                shared_type,
            </if>
            <if test="taxAgentIds != null">
                tax_agent_ids,
            </if>
            <if test="sortedIndex != null">
                sorted_index,
            </if>
            <if test="width != null">
                width,
            </if>
            <if test="defaultValue != null">
                default_value,
            </if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="name != null">
                #{name},
            </if>
            <if test="code != null">
                #{code},
            </if>
            <if test="systemType != null">
                #{systemType},
            </if>
            <if test="sysSalaryItemId != null">
                #{sysSalaryItemId},
            </if>
            <if test="useDefault != null">
                #{useDefault},
            </if>
            <if test="useInEmployeeSalary != null">
                #{useInEmployeeSalary},
            </if>
            <if test="hideDefault != null">
                #{hideDefault},
            </if>
            <if test="roundingMode != null">
                #{roundingMode},
            </if>
            <if test="pattern != null">
                #{pattern},
            </if>
            <if test="valueType != null">
                #{valueType},
            </if>
            <if test="formulaId != null">
                #{formulaId},
            </if>
            <if test="description != null">
                #{description},
            </if>
            <if test="canEdit != null">
                #{canEdit},
            </if>
            <if test="creator != null">
                #{creator},
            </if>
            <if test="deleteType != null">
                #{deleteType},
            </if>
            <if test="tenantKey != null">
                #{tenantKey},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
            <if test="dataType != null">
                #{dataType},
            </if>
            <if test="sharedType != null">
                #{sharedType},
            </if>
            <if test="taxAgentIds != null">
                #{taxAgentIds},
            </if>
            <if test="sortedIndex != null">
                #{sortedIndex},
            </if>
            <if test="width != null">
                #{width},
            </if>
            <if test="defaultValue != null">
                #{defaultValue},
            </if>
        </trim>
    </insert>


    <!-- 更新不为NULL的字段 -->
    <update id="updateIgnoreNull" parameterType="com.engine.salary.entity.salaryitem.po.SalaryItemPO">
        UPDATE hrsa_salary_item
        <set>
            <if test="name != null">
                name=#{name},
            </if>
            <if test="code != null">
                code=#{code},
            </if>
            <if test="systemType != null">
                system_type=#{systemType},
            </if>
            <if test="sysSalaryItemId != null">
                sys_salary_item_id=#{sysSalaryItemId},
            </if>
            <if test="useDefault != null">
                use_default=#{useDefault},
            </if>
            <if test="useInEmployeeSalary != null">
                use_in_employee_salary=#{useInEmployeeSalary},
            </if>
            <if test="hideDefault != null">
                hide_default=#{hideDefault},
            </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="formulaId != null">
                formula_id=#{formulaId},
            </if>
            <if test="description != null">
                description=#{description},
            </if>
            <if test="canEdit != null">
                can_edit=#{canEdit},
            </if>
            <if test="creator != null">
                creator=#{creator},
            </if>
            <if test="deleteType != null">
                delete_type=#{deleteType},
            </if>
            <if test="tenantKey != null">
                tenant_key=#{tenantKey},
            </if>
            <if test="createTime != null">
                create_time=#{createTime},
            </if>
            <if test="updateTime != null">
                update_time=#{updateTime},
            </if>
            <if test="dataType != null">
                data_type=#{dataType},
            </if>
            <if test="sharedType != null">
                shared_type=#{sharedType},
            </if>
            <if test="taxAgentIds != null">
                tax_agent_ids=#{taxAgentIds},
            </if>
            <if test="width != null">
                width=#{width},
            </if>
            <if test="sortedIndex != null">
                sorted_index=#{sortedIndex},
            </if>
            <if test="defaultValue != null">
                default_value=#{defaultValue},
            </if>
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


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


    <insert id="batchInsert">
        INSERT INTO hrsa_salary_item
        (id, name, code, system_type, sys_salary_item_id, use_default, use_in_employee_salary,
        rounding_mode, pattern, value_type, formula_id, description, can_edit, create_time,
        update_time, creator, delete_type, tenant_key, data_type, shared_type, tax_agent_ids)
        VALUES
        <foreach collection="collection" item="item" separator=",">
            (#{item.id},#{item.name}, #{item.code}, #{item.systemType}, #{item.sysSalaryItemId},
            #{item.useDefault}, #{item.useInEmployeeSalary}, #{item.roundingMode},
            #{item.pattern},
            #{item.valueType}, #{item.formulaId}, #{item.description}, #{item.canEdit},
            #{item.createTime}, #{item.updateTime}, #{item.creator}, 0, #{item.tenantKey}, #{item.dataType},
            #{item.sharedType}, #{item.taxAgentIds})
        </foreach>
    </insert>
    <insert id="batchInsert" databaseId="oracle">
        INSERT INTO hrsa_salary_item
        (id,name, code, system_type, sys_salary_item_id, use_default, use_in_employee_salary,
        rounding_mode, pattern, value_type, formula_id, description, can_edit, create_time,
        update_time, creator, delete_type, tenant_key, data_type, shared_type, tax_agent_ids)

        <foreach collection="collection" item="item" separator="union all">
            select
            #{item.id,jdbcType=DOUBLE},
            #{item.name,jdbcType=VARCHAR},
            #{item.code,jdbcType=VARCHAR},
            #{item.systemType,jdbcType=INTEGER},
            #{item.sysSalaryItemId,jdbcType=DOUBLE},
            #{item.useDefault,jdbcType=INTEGER},
            #{item.useInEmployeeSalary,jdbcType=INTEGER},
            #{item.roundingMode,jdbcType=INTEGER},
            #{item.pattern,jdbcType=INTEGER},
            #{item.valueType,jdbcType=INTEGER},
            #{item.formulaId,jdbcType=DOUBLE},
            #{item.description,jdbcType=VARCHAR},
            #{item.canEdit,jdbcType=INTEGER},
            #{item.createTime,jdbcType=DATE},
            #{item.updateTime,jdbcType=DATE},
            #{item.creator,jdbcType=DOUBLE},
            0,
            #{item.tenantKey,jdbcType=VARCHAR},
            #{item.dataType,jdbcType=VARCHAR},
            #{item.sharedType,jdbcType=INTEGER},
            #{item.taxAgentIds,jdbcType=VARCHAR}
            from dual
        </foreach>
    </insert>
    <insert id="batchInsert" databaseId="sqlserver">
        <foreach collection="collection" item="item" separator=";">
            INSERT INTO hrsa_salary_item
            (id,name, code, system_type, sys_salary_item_id, use_default, use_in_employee_salary,
            rounding_mode, pattern, value_type, formula_id, description, can_edit, create_time,
            update_time, creator, delete_type, tenant_key, data_type, shared_type, tax_agent_ids)
            VALUES
            (#{item.id},#{item.name}, #{item.code}, #{item.systemType}, #{item.sysSalaryItemId},
            #{item.useDefault}, #{item.useInEmployeeSalary}, #{item.roundingMode},
            #{item.pattern},
            #{item.valueType}, #{item.formulaId}, #{item.description}, #{item.canEdit},
            #{item.createTime}, #{item.updateTime}, #{item.creator}, 0, #{item.tenantKey}, #{item.dataType},
            #{item.sharedType}, #{item.taxAgentIds})
        </foreach>
    </insert>

    <update id="deleteByIds">
        UPDATE hrsa_salary_item
        SET delete_type = 1
        WHERE delete_type = 0
        AND id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </update>
    <update id="batchUpdateSortedIndex">
        update hrsa_salary_item
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="sorted_index =case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.sortedIndex!=null">
                        when id=#{item.id} then #{item.sortedIndex}
                    </if>
                </foreach>
            </trim>
        </trim>
        where
        id in
        <foreach collection="collection" item="item" index="index" separator="," open="(" close=")">
            #{item.id}
        </foreach>
    </update>

    <select id="listByParam" resultType="com.engine.salary.entity.salaryitem.po.SalaryItemPO">
        SELECT
        <include refid="baseColumns"/>
        FROM
        hrsa_salary_item t
        WHERE
        delete_type = 0
        <include refid="paramSql"/>
        ORDER BY sorted_index DESC, create_time DESC
    </select>

    <select id="listByParamOrderById" resultType="com.engine.salary.entity.salaryitem.po.SalaryItemPO">
        SELECT
        <include refid="baseColumns"/>
        FROM
        hrsa_salary_item t
        WHERE
        delete_type = 0
        <include refid="paramSql"/>
        ORDER BY id DESC
    </select>


    <sql id="paramSql">
        <if test="param.name != null and param.name != ''">
            AND name like CONCAT('%',#{param.name},'%')
        </if>
        <if test="param.description != null and param.description != ''">
            AND description like CONCAT('%',#{param.description},'%')
        </if>
        <if test="param.excludeIds != null and param.excludeIds.size()>0">
            AND id NOT IN
            <foreach collection="param.excludeIds" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="param.useInEmployeeSalary != null">
            AND use_in_employee_salary = #{param.useInEmployeeSalary}
        </if>
        <if test="param.useDefault != null">
            AND use_default = #{param.useDefault}
        </if>
        <if test="param.valueType != null">
            AND value_type = #{param.valueType}
        </if>
    </sql>
    <sql id="paramSql" databaseId="oracle">
        <if test="param.name != null and param.name != ''">
            AND name like '%'||#{param.name}||'%'
        </if>
        <if test="param.description != null">
            AND description like '%'||#{param.description}||'%'
        </if>
        <if test="param.excludeIds != null and param.excludeIds.size()>0">
            AND id NOT IN
            <foreach collection="param.excludeIds" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="param.useInEmployeeSalary != null">
            AND use_in_employee_salary = #{param.useInEmployeeSalary}
        </if>
        <if test="param.useDefault != null">
            AND use_default = #{param.useDefault}
        </if>
        <if test="param.valueType != null">
            AND value_type = #{param.valueType}
        </if>
    </sql>
    <sql id="paramSql" databaseId="sqlserver">
        <if test="param.name != null and param.name != ''">
            AND name like '%'+#{param.name}+'%'
        </if>
        <if test="param.description != null and param.description != ''">
            AND description like '%'+#{param.description}+'%'
        </if>
        <if test="param.excludeIds != null and param.excludeIds.size()>0">
            AND id NOT IN
            <foreach collection="param.excludeIds" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="param.useInEmployeeSalary != null">
            AND use_in_employee_salary = #{param.useInEmployeeSalary}
        </if>
        <if test="param.useDefault != null">
            AND use_default = #{param.useDefault}
        </if>
        <if test="param.valueType != null">
            AND value_type = #{param.valueType}
        </if>
    </sql>

    <select id="getByName" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_salary_item t
        WHERE name = #{name} AND delete_type = 0
    </select>

    <select id="getByCode" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_salary_item t
        WHERE code = #{code} AND delete_type = 0
    </select>


    <resultMap id="configResultMap" type="com.engine.salary.entity.salaryitem.config.SalaryItemExcelConfig">
        <result column="name" property="name"/>
        <result column="use_default" property="useDefault"/>
        <result column="hide_default" property="hideDefault"/>
        <result column="data_type" property="dataType"/>
        <result column="rounding_mode" property="roundingMode"/>
        <result column="pattern" property="pattern"/>
        <result column="value_type" property="valueType"/>
        <result column="formula" property="formula"/>
        <result column="extend_param" property="extendParam"/>
        <result column="sorted_index" property="sortedIndex"/>
        <result column="width" property="width"/>
        <result column="default_value" property="defaultValue"/>
        <result column="description" property="description"/>
    </resultMap>


    <sql id="itemColumns">
        item.name                         as   name
        , item.use_default                 as   use_default
        , item.hide_default                as   hide_default
        , item.data_type                   as   data_type
        , item.rounding_mode               as   rounding_mode
        , item.pattern                     as   pattern
        , item.value_type                  as   value_type
        , f.formula                        as   formula
        , f.extend_param                   as   extend_param
        , item.sorted_index                as   sorted_index
        , item.width                       as   width
        , item.default_value               as   default_value
        , item.description                 as   description
    </sql>


    <select id="getConfig" resultMap="configResultMap">
        select
        <include refid="itemColumns"/>
        from hrsa_salary_item item
        left join hrsa_formula f on item.formula_id = f.id
        where item.delete_type=0
        and item.use_in_employee_salary = 0
        and item.system_type = 0
        <if test="ids != null and ids.size()>0">
            AND item.id IN
            <foreach collection="ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>

    </select>


</mapper>