<?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="MdeTicketCategoryMapper">
    
	<sql id="mdeTicketCategoryColumns">
		a.tc_code AS "tcCode",
		a.issued_type AS "issuedType",
		a.identity_flag AS "identityFlag",
		a.receive_flag AS "receiveFlag",
		a.share_flag AS "shareFlag",
		a.receive_from AS "receiveFrom",
		a.receive_to AS "receiveTo",
		a.available_from AS "availableFrom",
		a.available_to AS "availableTo",
		a.title AS "title",
		a.description AS "description",
		a.rule AS "rule",
		a.title_en AS "titleEn",
		a.description_en AS "descriptionEn",
		a.rule_en AS "ruleEn",
		a.issued_num AS "issuedNum",
		a.left_num AS "leftNum",
		a.money AS "money",
		a.version_no AS "versionNo",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.create_by AS "createBy",
		a.create_date AS "createDate",
		a.update_by AS "updateBy",
		a.update_date AS "updateDate"
	</sql>
	
	<sql id="mdeTicketCategoryJoins">
	</sql>
    
	<select id="get" resultType="com.wechat.model.dbEntity.MdeTicketCategory">
		SELECT 
			<include refid="mdeTicketCategoryColumns"/>
		FROM mde_ticket_category a
		<include refid="mdeTicketCategoryJoins"/>
		WHERE a.tc_code = #{tcCode} and a.del_flag=0
		limit 1
	</select>
	<select id="getByCode" resultType="com.wechat.model.dbEntity.MdeTicketCategory">
		SELECT 
			a.tc_code AS "tcCode",
			a.issued_type AS "issuedType",
			a.identity_flag AS "identityFlag",
			a.receive_flag AS "receiveFlag",
			a.share_flag AS "shareFlag",
			a.receive_from AS "receiveFrom",
			a.receive_to AS "receiveTo",
			a.available_from AS "availableFrom",
			a.available_to AS "availableTo",
			<choose>
				<when test="lang != null and lang == 'en'.toString()">
					a.title_en AS "title",
					a.description_en AS "description",
					a.rule_en AS "rule",
				</when>
				<otherwise>
					a.title AS "title",
					a.description AS "description",
					a.rule AS "rule",
				</otherwise>
			</choose>
			a.money AS "money"
		FROM mde_ticket_category a
		<include refid="mdeTicketCategoryJoins"/>
		WHERE a.tc_code = #{tcCode} and a.del_flag=0
		limit 1
	</select>
	<!-- 获取用户的抵用券，按照类别获取 -->
	<select id="accquireTicket" resultType="com.wechat.model.dbEntity.MdeTicketCategory">
		SELECT 
			<include refid="mdeTicketCategoryColumns"/>
		FROM mde_ticket_category a
		<include refid="mdeTicketCategoryJoins"/>
		WHERE a.del_flag=0  
			and a.issued_type=#{issuedType}
			and (a.receive_flag =#{receiveFlag} or a.receive_flag=9)
			and DATE_FORMAT(a.receive_from,'%Y-%m-%d') &lt;= DATE_FORMAT(NOW(),'%Y-%m-%d')
			and DATE_FORMAT(a.receive_to,'%Y-%m-%d') &gt;= DATE_FORMAT(NOW(),'%Y-%m-%d')
	</select>
	
	<update id="reduceLeftNum">
		UPDATE mde_ticket_category 
		SET left_num = left_num - 1
		WHERE tc_code = #{tcCode}
	</update>
	
	<select id="findList" resultType="com.wechat.model.dbEntity.MdeTicketCategory">
		SELECT 
			<include refid="mdeTicketCategoryColumns"/>
		FROM mde_ticket_category a
		<include refid="mdeTicketCategoryJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="tcCode != null and tcCode != ''">
				AND a.tc_code = #{tcCode}
			</if>
			<if test="issuedType != null and issuedType != ''">
				AND a.issued_type = #{issuedType}
			</if>
			<if test="identityFlag != null and identityFlag != ''">
				AND a.identity_flag = #{identityFlag}
			</if>
			<if test="receiveFrom != null and receiveFrom != ''">
				AND a.receive_from = #{receiveFrom}
			</if>
			<if test="receiveTo != null and receiveTo != ''">
				AND a.receive_to = #{receiveTo}
			</if>
			<if test="availableTo != null and availableTo != ''">
				AND a.available_to = #{availableTo}
			</if>
			<if test="title != null and title != ''">
				AND a.title LIKE 
					<if test="dbName == 'oracle'">'%'||#{title}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{title}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{title},'%')</if>
			</if>
		</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>
	
	<select id="findAllList" resultType="com.wechat.model.dbEntity.MdeTicketCategory">
		SELECT 
			<include refid="mdeTicketCategoryColumns"/>
		FROM mde_ticket_category a
		<include refid="mdeTicketCategoryJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</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_category(
			tc_code,
			issued_type,
			identity_flag,
			receive_flag,
			share_flag,
			receive_from,
			receive_to,
			available_from,
			available_to,
			title,
			description,
			rule,
			title_en,
			description_en,
			rule_en,
			issued_num,
			left_num,
			money,
			version_no,
			remarks,
			del_flag,
			create_by,
			create_date,
			update_by,
			update_date
		) VALUES (
			#{tcCode},
			#{issuedType},
			#{identityFlag},
			#{receiveFlag},
			#{shareFlag},
			#{receiveFrom},
			#{receiveTo},
			#{availableFrom},
			#{availableTo},
			#{title},
			#{description},
			#{rule},
			#{titleEn},
			#{descriptionEn},
			#{ruleEn},
			#{issuedNum},
			#{leftNum},
			#{money},
			#{versionNo},
			#{remarks},
			#{delFlag},
			#{createBy},
			#{createDate},
			#{updateBy},
			#{updateDate}
		)
	</insert>
	
	<update id="update">
		UPDATE mde_ticket_category SET 	
			<if test="tcCode != null">tc_code = #{tcCode},                </if>
			<if test="issuedType != null">issued_type = #{issuedType},        </if>
			<if test="identityFlag != null">identity_flag = #{identityFlag},    </if>
			<if test="receiveFlag != null">receive_flag = #{receiveFlag},      </if>
			<if test="shareFlag != null">share_flag = #{shareFlag},          </if>
			<if test="receiveFrom != null">receive_from = #{receiveFrom},      </if>
			<if test="receiveTo != null">receive_to = #{receiveTo},          </if>
			<if test="availableFrom != null">available_from = #{availableFrom},  </if>
			<if test="availableTo != null">available_to = #{availableTo},      </if>
			<if test="title != null">title = #{title},                   </if>
			<if test="description != null">description = #{description},       </if>
			<if test="rule != null">rule = #{rule},                     </if>
			<if test="titleEn != null">title_en = #{titleEn},              </if>
			<if test="descriptionEn != null">description_en = #{descriptionEn}, </if>
			<if test="ruleEn != null">rule_en = #{ruleEn},                </if>
			<if test="issuedNum != null">issued_num = #{issuedNum},          </if>
			<if test="leftNum != null">left_num = #{leftNum},              </if>
			<if test="money != null">money = #{money},                   </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 tc_code = #{tcCode} <if test=" != null">and version_no = #{versionNo}</if>
	</update>
	
	<update id="delete">
		UPDATE mde_ticket_category SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE tc_code = #{tcCode}  <if test=" != null">and version_no = #{versionNo}</if>
	</update>
	
</mapper>