<?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.WxCardInfoMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.WxCardInfo">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="tenant_id" jdbcType="VARCHAR" property="tenantId" />
        <result column="coupon_id" jdbcType="BIGINT" property="couponId" />
		<result column="transaction_id" jdbcType="VARCHAR" property="transactionId" />
		<result column="amount" jdbcType="INTEGER" property="amount" />
        <result column="sale_amount" jdbcType="INTEGER" property="saleAmount" />
		<result column="remaining_amount" jdbcType="INTEGER" property="remainingAmount" />
		<result column="service_fee_amount" jdbcType="INTEGER" property="serviceFeeAmount" />
		<result column="share_fee_amount" jdbcType="INTEGER" property="shareFeeAmount" />
		<result column="remaining_share_fee_amount" jdbcType="INTEGER" property="remainingShareFeeAmount" />
        <result column="rate_amount" jdbcType="INTEGER" property="rateAmount" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
		<result column="support_transfer" jdbcType="INTEGER" property="supportTransfer"/>
	
	</resultMap>
	
	<sql id="allColumns">
	    `id`,`tenant_id`,`coupon_id`,`transaction_id`,
	    `amount`,`sale_amount`,`remaining_amount`,`service_fee_amount`,
	    `share_fee_amount`,`remaining_share_fee_amount`, `rate_amount`,
	    `create_date`,`update_date`,`support_transfer`
	</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 != transactionId ">
		    and `transaction_id` = #{transactionId}
		</if>    
		<if test=" null != amount ">
		    and `amount` = #{amount}
		</if>
        <if test=" null != saleAmount ">
            and `sale_amount` = #{saleAmount}
        </if>
		<if test=" null != remainingAmount ">
		    and `remaining_amount` = #{remainingAmount}
		</if>    
		<if test=" null != serviceFeeAmount ">
		    and `service_fee_amount` = #{serviceFeeAmount}
		</if>    
		<if test=" null != shareFeeAmount ">
		    and `share_fee_amount` = #{shareFeeAmount}
		</if>    
		<if test=" null != remainingShareFeeAmount ">
		    and `remaining_share_fee_amount` = #{remainingShareFeeAmount}
		</if>
        <if test=" null != rateAmount ">
            and `rate_amount` = #{rateAmount}
        </if>
		<if test=" null != createDate ">
		    and `create_date` = #{createDate}
		</if>    
		<if test=" null != updateDate ">
		    and `update_date` = #{updateDate}
		</if>
		<if test=" null != startdate and  null!=enddate">
			and `create_date` between #{startdate} and #{enddate}
		</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>

	<resultMap id="VoBaseResultMap" type="com.iformall.domain.vo.WxCardVo">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="tenant_id" jdbcType="VARCHAR" property="tenantId" />
		<result column="coupon_id" jdbcType="BIGINT" property="couponId" />
		<result column="transaction_id" jdbcType="VARCHAR" property="transactionId" />
		<result column="amount" jdbcType="INTEGER" property="amount" />
		<result column="sale_amount" jdbcType="INTEGER" property="saleAmount" />
		<result column="remaining_amount" jdbcType="INTEGER" property="remainingAmount" />
		<result column="service_fee_amount" jdbcType="INTEGER" property="serviceFeeAmount" />
		<result column="share_fee_amount" jdbcType="INTEGER" property="shareFeeAmount" />
		<result column="remaining_share_fee_amount" jdbcType="INTEGER" property="remainingShareFeeAmount" />
        <result column="rate_amount" jdbcType="INTEGER" property="rateAmount" />
		<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="cover_picture" jdbcType="VARCHAR" property="coverPicture" />
        <result column="detail_picture" jdbcType="VARCHAR" property="detailPicture" />
		<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="sale_price" jdbcType="INTEGER" property="salePrice"/>
		<result column="price" jdbcType="INTEGER" property="price"/>
		<result column="subsidy_type" jdbcType="INTEGER" property="subsidyType"/>
		<result column="subsidy_num" jdbcType="INTEGER" property="subsidyNum"/>

		<result column="c_user_id" jdbcType="BIGINT" property="cuserId" />
		<result column="owner_id" jdbcType="BIGINT" property="ownerId" />
		<result column="expired_time" jdbcType="TIMESTAMP" property="expiredTime" />
		<result column="status" jdbcType="INTEGER" property="status"/>

		<result column="c_nick_name" jdbcType="VARCHAR" property="cnickName"/>
		<result column="o_nick_name" jdbcType="VARCHAR" property="onickName"/>
		<result column="ou_phone" jdbcType="VARCHAR" property="ouPhone"/>

	</resultMap>

	<sql id="allVoColumns">
	    ci.`id`,ci.`tenant_id`,ci.`coupon_id`,ci.`transaction_id`,
	    ci.`amount`,ci.`sale_amount`,ci.`remaining_amount`,ci.`service_fee_amount`,
	    ci.`share_fee_amount`,ci.`remaining_share_fee_amount`, ci.`rate_amount`,
	    ci.`create_date`,ci.`update_date`,
	    c.`cover_img`, c.`cover_picture`,c.`detail_picture`,c.`title`, c.`sub_title`, c.`detail`, c.`unit`, c.`remark`, c.`sale_price`, c.`price`, c.`subsidy_type`, c.`subsidy_num`,
	    co.`c_user_id`, co.`owner_id`, co.`expired_time`, co.`coupon_order_status` as status,
	    cu.`nick_name` as c_nick_name, ou.`nick_name` as o_nick_name, ou.`phone` as ou_phone
	</sql>

	<sql id="dynamicVoWhereConditions">
		where 1 = 1
		<if test=" null != id ">
			and ci.`id` = #{id}
		</if>
		<if test=" null != tenantId ">
			and ci.`tenant_id` = #{tenantId}
		</if>
		<if test=" null != couponId ">
			and ci.`coupon_id` = #{couponId}
		</if>
		<if test=" null != transactionId ">
			and ci.`transaction_id` = #{transactionId}
		</if>
		<if test=" null != amount ">
			and ci.`amount` = #{amount}
		</if>
		<if test=" null != saleAmount ">
			and ci.`sale_amount` = #{saleAmount}
		</if>
		<if test=" null != remainingAmount ">
			and ci.`remaining_amount` = #{remainingAmount}
		</if>
		<if test=" null != serviceFeeAmount ">
			and ci.`service_fee_amount` = #{serviceFeeAmount}
		</if>
		<if test=" null != shareFeeAmount ">
			and ci.`share_fee_amount` = #{shareFeeAmount}
		</if>
		<if test=" null != remainingShareFeeAmount ">
			and ci.`remaining_share_fee_amount` = #{remainingShareFeeAmount}
		</if>
        <if test=" null != rateAmount ">
            and ci.`rate_amount` = #{rateAmount}
        </if>
		<if test=" null != createDate ">
			and ci.`create_date` = #{createDate}
		</if>
		<if test=" null != updateDate ">
			and ci.`update_date` = #{updateDate}
		</if>
		<if test=" null != status ">
			and co.`coupon_order_status` = #{status}
		</if>
		<if test=" null != ouPhone and '' != ouPhone">
			and ou.`phone` = #{ouPhone}
		</if>
		<if test=" null != startdate and  null!=enddate">
			and ci.`create_date` between #{startdate} and #{enddate}
		</if>
		<if test=" null != onickName and ''!=onickName">
			and ou.`nick_name` like concat('%',#{onickName},'%')
		</if>
		<if test=" null != statusS ">
			and co.`coupon_order_status` in
			<foreach collection="statusS" index="index" item="sItem" open="(" separator="," close=")">
				#{sItem}
			</foreach>
		</if>
		<if test=" null != ids ">
			and ci.id in
			<foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
				#{idItem}
			</foreach>
		</if>
		order by ci.id desc
	</sql>

	<select id="findVoList" parameterType="com.iformall.domain.vo.WxCardVo" resultMap="VoBaseResultMap">
		select <include refid="allVoColumns" />
		from wx_card_info ci
		left join wx_coupon c on c.id = ci.coupon_id
		left join wx_coupon_order co on co.id = ci.id
		left join wx_c_user cu on cu.id = co.c_user_id
		left join wx_c_user ou on ou.id = co.owner_id
		<include refid="dynamicVoWhereConditions" />
	</select>
	
	<update id="updateTransferStatusByCouponId" parameterType="Long">
		update wx_card_info set support_transfer=0,update_date=now() where support_transfer=1 and coupon_id=#{couponId}
	</update>
	
</mapper>
