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

	<resultMap type="ErrCode" id="errCodeResultMap">
		<id column="id" property="id" />
		<id column="errcode" property="errcode" />
		<id column="reason" property="reason" />
		<result column="process_mode" property="process_mode" />
		<result column="supplier" property="supplier" />
		<result column="operator" property="operator" />
		<result column="username" property="username" />
	</resultMap>


	<insert id="insertErrCode" useGeneratedKeys="true"
		keyProperty="id" parameterType="ErrCode">
		INSERT INTO
		sms_err_code(errcode,reason,process_mode
		,supplier,operator,username,create_time) values
		(#{errcode,jdbcType=VARCHAR},
		#{reason,jdbcType=VARCHAR},
		#{process_mode,jdbcType=VARCHAR},
		#{supplier,jdbcType=VARCHAR},
		#{operator,jdbcType=VARCHAR},
		#{username,jdbcType=VARCHAR},
		sysdate() )
	</insert>

	<update id="updateErrCodeByID" parameterType="ErrCode">
		update
		sms_err_code
		set
		errcode=#{errcode},
		reason=#{reason},
		process_mode=#{process_mode},
		supplier=#{supplier},
		operator=#{operator},
		username=#{username}
		where
		id=#{id}
	</update>

	<select id="listPageErrCode" parameterType="ErrCode"
		resultMap="errCodeResultMap">
		select id,errcode,reason,process_mode
		,supplier,operator,username,create_time
		from sms_err_code where 1=1
		<if test="errcode != null and errcode != ''">
			and errcode like CONCAT('%',#{errcode},'%')
		</if>
		<if test="reason != null and reason != ''">
			and reason like CONCAT('%',#{reason},'%')
		</if>

	</select>

	<select id="getErrCodeByID" parameterType="int"
		resultMap="errCodeResultMap">
		select id,errcode,reason,process_mode
		,supplier,operator,username
		from sms_err_code where id = #{id}
	</select>

	<select id="exportCode" parameterType="ErrCode" resultType="Map">
		select id,errcode,reason,process_mode
		,supplier,operator,username,create_time
		from sms_err_code where 1=1
		<if test="errcode != null and errcode != ''">
			and errcode like CONCAT('%',#{errcode},'%')
		</if>
	</select>

	<insert id="addErrCodeBatch" parameterType="ArrayList">
		<selectKey resultType="java.lang.Integer" keyProperty="id"
			order="AFTER">
			SELECT LAST_INSERT_ID();
		</selectKey>
		INSERT INTO sms_err_code(errcode,reason,process_mode
		,supplier,operator,username,create_time) values
		<foreach collection="list" item="item" index="index"
			separator=",">
			(
			#{item.errcode,jdbcType=VARCHAR},
			#{item.reason,jdbcType=VARCHAR},
			#{item.process_mode,jdbcType=VARCHAR},
			#{item.supplier,jdbcType=VARCHAR},
			#{item.operator,jdbcType=VARCHAR},
			#{item.username,jdbcType=VARCHAR},
			sysdate()
			)


		</foreach>
	</insert>

	<select id="getErrCodeByResc" parameterType="TaskDetailInfo"
		resultMap="errCodeResultMap">
		select id,errcode,reason,process_mode
		,supplier,operator,username
		from sms_err_code where errcode = #{stat_desc} 
		<if test="opoperator != null and opoperator != ''">
			and operator like CONCAT('%',#{opoperator},'%')
		</if>
		
		limit 1
	</select>

	<select id="listPageErrCodeSummaryByDay" resultType="cn.roadm.bo.ErrCodeSummary">
		Select errcode, sum(`count`) as `count`
		<if test="bySpId != null and bySpId != '1'">
			,spId as sp_id
		</if>
		<if test="bySign != null and bySign != '1'">
			,sign
		</if>
		<if test="byChannelId != null and byChannelId != '1'">
			,channelId as channelid
		</if>
		<if test="byProvince != null and byProvince != '1'">
			,province
		</if>
		from sms_day_errcode_detail
		<where>
			<if test="sp_id != null and sp_id != ''">
				spId = #{sp_id}
			</if>
			<if test="errcode != null and errcode != ''">
				and errCode = #{errcode}
			</if>
			<if test="sign != null and sign != ''">
				and sign = #{sign}
			</if>
			<if test="channelid != null and channelid != ''">
				and channelId = #{channelid}
			</if>
			<if test="startTime != null and startTime != ''">
				and statTime &gt;= #{startTime}
			</if>
			<if test="endTime != null and endTime != ''">
				and statTime &lt;= #{endTime}
			</if>
			<if test="province != null and province != ''">
				and province = #{province}
			</if>
		</where>
		group by errCode
		<if test="bySpId != null and bySpId != '1'">
			,spId
		</if>
		<if test="bySign != null and bySign != '1'">
			,sign
		</if>
		<if test="byChannelId != null and byChannelId != '1'">
			,channelid
		</if>
		<if test="byProvince != null and byProvince != '1'">
			,province
		</if>
		order by count desc
    </select>

	<select id="countErrCodeSummaryByDay" resultType="java.lang.Long">
			Select sum(IFNULL(`count`, 0)) from sms_day_errcode_detail
		<where>
			<if test="sp_id != null and sp_id != ''">
				spId = #{sp_id}
			</if>
			<if test="errcode != null and errcode != ''">
				and errCode = #{errcode}
			</if>
			<if test="sign != null and sign != ''">
				and sign = #{sign}
			</if>
			<if test="channelid != null and channelid != ''">
				and channelId = #{channelid}
			</if>
			<if test="startTime != null and startTime != ''">
				and statTime &gt;= #{startTime}
			</if>
			<if test="endTime != null and endTime != ''">
				and statTime &lt;= #{endTime}
			</if>
		</where>
	</select>
</mapper>