<?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.roadm.mapper.SmsLogisticsPreventFraudMapper">

    <resultMap id="smsLogisticsPreventFraudList" type="cn.roadm.bo.SmsLogisticsPreventFraudBO">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="sign_list" property="sign_list"/>
        <result column="strategy" property="strategy"/>
        <result column="keyword_repository_id" property="keyword_repository_id"/>
        <result column="status" property="status"/>
        <result column="create_time" property="create_time"/>
        <result column="update_time" property="update_time"/>
        <result column="times" property="times"/>
        <result column="minute" property="minute"/>
        <result column="sp_list" property="sp_list"/>
    </resultMap>

    <select id="listPageSmsLogisticsPreventFraud" resultMap="smsLogisticsPreventFraudList">
        SELECT a.id,
               a.name,
               a.sign_list,
               a.strategy,
               a.keyword_repository_id,
               a.status,
               a.create_time,
               a.update_time,
               a.times,
               a.minute,
               b.sp_list
        FROM sms_logistics_prevent_fraud a
                 LEFT JOIN (SELECT strategy_id, GROUP_CONCAT(sp_id) as sp_list
                            FROM sms_logistics_prevent_fraud_sp
                            GROUP BY strategy_id) b ON a.id = b.strategy_id
        <where>
            a.status = 0
            <if test="name != null and name != ''">
                and a.name like concat('%', #{name}, '%')
            </if>
            <if test="sign_list != null and sign_list != ''">
                and a.sign_list like concat('%', #{sign_list}, '%')
            </if>
            <if test="sp_id != null and sp_id != ''">
                and b.sp_list like concat('%', #{sp_id}, '%')
            </if>
        </where>
        order by a.create_time desc
    </select>

    <select id="getSmsLogisticsPreventFraudById" resultMap="smsLogisticsPreventFraudList">
        select id,
               name,
               sign_list,
               strategy,
               keyword_repository_id,
               status,
               create_time,
               update_time,
               times,
               minute
        from sms_logistics_prevent_fraud
        where id = #{id}
    </select>

    <insert id="insertSmsLogisticsPreventFraud" useGeneratedKeys="true" keyProperty="id">
        insert into sms_logistics_prevent_fraud
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="sign_list != null and sign_list != ''">sign_list,</if>
            <if test="strategy != null and strategy != ''">strategy,</if>
            <if test="keyword_repository_id != null">keyword_repository_id,</if>
            <if test="times != null">times,</if>
            <if test="minute != null">minute,</if>
            status,
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="sign_list != null and sign_list != ''">#{sign_list},</if>
            <if test="strategy != null and strategy != ''">#{strategy},</if>
            <if test="keyword_repository_id != null">#{keyword_repository_id},</if>
            <if test="times != null">#{times},</if>
            <if test="minute != null">#{minute},</if>
            0,
            now()
        </trim>
    </insert>

    <update id="updateSmsLogisticsPreventFraud">
        update sms_logistics_prevent_fraud
        <set>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="sign_list != null and sign_list != ''">sign_list = #{sign_list},</if>
            <if test="strategy != null and strategy != ''">strategy = #{strategy},</if>
            <if test="keyword_repository_id != null">keyword_repository_id = #{keyword_repository_id},</if>
            <if test="times != null">times = #{times},</if>
            <if test="minute != null">minute = #{minute},</if>
            update_time = now()
        </set>
        where id = #{id}
    </update>

    <update id="deleteSmsLogisticsPreventFraud">
        update sms_logistics_prevent_fraud
        set status = 1
        where id = #{id}
    </update>

    <select id="querySmsLogisticsPreventFraudSp" resultType="cn.roadm.bo.SmsLogisticsPreventFraudSpBO">
        select sp_id, strategy_id from sms_logistics_prevent_fraud_sp where strategy_id = #{strategy_id}
    </select>

    <select id="queryAllSp" resultType="cn.roadm.bo.SmsLogisticsPreventFraudSpBO">
        select id as sp_id, sp_name from sms_sp_info where status = 1
    </select>

    <delete id="deleteThisStrategySp">
        delete from sms_logistics_prevent_fraud_sp where strategy_id = #{id}
    </delete>

    <insert id="addSmsLogisticsPreventFraudSp">
        replace into sms_logistics_prevent_fraud_sp (sp_id, strategy_id) values
        <foreach collection="spList" item="sp" separator=",">
            (#{sp}, #{tid})
        </foreach>
    </insert>

    <select id="listSmsLogisticsPreventFraud" resultMap="smsLogisticsPreventFraudList">
        select id, name, sign_list, strategy, keyword_repository_id, times, minute
        from sms_logistics_prevent_fraud
        where status = 0
        order by create_time desc
    </select>

    <select id="queryWordTypeList" resultType="cn.roadm.bo.PageSelectOptionsVO">
        select id as value, name as label
        from sms_data_dictionary
        where status = '1'
        order by id desc
    </select>

</mapper> 