<?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="cn.ey.biz.module.system.dal.mapper.engagement.task.SystemEngagementTaskRuleMapper">
    <resultMap id="BaseResultMap"
               type="cn.ey.biz.module.system.controller.engagement.task.vo.EngagementTaskRuleRespVO">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="country_code" jdbcType="VARCHAR" property="countryCode"/>
        <result column="mr_code" jdbcType="VARCHAR" property="mrCode"/>
        <result column="sl_code" jdbcType="VARCHAR" property="slCode"/>
        <result column="ssl_code" jdbcType="VARCHAR" property="sslCode"/>
        <result column="competency_code" jdbcType="VARCHAR" property="competencyCode"/>
        <result column="smu_code" jdbcType="VARCHAR" property="smuCode"/>
        <result column="task_type" jdbcType="VARCHAR" property="taskType"/>
        <result column="date_type" jdbcType="VARCHAR" property="dateType"/>
        <result column="warning_value" jdbcType="INTEGER" property="warningValue"/>
        <result column="overdue_day" jdbcType="VARCHAR" property="overdueDay"/>
        <result column="task_rule_desc" jdbcType="VARCHAR" property="taskRuleDesc"/>
        <result column="application_desc" jdbcType="VARCHAR" property="applicationDesc"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    </resultMap>
    <sql id="Base_Column_List">
        id, country_code, mr_code, sl_code, ssl_code, competency_code, smu_code, task_type,
        date_type, warning_value,overdue_day, task_rule_desc, application_desc, creator, create_time, updater,
        update_time, deleted
    </sql>

    <insert id="insert" keyColumn="id" keyProperty="id"
            parameterType="cn.ey.biz.module.system.dal.dataobject.engagement.task.SystemEngagementTaskRuleDO"
            useGeneratedKeys="true">
        insert into system_engagement_task_rule (country_code, mr_code, sl_code,
        ssl_code, competency_code, smu_code,
        task_type, date_type, warning_value,overdue_day,
        task_rule_desc, application_desc,
        creator, create_time, updater,
        update_time, deleted)
        values (#{countryCode,jdbcType=VARCHAR}, #{mrCode,jdbcType=VARCHAR}, #{slCode,jdbcType=VARCHAR},
        #{sslCode,jdbcType=VARCHAR}, #{competencyCode,jdbcType=VARCHAR}, #{smuCode,jdbcType=VARCHAR},
        #{taskType,jdbcType=BIT}, #{dateType,jdbcType=BIT}, #{warningValue,jdbcType=TINYINT},#{overdueDay},
        #{taskRuleDesc,jdbcType=VARCHAR}, #{applicationDesc,jdbcType=VARCHAR},
        #{creator,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updater,jdbcType=VARCHAR},
        #{updateTime,jdbcType=TIMESTAMP}, #{deleted,jdbcType=BIT})
    </insert>
    <select id="selectByTaskRuleDescAndApplicationDesc"
            resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from system_engagement_task_rule
        where
        task_type=#{taskType}
        and application_desc=#{applicationDesc}
        and deleted=0
    </select>
    <select id="selectById" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from system_engagement_task_rule
        where id=#{id} and deleted=0
    </select>

    <update id="updateBatchByIds">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            update system_engagement_task_rule
            <set>
                <if test="item.dateType !=null">
                    date_type = #{item.dateType},
                </if>
                <if test="item.overdueDay !=null">
                    overdue_day = #{item.overdueDay},
                </if>
                <if test="item.warningValue !=null and item.warningValue!=''">
                    warning_value = #{item.warningValue},
                </if>
                <if test="item.taskRuleDesc !=null and item.taskRuleDesc!=''">
                    task_rule_desc = #{item.taskRuleDesc},
                </if>
                <if test="item.applicationDesc !=null and item.applicationDesc!=''">
                    application_desc = #{item.applicationDesc},
                </if>
            </set>
            where id = #{item.id}
        </foreach>
    </update>

    <update id="deleteBatchByIds">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            update system_engagement_task_rule
            <set>
                deleted = 1
            </set>
            where id = #{item}
        </foreach>
    </update>

    <select id="selectListByPage" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from system_engagement_task_rule
        <include refid="queryByPage"/>
        order by id desc
        limit #{pageNo} offset #{pageSize}
    </select>

    <select id="selectCountByPage" resultType="java.lang.Integer">
        select count(1) from system_engagement_task_rule
        <include refid="queryByPage"/>
    </select>

    <sql id="queryByPage">
        <where>
            <if test="countryCode != null and countryCode != ''">
                and country_code = #{countryCode,jdbcType=VARCHAR}
            </if>
            <if test="mrCode != null and mrCode != ''">
                and mr_code = #{mrCode,jdbcType=VARCHAR}
            </if>
            <if test="slCode != null and slCode != ''">
                and sl_code = #{slCode,jdbcType=VARCHAR}
            </if>
            <if test="sslCode != null and sslCode != ''">
                and ssl_code = #{sslCode,jdbcType=VARCHAR}
            </if>
            <if test="competencyCode != null and competencyCode != ''">
                and competency_code = #{competencyCode,jdbcType=VARCHAR}
            </if>
            <if test="smuCode != null and smuCode != ''">
                and smu_code = #{smuCode,jdbcType=VARCHAR}
            </if>
            <if test="taskType != null">
                and task_type = #{taskType}
            </if>
            and deleted=0
        </where>
    </sql>
</mapper>