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

	<resultMap id="fileListMap" type="cn.roadm.bo.ComplaintPhoneInfo">
		<id column="id" property="id" jdbcType="INTEGER"/>
		<result column="file_id" property="fileId" jdbcType="INTEGER"/>
		<result column="phone" property="phone" jdbcType="VARCHAR"/>
		<result column="true_phone" property="truePhone" jdbcType="VARCHAR"/>
		<result column="content" property="content" jdbcType="VARCHAR"/>
		<result column="channel_id" property="channelId" jdbcType="INTEGER"/>
		<result column="short_link" property="shortLink" jdbcType="VARCHAR"/>
		<result column="send_time" property="sendTime" jdbcType="VARCHAR"/>
		<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
		<result column="trans_time" property="transTime" jdbcType="TIMESTAMP"/>
		<result column="trans_type" property="transType" jdbcType="INTEGER"/>
		<result column="status" property="status" jdbcType="INTEGER"/>
		<result column="task_id" property="taskId" jdbcType="BIGINT"/>
		<result column="callmdn" property="callmdn" jdbcType="VARCHAR"/>
		<result column="sp_id" property="spId" jdbcType="INTEGER"/>
		<result column="sub_send_time" property="subSendTime" jdbcType="VARCHAR"/>
		<result column="sign" property="sign" jdbcType="VARCHAR"/>
		<result column="otype" property="otype" jdbcType="INTEGER"/>
		<result column="province" property="province" jdbcType="VARCHAR"/>
		<result column="data_type" property="dataType" jdbcType="INTEGER"/>
	</resultMap>

