<?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.CardSmsMapper">

    <resultMap id="aimAbilityFilterDTO" type="cn.roadm.bo.AimAbilityFilterDTO">
        <id column="id" property="id"/>
        <result column="aim_account" property="aimAccount"/>
        <result column="aim_template" property="aimTemplate"/>
        <result column="filter_file" property="filterFile"/>
        <result column="all_number" property="allNumber"/>
        <result column="support_num" property="supportNum"/>
        <result column="unsupport_num" property="unsupportNum"/>
        <result column="support_file" property="supportFile"/>
        <result column="unsupport_file" property="unsupportFile"/>
        <result column="create_time" property="createTime"/>
        <result column="complete_time" property="completeTime"/>
    </resultMap>
    <!-- 查询远程模板列表 -->
    <select id="listPageQueryCardSmsTemplate" resultType="cn.roadm.bo.AimTemplateRemote">
        SELECT tmp.*, tmp.send_package_size as sendPackageSize, GROUP_CONCAT(sp.id) as spIds
        FROM kp_template_remote tmp
        left join kp_aim_sp aim on aim.aim_id = tmp.id
        left join sms_sp_info sp on sp.id = aim.sp_id
        <where>
            <if test="remoteId != null and remoteId != ''">
                tmp.remote_id = #{remoteId}
            </if>
            <if test="localId != null">
                and tmp.local_id = #{localId}
            </if>
            <if test="tpl_name != null and tpl_name != ''">
                AND tmp.remote_name LIKE CONCAT('%', #{tpl_name}, '%')
            </if>
            <if test="startTime != null and startTime != ''">
                and tmp.create_time &gt;= str_to_date(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and tmp.create_time &lt;= DATE_ADD(str_to_date(#{endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
        </where>
        group by tmp.id
        order by tmp.id desc
    </select>

    <update id="updateCardSmsTemplate">
        update kp_template_local
        set tpl_name = #{tpl_name},
            tpl_id   = #{tpl_id},
            style    = #{style},
            content  = #{content}
        where id = #{id}
    </update>

    <update id="disableSmsTemplate">
        update kp_template_remote set state = #{status} where remote_id = #{id}
    </update>

    <select id="queryAllResources" resultType="cn.roadm.bo.AimResource">
        select id, create_time, url
        from kp_aim_resource
    </select>

    <select id="queryOneTemplate" resultType="cn.roadm.bo.AimTemplate">
       select id, tpl_name, tpl_id, baseInfo, style, content, create_time, state from kp_template_local where id = #{localId,jdbcType=BIGINT}
    </select>

    <select id="queryAimResource" resultType="cn.roadm.bo.AimResource">
        select id, create_time as createTime, url, local, factory
        from kp_aim_resource
        <where>
            <if test="factory != null and factory != ''">
                factory = #{factory}
            </if>
        </where>
    </select>

    <select id="listPageQueryAimResource" resultType="cn.roadm.bo.AimResource">
        select id, create_time as createTime, url, local, factory
        from kp_aim_resource
        <where>
            <if test="factory != null and factory != ''">
                factory = #{factory}
            </if>
        </where>
    </select>

    <insert id="addAimResource">
        insert into kp_aim_resource(create_time, url, factory)
        values (#{createTime}, #{url}, #{factory})
    </insert>

    <insert id="addAimTemplateInfo">
        insert into kp_template_local(id, tpl_name, tpl_id, baseInfo, style, content, create_time)
        VALUES (#{id}, #{tpl_name}, #{tpl_id}, #{baseInfo}, #{style}, #{content}, #{create_time})
    </insert>

    <update id="updateAimTemplateInfo">
        update kp_template_local
        set tpl_name = #{tpl_name},
            tpl_id   = #{tpl_id},
            baseInfo = #{baseInfo},
            style    = #{style},
            content  = #{content}
        where id = #{id}
    </update>

    <select id="queryJoinSp" resultType="java.util.Map">
        select tp.id as tplId, sp.id as spId  from kp_template_remote tp, kp_aim_sp aim, sms_sp_info sp
        where tp.id = aim.aim_id and aim.sp_id = sp.id
        and tp.id = #{id}
    </select>

    <delete id="removeTemplateSp">
        delete from kp_aim_sp where aim_id = #{id}
    </delete>

    <delete id="addTemplateSp">
        insert into kp_aim_sp(aim_id, sp_id) values

        <foreach collection="spId.split(',')" item="sp" separator=",">
            (#{id}, #{sp})
        </foreach>
    </delete>

    <select id="getResourceUrlByLocalPath" resultType="java.lang.String">
        select url from kp_aim_resource where local = #{path} limit 1
    </select>

    <update id="deleteTemplate">
        delete from kp_template_remote where id = #{id}
    </update>

    <insert id="insertCustomeAimTemplate">
        insert into kp_template_remote(remote_id, remote_name, variable, factory, create_time, create_type, state, audit, local_id, send_package_size, content)
        VALUES (#{remoteId}, #{tplName}, #{variable}, #{factory}, now(), #{createType}, #{state}, #{audit}, -1, #{sendPackageSize}, #{content})
    </insert>

    <select id="listPageQueryLocalTemplate" resultType="cn.roadm.bo.AimTemplateLocal">
        select id, tpl_name, tpl_id, baseInfo, style, content, date_format(create_time, '%Y-%m-%d %H:%i:%s') as create_time, state from kp_template_local
        <where>
            <if test="localId != null">
                id = #{localId}
            </if>
            <if test="tpl_name != null and tpl_name != ''">
                and tpl_name like CONCAT('%', #{tpl_name}, '%')
            </if>
            <if test="startTime != null and startTime != ''">
                and create_time &gt;= str_to_date(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and create_time &lt;= DATE_ADD(str_to_date(#{endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="queryOneTemplateById" resultType="cn.roadm.bo.AimTemplate">
        select id as localId, tpl_name, tpl_id, baseInfo, style, content, create_time, state from kp_template_local where id = #{localId, jdbcType=VARCHAR}
    </select>

    <select id="getTplTypeIdByLocalId" resultType="java.lang.String">
        select tpl_id from kp_template_local where id = #{localId}
    </select>

    <select id="queryAimAccountInfo" resultType="cn.roadm.bo.AimAccountDO">
        select id, aim_account as aimAccount, aim_password as aimPassword, ability_thread as abilityThread,
             sp_id as spId, DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s') as createTime, `status` from kp_aim_account
        order by create_time desc
    </select>

    <update id="disabledAimAccount">
        update kp_aim_account set `status` = #{status} where id = #{id}
    </update>

    <select id="queryAimAccountInfoById" resultType="cn.roadm.bo.AimAccountDO">
        select id, aim_account as aimAccount, aim_password as aimPassword, ability_thread as abilityThread,
             sp_id as spId, DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s') as createTime, `status` ,
        template_id as templateId,query_url as queryUrl,create_short_url as createShortUrl,char_value as charValue
        from kp_aim_account where id = #{id}
    </select>

    <insert id="addAimAccountInfo">
        insert into kp_aim_account(aim_account, aim_password, ability_thread, sp_id, create_time, `status`,template_id,
        query_url,create_short_url,char_value)
        values (#{aimAccount}, #{aimPassword}, #{abilityThread}, #{spId}, now(), 0,#{templateId},
        #{queryUrl},#{createShortUrl},#{charValue})
    </insert>

    <update id="updateAimAccountInfo">
        update kp_aim_account set aim_account = #{aimAccount}, aim_password = #{aimPassword},
        ability_thread = #{abilityThread}, sp_id = #{spId},template_id = #{templateId},
        query_url = #{queryUrl},create_short_url = #{createShortUrl},char_value = #{charValue}
        where id = #{id}
    </update>

    <update id="updatePackageSize">
        update kp_template_remote set send_package_size = #{sendPackageSize} where id = #{id}
    </update>

    <delete id="deleteAimAccount">
        delete from kp_aim_account where id = #{id}
    </delete>

    <select id="queryRemoteTemplateById" resultType="cn.roadm.bo.AimTemplateRemote">
        select id, remote_id, remote_name, variable, factory, send_package_size as sendPackageSize,
        content from kp_template_remote where id = #{id}
    </select>

    <update id="updateAimTemplate">
        update kp_template_remote set remote_name = #{tplName}, variable = #{variable}, factory = #{factory}, send_package_size = #{sendPackageSize},
        content = #{content}, tplMask = #{tplMask},remote_id = #{remoteId}
        where id = #{id}
    </update>

    <select id="listPageQueryAimAbilityFilterTask" resultMap="aimAbilityFilterDTO">
        select id, aim_account, aim_template, filter_file, all_number, support_num,
               unsupport_num, state, support_file, unsupport_file, create_time,
               complete_time from aim_ability_filter order by create_time desc
    </select>

    <select id="queryAimTemplate" resultType="cn.roadm.bo.AimTemplateRemote">
        select remote_id, remote_name from kp_template_remote where state = 0 and audit = 1 order by create_time desc
    </select>

    <insert id="addAimAbilityTask" useGeneratedKeys="true" keyProperty="taskId">
        insert into aim_ability_filter(aim_account, aim_template, filter_file, `state`, create_time)
        values (#{aimAccount}, #{aimTemplate}, #{filterFile}, 0, now())
    </insert>

    <update id="updateAimAbilityTask">
        update aim_ability_filter set all_number = #{totalNum}, support_num = #{supportNum},
        unsupport_num = #{unSupportNum}, state = #{state}, support_file = #{supportFile},
        unsupport_file = #{unSupportFile}, complete_time = now()
        where id = #{taskId}
    </update>

    <update id="updateAimAbilityTaskException">
        update aim_ability_filter set state = #{state}, complete_time = now()
        where id = #{taskId}
    </update>
</mapper>
