<?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.pluss.platform.mapper.ActivityActivateMapper">

	   	<insert id="saveActivityActivate" parameterType="cn.pluss.platform.entity.ActivityActivate">
        	insert into tb_pluss_activity_activate(userId,merchantCode,merchantName,giveMoney,storeId,storeName,name,createDt,posterUrl)VALUES(#{userId},#{merchantCode},#{merchantName},#{giveMoney},#{storeId},#{storeName},#{name},now(),#{posterUrl})
    	</insert>

	   	<insert id="saveActivityActivateBatch">
        	insert into tb_pluss_activity_activate(userId,merchantCode,merchantName,giveMoney,storeId,storeName,name,createDt) VALUES 
    			<foreach collection="list" item="item" index="index" separator=",">
			(#{userId},#{merchantCode},#{merchantName},#{giveMoney},#{storeId},#{storeName},#{name},#{createDt}		)
				</foreach>
    	</insert>

		<select id="queryActivityActivate" parameterType="cn.pluss.platform.entity.ActivityActivate" resultType="cn.pluss.platform.entity.ActivityActivate">
			SELECT * from tb_pluss_activity_activate 
				<where> 
					<if test="id!= null"> 
					 	and id = #{id}
					 </if> 
					<if test="userId!= null"> 
					 	and userId = #{userId}
					 </if> 
					<if test="merchantCode!= null"> 
					 	and merchantCode = #{merchantCode}
					 </if> 
					<if test="merchantName!= null"> 
					 	and merchantName = #{merchantName}
					 </if> 
					<if test="giveMoney!= null"> 
					 	and giveMoney = #{giveMoney}
					 </if> 
					<if test="storeId!= null"> 
					 	and storeId = #{storeId}
					 </if> 
					<if test="storeName!= null"> 
					 	and storeName = #{storeName}
					 </if> 
					<if test="name!= null"> 
					 	and name = #{name}
					 </if> 
					<if test="createDt!= null"> 
					 	and createDt = #{createDt}
					 </if> 
					 <if test="posterUrl!= null"> 
					 	and posterUrl = #{posterUrl}
					 </if>
				 </where> 
		</select>

		<select id="queryActivityActivateList" parameterType="cn.pluss.platform.entity.ActivityActivate" resultType="cn.pluss.platform.entity.ActivityActivate">
			SELECT * from tb_pluss_activity_activate 
				<where> 
					<if test="id!= null"> 
					 and 	id = #{id}
					 </if> 
					<if test="userId!= null"> 
					 and 	userId = #{userId}
					 </if> 
					<if test="merchantCode!= null"> 
					 and 	merchantCode = #{merchantCode}
					 </if> 
					<if test="merchantName!= null"> 
					 and 	merchantName = #{merchantName}
					 </if> 
					<if test="giveMoney!= null"> 
					 and 	giveMoney = #{giveMoney}
					 </if> 
					<if test="storeId!= null"> 
					 and 	storeId = #{storeId}
					 </if> 
					<if test="storeName!= null"> 
					 and 	storeName = #{storeName}
					 </if> 
					<if test="name!= null"> 
					 and 	name = #{name}
					 </if> 
					<if test="createDt!= null"> 
					 and 	createDt = #{createDt}
					 </if> 
					 <if test="posterUrl!= null"> 
					 and 	posterUrl = #{posterUrl}
					 </if> 
				 </where> 
		</select>

		<update id="updateActivityActivate" parameterType="cn.pluss.platform.entity.ActivityActivate" >
			update tb_pluss_activity_activate 
				  <set> 
					<if test="userId!= null"> 
					 	userId = #{userId},
					 </if> 
					<if test="merchantCode!= null"> 
					 	merchantCode = #{merchantCode},
					 </if> 
					<if test="merchantName!= null"> 
					 	merchantName = #{merchantName},
					 </if> 
					<if test="giveMoney!= null"> 
					 	giveMoney = #{giveMoney},
					 </if> 
					<if test="storeId!= null"> 
					 	storeId = #{storeId},
					 </if> 
					<if test="storeName!= null"> 
					 	storeName = #{storeName},
					 </if> 
					<if test="name!= null"> 
					 	name = #{name},
					 </if> 
					<if test="createDt!= null"> 
					 	createDt = #{createDt},
					 </if> 
					 <if test="posterUrl!= null"> 
					 	posterUrl = #{posterUrl}
					 </if> 
				</set>
			where id=#{id} 
		</update>

		<delete id="deleteActivityActivate" parameterType="cn.pluss.platform.entity.ActivityActivate" >
			DELETE FROM tb_pluss_activity_activate where id=#{id}
		</delete>

		<select id="queryActivityActivatePage" parameterType="java.util.Map" resultType="cn.pluss.platform.entity.ActivityActivate">
			SELECT * from tb_pluss_activity_activate 
				<where> 
					<if test="id!= null"> 
					and id = #{id}
					 </if> 
					<if test="userId!= null"> 
					and userId = #{userId}
					 </if> 
					<if test="merchantCode!= null"> 
					and merchantCode = #{merchantCode}
					 </if> 
					<if test="merchantName!= null"> 
					and merchantName = #{merchantName}
					 </if> 
					<if test="giveMoney!= null"> 
					and giveMoney = #{giveMoney}
					 </if> 
					<if test="storeId!= null"> 
					and storeId = #{storeId}
					 </if> 
					<if test="storeName!= null"> 
					and storeName = #{storeName}
					 </if> 
					<if test="name!= null"> 
					and name Like CONCAT("%",#{name},"%")
					 </if> 
					<if test="createDt!= null"> 
					and createDt = #{createDt}
					 </if> 
					 <if test="posterUrl!= null"> 
					and posterUrl = #{createDt}
					 </if> 
				 </where> 
			order by id desc  limit #{pageSize}  offset  #{offset}  
		</select>

	<select id="queryNoActivityActivatePage" parameterType="java.util.Map" resultType="cn.pluss.platform.entity.ActivityActivate">
		SELECT * from tb_pluss_activity_activate
		<where>
			<if test="id!= null">
				and id = #{id}
			</if>
			<if test="userId!= null">
				and userId = #{userId}
			</if>
			<if test="merchantCode!= null">
				and merchantCode IN (SELECT merchantCode
				FROM tb_pluss_merchant_base_info
				WHERE bindingCode = (SELECT bindingCode
				FROM tb_pluss_merchant_base_info
				where merchantCode = #{merchantCode}))
			</if>
			<if test="merchantName!= null">
				and merchantName = #{merchantName}
			</if>
			<if test="giveMoney!= null">
				and giveMoney = #{giveMoney}
			</if>
			<if test="storeId!= null">
				and storeId = #{storeId}
			</if>
			<if test="storeName!= null">
				and storeName = #{storeName}
			</if>
			<if test="name!= null">
				and name Like CONCAT("%",#{name},"%")
			</if>
			<if test="createDt!= null">
				and createDt = #{createDt}
			</if>
			<if test="posterUrl!= null">
				and posterUrl = #{createDt}
			</if>
		</where>
		order by id desc  limit #{pageSize}  offset  #{offset}
	</select>

		<select id="queryActivityActivatePageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
			SELECT count(*) from tb_pluss_activity_activate 
					<where> 
					<if test="id!= null"> 
					 and id = #{id}
					 </if> 
					<if test="userId!= null"> 
					 and userId = #{userId}
					 </if> 
					<if test="merchantCode!= null"> 
					 and merchantCode = #{merchantCode}
					 </if> 
					<if test="merchantName!= null"> 
					 and merchantName = #{merchantName}
					 </if> 
					<if test="giveMoney!= null"> 
					 and giveMoney = #{giveMoney}
					 </if> 
					<if test="storeId!= null"> 
					 and storeId = #{storeId}
					 </if> 
					<if test="storeName!= null"> 
					 and storeName = #{storeName}
					 </if> 
					<if test="name!= null"> 
					 and name  Like CONCAT("%",#{name},"%")  
					 </if> 
					<if test="createDt!= null"> 
					 and createDt = #{createDt}
					 </if> 
				</where>
		</select>

	<select id="queryNoActivityActivatePageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
		SELECT count(*) from tb_pluss_activity_activate
		<where>
			<if test="id!= null">
				and id = #{id}
			</if>
			<if test="userId!= null">
				and userId = #{userId}
			</if>
			<if test="merchantCode!= null">
				and merchantCode IN (SELECT merchantCode
				FROM tb_pluss_merchant_base_info
				WHERE bindingCode = (SELECT bindingCode
				FROM tb_pluss_merchant_base_info
				where merchantCode = #{merchantCode}))
			</if>
			<if test="merchantName!= null">
				and merchantName = #{merchantName}
			</if>
			<if test="giveMoney!= null">
				and giveMoney = #{giveMoney}
			</if>
			<if test="storeId!= null">
				and storeId = #{storeId}
			</if>
			<if test="storeName!= null">
				and storeName = #{storeName}
			</if>
			<if test="name!= null">
				and name  Like CONCAT("%",#{name},"%")
			</if>
			<if test="createDt!= null">
				and createDt = #{createDt}
			</if>
		</where>
	</select>

	<select id="getNewestActivityJoin" resultType="java.util.Map" parameterType="java.lang.String">
		SELECT distinct a.id,a.createDt,a.giveMoney,a.rechargeMoney,a.type,a.reType,a.percent,a.timeType,a.startDt,a.endDt,a.storeId FROM (
		(
		SELECT id,createDt,giveMoney,rechargeMoney,3 'type',-1 AS 'reType',0 AS 'percent',timeType,startDt,endDt,storeId FROM tb_pluss_activity_recharge WHERE storeId = #{storeId} AND timeType = 0
		)
		UNION
		(
		SELECT id,createDt,giveMoney,rechargeMoney,3 'type',-1 AS 'reType',0 AS 'percent',timeType,startDt,endDt,storeId FROM tb_pluss_activity_recharge
		WHERE storeId = #{storeId} AND timeType = 1 and DATE_FORMAT(startDt,'%Y-%m-%d') <![CDATA[<=]]> DATE_FORMAT(NOW(),'%Y-%m-%d') AND DATE_FORMAT(endDt,'%Y-%m-%d') <![CDATA[>=]]> DATE_FORMAT(NOW(),'%Y-%m-%d')
		)
		UNION
		(
		SELECT id,createDt,returnMoney AS 'giveMoney',comsumeMoney AS 'rechargeMoney', 2 'type', type AS 'reType',percent,timeType,startDt,endDt,storeId from tb_pluss_activity_consum_return WHERE storeId = #{storeId} and timeType = 0
		)
		UNION
		(
		SELECT id,createDt,returnMoney AS 'giveMoney',comsumeMoney AS 'rechargeMoney', 2 'type', type AS 'reType',percent,timeType,startDt,endDt,storeId from tb_pluss_activity_consum_return WHERE storeId = #{storeId} and timeType = 1 and DATE_FORMAT(startDt,'%Y-%m-%d') <![CDATA[<=]]> DATE_FORMAT(NOW(),'%Y-%m-%d') AND DATE_FORMAT(endDt,'%Y-%m-%d') <![CDATA[>=]]> DATE_FORMAT(NOW(),'%Y-%m-%d')
		)
		UNION
		(
		SELECT id,createDt,giveMoney, 0 'rechargeMoney',1 'type' , -1 AS 'reType' , 0 as 'percent', 0 as 'timeType', null as startDt,null as endDt,storeId FROM tb_pluss_activity_activate WHERE storeId = #{storeId}
		)
		) a ORDER BY a.createDt desc

	</select>

</mapper>