<!--	<resultMap type="BillInfo" id="BillInfoResultMap">-->
<!--		<id column="id" property="id" />-->
<!--		<result column="month" property="month" />-->
<!--		<result column="sp_id" property="sp_id" />-->
<!--		<result column="supplier" property="supplier" />-->
<!--		<result column="supplier_name" property="supplier_name" />-->
<!--		<result column="carrieroperator" property="carrieroperator" />-->
<!--		<result column="arrive_num" property="arrive_num" />-->
<!--		<result column="unitprice" property="unitprice" />-->
<!--	</resultMap>-->

	<select id="listPageGetFileList" resultType="cn.roadm.bo.ComplaintFileInfo">
		SELECT a.id,a.file_name as fileName,a.phone_count as phoneCount,DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i:%s') as createTime,a.status,
		b.transSuccCount,b.transFailCount,c.transDataCount FROM
		(select id,file_name,phone_count,create_time,status from complaint_file_info
		<where>
			<if test="fileName != null and fileName != ''">
				and file_name like CONCAT('%',#{fileName},'%')
			</if>
			<if test="status != null and status != -1">
				and `status` = #{status}
			</if>
		</where>
		) a
		LEFT JOIN (SELECT file_id,
			SUM(CASE WHEN STATUS = 2 THEN 1 ELSE 0 END) AS transFailCount,
			SUM(CASE WHEN STATUS = 1 THEN 1 ELSE 0 END) AS transSuccCount
		FROM
			complaint_phone_info where data_type = 1
		GROUP BY
			file_id) b ON a.id = b.file_id

		LEFT JOIN (SELECT file_id,count(file_id) as transDataCount FROM complaint_phone_info where data_type = 2 GROUP BY file_id) c
		ON a.id = c.file_id

		order by a.create_time desc
	</select>


	<insert id="insertComplaintFileInfo" parameterType="cn.roadm.bo.ComplaintFileInfo" useGeneratedKeys="true" keyProperty="id">
		insert into complaint_file_info (file_name,file_path,phone_count,create_time,status)
		value(#{fileName},#{filePath},null,now(),0)
	</insert>

	<update id="updateFileInfoStatusById">
		update complaint_file_info set status=#{status} where id = #{id}
	</update>

	<select id="getFileInfoById" resultType="cn.roadm.bo.ComplaintFileInfo">
		select id,file_name as fileName,file_path as filePath,phone_count as phoneCount,create_time as createTime,status
		from complaint_file_info where id = #{id}
	</select>

	<select id="getFileInfoListByIds" resultMap="fileListMap" parameterType="java.util.List">
		SELECT id, file_id, phone, true_phone as truePhone, content, channel_id, short_link, send_time, create_time, trans_time,
		trans_type, status, task_id, callmdn, sp_id, sub_send_time, sign, otype, province, data_type,
		customerMdn,customerMessageid
		FROM complaint_phone_info
		<where>
			<if test="ids != null and ids.size() > 0">
				id IN
				<foreach collection="ids" item="id" open="(" separator="," close=")">
					#{id, jdbcType=INTEGER}
				</foreach>
			</if>
		</where>
	</select>

	<insert id="batchInsertComplaintPhoneInfo">
		insert into complaint_phone_info (file_id,phone,content,channel_id,send_time,create_time,
		trans_time,trans_type,status,province,data_type,sign,short_link) values
		<foreach collection="list" separator="," item="item">
			(#{item.fileId},#{item.phone},#{item.content},#{item.channelId},
			#{item.sendTime},now(),null,#{item.transType},#{item.status},#{item.province},#{item.dataType},
			#{item.sign},#{item.shortLink})
		</foreach>
	</insert>

	<insert id="insertComplaintPhoneInfo">
		insert into complaint_phone_info (file_id,phone,content,channel_id,send_time,create_time,trans_time,trans_type,
		                                  status,true_phone,task_id,callmdn,sp_id,sub_send_time,sign,otype,province,
		                                  data_type,customerMdn,customerMessageid) values
		(#{fileId},#{phone},#{content},#{channelId},#{sendTime},now(),now(),#{transType},
		 #{status},#{truePhone},#{taskId},#{callmdn},#{spId},#{subSendTime},#{sign},#{otype},#{province},
		 #{dataType},#{customerMdn},#{customerMessageid})
	</insert>

	<update id="updateCountById">
		update complaint_file_info set phone_count = #{size} where id = #{id}
	</update>

	<select id="selectComplaintPhoneInfoByFileId" resultType="cn.roadm.bo.ComplaintPhoneInfo">
		select id,file_id as fileId,phone,content,channel_id as channelId, send_time as sendTime ,
		create_time as createTime,trans_time as transTime,trans_type as transType,status,data_type as dataType,
		sign,short_link as shortLink
		from complaint_phone_info
		where file_id = #{fileId} and data_type = 1
		<if test="transId != null and transId == 2">
			and status != 1
		</if>
	</select>


	<select id="selectSubmitInfoCount" resultType="int">
		select count(1) from sms_submit where mdn like #{phone} and channelid =#{channelId}
		 and send_time >= str_to_date(#{sendTime},'%Y-%m-%d %H:%i:%s')
		<if test="content != null and content != ''">
			and res_content = #{content}
		</if>
	</select>

	<select id="selectSubmitInfo" resultType="cn.roadm.bo.ComplaintPhoneInfo">
		SELECT a.*,b.RESULT FROM (select taskId,mdn as truePhone,callmdn,res_content as content,sp_id as spId,send_time as
        subSendTime,
        channelId,sign,otype,province,sn as sn,num from sms_submit FORCE INDEX (CREATE_TIME,SIGN,SMS_SUBMIT_MDN,channelid)
        where mdn like #{phone} and channelid =#{channelId} and sign = #{sign} and RES_RESULT = 0
        and create_time >= str_to_date(#{sendTime},'%Y-%m-%d %H:%i:%s')
		<if test="endTime != null and endTime != ''">
        	and create_time <![CDATA[<=]]> str_to_date(#{endTime},'%Y-%m-%d %H:%i:%s')
        </if>
		<if test="content != null and content != ''">    and res_content = #{content}
		</if>
		<if test="shortLink != null and shortLink != ''">
			and res_content like CONCAT('%', #{shortLink}, '%')
		</if>
		<if test="province != null and province != ''">
			and province = #{province}
		</if>
        ) a LEFT JOIN (SELECT * FROM  sms_arrive WHERE RESULT = 0 AND channelid =#{channelId} and sign = #{sign}
		and create_time >= str_to_date(#{sendTime},'%Y-%m-%d %H:%i:%s')
		<if test="endTime != null and endTime != ''">
		   and create_time <![CDATA[<=]]> DATE_ADD(str_to_date(#{endTime},'%Y-%m-%d %H:%i:%s'), INTERVAL 3 DAY)
        </if>
		) b ON a.TASKID = b.taskId AND a.truePhone = b.mdn AND a.num = b.num WHERE b.RESULT = 0
	</select>

	<select id="selectSubmitInfoByUpStreamData" resultType="cn.roadm.bo.ComplaintPhoneInfo">
		SELECT
			taskId,mdn as truePhone,callmdn,res_content as content,sp_id as spId,send_time as subSendTime,
			channelId,sign,otype,province,sn as sn
		FROM sms_submit
		WHERE RES_RESULT = 0
		<if test="mdn != null and mdn != ''">
			and mdn = #{mdn}
		</if>
		<if test="callMdn != null and callMdn != ''">
			and callmdn = #{callMdn}
		</if>
		<if test="messageId != null and messageId != ''">
			and MESSAGEID = #{messageId}
		</if>
	</select>

	<select id="selectArriveInfo" resultType="java.lang.Integer">
		SELECT COUNT(1) FROM sms_arrive
		WHERE TASKID = #{taskId} AND mdn = #{phone}  AND RESULT = 0
		AND CREATE_TIME >= STR_TO_DATE(#{sendTime}, '%Y-%m-%d %H:%i:%s')
		<if test="endTime != null and endTime != ''">
			AND CREATE_TIME <![CDATA[<=]]> STR_TO_DATE(#{endTime}, '%Y-%m-%d %H:%i:%s')
		</if>
		limit 1
	</select>

	<select id="selectUpStreamArriveInfo" resultType="java.lang.Integer">
		SELECT COUNT(1) FROM sms_arrive
		WHERE TASKID = #{taskId} AND mdn like #{phone}  AND RESULT = 0
	</select>

	<update id="updateComplaintPhoneInfoStatusById">
		update complaint_phone_info set trans_type = #{transType} ,status = #{status},trans_time = now()
		<if test="truePhone != null and truePhone != ''">
			,true_phone = #{truePhone}
		</if>
		<if test="taskId != null">
			,task_id = #{taskId}
		</if>
		<if test="callmdn != null and callmdn != ''">
			,callmdn = #{callmdn}
		</if>
		<if test="spId != null">
			,sp_id = #{spId}
		</if>
		<if test="subSendTime != null and subSendTime != ''">
			,sub_send_time = #{subSendTime}
		</if>
		<if test="sign != null and sign != ''">
			,sign = #{sign}
		</if>
		<if test="otype != null">
			,otype = #{otype}
		</if>
		<if test="province != null and province != ''">
			,province = #{province}
		</if>
		where id = #{id}
	</update>

	<delete id="deleteComplaintPhoneInfoById">
		delete from complaint_phone_info where id = #{id}
	</delete>

	<select id="pageListComplaintPhoneInfo" resultType="cn.roadm.bo.ComplaintPhoneInfo">
		select cpi.id,
		cpi.file_id as fileId,
		cpi.phone,content,
		cpi.channel_id as channelId,
		cpi.trans_type as transType,
		DATE_FORMAT(cpi.trans_time,'%Y-%m-%d %H:%i:%s') as transTime,
		cpi.true_phone as truePhone,
		cpi.task_id as taskId,
		cpi.sp_id as spId,
		cpi.callmdn,
		cpi.province,
		cpi.sub_send_time as subSendTime,
		cpi.customerMdn,
		cpi.customerMessageid,
		ssi.sp_name as spName
		from complaint_phone_info cpi
		LEFT JOIN sms_sp_info ssi ON cpi.sp_id = ssi.id
		where cpi.file_id = #{fileId}
		<if test="status != null">
			and cpi.status = #{status}
		</if>
		<if test="status != null and status == 1">
			and cpi.data_type = 2
		</if>
		<if test="truePhone != null and truePhone != ''">
			and cpi.true_phone = #{truePhone}
		</if>
		<if test="taskId != null">
			and cpi.task_id = #{taskId}
		</if>
		<if test="channelId != null">
			and cpi.channel_id = #{channelId}
		</if>
		<if test="spId != null">
			and cpi.sp_id = #{spId}
		</if>
		<if test="transType != null and transType != -1">
			and cpi.trans_type = #{transType}
		</if>
		<if test="content != null and content != ''">
			and cpi.content like CONCAT('%', #{content}, '%')
		</if>


	</select>

	<select id="listPageListComplaintPhoneInfo" resultType="cn.roadm.bo.ComplaintPhoneInfo">
		select cpi.id,
		       cpi.file_id as fileId,
		       cpi.phone,content,
		       cpi.channel_id as channelId,
		       cpi.trans_type as transType,
			   DATE_FORMAT(cpi.trans_time,'%Y-%m-%d %H:%i:%s') as transTime,
			   cpi.true_phone as truePhone,
			   cpi.task_id as taskId,
			   cpi.sp_id as spId,
			   cpi.callmdn,
			   cpi.province,
			   cpi.sub_send_time as subSendTime,
			   cpi.customerMdn,
			   cpi.customerMessageid,
			   ssi.sp_name as spName
		from complaint_phone_info cpi
		LEFT JOIN sms_sp_info ssi ON cpi.sp_id = ssi.id
		where cpi.file_id = #{fileId}
		<if test="status != null">
			and cpi.status = #{status}
		</if>
		<if test="status != null and status == 1">
			and cpi.data_type = 2
		</if>
		<if test="truePhone != null and truePhone != ''">
			and cpi.true_phone = #{truePhone}
		</if>
		<if test="taskId != null">
			and cpi.task_id = #{taskId}
		</if>
		<if test="channelId != null">
			and cpi.channel_id = #{channelId}
		</if>
		<if test="spId != null">
			and cpi.sp_id = #{spId}
		</if>
		<if test="transType != null and transType != -1">
			and cpi.trans_type = #{transType}
		</if>
		<if test="content != null and content != ''">
			and cpi.content like CONCAT('%', #{content}, '%')
		</if>

	</select>

	<select id="selectTruePhoneByIds" resultType="cn.roadm.bo.ComplaintPhoneInfo">
		select id,file_id as fileId, phone,true_phone as truePhone ,content ,channel_id as channelId,
		send_time as sendTime,task_id as taskId,callmdn,sp_id as spId,sign,otype,province ,sub_send_time as subSendTime
		from complaint_phone_info where id in (${ids})
	</select>

	<select id="selectComplaintByTaskIdAndTruePhone" resultType="cn.roadm.bo.ComplaintPhoneInfo">
		select id,file_id as fileId, phone,true_phone as truePhone ,content ,channel_id as channelId,
		send_time as sendTime,task_id as taskId,callmdn,sp_id as spId,sign,otype,province ,sub_send_time as subSendTime
		from complaint_phone_info where true_phone = #{truePhone} and task_id = #{taskId}
	</select>

</mapper>
