<?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.admin.mapper.ReplyMapper">

    <resultMap id="replyRecordMap" type="cn.roadm.admin.dto.SmsReplyRecordDO">
        <id column="id" property="id"/>
        <result column="sp_id" property="spId"/>
        <result column="callmdn" property="callMdn"/>
        <result column="mobile" property="mobile"/>
        <result column="content" property="content"/>
        <result column="createtime" property="createTime"/>
        <result column="reply_time" property="replyTime"/>
        <result column="channelid" property="channelId"/>
        <result column="status" property="status"/>
        <result column="subport" property="subPort"/>
        <result column="task_id" property="taskId"/>
        <result column="reason" property="reason"/>
        <result column="region" property="region"/>
        <result column="res_content" property="resContent"/>
        <result column="channel_name" property="channelName"/>
    </resultMap>

    <select id="listPageQueryReplyRecord" resultMap="replyRecordMap">
        select rd.id,
               rd.sp_id,
               rd.callmdn,
               rd.mobile,
               rd.content,
               str_to_date(rd.reply_time, '%Y%m%d%H%i%S') as reply_time,
               rd.createtime,
               rd.channelid,
               channel_name,
               rd.status,
               rd.subport,
               rd.TASK_ID,
               rd.reason,
               rd.region,
               rd.res_content
        from sms_reply_record rd
                 left join sms_send_channel_info sc on sc.id = rd.channelid
                 left join sms_sp_info sp on rd.sp_id = sp.id
        <where>
            rd.IS_DEL = 0
            <if test="queryVO.callMdn != null and queryVO.callMdn != ''">
                and rd.callmdn = #{queryVO.callMdn}
            </if>
            <if test="queryVO.taskId != null and queryVO.taskId != ''">
                and rd.TASK_ID = #{queryVO.taskId}
            </if>
            <if test="queryVO.spId != null and queryVO.spId >= 0">
                and rd.sp_id = #{queryVO.spId}
            </if>
            <if test="queryVO.status != null">
                and rd.status = #{queryVO.status}
            </if>
            <if test="queryVO.channelId != null and queryVO.channelId != ''">
                and rd.channelid = #{queryVO.channelId}
            </if>
            <if test="queryVO.mdn != null and queryVO.mdn != ''">
                and rd.mobile = #{queryVO.mdn}
            </if>

            <if test="queryVO.replyContent != null and queryVO.replyContent != ''">
                and rd.content like CONCAT('%', #{queryVO.replyContent}, '%')
            </if>

            <if test="queryVO.resContent != null and queryVO.resContent != ''">
                and rd.res_content like CONCAT('%', #{queryVO.resContent}, '%')
            </if>

            <if test="queryVO.startTime != null and queryVO.startTime != ''">
                and rd.createtime &gt;= str_to_date(#{queryVO.startTime}, '%Y-%m-%d')
            </if>

            <if test="queryVO.endTime != null and queryVO.endTime != ''">
                and rd.createtime &lt;= DATE_ADD(str_to_date(#{queryVO.endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
            <if test="queryVO.region != null and queryVO.region != ''">
                and rd.region like CONCAT(#{queryVO.region}, '%')
            </if>
        </where>
        order by rd.createtime desc
    </select>

    <insert id="reInsertReplyDetailInfo" parameterType="int">
        insert into sms_reply_record(CALLMDN,MOBILE,CONTENT,REPLY_TIME,CREATETIME,SP_ID,STATUS,SUBPORT,IS_DEL,CHANNELID,task_id,reason,region,RES_CONTENT,SN)
        select CALLMDN,MOBILE,CONTENT,REPLY_TIME,CREATETIME,SP_ID,0,SUBPORT,IS_DEL,CHANNELID,task_id,reason,region,RES_CONTENT,SN from sms_reply_record where id=#{id}
    </insert>
    <delete id="deleteReplyDetailInfoById" parameterType="int">
        delete from sms_reply_record where id=#{id}
    </delete>

    <update id="updateReplyDetailInfoBySearch" parameterType="cn.roadm.admin.controller.vo.ReplyEditSpReqVO">
        update
        sms_reply_record set
        status=#{status}
        where 1=1
        <if test="callMdn!=null and callMdn!=''">
            and callmdn=#{callMdn}
        </if>
        <if test="mobile!=null and mobile !=''">
            and mobile=#{mobile}
        </if>
        <if test="replyContent!=null and replyContent !=''">
            and content=#{replyContent}
        </if>
        <if test="channelId!=null and channelId >=0">
            and channelid=#{channelId}
        </if>
        <if test="spId!=null and spId >=0">
            and sp_id=#{spId}
        </if>
        <if test="startTime!=null and startTime!=''">
            and createtime&gt;=str_to_date(#{startTime},'%Y-%m-%d')
        </if>

        <if test="endTime!=null and endTime!=''">
            and createtime&lt;=DATE_ADD(str_to_date(#{endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>
        <if test="region != null and region != ''">
            and region like CONCAT(#{region}, '%')
        </if>
    </update>
    <delete id="deleteReplyDetailInfoBySearch" parameterType="cn.roadm.admin.controller.vo.ReplySearchEditSpReqVO">
        delete from sms_reply_record
        where 1=1 and status= 2
        <if test="mobile!=null and mobile !=''">
            and mobile=#{mobile}
        </if>
        <if test="replyContent!=null and replyContent !=''">
            and content=#{replyContent}
        </if>
        <if test="callMdn!=null and callMdn!=''">
            and callmdn=#{callMdn}
        </if>
        <if test="channelId!=null and channelId >=0">
            and channelid=#{channelId}
        </if>
        <if test="spId!=null and spId >=0">
            and sp_id=#{spId}
        </if>
        <if test="startTime!=null and startTime!=''">
            and createtime&gt;=str_to_date(#{startTime},'%Y-%m-%d')
        </if>

        <if test="endTime!=null and endTime!=''">
            and createtime&lt;=DATE_ADD(str_to_date(#{endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>
        <if test="region != null and region != ''">
            and region like CONCAT(#{region}, '%')
        </if>
    </delete>
    <insert id="reInsertReplyDetailInfoBySearch"   useGeneratedKeys="true" parameterType="cn.roadm.admin.controller.vo.ReplySearchEditSpReqVO">
        insert into sms_reply_record(CALLMDN,MOBILE,CONTENT,REPLY_TIME,CREATETIME,SP_ID,STATUS,SUBPORT,IS_DEL,CHANNELID,task_id,reason,region,res_content,sn)
        select CALLMDN,MOBILE,CONTENT,REPLY_TIME,CREATETIME,ifnull(#{newSpid},sp_id) SP_ID,0,SUBPORT,IS_DEL,CHANNELID,task_id,reason,region,res_content,sn from sms_reply_record
        where 1=1
        <if test="mobile!=null and mobile !=''">
            and mobile=#{mobile}
        </if>
        <if test="replyContent!=null and replyContent !=''">
            and content=#{replyContent}
        </if>
        <if test="callMdn!=null and callMdn!=''">
            and callmdn=#{callMdn}
        </if>
        <if test="spId!=null and spId >=0">
            and sp_id=#{spId}
        </if>
        <if test="channelId!=null and channelId >=0">
            and channelid=#{channelId}
        </if>
        <if test="startTime!=null and startTime!=''">
            and createtime&gt;=str_to_date(#{startTime},'%Y-%m-%d')
        </if>

        <if test="endTime!=null and endTime!=''">
            and createtime&lt;=DATE_ADD(str_to_date(#{endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>
        <if test="region != null and region != ''">
            and region like CONCAT(#{region}, '%')
        </if>
    </insert>

    <update id="updateReplySpId" parameterType="cn.roadm.admin.controller.vo.ReplyEditSpReqVO">
        update
            sms_reply_record set
            sp_id=#{spId}
        where
            id=#{id}
    </update>
    <update id="deleteReplayOne" parameterType="int">
        update sms_reply_record set IS_DEL = 1 where ID = #{id}
    </update>

    <select id="exportReply" parameterType="cn.roadm.admin.controller.vo.ReplyListReqVO" resultType="cn.roadm.admin.dto.SmsReplyRecordDO">
        select rd.id,
        rd.sp_id spId,
        sp.sp_name spName,
        rd.callmdn callMdn,
        rd.mobile,
        rd.content,
        rd.channelid channelId,
        sc.channel_name channelName,
        str_to_date(rd.reply_time,'%Y%m%d%H%i%S') replyTime,
        rd.TASK_ID taskId,
        rd.SUBPORT subPort,
        rd.region region,
        rd.createtime createTime
        <!-- rd.status -->
        from sms_reply_record rd
        left join sms_sp_info sp on rd.sp_id = sp.id
        left join sms_send_channel_info sc on sc.id = rd.channelid
        <where>
            rd.IS_DEL = 0
            <if test="spId!=null and spId>=0">
                and rd.sp_id=#{spId}
            </if>
            <if test="callMdn!=null and callMdn!=''">
                and rd.callmdn=#{callMdn}
            </if>
            <if test="spSaler!=null and spSaler!=''">
                and sp.sp_saler=#{spSaler}
            </if>
            <if test="mdn!=null and mdn!=''">
                and rd.mobile=#{mdn}
            </if>
            <if test="channelId !=null and channelId !='' ">
                and rd.channelid=#{channelId}
            </if>
            <if test="replyContent!=null and replyContent!=''">
                and rd.content like CONCAT('%',#{replyContent},'%')
            </if>
            <if test="taskId!=null and taskId>0">
                and rd.task_id=#{taskId}
            </if>
            <if test="status != null">
                and rd.status = #{status}
            </if>
            <if test="startTime!=null and startTime!=''">
                and rd.createtime&gt;=str_to_date(#{startTime},'%Y-%m-%d')
            </if>
            <if test="endTime!=null and endTime!=''">
                and rd.createtime&lt;=DATE_ADD(str_to_date(#{endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
            <if test="region != null and region != ''">
                and rd.region like CONCAT(#{region}, '%')
            </if>
            <if test="endId != null">
                and rd.id &lt;  #{endId}
            </if>
        </where>
        order by rd.id desc
        <if test="limit != null and limit > 0">
            limit #{limit}
        </if>
    </select>

    <select id="listPageQueryReplyTransferConfigRecord" resultType="cn.roadm.admin.dto.SmsReplyTransferDO">
        SELECT id, sid, channelId, tid, dayLimit, contain, createTime FROM `sms_reply_transfer`
        <where>
            <if test="queryVO.sid != null and queryVO.sid != ''">
                sid = #{queryVO.sid}
            </if>
            <if test="queryVO.tid != null and queryVO.tid != ''">
                and tid = #{queryVO.tid}
            </if>
            <if test="queryVO.contain != null and queryVO.contain != ''">
                and contain like CONCAT('%', #{queryVO.contain}, '%')
            </if>
            <if test="queryVO.startTime !=null and queryVO.startTime !='' ">
                and createTime &gt;= str_to_date(#{queryVO.startTime},'%Y-%m-%d')
            </if>
            <if test="queryVO.endTime !=null and queryVO.endTime !='' ">
                and createTime &lt;= DATE_ADD(str_to_date(#{queryVO.endTime},'%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
        </where>
    </select>

    <select id="getReplyTransferConfigDetail" resultType="cn.roadm.admin.dto.SmsReplyTransferDO">
        select id, sid, channelId, tid, dayLimit, contain, createTime from sms_reply_transfer where id = #{id}
    </select>

    <insert id="addReplyTransferConfig">
        insert into sms_reply_transfer (sid, tid, channelId, dayLimit, contain, createTime) VALUES (
        #{sid}, #{tid}, #{channelId}, #{dayLimit}, #{contain}, NOW())
    </insert>

    <update id="updateReplyTransferConfig">
        update sms_reply_transfer set dayLimit = #{dayLimit}, contain = #{contain} where id = #{id}
    </update>

    <delete id="deleteReplyTransferConfig">
        delete from sms_reply_transfer where id = #{id}
    </delete>

</mapper>
