<?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.WxCouponCarMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.WxCouponCar">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="tenant_id" jdbcType="BIGINT" property="tenantId" />
		<result column="park_id" jdbcType="BIGINT" property="parkId" />
		<result column="vendor_type" jdbcType="INTEGER" property="vendorType" />
		<result column="vendor_params" jdbcType="VARCHAR" property="vendorParams" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
	</resultMap>
	
	<sql id="allColumns">
	    `id`,`tenant_id`,`park_id`,`vendor_type`,`vendor_params`,`create_date`,`update_date`
	</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 != parkId "> 
		and `park_id` = #{parkId} 
       	
		</if>
	    
		<if test=" null != vendorType "> 
		and `vendor_type` = #{vendorType} 
       	
		</if>    
	    
		<if test=" null != vendorParams "> 
		and `vendor_params`  like concat('%', #{vendorParams},'%')
       	
		</if>    
	    
		<if test=" null != createDate "> 
		and `create_date` = #{createDate} 
       	
		</if>    
	    
		<if test=" null != updateDate "> 
		and `update_date` = #{updateDate} 
       	
		</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.WxCouponCar" resultMap="BaseResultMap">
		select <include refid="allColumns" /> from wx_coupon_car
		<include refid="dynamicWhereConditions" />
	</select>

	<select id="findTemplateAmtCount" parameterType="Long" resultType="Integer">
		select IFNULL(sum(c.inventory),0) as count
		from wx_coupon_car car, wx_coupon c
		where car.id = c.id
		and c.status = 0
		and json_extract(vendor_params,'$.id') = ${value}
	</select>

	<select id="findTemplateAvailCount" parameterType="Long" resultType="Integer">
		select IFNULL(sum(c.remain_inventory),0) as count
		from wx_coupon_car car, wx_coupon c
		where car.id = c.id
		and c.status = 0
		and json_extract(vendor_params,'$.id') = ${value}
	</select>

	<resultMap id="CouponCarMap" type="com.iformall.domain.vo.WxCouponCarVo">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="tenant_id" jdbcType="VARCHAR" property="tenantId" />
		<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="use_limit_quantity" jdbcType="INTEGER" property="useLimitQuantity" />
		<result column="send_type" jdbcType="INTEGER" property="sendType" />
		<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="valid_days" jdbcType="INTEGER" property="validDays" />
		<result column="detail" jdbcType="VARCHAR" property="detail" />
		<result column="price" jdbcType="INTEGER" property="price" />
		<result column="unit" jdbcType="INTEGER" property="unit" />
		<result column="remain_inventory" jdbcType="INTEGER" property="remainInventory" />
		<result column="inventory" jdbcType="INTEGER" property="inventory" />
		<result column="remark" jdbcType="VARCHAR" property="remark" />
		<result column="status" jdbcType="INTEGER" property="status" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
		<result column="business" jdbcType="VARCHAR" property="business" />
		<result column="credit_price" jdbcType="INTEGER" property="creditPrice" />

		<result column="vendor_type" jdbcType="INTEGER" property="vendorType" />
		<result column="vendor_params" jdbcType="VARCHAR" property="vendorParams" />
	</resultMap>

	<sql id="CouponCarColumns">
	    c.id,c.tenant_id,c.type,c.cover_img,c.title,c.sub_title,c.sale_price,c.use_price,c.use_limit_quantity,c.send_type,c.valid_type,c.valid_start_date,c.valid_end_date,c.valid_days,c.detail,c.price,c.unit,c.remain_inventory,c.inventory,c.remark,c.status,c.create_date,c.update_date,c.business,
		car.vendor_type, car.vendor_params,c.credit_price
	</sql>
	<select id="findVoDetail" parameterType="java.lang.Long" resultMap="CouponCarMap">
		select <include refid="CouponCarColumns" />
		from
		wx_coupon c,wx_coupon_car car
		where c.id = car.id
		and c.id = #{id}
	</select>
	
	
	
</mapper>
