<?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="com.zcloud.mapper.datasource.sms.MSMLoginMapper">

	<!--表名 -->
	<sql id="tableName">
		BUS_SMS_LOG
	</sql>

	<!--数据字典表名 -->
	<sql id="dicTableName">
		SYS_DICTIONARIES
	</sql>

	<!-- 字段 -->
	<sql id="Field">
		f.PHONE,
		f.CONTENT,
		f.STATE,
		f.CREATTIME,
		f.CORPINFO_ID,
		f.MSMLOGIN_ID,
		f.ISDELETE
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		PHONE,
		CONTENT,
		STATE,
		CREATTIME,
		CORPINFO_ID,
		MSMLOGIN_ID,
		ISDELETE
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{PHONE},
		#{CONTENT},
		#{STATE},
		#{CREATTIME},
		#{CORPINFO_ID},
		#{MSMLOGIN_ID},
		#{ISDELETE}

	</sql>

	<!-- 新增-->
	<insert id="save" parameterType="pd">
		insert into
	<include refid="tableName"></include>
		(
	<include refid="Field2"></include>
		) values (
	<include refid="FieldValue"></include>
		)
	</insert>

	<!-- 删除-->
	<delete id="delete" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			MSMLOGIN_ID = #{MSMLOGIN_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			PHONE = #{PHONE},
			CONTENT = #{CONTENT},
			STATE = #{STATE},
			CREATTIME = #{CREATTIME},
			CORPINFO_ID = #{CORPINFO_ID},
			MSMLOGIN_ID = MSMLOGIN_ID
		where
			MSMLOGIN_ID = #{MSMLOGIN_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
			f.MSMLOGIN_ID = #{MSMLOGIN_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,c.CORP_NAME as corpName,
		(select NAME from sys_dictionaries where BIANMA=f.CONTENT) as mbmc
		from
		<include refid="tableName"></include> f  LEFT JOIN bus_corp_info c on c.CORPINFO_ID = f.CORPINFO_ID
		where f.ISDELETE = '0'

		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''"><!-- 关键词检索 -->
		and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
				c.CORP_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
			<if test="pd.phone != null and pd.phone != ''"><!-- 关键词检索 -->
			and
				(
				f.PHONE LIKE CONCAT(CONCAT('%', #{pd.phone}),'%')
				)
		</if>
		ORDER by CREATTIME desc
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		c.CORP_NAME as corpName
		from
		<include refid="tableName"></include> f
		 LEFT JOIN bus_corp_info c on c.CORPINFO_ID = f.CORPINFO_ID
		 where f.ISDELETE = '0'
		 <if test="STATE != null and STATE != ''"><!-- 关键词检索 -->
			and f.STATE=#{STATE}
		</if>
		  <if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
			and f.CORPINFO_ID=#{CORPINFO_ID}
		</if>
	</select>


	<!-- 本公司已发送短信数 -->
	<select id="msUsedCount" parameterType="pd" resultType="int">
		select
			count(1)
		from
		<include refid="tableName"></include> f
		 LEFT JOIN bus_corp_info c on c.CORPINFO_ID = f.CORPINFO_ID
		 where f.ISDELETE = '0'
		 <if test="STATE != null and STATE != ''"><!-- 关键词检索 -->
			and f.STATE=#{STATE}
		</if>
		  <if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
			and f.CORPINFO_ID=#{CORPINFO_ID}
		</if>
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			MSMLOGIN_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>

	<select id="getCountByCorpInfoIds" parameterType="page" resultType="pd">

			select  COUNT(*) as msmcount ,CORPINFO_ID  from
			<include refid="tableName"></include> f
				where
			CORPINFO_ID in
			<foreach item="item" index="index" collection="pd.corpIds" open="(" separator="," close=")">
                 #{item}
			</foreach>
			group by CORPINFO_ID
		</select>
</mapper>
