<?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.ym.mec.biz.dal.dao.SysCouponCodeDao">

	<resultMap type="com.ym.mec.biz.dal.entity.SysCouponCode" id="SysCouponCode">
		<result column="id_" property="id" />
		<result column="coupon_id_" property="couponId" />
		<result column="code_" property="code" />
		<result column="usage_status_" property="usageStatus" />
		<result column="user_id_" property="userId" />
		<result column="used_time_" property="usedTime" />
		<result column="use_start_date_" property="useStartDate" />
		<result column="use_deadline_date_" property="useDeadlineDate" />
		<result column="payment_order_id_" property="paymentOrderId" />
		<result column="issue_id_" property="issueId" />
		<result column="type_" property="type" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
        <result column="tenant_id_" property="tenantId"/>
	</resultMap>

	<resultMap id="SysCouponCodeDto" type="com.ym.mec.biz.dal.dto.SysCouponCodeDto">
		<result column="organ_id_" property="organId"/>
		<result column="organ_name_" property="organName"/>
		<result column="user_id_" property="userId"/>
		<result column="username_" property="userName"/>
		<result column="phone_" property="phone"/>
		<result column="coupon_code_id_" property="couponCodeId"/>
		<result column="coupon_id_" property="couponId"/>
		<result column="coupon_type_" property="couponType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="coupon_name_" property="couponName"/>
		<result column="coupon_description_" property="couponDescription"/>
		<result column="code_" property="code"/>
		<result column="face_value_" property="faceValue" />
		<result column="full_amount_" property="fullAmount" />
		<result column="payment_order_id_" property="paymentOrderId"/>
		<result column="usage_status_" property="usageStatus"/>
		<result column="used_time_" property="usedTime"/>
		<result column="use_start_date_" property="useStartDate"/>
		<result column="use_deadline_date_" property="useDeadlineDate"/>
		<result column="issueType" property="issueType"/>
		<result column="typeDetail" property="typeDetail"/>
	</resultMap>

	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="SysCouponCode" >
		SELECT * FROM sys_coupon_code WHERE id_ = #{id}
	</select>

	<!-- 全查询 -->
	<select id="findAll" resultMap="SysCouponCode">
		SELECT * FROM sys_coupon_code where tenant_id_ = #{tenantId} ORDER BY id_
	</select>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.SysCouponCode" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		<!--
		<selectKey resultClass="int" keyProperty="id" > 
		SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL 
		</selectKey>
		-->
		INSERT INTO sys_coupon_code (coupon_id_,code_,usage_status_,user_id_,used_time_,use_start_date_,use_deadline_date_,
		                             payment_order_id_,issue_id_,type_,create_time_,update_time_,tenant_id_)
							 VALUES(#{couponId},#{code},#{usageStatus},#{userId},#{usedTime},#{useStartDate},#{useDeadlineDate},
									#{paymentOrderId},#{issueId},#{type},NOW(),NOW(),#{tenantId})
	</insert>

	<insert id="batchInsert" parameterType="com.ym.mec.biz.dal.entity.SysCouponCode" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO sys_coupon_code (coupon_id_,code_,usage_status_,user_id_,used_time_,use_start_date_,use_deadline_date_,
		payment_order_id_,issue_id_,type_,create_time_,update_time_,tenant_id_)
		VALUE
		<foreach collection="couponCodes" item="couponCode" separator=",">
			(#{couponCode.couponId},#{couponCode.code},#{couponCode.usageStatus},#{couponCode.userId},#{couponCode.usedTime},#{couponCode.useStartDate},#{couponCode.useDeadlineDate},
			#{couponCode.paymentOrderId},#{couponCode.issueId},#{couponCode.type},NOW(),NOW(),#{couponCode.tenantId})
		</foreach>
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.SysCouponCode">
		UPDATE sys_coupon_code
		<set>
			<if test="couponId != null">
				coupon_id_ = #{couponId},
			</if>
			<if test="usageStatus != null">
				usage_status_ = #{usageStatus},
			</if>
			<if test="useStartDate != null">
				use_start_date_ = #{useStartDate},
			</if>
			<if test="userId != null">
				user_id_ = #{userId},
			</if>
			<if test="code != null">
				code_ = #{code},
			</if>
			<if test="updateTime != null">
				update_time_ = #{updateTime},
			</if>
			<if test="paymentOrderId != null">
				payment_order_id_ = #{paymentOrderId},
			</if>
            <if test="issueId != null">
                issue_id_ = #{issueId},
            </if>
            <if test="type != null">
                type_ = #{type},
            </if>
			<if test="useDeadlineDate != null">
				use_deadline_date_ = #{useDeadlineDate},
			</if>
				used_time_ = NOW()
		</set>
		WHERE id_ = #{id} and tenant_id_ = #{tenantId}
	</update>
	<update id="quit">
		UPDATE sys_coupon_code SET usage_status_ = 0,used_time_ = NULL,update_time_ = NOW() WHERE FIND_IN_SET(id_,#{couponCodeId})
	</update>
	<update id="use">
		UPDATE sys_coupon_code SET usage_status_ = 1,used_time_ = NOW(),update_time_ = NOW()
		WHERE id_ IN
		<foreach collection="couponIdList" open="(" close=")" item="item" separator=",">
			#{item}
		</foreach>
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM sys_coupon_code WHERE id_ = #{id}
	</delete>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="SysCouponCode" parameterType="map">
		SELECT * FROM sys_coupon_code where tenant_id_ = #{tenantId} ORDER BY id_ <include refid="global.limit"/>
	</select>

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM sys_coupon_code where tenant_id_ = #{tenantId}
	</select>

	<select id="countWithUserAndCoupon" resultType="int">
		SELECT COUNT(id_) FROM sys_coupon_code WHERE user_id_=#{userId} AND coupon_id_=#{couponId}
		                                         AND usage_status_  <![CDATA[ <> ]]> 2
	</select>

	<sql id="querySysCouponUseListCondition">
		<where>
			scc.tenant_id_ = #{tenantId}
			<if test="userId!=null">
				AND scc.user_id_=#{userId}
			</if>
			<if test="couponName!=null and couponName!=''">
				AND sc.name_ LIKE CONCAT('%', #{couponName}, '%')
			</if>
			<if test="paymentOrderId!=null">
				AND scc.payment_order_id_=#{paymentOrderId}
			</if>
			<if test="usageStatus!=null">
				<if test="usageStatus == 0">
					AND scc.usage_status_=0
					AND scc.use_deadline_date_ &gt;= NOW()
				</if>
				<if test="usageStatus == 1">
					AND scc.usage_status_=1
				</if>
				<if test="usageStatus == 2">
					AND scc.usage_status_=0
					AND scc.use_deadline_date_ &lt; NOW()
				</if>
			</if>
			<if test="organId!=null">
				AND su.organ_id_=#{organId}
				and (find_in_set(#{organId},JSON_UNQUOTE(json_extract(sc.use_condition_,'$.organId')))
				or JSON_UNQUOTE(json_extract(sc.use_condition_,'$.organId')) is null
				or JSON_UNQUOTE(json_extract(sc.use_condition_,'$.organId')) = "")
			</if>
			<if test="search!=null and search!=''">
				AND (scc.user_id_=#{search} OR su.phone_=#{search} OR su.username_ LIKE CONCAT('%', #{search}, '%'))
			</if>
			<if test="issueType !=null">
                AND scc.type_ = #{issueType}
            </if>
		</where>
	</sql>

	<select id="querySysCouponUseList" resultMap="SysCouponCodeDto">
		SELECT
			organ.name_ organ_name_,
		    scc.user_id_,
			su.username_,
			su.phone_,
			scc.id_ coupon_code_id_,
			scc.coupon_id_,
			sc.type_ coupon_type_,
			sc.name_ coupon_name_,
		    sc.face_value_,
		    sc.full_amount_,
			sc.description_ coupon_description_,
            sc.type_detail_ as typeDetail,
			scc.code_,
			scc.payment_order_id_,
			scc.use_start_date_,
			scc.use_deadline_date_,
			scc.used_time_,
            scc.type_ as issueType,
		    IF(scc.usage_status_ = 0 AND scc.use_deadline_date_ &lt; NOW(),2,scc.usage_status_) usage_status_
		FROM sys_coupon_code scc
			 LEFT JOIN sys_coupon sc ON scc.coupon_id_=sc.id_
			 LEFT JOIN sys_user su ON scc.user_id_=su.id_
			 LEFT JOIN organization organ ON su.organ_id_=organ.id_
		<include refid="querySysCouponUseListCondition"></include>
		ORDER BY scc.id_ DESC
		<include refid="global.limit"></include>
	</select>

	<select id="countSysCouponUseList" resultType="java.lang.Integer">
		SELECT
			COUNT(scc.id_)
		FROM sys_coupon_code scc
			LEFT JOIN sys_coupon sc ON scc.coupon_id_=sc.id_
			LEFT JOIN sys_user su ON scc.user_id_=su.id_
			LEFT JOIN organization organ ON su.organ_id_=organ.id_
		<include refid="querySysCouponUseListCondition"></include>
	</select>
	<resultMap id="HorseRaceLampDto" type="com.ym.mec.biz.dal.dto.HorseRaceLampDto">
		<result property="username" column="username_"/>
		<result property="num" column="num_"/>
		<result property="faceValue" column="face_value_"/>
		<result property="prizeName" column="prize_name_"/>
	</resultMap>
	<select id="queryHorseRaceLampDtoList" resultMap="HorseRaceLampDto">
		SELECT COUNT(suc.coupon_id_) num_,sc.face_value_,su.username_ FROM sys_coupon_code suc
		LEFT JOIN sys_user su ON su.id_ = suc.user_id_
		LEFT JOIN sys_coupon sc ON sc.id_ = suc.coupon_id_
		where suc.tenant_id_ = #{tenantId}
		GROUP BY suc.user_id_,suc.payment_order_id_,suc.coupon_id_
		<include refid="global.limit"/>
	</select>
	<select id="countHorseRaceLampDtoList" resultType="java.lang.Integer">
		SELECT COUNT(t.num_) FROM (
		SELECT COUNT(suc.coupon_id_) num_,sc.face_value_,su.username_ FROM sys_coupon_code suc
		LEFT JOIN sys_user su ON su.id_ = suc.user_id_
		LEFT JOIN sys_coupon sc ON sc.id_ = suc.coupon_id_
		where suc.tenant_id_ = #{tenantId}
		GROUP BY suc.user_id_,suc.payment_order_id_,suc.coupon_id_)t
	</select>
	<select id="findByIdList" resultMap="SysCouponCodeDto">
		SELECT
		scc.user_id_,
		scc.id_ coupon_code_id_,
		scc.coupon_id_,
		sc.type_ coupon_type_,
		sc.name_ coupon_name_,
		sc.face_value_,
		sc.full_amount_,
		sc.description_ coupon_description_,
		scc.code_,
		scc.payment_order_id_,
		scc.use_start_date_,
		scc.use_deadline_date_,
		scc.used_time_,
		scc.type_ as issueType,
        sc.type_detail_ as typeDetail,
		IF(scc.usage_status_ = 0 AND scc.use_deadline_date_ &lt; NOW(),2,scc.usage_status_) usage_status_
		FROM sys_coupon_code scc
		LEFT JOIN sys_coupon sc ON scc.coupon_id_=sc.id_
		WHERE scc.id_ IN
		<foreach collection="couponIdList" open="(" close=")" item="item" separator=",">
			#{item}
		</foreach>
		ORDER BY scc.id_ DESC
	</select>
</mapper>
