<?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="MdeTicketDetailMapper">
    
	<sql id="mdeTicketDetailColumns">
		a.td_id AS "tdId",
		a.td_code AS "tdCode",
		a.tc_code AS "tcCode",
		a.user_code AS "userCode",
		a.user_name AS "userName",
		a.share_by AS "shareBy",
		a.identity_flag AS "identityFlag",
		a.share_flag AS "shareFlag",
		a.available_from AS "availableFrom",
		a.available_to AS "availableTo",
		a.used_flag AS "usedFlag",
		a.version_no AS "versionNo",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		<choose>
			<when test="lang != null and lang == 'en'.toString()">
				b.title_en AS "title",
				b.description_en AS "description",
				b.rule_en AS "rule",
			</when>
			<otherwise>
				b.title AS "title",
				b.description AS "description",
				b.rule AS "rule",
			</otherwise>
		</choose>
		b.money as 'money' 
	</sql>
	
	<sql id="mdeTicketDetailJoins">
	inner join mde_ticket_category b on a.tc_code=b.tc_code and b.del_flag=0
	</sql>
    
	<select id="get" resultType="com.wechat.model.dbEntity.MdeTicketDetail">
		SELECT 
			<include refid="mdeTicketDetailColumns"/>
		FROM mde_ticket_detail a
		<include refid="mdeTicketDetailJoins"/>
		WHERE a.td_id = #{tdId} or a.td_code = #{tdId}
		limit 1
	</select>
	<select id="getByTcCodeAndUserName" resultType="com.wechat.model.dbEntity.MdeTicketDetail">
		SELECT 
			<include refid="mdeTicketDetailColumns"/>
		FROM mde_ticket_detail a
		<include refid="mdeTicketDetailJoins"/>
		WHERE a.tc_code = #{tcCode} and a.user_name = #{contactInfo}
		limit 1
	</select>
	
	<select id="checkTdByCode" resultType="com.wechat.model.dbEntity.MdeTicketDetail">
		SELECT 
			<include refid="mdeTicketDetailColumns"/>
		FROM mde_ticket_detail a
		<include refid="mdeTicketDetailJoins"/>
		inner join mde_user c on c.user_code = a.user_code and c.identity_flag = a.identity_flag and c.del_flag=0
		WHERE a.tc_code = #{tcCode} and a.td_code =#{tdCode} and a.user_code = #{userCode} and a.used_flag=1
		limit 1
	</select>
	
	<select id="getByTdCode" resultType="com.wechat.model.dbEntity.MdeTicketDetail">
		SELECT 
			<include refid="mdeTicketDetailColumns"/>
		FROM mde_ticket_detail a
		<include refid="mdeTicketDetailJoins"/>
		WHERE a.td_code = #{tdCode} and used_flag =1
			and DATE_FORMAT(a.available_from,'%Y-%m-%d') &lt;= DATE_FORMAT(NOW(),'%Y-%m-%d')
			and DATE_FORMAT(a.available_to,'%Y-%m-%d') &gt;= DATE_FORMAT(NOW(),'%Y-%m-%d')
		limit 1
	</select>
	
	<select id="getByUserCode" resultType="com.wechat.model.dbEntity.MdeTicketDetail">
		SELECT 
			<include refid="mdeTicketDetailColumns"/>
		FROM mde_ticket_detail a
		<include refid="mdeTicketDetailJoins"/>
		WHERE a.del_flag=0 and a.user_code=#{userCode}
		order by a.used_flag ASC ,a.available_to DESC
	</select>
	
	<select id="getValidTicketByUser" resultType="com.wechat.model.dbEntity.MdeTicketDetail">
		SELECT 
			<include refid="mdeTicketDetailColumns"/>
		FROM mde_ticket_detail a
		<include refid="mdeTicketDetailJoins"/>
		WHERE a.del_flag=0 and a.used_flag =1 and a.user_code=#{userCode} and a.identity_flag =#{identityFlag}
		order by a.available_to DESC
	</select>
	
	<select id="getTicketDetails" resultType="com.wechat.model.dbEntity.MdeTicketDetail">
		SELECT 
			<include refid="mdeTicketDetailColumns"/>
		FROM mde_ticket_detail a
		<include refid="mdeTicketDetailJoins"/>
		<where>
			a.del_flag = 0
			<if test="userCode != null and userCode != ''">
				AND a.user_code = #{userCode}
			</if>
			<if test="identityFlag != null and identityFlag != ''">
				AND a.identity_flag = #{identityFlag}
			</if>
			<if test="usedFlag != null and usedFlag != ''">
				AND a.used_flag = #{usedFlag}
			</if>
		</where>
		ORDER BY a.update_date DESC
	</select>
	<select id="getTicketDetailCounts" resultType="java.lang.Integer">
		SELECT 
			count(1)
		FROM mde_ticket_detail a
		<include refid="mdeTicketDetailJoins"/>
		<where>
			a.del_flag = 0
			<if test="userCode != null and userCode != ''">
				AND a.user_code = #{userCode}
			</if>
			<if test="identityFlag != null and identityFlag != ''">
				AND a.identity_flag = #{identityFlag}
			</if>
			<if test="usedFlag != null and usedFlag != ''">
				AND a.used_flag = #{usedFlag}
			</if>
		</where>
	</select>
	
	<select id="findAllList" resultType="com.wechat.model.dbEntity.MdeTicketDetail">
		SELECT 
			<include refid="mdeTicketDetailColumns"/>
		FROM mde_ticket_detail a
		<include refid="mdeTicketDetailJoins"/>
		<where>
			a.del_flag = 0
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY #{page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO mde_ticket_detail(
			td_code,
			tc_code,
			user_code,
			user_name,
			share_by,
			identity_flag,
			share_flag,
			available_from,
			available_to,
			used_flag,
			version_no,
			remarks,
			del_flag,
			create_by,
			create_date,
			update_by,
			update_date
		) VALUES (
			#{tdCode},
			#{tcCode},
			#{userCode},
			#{userName},
			#{shareBy},
			#{identityFlag},
			#{shareFlag},
			#{availableFrom},
			#{availableTo},
			#{usedFlag},
			#{versionNo},
			#{remarks},
			#{delFlag},
			#{createBy},
			#{createDate},
			#{updateBy},
			#{updateDate}
		)
	</insert>
	
	<update id="update">
		UPDATE mde_ticket_detail SET 	
			<if test="tdCode !=null">td_code = #{tdCode},                </if>
			<if test="tcCode !=null">tc_code = #{tcCode},                </if>
			<if test="userCode !=null">user_code = #{userCode},            </if>
			<if test="userName !=null">user_name =#{userName},            </if>
			<if test="shareBy !=null">share_by = #{shareBy},              </if>
			<if test="identityFlag !=null">identity_flag = #{identityFlag},    </if>
			<if test="shareFlag !=null">share_flag = #{shareFlag},          </if>
			<if test="availableFrom !=null">available_from = #{availableFrom},  </if>
			<if test="availableTo !=null">available_to = #{availableTo},      </if>
			<if test="usedFlag !=null">used_flag = #{usedFlag},            </if>
			<if test="remarks !=null">remarks = #{remarks},               </if>
			<if test="updateBy !=null">update_by = #{updateBy},            </if>
			<if test="updateDate !=null">update_date = #{updateDate},        </if>
			version_no = version_no + 1
		WHERE td_id = #{tdId} <if test="versionNo !=null">and version_no = #{versionNo}</if>
	</update>
	
	<update id="updateShareTicket" parameterType="com.wechat.model.dbEntity.MdeTicketDetail">
        UPDATE mde_ticket_detail SET
            <if test="tdCode !=null">td_code = #{tdCode},                </if>    
            user_code = #{userCode},
            <if test="userName !=null">user_name =#{userName},            </if>
            <if test="shareBy !=null">share_by = #{shareBy},              </if>
            <if test="updateBy !=null">update_by = #{updateBy},            </if>
            <if test="updateDate !=null">update_date = #{updateDate},        </if>
            version_no = version_no + 1
        WHERE td_id = #{tdId} <if test="versionNo !=null">and version_no = #{versionNo}</if>
    </update>
	
	<update id="updateUsedFlag">
		UPDATE mde_ticket_detail SET used_flag = 2, version_no = version_no + 1
		WHERE td_id = #{tdId} 
	</update>
	
	<update id="restartTicket">
		UPDATE mde_ticket_detail SET used_flag = 1, version_no = version_no + 1
		WHERE td_code = #{tdCode} 
	</update>
	
	<update id="updateByUserName">
		UPDATE mde_ticket_detail SET user_code = #{userCode} where user_name = #{userName}
	</update>
	
	<update id="delete">
		UPDATE mde_ticket_detail SET 
			del_flag = #{delFlag}
		WHERE a.td_id = #{tdId} <if test="versionNo !=null">and version_no = #{versionNo}</if>
	</update>
	
</mapper>