<?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.jxy.marketall.mapper.MsgSubMapper">

	<insert id="subscripition">
		insert into jxy_cloud.user_subscription(
		<if test="userId != null and userId != 0">user_id,</if>
		<if test="email != null and email != ''">email,</if>
		<if test="vo.channel != null and vo.channel != ''">channel,</if>
		<if test="vo.metrics != null and vo.metrics != ''">metrics,</if>
		<if test="vo.operator != null and vo.operator != ''">operator,</if>
		<if test="vo.content != null and vo.content != ''">content,</if>
		<if test="vo.addresses != null and vo.addresses != ''">addresses,</if>
		<if test="vo.cycle != null and vo.cycle != ''">`cycle`,</if>
		<if test="vo.noticeWay != null and vo.noticeWay != ''">notice_way,</if>
		create_time)
		VALUE(
		<if test="userId != null and userId != 0">#{userId},</if>
		<if test="email != null and email != ''">#{email},</if>
		<if test="vo.channel != null and vo.channel != ''">#{vo.channel},</if>
		<if test="vo.metrics != null and vo.metrics != ''">#{vo.metrics},</if>
		<if test="vo.operator != null and vo.operator != ''">#{vo.operator},</if>
		<if test="vo.content != null and vo.content != ''">#{vo.content},</if>
		<if test="vo.addresses != null and vo.addresses != ''">#{vo.addresses},</if>
		<if test="vo.cycle != null and vo.cycle != ''">#{vo.cycle},</if>
		<if test="vo.noticeWay != null and vo.noticeWay != ''">#{vo.noticeWay},</if>
		SYSDATE())
	</insert>


	<select id="checkSubByUserIdAndSubId" resultType="java.lang.Boolean">
		select
			count(*)
		from
		jxy_cloud.user_subscription
		<where>
			user_id = #{userId}
			and id = #{id}
		</where>
	</select>

	<delete id="cancelSubscription">
		delete from jxy_cloud.user_subscription
		<where>
			id = #{id}
		</where>
	</delete>

	<select id="getSubscriptionListByUserId" parameterType="java.lang.Long" resultType="com.jxy.marketall.dto.SubscriptionContentDto">
		select
			id,
			user_id userId,
			email,
			channel,
			metrics,
			operator,
			content,
			addresses,
			`cycle`,
			notice_way noticeWay,
			create_time `date`
		from
			jxy_cloud.user_subscription
		<where>
			user_id =#{userId}
		</where>
	</select>

	<select id="checkSubIfExist" resultType="java.lang.Long">
		select
			id
		from
			jxy_cloud.user_subscription
		<where>
			user_id = #{userId}
			and channel = #{vo.channel}
			and metrics = #{vo.metrics}
		</where>
	</select>

	<select id="getSubListByChannel" resultType="com.jxy.marketall.dto.SubscriptionContentDto">
		select
			id,
			user_id userId,
			email,
			channel,
			metrics,
			operator,
			content,
			addresses,
			`cycle`,
			notice_way noticeWay,
			create_time `date`
		from
			jxy_cloud.user_subscription
		<where>
			channel =#{channel}
		</where>
		order by channel,metrics
	</select>


</mapper>
