<?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.iamberry.wechat.service.mapper.TaskMapper">
	
	<!-- 获取某一页的数据 -->
	<select id="getAll" parameterType="PaperBean" resultType="WechatTask">
		SELECT
			t.*, type.child_type_name "productType.typeName"
		FROM
			TB_IAMBERRY_MEMBER_TASK t
		LEFT JOIN
			tb_iamberry_product_child_type type
		ON
			type.child_type_id = t.TASK_PRODUCT_TYPE
		LIMIT
			#{minNum}, #{maxNum}
	</select>
	
	<!-- 获取总的记录数据 -->
	<select id="getCount" resultType="int">
		SELECT
			COUNT(TASK_ID)
		FROM
			TB_IAMBERRY_MEMBER_TASK
	</select>
	
	<!-- 根据id，获取具体的任务数据 -->
	<select id="getOne" resultType="WechatTask" parameterType="java.lang.Integer">
		SELECT
			*
		FROM
			TB_IAMBERRY_MEMBER_TASK
		WHERE
			TASK_ID = #{taskID}
	</select>

	<!-- 保存任务信息 -->
	<insert id="insertTask" parameterType="WechatTask">
		INSERT INTO
			TB_IAMBERRY_MEMBER_TASK
				(
					TASK_DESC, TASK_SALES_NUM, TASK_MONEY, TASK_TYPE, TASK_START_DATE,
					TASK_END_DATE, TASK_PRODUCT_TYPE, TASK_CREATE_DATE, TASK_UPDATE_DATE
				)
		VALUES
			(
				#{taskDesc}, #{taskSalesNum}, #{taskMoney}, #{taskType}, #{taskStartDate},
				#{taskEndDate}, 2, NOW(), NOW()
			)
	</insert>
	
	<!-- 修改任务 -->
	<update id="updateTask" parameterType="WechatTask">
		UPDATE
			TB_IAMBERRY_MEMBER_TASK
		SET
			TASK_DESC 		= #{taskDesc},
			TASK_SALES_NUM 	= #{taskSalesNum},
			TASK_MONEY 		= #{taskMoney},
			TASK_TYPE		= #{taskType},
			TASK_START_DATE = #{taskStartDate},
			TASK_END_DATE 	= #{taskEndDate},
			TASK_PRODUCT_TYPE= #{taskProductType},
			TASK_UPDATE_DATE= NOW()
		WHERE
			TASK_ID = #{taskId}
	</update>
	
	<!-- 保存任务奖励 -->
	<insert id="insertLogs" parameterType="WechatTaskLogs">
		INSERT INTO
			TB_IAMBERRY_TASK_REWARDS_LOGS
				(
					LOGS_TASK_ID, LOGS_USER_OPENID, LOGS_PRODUCT_TYPE, LOGS_NUM, LOGS_MONEY,
					LOGS_START_DATE, LOGS_END_DATE, LOGS_DESC, LOGS_CREATE_TIME
				)
		VALUES
			(
				#{logsTaskId}, #{logsUserOpenId}, #{logsProductType}, #{logsNum}, #{logsMoney},
				#{logsStartDate}, #{logsEndDate}, #{logsDesc}, #{logsCreateTime}
			)
	</insert>
	
	<!-- 根据任务id，查询完成任务的数据 -->
	<select id="getByTaskID" parameterType="java.lang.Integer" resultType="WechatTaskLogs">
		SELECT
			*
		FROM
			TB_IAMBERRY_TASK_REWARDS_LOGS
		WHERE
			LOGS_TASK_ID = #{taskID}
	</select>
	
	<!-- 根据Openid、任务id，获取数据 -->
	<select id="getByTaskIdAndOpenId" parameterType="WechatTaskLogs" resultType="java.lang.Integer">
		SELECT
			LOGS_ID
		FROM
			TB_IAMBERRY_TASK_REWARDS_LOGS
		WHERE
				LOGS_TASK_ID = #{logsTaskId}
			AND
				LOGS_USER_OPENID = #{logsUserOpenId}
		LIMIT
			0, 1
	</select>
	
	<!-- 获取当前在运行的任务:已经开始，没有过期 -->
	<select id="getRunTask" resultType="WechatTask" parameterType="WechatTask">
		SELECT
			*
		FROM
			TB_IAMBERRY_MEMBER_TASK
		WHERE
			<![CDATA[
				TASK_START_DATE >= #{taskStartDate} AND TASK_END_DATE <= #{taskEndDate}
			]]>
	</select>
</mapper>