<?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.iformall.mapper.WxCouponOrderMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.WxCouponOrder">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="tenant_id" jdbcType="BIGINT" property="tenantId" />
		<result column="coupon_id" jdbcType="BIGINT" property="couponId" />
		<result column="coupon_type" jdbcType="INTEGER" property="couponType" />
		<result column="c_user_id" jdbcType="BIGINT" property="cUserId" />
		<result column="owner_id" jdbcType="BIGINT" property="ownerId" />
		<result column="b_user_id" jdbcType="BIGINT" property="bUserId" />
		<result column="order_id" jdbcType="BIGINT" property="orderId" />
		<result column="expired_time" jdbcType="TIMESTAMP" property="expiredTime" />
		<result column="coupon_order_status" jdbcType="INTEGER" property="couponOrderStatus" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
		<result column="coupon_price" jdbcType="INTEGER" property="couponPrice" />
		<result column="auto_refund" jdbcType="INTEGER" property="autoRefund" />
		<result column="verify_type" jdbcType="INTEGER" property="verifyType" />
	</resultMap>

	<sql id="allColumns">
	    `id`,`tenant_id`,`coupon_id`,`coupon_type`,`c_user_id`,`owner_id`,`b_user_id`,`order_id`,`expired_time`,`coupon_order_status`,`create_date`,`update_date`,`coupon_price`, `auto_refund`, `verify_type`
	</sql>

	<sql id="dynamicWhereConditions">
		where 1 = 1

		<if test=" null != id ">
			and `id` = #{id}
		</if>

		<if test=" null != tenantId ">
			and `tenant_id` = #{tenantId}
		</if>

		<if test=" null != couponId ">
			and `coupon_id` = #{couponId}
		</if>

		<if test=" null != couponType ">
			and `coupon_type` = #{couponType}
		</if>

		<if test=" null != cUserId ">
			and `c_user_id` = #{cUserId}
		</if>

		<if test=" null != ownerId ">
			and `owner_id` = #{ownerId}
		</if>

		<if test=" null != bUserId ">
			and `b_user_id` = #{bUserId}
		</if>

		<if test=" null != orderId ">
			and `order_id` = #{orderId}
		</if>

		<if test=" null != expiredTime ">
			and `expired_time` = #{expiredTime}
		</if>

		<if test=" null != couponOrderStatus ">
			and `coupon_order_status` = #{couponOrderStatus}
		</if>

		<if test=" null != createDate ">
			and `create_date` = #{createDate}
		</if>

		<if test=" null != updateDate ">
			and `update_date` = #{updateDate}
		</if>

		<if test=" null != couponPrice ">
			and `coupon_price` = #{couponPrice}
		</if>

		<if test=" null != autoRefund ">
			and `auto_refund` = #{autoRefund}
		</if>

		<if test=" null != ids ">
			and id in
			<foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
				#{idItem}
			</foreach>
		</if>
		<if test=" null != sortColumns"> order by ${sortColumns} </if>
	</sql>

	<select id="findList" parameterType="com.iformall.domain.po.WxCouponOrder" resultMap="BaseResultMap">
		select <include refid="allColumns" /> from wx_coupon_order
		<include refid="dynamicWhereConditions" />
	</select>

    <update id="updateVerifyCouponOrder" parameterType="com.iformall.domain.po.WxCouponOrder">
		update wx_coupon_order
		set `coupon_order_status` = #{couponOrderStatus},
		  `b_user_id` = #{bUserId},
		  `update_date` = #{updateDate},
		  `verify_type` = #{verifyType}
		where `tenant_id`=#{tenantId}
		and id=#{id} and `b_user_id` is null
	</update>

	<update id="updateVerifyCouponOrderStatus" parameterType="com.iformall.domain.po.WxCouponOrder">
		update wx_coupon_order
		set `coupon_order_status` = #{couponOrderStatus},
		  `b_user_id` = #{bUserId},
		  `update_date` = #{updateDate},
		  `verify_type` = #{verifyType}
		where 1=1
		 and `tenant_id`=#{tenantId}
		 and id=#{id}
		<if test=" null == #{bUserId} ">
			and `b_user_id` is null
		</if>
		<if test=" null != #{bUserId} ">
			and `b_user_id` = #{bUserId}
		</if>

	</update>

	<update id="cancelVerifyCouponOrder" parameterType="com.iformall.domain.po.WxCouponOrder">
		update wx_coupon_order
		set `coupon_order_status` = #{couponOrderStatus},
		  `b_user_id` = null,
		  `update_date` = #{updateDate},
		  `verify_type` = null
		where `tenant_id`=#{tenantId}
		and id=#{id}
	</update>

	<select id="couponDataMap" resultType="com.iformall.domain.vo.MarkingCouponDataReportVo" parameterType="hashmap">
		SELECT DATE_FORMAT(create_date,'%m/%d') as createTime,
		COUNT(DISTINCT c_user_id) as couponUserCount,
		count(*) as couponCount,
		(select Count(*)
			from wx_coupon_order c
			where coupon_order_status=1
			AND DATE_FORMAT(create_date,'%m/%d')=createTime
			AND tenant_id=#{tenantId}
			AND c.update_date &gt;= #{startTime}
			AND c.update_date &lt;= #{endTime}) as verifyCount,
		(select Count(DISTINCT c_user_id)
			from wx_coupon_order d
			where coupon_order_status=1
			AND DATE_FORMAT(create_date,'%m/%d')=createTime
			AND tenant_id=#{tenantId}
			AND d.update_date &gt;= #{startTime}
			AND d.update_date &lt;= #{endTime}) as verifyUserCount
		from wx_coupon_order
			where tenant_id=#{tenantId}
			AND create_date &gt;= #{startTime}
			AND create_date &lt;= #{endTime}
			GROUP BY createTime
	</select>

	<select id="couponDataList" resultType="com.iformall.domain.vo.MarkingCouponDataReportVo" parameterType="hashmap">
		SELECT createTime,couponId,couponCount,couponUserCount,verifyCount,verifyUserCount
		from view_coupon_data
		where view_coupon_data.tenant_id=#{tenantId}
		<if test="startTime != null">
			and view_coupon_data.createTime &gt;= DATE_FORMAT(#{startTime},'%Y-%m-%d')
		</if>
		<if test="endTime != null">
			and view_coupon_data.createTime &lt;= DATE_FORMAT(#{endTime},'%Y-%m-%d')
		</if>
		<if test="couponType != null">
			and view_coupon_data.couponType = #{couponType}
		</if>
		<if test="couponTitle != null">
			and view_coupon_data.couponTitle like concat('%', #{couponTitle},'%')
		</if>
			order by view_coupon_data.createTime desc 

	</select>

	<select id="touchUsersReportList" resultType="com.iformall.domain.vo.TouchUsersReportVo" parameterType="com.iformall.domain.dto.MarkingCouponDataReportDto">
		SELECT xTime, tenant_id, pv, uv, couponCount, userCount, verifyCount, verifyUserCount
		FROM view_touch_user
		WHERE view_touch_user.tenant_id=#{tenantId}
		<if test="startTime != null">
			and view_touch_user.xTime &gt;= DATE_FORMAT(#{startTime},'%Y-%m-%d')
		</if>
		<if test="endTime != null">
			and view_touch_user.xTime &lt;= DATE_FORMAT(#{endTime},'%Y-%m-%d')
		</if>
		GROUP BY xTime
		order by xTime desc
	</select>


	<select id="queryForSceneRepotyHistoryVerified" resultType="com.iformall.domain.vo.MarkingSceneDataVo" parameterType="hashmap">
		select DATE_FORMAT(update_date,'%Y-%m-%d') xTime, count(c.id) triggerCount
		from wx_coupon_order c
		where c.tenant_id = #{tenantId}
		and c.coupon_order_status = 1
		<if test=" null != startTime and null != endTime">
			and  c.update_date BETWEEN #{startTime} and #{endTime}
		</if>
		group by xTime
	</select>

	<select id="queryForSceneRepotyHistoryOrdered" resultType="com.iformall.domain.vo.MarkingSceneDataVo" parameterType="hashmap">
		select DATE_FORMAT(update_date,'%Y-%m-%d') xTime, count(c.id) triggerCount
		from wx_coupon_order c
		where c.tenant_id = #{tenantId}
		<if test=" null != startTime and null != endTime">
			and  c.create_date BETWEEN #{startTime} and #{endTime}
		</if>
		group by xTime
	</select>

	<select id="queryForSceneRepotyMechantHistoryOrdered" resultType="com.iformall.domain.vo.MarkingSceneDataVo" parameterType="hashmap">
		select DATE_FORMAT(update_date,'%Y-%m-%d') xTime, count(c.id) triggerCount
		from wx_coupon_order c left join wx_coupon_action_log cal on cal.tenant_id = c.tenant_id
		where cal.coupon_order_id=c.id
		and c.tenant_id=#{tenantId}
		<if test=" null != channelType and null != channelType">
			and cal.channel_type=#{channelType}
		</if>
		<if test=" null != startTime and null != endTime">
			and  c.create_date BETWEEN #{startTime} and #{endTime}
		</if>
		group by xTime
	</select>

	<select id="queryPriceTotal" parameterType="com.iformall.domain.po.WxCouponOrder" resultType="java.lang.Integer" >
		SELECT IFNULL(SUM(coupon_price),0)  from wx_coupon_order
		where 1=1
		<if test=" null != tenantId">
			and tenant_id = #{tenantId}
		</if>
		<if test=" null != couponOrderStatus">
			and coupon_order_status = #{couponOrderStatus}
		</if>
		<if test=" null != cUserId">
			and c_user_id = #{cUserId}
		</if>
		<if test=" null != couponId ">
			and coupon_id = #{couponId}
		</if>
		<if test=" null != couponType ">
			and coupon_type = #{couponType}
		</if>
		<if test=" null != startTime">
			and create_date &gt;= #{startTime}
		</if>
		<if test=" null != endTime">
			and create_date &lt;= #{endTime}
		</if>
	</select>

	<select id="findCount" parameterType="com.iformall.domain.po.WxCouponOrder" resultType="java.lang.Integer">
		select COUNT(*) FROM wx_coupon_order co
		<if test=" null != businessId ">
			inner join wx_coupon c
			on c.id = co.coupon_id
			and c.business = #{businessId}
			<if test=" null != subBusinessId ">
				and c.sub_business = #{subBusinessId}
			</if>
		</if>
		where 1=1
		<if test=" null != tenantId">
			and co.tenant_id = #{tenantId}
		</if>
		<if test=" null != couponOrderStatus">
			and co.coupon_order_status = #{couponOrderStatus}
		</if>
		<if test=" null != cUserId">
			and co.c_user_id = #{cUserId}
		</if>
		<if test=" null != couponId ">
			and co.coupon_id = #{couponId}
		</if>
		<if test=" null != couponType ">
			and co.coupon_type = #{couponType}
		</if>

		<if test=" 1 == couponOrderStatus">
			<if test=" null != startTime">
				and co.update_date &gt;= #{startTime}
			</if>
			<if test=" null != endTime">
				and co.update_date &lt;= #{endTime}
			</if>
			<if test="  0 == timeSlot">
				and date_format(co.update_date,'%H:%m') &gt;= '06:00'
				and date_format(co.update_date,'%H:%m') &lt;  '18:00'
				and date_format(co.update_date,'%w') in (1,2,3,4,5)
			</if>
			<if test="  1 == timeSlot">
				and ((date_format(co.update_date,'%H:%m') &gt;= '18:00'
					and date_format(co.update_date,'%w') in (1,2,3,4))
					or
					((date_format(co.update_date,'%H:%m') &lt;  '06:00')
					and date_format(co.update_date,'%w') in (1,2,3,4,5)))
			</if>
			<if test="  2 == timeSlot">
				and date_format(co.update_date,'%H:%m') &gt;= '06:00'
				and date_format(co.update_date,'%H:%m') &lt;  '18:00'
				and date_format(co.update_date,'%w') in (0,6)
			</if>
			<if test="  3 == timeSlot">
				and ((date_format(co.update_date,'%H:%m') &gt;= '18:00'
					and date_format(co.update_date,'%w') in (5,6,0))
				  	or
					((date_format(co.update_date,'%H:%m') &lt;  '06:00')
					and date_format(co.update_date,'%w') in (6,0)))
			</if>
		</if>

		<if test=" 1 != couponOrderStatus">
			<if test=" null != startTime">
				and co.create_date &gt;= #{startTime}
			</if>
			<if test=" null != endTime">
				and co.create_date &lt;= #{endTime}
			</if>
			<if test="  0 == timeSlot">
				and date_format(co.create_date,'%H:%m') &gt;= '06:00'
				and date_format(co.create_date,'%H:%m') &lt;  '18:00'
				and date_format(co.create_date,'%w') in (1,2,3,4,5)
			</if>
			<if test="  1 == timeSlot">
				and ((date_format(co.create_date,'%H:%m') &gt;= '18:00'
				and date_format(co.create_date,'%w') in (1,2,3,4))
				or
				((date_format(co.create_date,'%H:%m') &lt;  '06:00')
				and date_format(co.create_date,'%w') in (1,2,3,4,5)))
			</if>
			<if test="  2 == timeSlot">
				and date_format(co.create_date,'%H:%m') &gt;= '06:00'
				and date_format(co.create_date,'%H:%m') &lt;  '18:00'
				and date_format(co.create_date,'%w') in (0,6)
			</if>
			<if test="  3 == timeSlot">
				and ((date_format(co.create_date,'%H:%m') &gt;= '18:00'
				and date_format(co.create_date,'%w') in (5,6,0))
				or
				((date_format(co.create_date,'%H:%m') &lt;  '06:00')
				and date_format(co.create_date,'%w') in (6,0)))
			</if>
		</if>
	</select>

	<select id="getCountByBusinessId" parameterType="com.iformall.domain.po.WxCouponOrder" resultType="hashmap">
		select c.business, COUNT(c.business) bu_count
		FROM wx_coupon_order co
		inner join wx_coupon c on c.id = co.coupon_id
		where 1=1
		<if test=" null != tenantId">
			and co.tenant_id = #{tenantId}
		</if>
		<if test=" null != couponOrderStatus">
			and co.coupon_order_status = #{couponOrderStatus}
		</if>
		<if test=" null != cUserId">
			and co.c_user_id = #{cUserId}
		</if>
		<if test=" null != couponId ">
			and co.coupon_id = #{couponId}
		</if>
		<if test=" null != couponType ">
			and co.coupon_type = #{couponType}
		</if>

		<if test=" 1 == couponOrderStatus">
			<if test=" null != startTime">
				and co.update_date &gt;= #{startTime}
			</if>
			<if test=" null != endTime">
				and co.update_date &lt;= #{endTime}
			</if>
			<if test="  0 == timeSlot">
				and date_format(co.update_date,'%H:%m') &gt;= '06:00'
				and date_format(co.update_date,'%H:%m') &lt;  '18:00'
				and date_format(co.update_date,'%w') in (1,2,3,4,5)
			</if>
			<if test="  1 == timeSlot">
				and ((date_format(co.update_date,'%H:%m') &gt;= '18:00'
				and date_format(co.update_date,'%w') in (1,2,3,4))
				or
				((date_format(co.update_date,'%H:%m') &lt;  '06:00')
				and date_format(co.update_date,'%w') in (1,2,3,4,5)))
			</if>
			<if test="  2 == timeSlot">
				and date_format(co.update_date,'%H:%m') &gt;= '06:00'
				and date_format(co.update_date,'%H:%m') &lt;  '18:00'
				and date_format(co.update_date,'%w') in (0,6)
			</if>
			<if test="  3 == timeSlot">
				and ((date_format(co.update_date,'%H:%m') &gt;= '18:00'
				and date_format(co.update_date,'%w') in (5,6,0))
				or
				((date_format(co.update_date,'%H:%m') &lt;  '06:00')
				and date_format(co.update_date,'%w') in (6,0)))
			</if>
		</if>

		<if test=" 1 != couponOrderStatus">
			<if test=" null != startTime">
				and co.create_date &gt;= #{startTime}
			</if>
			<if test=" null != endTime">
				and co.create_date &lt;= #{endTime}
			</if>
			<if test="  0 == timeSlot">
				and date_format(co.create_date,'%H:%m') &gt;= '06:00'
				and date_format(co.create_date,'%H:%m') &lt;  '18:00'
				and date_format(co.create_date,'%w') in (1,2,3,4,5)
			</if>
			<if test="  1 == timeSlot">
				and ((date_format(co.create_date,'%H:%m') &gt;= '18:00'
				and date_format(co.create_date,'%w') in (1,2,3,4))
				or
				((date_format(co.create_date,'%H:%m') &lt;  '06:00')
				and date_format(co.create_date,'%w') in (1,2,3,4,5)))
			</if>
			<if test="  2 == timeSlot">
				and date_format(co.create_date,'%H:%m') &gt;= '06:00'
				and date_format(co.create_date,'%H:%m') &lt;  '18:00'
				and date_format(co.create_date,'%w') in (0,6)
			</if>
			<if test="  3 == timeSlot">
				and ((date_format(co.create_date,'%H:%m') &gt;= '18:00'
				and date_format(co.create_date,'%w') in (5,6,0))
				or
				((date_format(co.create_date,'%H:%m') &lt;  '06:00')
				and date_format(co.create_date,'%w') in (6,0)))
			</if>
		</if>
		group by c.business
	</select>

	<select id="getCountBySubBusinessId" parameterType="com.iformall.domain.po.WxCouponOrder" resultType="hashmap">
		select c.sub_business, COUNT(c.sub_business) bu_count
		FROM wx_coupon_order co
		<if test=" null != businessId ">
			inner join wx_coupon c on c.id = co.coupon_id and c.business = #{businessId}
		</if>
		where 1=1
		<if test=" null != tenantId">
			and co.tenant_id = #{tenantId}
		</if>
		<if test=" null != couponOrderStatus">
			and co.coupon_order_status = #{couponOrderStatus}
		</if>
		<if test=" null != cUserId">
			and co.c_user_id = #{cUserId}
		</if>
		<if test=" null != couponId ">
			and co.coupon_id = #{couponId}
		</if>
		<if test=" null != couponType ">
			and co.coupon_type = #{couponType}
		</if>

		<if test=" 1 == couponOrderStatus">
			<if test=" null != startTime">
				and co.update_date &gt;= #{startTime}
			</if>
			<if test=" null != endTime">
				and co.update_date &lt;= #{endTime}
			</if>
			<if test="  0 == timeSlot">
				and date_format(co.update_date,'%H:%m') &gt;= '06:00'
				and date_format(co.update_date,'%H:%m') &lt;  '18:00'
				and date_format(co.update_date,'%w') in (1,2,3,4,5)
			</if>
			<if test="  1 == timeSlot">
				and ((date_format(co.update_date,'%H:%m') &gt;= '18:00'
				and date_format(co.update_date,'%w') in (1,2,3,4))
				or
				((date_format(co.update_date,'%H:%m') &lt;  '06:00')
				and date_format(co.update_date,'%w') in (1,2,3,4,5)))
			</if>
			<if test="  2 == timeSlot">
				and date_format(co.update_date,'%H:%m') &gt;= '06:00'
				and date_format(co.update_date,'%H:%m') &lt;  '18:00'
				and date_format(co.update_date,'%w') in (0,6)
			</if>
			<if test="  3 == timeSlot">
				and ((date_format(co.update_date,'%H:%m') &gt;= '18:00'
				and date_format(co.update_date,'%w') in (5,6,0))
				or
				((date_format(co.update_date,'%H:%m') &lt;  '06:00')
				and date_format(co.update_date,'%w') in (6,0)))
			</if>
		</if>

		<if test=" 1 != couponOrderStatus">
			<if test=" null != startTime">
				and co.create_date &gt;= #{startTime}
			</if>
			<if test=" null != endTime">
				and co.create_date &lt;= #{endTime}
			</if>
			<if test="  0 == timeSlot">
				and date_format(co.create_date,'%H:%m') &gt;= '06:00'
				and date_format(co.create_date,'%H:%m') &lt;  '18:00'
				and date_format(co.create_date,'%w') in (1,2,3,4,5)
			</if>
			<if test="  1 == timeSlot">
				and ((date_format(co.create_date,'%H:%m') &gt;= '18:00'
				and date_format(co.create_date,'%w') in (1,2,3,4))
				or
				((date_format(co.create_date,'%H:%m') &lt;  '06:00')
				and date_format(co.create_date,'%w') in (1,2,3,4,5)))
			</if>
			<if test="  2 == timeSlot">
				and date_format(co.create_date,'%H:%m') &gt;= '06:00'
				and date_format(co.create_date,'%H:%m') &lt;  '18:00'
				and date_format(co.create_date,'%w') in (0,6)
			</if>
			<if test="  3 == timeSlot">
				and ((date_format(co.create_date,'%H:%m') &gt;= '18:00'
				and date_format(co.create_date,'%w') in (5,6,0))
				or
				((date_format(co.create_date,'%H:%m') &lt;  '06:00')
				and date_format(co.create_date,'%w') in (6,0)))
			</if>
		</if>
		group by c.sub_business
	</select>

	<select id="queryPriceTotalGroup" resultType="com.iformall.domain.vo.CUserDateAmountVo" >
		SELECT DATE_FORMAT(create_date,'%Y-%m-%d') as xTime,IFNULL(SUM(coupon_price),0) as price from wx_coupon_order
		 where tenant_id=#{tenantId}
		 AND create_date &gt;= #{startTime} and create_date &lt; #{endTime}   GROUP BY xTime
	</select>


	<select id="findExpiredFreeCouponOrderByValidDate" parameterType="com.iformall.domain.po.WxCouponOrder" resultMap="BaseResultMap">
        select <include refid="allColumns" /> from wx_coupon_order
        where expired_time >= date_sub(curdate(),interval 360 day) and expired_time &lt; now() and coupon_price = 0 and coupon_order_status = 0
	</select>

	<select id="findExpiredCouponOrderByValidDate" parameterType="com.iformall.domain.po.WxCouponOrder" resultMap="BaseResultMap">
		select <include refid="allColumns" /> from wx_coupon_order
		where expired_time >= date_sub(curdate(),interval 360 day) and expired_time &lt; now() and coupon_price &gt; 0 and coupon_order_status = 0
	</select>


	<resultMap id="BVoResultMap" type="com.iformall.domain.vo.WxCouponOrderBVo">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="tenant_id" jdbcType="BIGINT" property="tenantId" />
		<result column="coupon_id" jdbcType="BIGINT" property="couponId" />
		<result column="coupon_type" jdbcType="INTEGER" property="couponType" />
		<result column="c_user_id" jdbcType="BIGINT" property="cUserId" />
		<result column="owner_id" jdbcType="BIGINT" property="ownerId" />
		<result column="b_user_id" jdbcType="BIGINT" property="bUserId" />
		<result column="order_id" jdbcType="BIGINT" property="orderId" />
		<result column="expired_time" jdbcType="TIMESTAMP" property="expiredTime" />
		<result column="coupon_order_status" jdbcType="INTEGER" property="couponOrderStatus" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
		<result column="coupon_price" jdbcType="INTEGER" property="couponPrice" />
		
		<result column="merchant_id" jdbcType="BIGINT" property="merchantId"/>
		
		<result column="type" jdbcType="INTEGER" property="type" />
		<result column="title" jdbcType="VARCHAR" property="title" />
		<result column="sale_price" jdbcType="INTEGER" property="salePrice" />
		<result column="use_price" jdbcType="INTEGER" property="usePrice" />
		<result column="price" jdbcType="INTEGER" property="price" />
		<result column="auto_refund" jdbcType="INTEGER" property="autoRefund" />
		<result column="business" jdbcType="INTEGER" property="business"/>
		<result column="subsidy_num" jdbcType="INTEGER" property="subsidyNum"/>
		<result column="subsidy_type" jdbcType="INTEGER" property="subsidyType"/>
		
		<result column="name" jdbcType="VARCHAR" property="bUserName" />
		<result column="bphone" jdbcType="VARCHAR" property="bUserPhone" />
		<result column="phone" jdbcType="VARCHAR" property="cUserPhone" />
		<result column="merchant_name" jdbcType="VARCHAR" property="merchantName" />
		
		<result column="verify_merchant_name" jdbcType="VARCHAR" property="verifyMerchantName"/>
		
		<collection column="{productId=coupon_id}" property="merchantVoList"
					ofType="com.iformall.domain.vo.WxMerchantVo"
					javaType="java.util.List"
					select="com.iformall.mapper.WxCouponMerchantMapper.findMerchantNameList" >
		</collection>
	</resultMap>

	<resultMap id="AdminBVoResultMap" type="com.iformall.domain.vo.WxCouponOrderBVo">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="tenant_id" jdbcType="BIGINT" property="tenantId" />
		<result column="coupon_id" jdbcType="BIGINT" property="couponId" />
		<result column="coupon_type" jdbcType="INTEGER" property="couponType" />
		<result column="c_user_id" jdbcType="BIGINT" property="cUserId" />
		<result column="owner_id" jdbcType="BIGINT" property="ownerId" />
		<result column="b_user_id" jdbcType="BIGINT" property="bUserId" />
		<result column="order_id" jdbcType="BIGINT" property="orderId" />
		<result column="expired_time" jdbcType="TIMESTAMP" property="expiredTime" />
		<result column="coupon_order_status" jdbcType="INTEGER" property="couponOrderStatus" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
		<result column="coupon_price" jdbcType="INTEGER" property="couponPrice" />

		<result column="merchant_id" jdbcType="BIGINT" property="merchantId"/>

		<result column="type" jdbcType="INTEGER" property="type" />
		<result column="title" jdbcType="VARCHAR" property="title" />
		<result column="sale_price" jdbcType="INTEGER" property="salePrice" />
		<result column="use_price" jdbcType="INTEGER" property="usePrice" />
		<result column="price" jdbcType="INTEGER" property="price" />
		<result column="auto_refund" jdbcType="INTEGER" property="autoRefund" />
		<result column="business" jdbcType="INTEGER" property="business"/>
		<result column="subsidy_num" jdbcType="INTEGER" property="subsidyNum"/>
		<result column="subsidy_type" jdbcType="INTEGER" property="subsidyType"/>

		<result column="name" jdbcType="VARCHAR" property="bUserName" />
		<result column="bphone" jdbcType="VARCHAR" property="bUserPhone" />
		<result column="phone" jdbcType="VARCHAR" property="cUserPhone" />
		<result column="merchant_name" jdbcType="VARCHAR" property="merchantName" />

		<result column="verify_merchant_name" jdbcType="VARCHAR" property="verifyMerchantName"/>
	</resultMap>

	<sql id="allAdminCouponOrderListColumns">
		co.id,co.tenant_id,co.coupon_id,co.coupon_type,co.expired_time,co.coupon_order_status,co.create_date,co.update_date,co.coupon_price,
		c.title,c.sale_price,c.use_price,c.price,c.unit,c.auto_refund,c.business,c.subsidy_type,ms.subsidy subsidy_num,
		(case when com.mc=1 then
			(select m.name from wx_merchant m, wx_coupon_merchant cm
			where m.id = cm.merchant_id and cm.product_id=co.coupon_id and cm.status = 0) else '[多商户通用]' end) as merchant_name,
		(case when com.mc = 1 then (select m.id from wx_merchant m,
		 wx_coupon_merchant cm where m.id = cm.merchant_id and cm.product_id = co.coupon_id
		 and cm.status = 0) else 0 end) as merchant_id,cu.phone,bu.verify_merchant_name,cal.channel_type
	</sql>

	<sql id="allAdminCouponOrderDetailColumns">
		co.id,co.tenant_id,co.coupon_id,co.coupon_type,co.c_user_id,co.owner_id,co.b_user_id,co.order_id,co.expired_time,co.coupon_order_status,co.create_date,co.update_date,co.coupon_price,
		c.type,c.title,c.sale_price,c.use_price,c.price,c.unit,c.auto_refund,c.business,c.subsidy_type,ms.subsidy subsidy_num,
		bu.name, bu.phone as bphone,
		cu.phone,
		m.name as merchant_name,m.id as merchant_id
	</sql>

	<select id="findListOfAdmin" parameterType="com.iformall.domain.vo.WxCouponOrderBVo" resultMap="AdminBVoResultMap">
		select <include refid="allAdminCouponOrderListColumns" />
		from wx_coupon_order co
		inner join wx_coupon c on c.id = co.coupon_id
		inner join (select tco.id as tcoid,
		(select count(*) from wx_coupon_merchant tcm
		where tcm.product_id = tco.coupon_id
		and tcm.status = 0) as mc
		from wx_coupon_order tco) com on com.tcoid = co.id
		inner join wx_c_user cu on cu.id=co.c_user_id
		left join (
		select bu.id as bu_id,m.name as verify_merchant_name
		from wx_merchant_b_user bu inner join wx_merchant m on bu.merchant_id=m.id
		where bu.tenant_id = #{tenantId}
		) bu on bu.bu_id = co.b_user_id
		left join wx_merchant_subsidy ms on co.id = ms.coupon_order_id and ms.type = 1
		left join wx_coupon_action_log cal on co.id = cal.coupon_order_id and co.coupon_id = cal.coupon_id
		where 1=1
		<choose>
			<when test=" null != couponType">
				and co.coupon_type = #{couponType}
			</when>
			<otherwise>
				and co.coupon_type &lt; 100
			</otherwise>
		</choose>
		<if test=" null != tenantId ">
			and co.tenant_id = #{tenantId}
		</if>

		<if test=" null != couponOrderStatus ">
			and co.coupon_order_status = #{couponOrderStatus}
		</if>
		
		<if test=" null != merchantName and merchantName!=''">
			and (case when com.mc=1
				then
					(select m.name from wx_merchant m, wx_coupon_merchant cm
					where m.id = cm.merchant_id
						and cm.product_id=co.coupon_id
						and cm.status = 0)
				else'[多商户通用]' end)
				like concat('%', #{merchantName},'%')
		</if>

		<if test="startDate != null">
			AND co.create_date &gt;  #{startDate,jdbcType=TIMESTAMP}
		</if>

		<if test="endDate != null">
			AND co.create_date &lt;  #{endDate,jdbcType=TIMESTAMP}
		</if>
		
		<if test="verifyStartDate != null">
			AND co.update_date &gt; #{verifyStartDate,jdbcType=TIMESTAMP}
		</if>
		
		<if test="verifyEndDate != null">
			AND co.update_date &lt; #{verifyEndDate,jdbcType=TIMESTAMP}
		</if>
		
		<if test=" null != id ">
			and co.id = #{id}
		</if>
		<if test=" null != couponId ">
			and c.id = #{couponId}
		</if>
		<if test=" null != business ">
			and c.business = #{business}
		</if>
		<if test=" null != verifyMerchantName and ''!= verifyMerchantName ">
			and bu.verify_merchant_name = #{verifyMerchantName}
		</if>
		
		<if test="null!=building">
			and co.coupon_id in(
			select cm.product_id from wx_merchant_shop ms left join wx_merchant m on ms.merchant_id=m.id
			left join wx_shop s on ms.shop_id=s.id
			left join wx_coupon_merchant cm on ms.merchant_id=cm.merchant_id
			where ms.is_del=0 and s.building=#{building}
			<if test=" null != tenantId ">
				and ms.tenant_id=#{tenantId}
			</if>
			<if test="null!=floor">
				and s.floor=#{floor}
			</if>
			)
		</if>
		<if test=" null != sortColumns"> order by ${sortColumns} </if>
		<if test=" null == sortColumns">order by co.create_date desc,co.id desc</if>
	
	</select>


	<select id="findDetailOfAdmin" parameterType="java.lang.Long" resultMap="BVoResultMap">
		select <include refid="allAdminCouponOrderDetailColumns" />
		from wx_coupon c, wx_coupon_order co
        left join wx_c_user cu on cu.id = co.c_user_id
		left join wx_merchant_b_user bu on co.b_user_id = bu.id
		left join wx_merchant m on m.id = bu.merchant_id
		left join wx_merchant_subsidy ms on co.id = ms.coupon_order_id and ms.type = 1
		where co.id = #{id}
		and c.id = co.coupon_id
	</select>

    <sql id="allBUserVerifiedCouponOrderColumns">
		co.id,co.tenant_id,co.coupon_id,co.coupon_type,co.c_user_id,co.owner_id,co.b_user_id,co.order_id,co.expired_time,co.coupon_order_status,co.create_date,co.update_date,co.coupon_price,
		c.type,c.title,c.sale_price,c.use_price,c.price,c.unit,
		bu.name,
		cu.phone
	</sql>

    <sql id="allBUserOrderedCouponOrderColumns">
		co.id,co.tenant_id,co.coupon_id,co.coupon_type,co.c_user_id,co.owner_id,co.b_user_id,co.order_id,co.expired_time,co.coupon_order_status,co.create_date,co.update_date,co.coupon_price,
		c.type,c.title,c.sale_price,c.use_price,c.price,c.unit,
		cu.phone
	</sql>


    <select id="findListOfOrderedByDateForBUser" parameterType="map" resultMap="BVoResultMap">
		select <include refid="allBUserOrderedCouponOrderColumns" />
		from wx_coupon_order co,wx_coupon c,wx_c_user cu,wx_coupon_merchant cm,
			(select tco.id as tcoid,
				(select count(*) from wx_coupon_merchant tcm
					where tcm.product_id = tco.coupon_id
                    and tcm.status = 0) as mc
			from wx_coupon_order tco) com
		where com.mc = 1
		and com.tcoid = co.id
		and cm.merchant_id = #{merchantId}
		and cm.product_id = c.id
        and cm.status = 0
		and co.coupon_id = c.id
		and cu.id = co.c_user_id
		<if test="startDate != null">
			AND co.create_date &gt;  #{startDate,jdbcType=TIMESTAMP}
		</if>
		<if test="endDate != null">
			AND co.create_date &lt;  #{endDate,jdbcType=TIMESTAMP}
		</if>
		order by co.create_date desc

	</select>

	<select id="findListOfVerifiedByDateForBUser" parameterType="map" resultMap="BVoResultMap">
		select <include refid="allBUserVerifiedCouponOrderColumns" />
		from wx_coupon_order co,wx_coupon c,wx_c_user cu,wx_merchant_b_user bu
		where co.coupon_id = c.id
		and cu.id = co.c_user_id
		and bu.id = co.b_user_id
		and bu.merchant_id = #{merchantId}
		<if test="startDate != null">
			AND co.update_date &gt;  #{startDate,jdbcType=TIMESTAMP}
		</if>
		<if test="endDate != null">
			AND co.update_date &lt;  #{endDate,jdbcType=TIMESTAMP}
		</if>
		AND co.coupon_order_status = '1'
		order by co.update_date desc
	</select>

	<sql id="allCouponOrderColumns">
		co.id,co.tenant_id,co.coupon_id,co.coupon_type,co.c_user_id,co.owner_id,co.b_user_id,co.order_id,co.expired_time,co.coupon_order_status,co.create_date,co.update_date,co.coupon_price
	</sql>

	<select id="findListOfOrderedByDate" parameterType="map" resultMap="BaseResultMap">
		select <include refid="allCouponOrderColumns" />
		from wx_coupon_order co,wx_coupon_merchant cm,
			(select tco.id as tcoid,
				(select count(*) from wx_coupon_merchant tcm
			  		where tcm.product_id = tco.coupon_id
                      and tcm.status = 0)
			as mc
			from wx_coupon_order tco) com
		where com.mc = 1
		and com.tcoid = co.id
        and cm.status = 0
		and cm.merchant_id = #{merchantId}
		and cm.product_id = co.coupon_id
		<if test="startDate != null">
			AND co.create_date &gt;  #{startDate,jdbcType=TIMESTAMP}
		</if>
		<if test="endDate != null">
			AND co.create_date &lt;  #{endDate,jdbcType=TIMESTAMP}
		</if>
	</select>

	<select id="findListOfVerifiedByDate" parameterType="map" resultMap="BaseResultMap">
		select <include refid="allCouponOrderColumns" />
		from wx_coupon_order co, wx_merchant_b_user mbu
		where mbu.id = co.b_user_id
		<if test="merchantId != null">
			and mbu.merchant_id = #{merchantId}
		</if>
		<if test="startDate != null">
			AND co.update_date &gt;  #{startDate,jdbcType=TIMESTAMP}
		</if>
		<if test="endDate != null">
			AND co.update_date &lt;  #{endDate,jdbcType=TIMESTAMP}
		</if>
		AND co.coupon_order_status = '1'
	</select>



	<sql id="allCUserCouponOrderListColumns">
		co.id,co.tenant_id,co.coupon_id,co.coupon_type,co.expired_time,co.coupon_order_status,co.create_date,co.update_date,co.coupon_price,co.verify_type,
		c.cover_img,c.title,c.sub_title,c.sale_price,c.use_price,c.price,c.unit,c.valid_type,c.valid_start_date,c.valid_end_date,c.auto_refund,
	    cal.channel_type as send_channel_type
    </sql>

	<sql id="allCUserCouponOrderDetailColumns">
		co.id,co.tenant_id,co.coupon_id,co.coupon_type,co.c_user_id,co.owner_id,co.b_user_id,co.order_id,co.expired_time,co.coupon_order_status,co.create_date,co.update_date,co.coupon_price,co.verify_type,
		c.type,c.cover_img,c.title,c.sub_title,c.sale_price,c.use_price,c.price,c.unit,c.detail,c.remark,c.valid_type,c.valid_start_date,c.valid_end_date,c.auto_refund
	</sql>

	<resultMap id="CVoResultMap" type="com.iformall.domain.vo.WxCouponOrderCVo">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="tenant_id" jdbcType="BIGINT" property="tenantId" />
		<result column="coupon_id" jdbcType="BIGINT" property="couponId" />
		<result column="coupon_type" jdbcType="INTEGER" property="couponType" />
		<result column="c_user_id" jdbcType="BIGINT" property="cUserId" />
		<result column="owner_id" jdbcType="BIGINT" property="ownerId" />
		<result column="b_user_id" jdbcType="BIGINT" property="bUserId" />
		<result column="order_id" jdbcType="BIGINT" property="orderId" />
		<result column="expired_time" jdbcType="TIMESTAMP" property="expiredTime" />
		<result column="coupon_order_status" jdbcType="INTEGER" property="couponOrderStatus" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
		<result column="coupon_price" jdbcType="INTEGER" property="couponPrice" />
		<result column="verify_type" jdbcType="INTEGER" property="verifyType" />

		<result column="type" jdbcType="INTEGER" property="type" />
		<result column="cover_img" jdbcType="VARCHAR" property="coverImg" />
		<result column="title" jdbcType="VARCHAR" property="title" />
		<result column="sub_title" jdbcType="VARCHAR" property="subTitle" />
		<result column="sale_price" jdbcType="INTEGER" property="salePrice" />
		<result column="use_price" jdbcType="INTEGER" property="usePrice" />
		<result column="detail" jdbcType="VARCHAR" property="detail" />
		<result column="price" jdbcType="INTEGER" property="price" />
		<result column="unit" jdbcType="INTEGER" property="unit" />
		<result column="remark" jdbcType="VARCHAR" property="remark" />
		<result column="valid_type" jdbcType="INTEGER" property="validType" />
		<result column="valid_start_date" jdbcType="TIMESTAMP" property="validStartDate"/>
		<result column="valid_end_date" jdbcType="TIMESTAMP" property="validEndDate"/>
		<result column="auto_refund" jdbcType="INTEGER" property="autoRefund" />

        <result column="send_channel_type" jdbcType="INTEGER" property="sendChannelType" />

		<collection column="{productId=coupon_id}" property="merchantVoList"
               ofType="com.iformall.domain.vo.WxMerchantVo"
               javaType="java.util.List"
               select="com.iformall.mapper.WxCouponMerchantMapper.findMerchantVoList" >
		</collection>
	</resultMap>


	<select id="findListOfCUser" parameterType="com.iformall.domain.vo.WxCouponOrderCVo" resultMap="CVoResultMap">
		select <include refid="allCUserCouponOrderListColumns" />
		from wx_coupon c,wx_coupon_order co
        left join wx_coupon_action_log cal on cal.coupon_order_id = co.id
		where 1=1
		and co.coupon_id = c.id
		and co.coupon_type != 5
		and co.coupon_type != 51
        and co.coupon_type &lt; 100
		<if test="cUserId != null">
			and co.c_user_id = #{cUserId}
		</if>
		<if test="ownerId != null">
			and co.owner_id = #{ownerId}
		</if>
		<if test="tenantId != null">
			and co.tenant_id = #{tenantId}
		</if>
		<if test="couponOrderStatus != null and 0 == couponOrderStatus">
			AND (co.coupon_order_status = #{couponOrderStatus} OR co.coupon_order_status = 100 )
		</if>
		<if test="couponOrderStatus != null and 0 != couponOrderStatus">
			AND co.coupon_order_status = #{couponOrderStatus}
		</if>
		<if test=" null != sortColumns"> order by ${sortColumns} </if>
	</select>




	<select id="findDetailOfCUser" parameterType="java.lang.Long" resultMap="CVoResultMap">
		select <include refid="allCUserCouponOrderDetailColumns" />
		from wx_coupon_order co,wx_coupon c
		where co.coupon_id = c.id
		and co.id = #{id}
	</select>

	<select id="findAvailCouponOrder" parameterType="java.util.Map" resultMap="CVoResultMap">
		select <include refid="allCUserCouponOrderDetailColumns" />
		from wx_coupon_order co
		inner join wx_coupon c on c.id = co.coupon_id
		inner join wx_coupon_merchant cm on cm.product_id = co.coupon_id
		where co.tenant_id = #{tenantId}
		and co.c_user_id = #{cUserId}
		and co.coupon_order_status = 0
		and cm.merchant_id = #{merchantId}
		and co.expired_time > now()
		and c.type in (1, 2, 6)
		union
		select <include refid="allCUserCouponOrderDetailColumns" />
		from wx_coupon_order co
		inner join wx_coupon c on c.id = co.coupon_id
		inner join wx_coupon_merchant cm on cm.product_id = co.coupon_id
		inner join pos_coupon_order_verify  pco on pco.coupon_order_id = co.id
		where co.tenant_id = #{tenantId}
		and co.c_user_id = #{cUserId}
		and co.coupon_order_status = 100
		and cm.merchant_id = #{merchantId}
		and co.expired_time > now()
		and c.type in (1, 2, 6)
		<if test="posOrderId != null">
			and pco.pos_order_id = #{posOrderId}
		</if>
	</select>

	<sql id="allCUserCouponOrderCarListColumns">
		co.id,co.coupon_id,co.expired_time,co.coupon_order_status,co.create_date,co.update_date,co.coupon_price,co.verify_type,
		c.cover_img,c.title,c.sub_title,c.sale_price,c.use_price,c.price,c.unit,
		json_extract(car.vendor_params,'$.id') as coupon_free_id
	</sql>

	<resultMap id="CCarVoResultMap" type="com.iformall.domain.vo.WxCouponOrderCarCVo">

		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="tenant_id" jdbcType="BIGINT" property="tenantId" />
		<result column="coupon_id" jdbcType="BIGINT" property="couponId" />
		<result column="coupon_type" jdbcType="INTEGER" property="couponType" />
		<result column="c_user_id" jdbcType="BIGINT" property="cUserId" />
		<result column="owner_id" jdbcType="BIGINT" property="ownerId" />
		<result column="b_user_id" jdbcType="BIGINT" property="bUserId" />
		<result column="order_id" jdbcType="BIGINT" property="orderId" />
		<result column="expired_time" jdbcType="TIMESTAMP" property="expiredTime" />
		<result column="coupon_order_status" jdbcType="INTEGER" property="couponOrderStatus" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
		<result column="coupon_price" jdbcType="INTEGER" property="couponPrice" />
		<result column="verify_type" jdbcType="INTEGER" property="verifyType" />

		<result column="type" jdbcType="INTEGER" property="type" />
		<result column="cover_img" jdbcType="VARCHAR" property="coverImg" />
		<result column="title" jdbcType="VARCHAR" property="title" />
		<result column="sub_title" jdbcType="VARCHAR" property="subTitle" />
		<result column="sale_price" jdbcType="INTEGER" property="salePrice" />
		<result column="use_price" jdbcType="INTEGER" property="usePrice" />
		<result column="detail" jdbcType="VARCHAR" property="detail" />
		<result column="price" jdbcType="INTEGER" property="price" />
		<result column="remark" jdbcType="VARCHAR" property="remark" />

		<result column="coupon_free_id" jdbcType="INTEGER" property="couponFreeId"/>

        <collection column="{productId=coupon_id}" property="merchantVoList"
                    ofType="com.iformall.domain.vo.WxMerchantVo"
                    javaType="java.util.List"
                    select="com.iformall.mapper.WxCouponMerchantMapper.findMerchantVoList" >
        </collection>

	</resultMap>


	<select id="findCarListOfCUser" parameterType="com.iformall.domain.vo.WxCouponOrderCarCVo" resultMap="CCarVoResultMap">
		select <include refid="allCUserCouponOrderCarListColumns" />
		from wx_coupon_order co, wx_coupon c, wx_coupon_car car
		where co.coupon_id = c.id
		and co.coupon_id = car.id
		and c.type in (5,51)
		<if test="cUserId != null">
			and co.c_user_id = #{cUserId}
		</if>
		<if test="tenantId != null">
			and co.tenant_id = #{tenantId}
		</if>
        <if test="id != null">
            and co.id = #{id}
        </if>
		<if test="couponOrderStatus != null and 0 == couponOrderStatus ">
			AND ( co.coupon_order_status = #{couponOrderStatus} OR co.coupon_order_status = 100 )
		</if>
		<if test="couponOrderStatus != null and 0 != couponOrderStatus ">
			AND co.coupon_order_status = #{couponOrderStatus}
		</if>
		<if test=" null != sortColumns"> order by ${sortColumns} </if>
	</select>

	<resultMap id="CCardVoResultMap" type="com.iformall.domain.vo.WxCardCVo">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="tenant_id" jdbcType="BIGINT" property="tenantId" />
		<result column="coupon_id" jdbcType="BIGINT" property="couponId" />
		<result column="coupon_type" jdbcType="INTEGER" property="couponType" />
		<result column="c_user_id" jdbcType="BIGINT" property="cUserId" />
		<result column="owner_id" jdbcType="BIGINT" property="ownerId" />
		<result column="b_user_id" jdbcType="BIGINT" property="bUserId" />
		<result column="order_id" jdbcType="BIGINT" property="orderId" />
		<result column="expired_time" jdbcType="TIMESTAMP" property="expiredTime" />
		<result column="coupon_order_status" jdbcType="INTEGER" property="couponOrderStatus" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />

		<result column="cover_img" jdbcType="VARCHAR" property="coverImg" />
		<result column="title" jdbcType="VARCHAR" property="title" />
		<result column="sub_title" jdbcType="VARCHAR" property="subTitle" />
		<result column="detail" jdbcType="VARCHAR" property="detail" />
		<result column="unit" jdbcType="INTEGER" property="unit" />
		<result column="remark" jdbcType="VARCHAR" property="remark" />

		<result column="amount" jdbcType="INTEGER" property="amount" />
		<result column="remaining_amount" jdbcType="INTEGER" property="remainingAmount" />
		<result column="support_transfer" jdbcType="INTEGER" property="supportTransfer"/>
		
		<collection column="{productId=coupon_id}" property="merchantVoList"
					ofType="com.iformall.domain.vo.WxMerchantVo"
					javaType="java.util.List"
					select="com.iformall.mapper.WxCouponMerchantMapper.findMerchantVoList" >
		</collection>
		<collection column="{cardId=id}" property="cardSpendList"
					ofType="com.iformall.domain.vo.WxCardSpendVo"
					javaType="java.util.List"
					select="com.iformall.mapper.WxCardSpendMapper.findCardSpendVoList" >
		</collection>
	</resultMap>

	<sql id="allCUserCardListColumns">
		co.id,co.tenant_id,co.coupon_id,co.coupon_type,co.expired_time,co.coupon_order_status,co.create_date,co.update_date,
		c.cover_img,c.title,c.sub_title,c.detail,c.unit,c.remark,
		a.amount,a.remaining_amount,a.support_transfer
	</sql>

	<sql id="allCUserCardDetailColumns">
		co.id,co.tenant_id,co.coupon_id,co.coupon_type,co.c_user_id,co.owner_id,co.b_user_id,co.order_id,co.expired_time,co.coupon_order_status,co.create_date,co.update_date,co.coupon_price,
		c.cover_img,c.title,c.sub_title,c.sale_price,c.price,c.unit,c.detail,c.remark,
		a.amount,a.remaining_amount,a.support_transfer
	</sql>


	<select id="findCardListOfCUser" parameterType="com.iformall.domain.vo.WxCardCVo" resultMap="CCardVoResultMap">
		
		select c.* from (
		select id,tenant_id,0 coupon_id,0 coupon_type,expired_time,`status`as
		coupon_order_status,create_date,update_date,'' cover_img,title,'' sub_title,'' detail,0 unit,
		'' remark,amount,remain_amount as remaining_amount,0 support_transfer
		from wx_card_transfer_info where 1=1
		<if test="tenantId != null">
			and tenant_id = #{tenantId}
		</if>
		<if test="ownerId != null">
			and from_user = #{ownerId}
		</if>
		<choose>
			<when test="couponOrderStatus != null">
				and status = #{couponOrderStatus}
			</when>
			<otherwise>
				and status=8
			</otherwise>
		</choose>
		union
		select
		<include refid="allCUserCardListColumns"/>
		from wx_coupon_order co
		inner join wx_coupon c on co.coupon_id = c.id
		inner join wx_card_info a on co.id=a.id
		where 1=1
		and co.coupon_type = 100
		<if test="tenantId != null">
			and co.tenant_id = #{tenantId}
		</if>
		<if test="cUserId != null">
			and co.c_user_id = #{cUserId}
		</if>
		<if test="ownerId != null">
			and co.owner_id = #{ownerId}
		</if>
		<if test="couponOrderStatus != null">
			and co.coupon_order_status = #{couponOrderStatus}
		</if>
        <if test=" null != statusS ">
            and co.coupon_order_status in
            <foreach collection="statusS" index="index" item="sItem" open="(" separator="," close=")">
                #{sItem}
            </foreach>
        </if>
		) c
		<if test=" null != sortColumns"> order by ${sortColumns} </if>
	</select>

	<select id="findCardDetailOfCUser" parameterType="java.lang.Long" resultMap="CCardVoResultMap">
		select <include refid="allCUserCardDetailColumns" />
		from wx_coupon_order co,wx_coupon c, wx_card_info a
		where co.coupon_id = c.id and co.id = a.id
		and co.id = #{id}
	</select>

</mapper>