<?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.organization.mapper.scheme.GradeMapper">
    <resultMap id="BaseResultMap" type="com.engine.organization.entity.scheme.po.GradePO">
        <result column="id" property="id"/>
        <result column="grade_no" property="gradeNo"/>
        <result column="grade_name" property="gradeName"/>
        <result column="description" property="description"/>
        <result column="scheme_id" property="schemeId"/>
        <result column="level_id" property="levelId"/>
        <result column="forbidden_tag" property="forbiddenTag"/>
        <result column="creator" property="creator"/>
        <result column="delete_type" property="deleteType"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="is_used" property="isUsed"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t
        .
        id
        , t.grade_no
        , t.grade_name
        , t.description
        , t.scheme_id
        , t.level_id
        , t.forbidden_tag
        , t.creator
        , t.delete_type
        , t.create_time
        , t.update_time
        , t.is_used
    </sql>
    <select id="getGradeByID" parameterType="com.engine.organization.entity.scheme.po.GradePO"
            resultMap="BaseResultMap">
        select
        <include refid="baseColumns"/>
        from jcl_org_grade t where id = #{id} AND delete_type = 0
    </select>

    <select id="listByNo" parameterType="com.engine.organization.entity.scheme.po.GradePO" resultMap="BaseResultMap">
        select
        <include refid="baseColumns"/>
        from jcl_org_grade t where grade_no = #{gradeNo} AND delete_type = 0
    </select>
    <select id="getGradeByNoAndSid" parameterType="com.engine.organization.entity.scheme.po.GradePO" resultMap="BaseResultMap">
        select
        <include refid="baseColumns"/>
        from jcl_org_grade t where grade_no = #{gradeNo} AND scheme_id = #{schemeId} AND delete_type = 0
    </select>

    <select id="getCountByTag" resultType="java.lang.Integer">
        select count(1) from jcl_org_grade where 1=1 AND delete_type = 0
        <if test=" tag != -1 ">
            and forbidden_tag = #{tag}
        </if>
    </select>
    <select id="listGradesBySchemeId" resultMap="BaseResultMap">
        select
        <include refid="baseColumns"/>
        from jcl_org_grade t where delete_type = 0 and scheme_id = #{schemeId}
    </select>
    <select id="listUsedId" resultType="java.lang.String">
        select grade_id
        from JCL_ORG_JOB
        where delete_type = 0
    </select>
    <select id="getGradeNameByIds" resultType="java.lang.String">
        select grade_name from jcl_org_grade where delete_type = 0
        AND id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </select>
    <select id="listGradessByIds" resultType="java.util.Map">
        select
        id as "id",
        grade_name as "name"
        from jcl_org_grade t
        WHERE delete_type = 0
        AND id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <update id="updateGrade" parameterType="com.engine.organization.entity.scheme.po.GradePO">
        update jcl_org_grade
        <set>
            creator=#{creator},
            update_time=#{updateTime},
            grade_no=#{gradeNo},
            grade_name=#{gradeName},
            description=#{description},
            scheme_id=#{schemeId},
            level_id=#{levelId},
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>

    <insert id="insertIgnoreNull" parameterType="com.engine.organization.entity.scheme.po.GradePO" keyProperty="id"
            keyColumn="id" useGeneratedKeys="true">
        INSERT INTO jcl_org_grade
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="creator != null">
                creator,
            </if>
            <if test="deleteType != null">
                delete_type,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="gradeNo != null ">
                grade_no,
            </if>
            <if test="gradeName != null ">
                grade_name,
            </if>
            <if test="description != null ">
                description,
            </if>
            <if test="schemeId != null ">
                scheme_id,
            </if>
            <if test="levelId != null ">
                level_id,
            </if>
            <if test="isUsed != null ">
                is_used,
            </if>
            forbidden_tag,
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="creator != null">
                #{creator},
            </if>
            <if test="deleteType != null">
                #{deleteType},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
            <if test="gradeNo != null ">
                #{gradeNo},
            </if>
            <if test="gradeName != null ">
                #{gradeName},
            </if>
            <if test="description != null ">
                #{description},
            </if>
            <if test="schemeId != null ">
                #{schemeId},
            </if>
            <if test="levelId != null ">
                #{levelId},
            </if>
            <if test="isUsed != null ">
                #{isUsed},
            </if>
            0,
        </trim>
    </insert>

    <insert id="insertIgnoreNull" parameterType="com.engine.organization.entity.scheme.po.GradePO" databaseId="oracle">
        <selectKey  keyProperty="id" resultType="long" order="AFTER">
            select JCL_ORG_GRADE_ID.currval from dual
        </selectKey>
        INSERT INTO jcl_org_grade
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="creator != null">
                creator,
            </if>
            <if test="deleteType != null">
                delete_type,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="gradeNo != null ">
                grade_no,
            </if>
            <if test="gradeName != null ">
                grade_name,
            </if>
            <if test="description != null ">
                description,
            </if>
            <if test="schemeId != null ">
                scheme_id,
            </if>
            <if test="levelId != null ">
                level_id,
            </if>
            forbidden_tag,
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="creator != null">
                #{creator},
            </if>
            <if test="deleteType != null">
                #{deleteType},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
            <if test="gradeNo != null ">
                #{gradeNo},
            </if>
            <if test="gradeName != null ">
                #{gradeName},
            </if>
            <if test="description != null ">
                #{description},
            </if>
            <if test="schemeId != null ">
                #{schemeId},
            </if>
            <if test="levelId != null ">
                #{levelId},
            </if>
            0,
        </trim>
    </insert>

    <update id="updateForbiddenTagById" parameterType="com.engine.organization.entity.scheme.po.GradePO">
        update jcl_org_grade
        <set>
            forbidden_tag=#{forbiddenTag},
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>

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

    <select id="getGradeByLevelId" resultType="com.engine.organization.entity.scheme.po.GradePO">
        select
        <include refid="baseColumns"/>
        from jcl_org_grade t where delete_type = 0
        <include refid="likeSQL"/>
    </select>
    <select id="checkRepeatNo" resultType="java.lang.Integer">
        select
        count(1)
        from jcl_org_grade
        where delete_type = 0
        and scheme_id =#{schemeId}
        and grade_no = #{no}
        <if test=" id!=null ">
            and id != #{id}
        </if>
    </select>

    <sql id="likeSQL">
        concat(',', level_id, ',') like concat('%,', cast(#{levelId} as CHAR), ',%')
    </sql>
    <sql id="likeSQL" databaseId="oracle">
        ','||level_id||',' like '%,'||#{levelId}+',%'
    </sql>
    <sql id="likeSQL" databaseId="postgresql">
        ','||level_id||',' like '%,'||#{levelId}+',%'
    </sql>
    <sql id="likeSQL" databaseId="sqlserver">
        ','+level_id+',' like '%,'+cast(#{levelId} as varchar(20))+',%'
    </sql>

</mapper>