<?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.SalarySobRangeMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.salarysob.po.SalarySobRangePO">
        <result column="id" property="id"/>
        <result column="salary_sob_id" property="salarySobId"/>
        <result column="target_type" property="targetType"/>
        <result column="target_id" property="targetId"/>
        <result column="target" property="target"/>
        <result column="employee_status" property="employeeStatus"/>
        <result column="include_type" property="includeType"/>
        <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.target_type
        , t.target_id
        , t.target
        , t.employee_status
        , t.include_type
        , t.creator
        , t.create_time
        , t.update_time
        , t.delete_type
        , t.tenant_key
        , t.employee_statuses
    </sql>

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

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

    <!-- 条件查询 -->
    <select id="listSome" resultMap="BaseResultMap"
            parameterType="com.engine.salary.entity.salarysob.po.SalarySobRangePO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_salary_sob_range t
        WHERE delete_type = 0
        <if test="id != null and id != ''">
            AND id = #{id}
        </if>
        <if test="salarySobId != null and salarySobId != ''">
            AND salary_sob_id = #{salarySobId}
        </if>
        <if test="targetType != null and targetType != ''">
            AND target_type = #{targetType}
        </if>
        <if test="targetId != null and targetId != ''">
            AND target_id = #{targetId}
        </if>
        <if test="target != null and target != ''">
            AND target = #{target}
        </if>
        <if test="employeeStatus != null and employeeStatus != ''">
            AND employee_status = #{employeeStatus}
        </if>
        <if test="includeType != null">
            AND include_type = #{includeType}
        </if>
        <if test="creator != null and creator != ''">
            AND creator = #{creator}
        </if>
        <if test="createTime != null and createTime != ''">
            AND create_time = #{createTime}
        </if>
        <if test="updateTime != null and updateTime != ''">
            AND update_time = #{updateTime}
        </if>
        <if test="deleteType != null and deleteType != ''">
            AND delete_type = #{deleteType}
        </if>
        <if test="tenantKey != null and tenantKey != ''">
            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>
        ORDER BY id DESC
    </select>


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

            <if test="id != null">
                id,
            </if>
            <if test="salarySobId != null">
                salary_sob_id,
            </if>
            <if test="targetType != null">
                target_type,
            </if>
            <if test="targetId != null">
                target_id,
            </if>
            <if test="target != null">
                target,
            </if>
            <if test="employeeStatus != null">
                employee_status,
            </if>
            <if test="includeType != null">
                include_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="salarySobId != null">
                #{salarySobId},
            </if>
            <if test="targetType != null">
                #{targetType},
            </if>
            <if test="targetId != null">
                #{targetId},
            </if>
            <if test="target != null">
                #{target},
            </if>
            <if test="employeeStatus != null">
                #{employeeStatus},
            </if>
            <if test="includeType != null">
                #{includeType},
            </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.salarysob.po.SalarySobRangePO" databaseId="oracle"
    >
        <selectKey  keyProperty="id" resultType="long" order="AFTER">
            select hrsa_salary_sob_range_id.currval from dual
        </selectKey>
        INSERT INTO hrsa_salary_sob_range
        <trim prefix="(" suffix=")" suffixOverrides=",">

            <if test="id != null">
                id,
            </if>
            <if test="salarySobId != null">
                salary_sob_id,
            </if>
            <if test="targetType != null">
                target_type,
            </if>
            <if test="targetId != null">
                target_id,
            </if>
            <if test="target != null">
                target,
            </if>
            <if test="employeeStatus != null">
                employee_status,
            </if>
            <if test="includeType != null">
                include_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="salarySobId != null">
                #{salarySobId},
            </if>
            <if test="targetType != null">
                #{targetType},
            </if>
            <if test="targetId != null">
                #{targetId},
            </if>
            <if test="target != null">
                #{target},
            </if>
            <if test="employeeStatus != null">
                #{employeeStatus},
            </if>
            <if test="includeType != null">
                #{includeType},
            </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.salarysob.po.SalarySobRangePO">
        UPDATE hrsa_salary_sob_range
        <set>
            salary_sob_id=#{salarySobId},
            target_type=#{targetType},
            target_id=#{targetId},
            target=#{target},
            employee_status=#{employeeStatus},
            include_type=#{includeType},
            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.salarysob.po.SalarySobRangePO">
        UPDATE hrsa_salary_sob_range
        <set>
            <if test="salarySobId != null">
                salary_sob_id=#{salarySobId},
            </if>
            <if test="targetType != null">
                target_type=#{targetType},
            </if>
            <if test="targetId != null">
                target_id=#{targetId},
            </if>
            <if test="target != null">
                target=#{target},
            </if>
            <if test="employeeStatuses != null">
                employee_statuses=#{employeeStatuses},
            </if>
            <if test="includeType != null">
                include_type=#{includeType},
            </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.salarysob.po.SalarySobRangePO">
        UPDATE hrsa_salary_sob_range
        SET delete_type=1
        WHERE id = #{id}
          AND delete_type = 0
    </delete>


    <insert id="batchInsert">
        INSERT INTO hrsa_salary_sob_range(salary_sob_id, target_type, target_id,target, employee_status, include_type,
        creator, create_time, update_time, delete_type, tenant_key,employee_statuses)
        VALUES
        <foreach collection="collection" item="item" separator=",">
            (
            #{item.salarySobId},
            #{item.targetType.value},
            #{item.targetId},
            #{item.target},
            #{item.employeeStatus.value},
            #{item.includeType},
            #{item.creator},
            #{item.createTime},
            #{item.updateTime},
            #{item.deleteType},
            #{item.tenantKey},
            #{item.employeeStatuses}
            )
        </foreach>
    </insert>
    <insert id="batchInsert" databaseId="oracle">
        INSERT INTO hrsa_salary_sob_range(salary_sob_id, target_type, target_id,target, employee_status, include_type,
        creator, create_time, update_time, delete_type, tenant_key,employee_statuses)

        <foreach collection="collection" item="item" separator="union all">
            select
            #{item.salarySobId,jdbcType=DOUBLE},
            #{item.targetType.value,jdbcType=INTEGER},
            #{item.targetId,jdbcType=DOUBLE},
            #{item.target,jdbcType=VARCHAR},
            #{item.employeeStatus.value,jdbcType=INTEGER},
            #{item.includeType,jdbcType=INTEGER},
            #{item.creator,jdbcType=DOUBLE},
            #{item.createTime,jdbcType=DATE},
            #{item.updateTime,jdbcType=DATE},
            #{item.deleteType,jdbcType=INTEGER},
            #{item.tenantKey,jdbcType=VARCHAR},
            #{item.employeeStatuses,jdbcType=VARCHAR}
            from dual
        </foreach>
    </insert>
    <insert id="batchInsert" databaseId="sqlserver">
        <foreach collection="collection" item="item" separator=";">
        INSERT INTO hrsa_salary_sob_range( salary_sob_id, target_type, target_id,target, employee_status, include_type,
        creator, create_time, update_time, delete_type, tenant_key,employee_statuses)
        VALUES
            (
            #{item.salarySobId},
            #{item.targetType.value},
            #{item.targetId},
            #{item.target},
            #{item.employeeStatus.value},
            #{item.includeType},
            #{item.creator},
            #{item.createTime},
            #{item.updateTime},
            #{item.deleteType},
            #{item.tenantKey},
            #{item.employeeStatuses}
            )
        </foreach>
    </insert>
    <update id="deleteBySalarySobIds">
        UPDATE hrsa_salary_sob_range
        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_range
        SET delete_type = 1
        WHERE  delete_type = 0
        AND id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="updateEmployeeStatuses">
        UPDATE hrsa_salary_sob_range set employee_statuses = #{param.employeeStatuses} WHERE id = #{param.id}
    </update>

    <select id="listEmployeeIds" resultType="long">
        SELECT id FROM hrmresource em
        WHERE  em.status not in (7)

        <if test="params != null and params.size() > 0">
            AND ( 1=2
            <foreach collection="params" item="param">
                OR
                (
                <if test="param.targetType == 'EMPLOYEE'">
                    em.id IN
                    <foreach collection="param.targetIds" open="(" item="targetId" separator="," close=")">
                        #{targetId}
                    </foreach>
                </if>
                <if test="param.targetType == 'DEPT'">
                    em.departmentid IN
                    <foreach collection="param.targetIds" open="(" item="targetId" separator="," close=")">
                        #{targetId}
                    </foreach>
                </if>
                <if test="param.targetType == 'SUBCOMPANY'">
                    em.subcompanyid1 IN
                    <foreach collection="param.targetIds" open="(" item="targetId" separator="," close=")">
                        #{targetId}
                    </foreach>
                </if>
                <if test="param.targetType == 'POSITION'">
                    em.jobtitle IN
                    <foreach collection="param.targetIds" open="(" item="targetId" separator="," close=")">
                        #{targetId}
                    </foreach>
                </if>
                <if test="param.employeeStatus != null and param.employeeStatus.size() > 0">
                    AND em.status IN
                    <foreach collection="param.employeeStatus" open="(" item="status" separator="," close=")">
                        #{status}
                    </foreach>
                </if>
                )
            </foreach>
            )
        </if>
    </select>


</mapper>