<?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.WxCouponMapper">
    <resultMap id="BaseResultMap" type="com.iformall.domain.po.WxCoupon">
        <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="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="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="INTEGER" property="business"/>
        <result column="sub_business" jdbcType="INTEGER" property="subBusiness"/>
        <result column="support_transfer" jdbcType="INTEGER" property="supportTransfer"/>
        <result column="subsidy_type" jdbcType="INTEGER" property="subsidyType"/>
        <result column="subsidy_num" jdbcType="INTEGER" property="subsidyNum"/>
        <result column="press_limit_num" jdbcType="INTEGER" property="pressLimitNum"/>
        <result column="press_limit_hours" jdbcType="INTEGER" property="pressLimitHours"/>
        <result column="auto_refund" jdbcType="INTEGER" property="autoRefund"/>
        <result column="credit_price" jdbcType="INTEGER" property="creditPrice"/>
        <result column="credit_refund" jdbcType="INTEGER" property="creditRefund"/>
        <result column="is_del" property="isDel"/>
        <result column="put_apply_status" property="putApplyStatus"/>
        <result column="stock_apply_status" property="stockApplyStatus"/>
        <result column="cancle_apply_status" property="cancleApplyStatus"/>
        <result column="conditions" jdbcType="VARCHAR" property="conditions"/>
        <result column="approval_type"  property="approvalType"/>
        <result column="password_support"  property="passwordSupport"/>
    </resultMap>
    <resultMap id="statisMap" type="com.iformall.domain.vo.WxCouponStatisVo">
        <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="cover_picture" jdbcType="VARCHAR" property="coverPicture"/>
        <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="INTEGER" property="business"/>
        <result column="sub_business" jdbcType="INTEGER" property="subBusiness"/>
        <result column="support_transfer" jdbcType="INTEGER" property="supportTransfer"/>
        <result column="subsidy_type" jdbcType="INTEGER" property="subsidyType"/>
        <result column="subsidy_num" jdbcType="INTEGER" property="subsidyNum"/>
        <result column="press_limit_num" jdbcType="INTEGER" property="pressLimitNum"/>
        <result column="press_limit_hours" jdbcType="INTEGER" property="pressLimitHours"/>
        <result column="auto_refund" jdbcType="INTEGER" property="autoRefund"/>

        <result column="sale_count" property="saleCount"/>
        <result column="transfer_count" property="transferCount"/>
        <result column="sale_amount" property="saleAmount"/>
        <result column="sum_payment" property="sumPayment"/>
        <result column="press_count" property="pressSendCount"/>
        <result column="press_succ_count" property="pressSuccCount"/>
        <result column="join_count" property="pressJoinCount"/>
        <result column="sum_subsidy" property="sumSubsidy"/>
        <result column="payment_rate" property="paymentRate"/>
        <result column="change_rate" property="changeRate"/>
    </resultMap>

    <sql id="allColumns">
        `id`,`tenant_id`,`type`,`cover_img`,`cover_picture`,`detail_picture`,`title`,`sub_title`,`sale_price`,`use_price`,`use_limit_quantity`,`send_type`,
        `valid_type`,`valid_start_date`,`valid_end_date`,`valid_days`,`detail`,`price`,`unit`,`remain_inventory`,`inventory`,
        `remark`,`status`,`create_date`,`update_date`,`business`,`sub_business`,`support_transfer`,`subsidy_num`,`subsidy_type`,
        `press_limit_num`, `press_limit_hours`, `auto_refund`,`credit_price`,`credit_refund`,`put_apply_status`,`stock_apply_status`,`cancle_apply_status`,`conditions`,approval_type
    </sql>

    <sql id="statusColumns">
        `id`,`remain_inventory`,`status`
    </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 != type ">
            and `type` = #{type}
        </if>

        <if test=" null != coverImg ">
            and `cover_img` like concat('%', #{coverImg},'%')
        </if>

        <if test=" null != title and ''!=title">
            and `title` like concat('%', #{title},'%')
        </if>

        <if test=" null != subTitle ">
            and `sub_title` like concat('%', #{subTitle},'%')
        </if>

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

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

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

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

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

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

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

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

        <if test=" null != detail ">
            and `detail` like concat('%', #{detail},'%')
        </if>

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

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

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

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

        <if test=" null != remark ">
            and `remark` like concat('%', #{remark},'%')
        </if>

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

        <if test=" null != startdate and  null!=enddate">
            and `create_date` between #{startdate} and #{enddate}
        </if>

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

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

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

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

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

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

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

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

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

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

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

        <if test=" 1 == filterCanPut ">
            and `credit_refund` = #{creditRefund}
        </if>

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

        <if test=" null != notinTypes ">
            and type not in
            <foreach collection="notinTypes" index="index" item="typeItem" open="(" separator="," close=")">
                #{typeItem}
            </foreach>
        </if>

        <if test=" null != sortColumns">order by ${sortColumns}</if>
    </sql>

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

    <update id="updateStockAndValidDate" parameterType="com.iformall.domain.po.WxCoupon">
        update wx_coupon
        SET
          `remain_inventory` = #{remainInventory}, `inventory` = #{inventory},
          `valid_type` = #{validType}, `valid_end_date` = #{validEndDate}, `valid_days` = #{validDays}
        WHERE 1=1
        <if test=" null != tenantId ">
            and `tenant_id` = #{tenantId}
        </if>
        and id = #{id} and `remain_inventory` = #{orgRemainInventory} and `inventory` = #{orgInventory}
    </update>


    <sql id="dynamicWhereConditionsForCoupon">
        where is_del = 0
        <if test=" null != targetAd ">
            and 0 = (select count(*) from wx_coupon_channel cc
            where c.`id` = cc.coupon_id
            and cc.status = 0
            and cc.target_ad = #{targetAd})
        </if>

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

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

        <if test=" null == type and null == types">
            and c.`type` &lt; 7
        </if>

        <if test=" null != type ">
            and c.`type` = #{type}
        </if>

        <if test=" null != coverImg ">
            and c.`cover_img` like concat('%', #{coverImg},'%')
        </if>

        <if test=" null != title and ''!=title">
            and c.`title` like concat('%', #{title},'%')
        </if>

        <if test=" null != subTitle ">
            and c.`sub_title` like concat('%', #{subTitle},'%')
        </if>

        <if test=" null != salePrice ">
            and c.`sale_price` = #{salePrice}
        </if>

        <if test=" null != usePrice ">
            and c.`use_price` = #{usePrice}
        </if>

        <if test=" null != useLimitQuantity ">
            and c.`use_limit_quantity` = #{useLimitQuantity}
        </if>

        <if test=" null != sendType ">
            and c.`send_type` = #{sendType}
        </if>

        <if test=" null != validType ">
            and c.`valid_type` = #{validType}
        </if>

        <if test=" null != validStartDate ">
            and c.`valid_start_date` = #{validStartDate}
        </if>

        <if test=" null != validEndDate ">
            and c.`valid_end_date` = #{validEndDate}
        </if>

        <if test=" null != validDays ">
            and c.`valid_days` = #{validDays}
        </if>

        <if test=" null != detail ">
            and c.`detail` like concat('%', #{detail},'%')
        </if>

        <if test=" null != price ">
            and c.`price` = #{price}
        </if>

        <if test=" null != unit ">
            and c.`unit` = #{unit}
        </if>

        <if test=" null != remainInventory ">
            and c.`remain_inventory` = #{remainInventory}
        </if>

        <if test=" null != inventory ">
            and c.`inventory` = #{inventory}
        </if>

        <if test=" null != remark ">
            and c.`remark` like concat('%', #{remark},'%')
        </if>

        <if test=" null != status">
            and c.`status` = #{status}
        </if>

        <if test=" null != startdate and  null!=enddate">
            and c.`create_date` between #{startdate} and #{enddate}
        </if>

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

        <if test=" null != business ">
            and c.`business` = #{business}
        </if>

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

        <if test=" null != supportTransfer ">
            and c.`support_transfer` = #{supportTransfer}
        </if>

        <if test=" null != subsidyType ">
            and c.`subsidy_type` = #{subsidyType}
        </if>

        <if test=" null != subsidyNum ">
            and c.`subsidy_num` = #{subsidyNum}
        </if>

        <if test=" null != pressLimitNum ">
            and c.`press_limit_num` = #{pressLimitNum}
        </if>

        <if test=" null != pressLimitHours ">
            and c.`press_limit_hours` = #{pressLimitHours}
        </if>

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

        <!--如果配置了审批模板，需要过滤投放审批状态-->
        <if test="0 != filterCanPut or null ==filterCanPut ">
            <if test=" null != pressModelId and 0l != pressModelId">
                and if(type=8,put_apply_status =2 or c.id in(select coupon_id from wx_coupon_channel where status=0 and tenant_id=#{tenantId}) ,1=1)
            </if>
            <if test=" null != groupModelId and 0l != groupModelId">
                and if(type=9,put_apply_status =2 or c.id in(select coupon_id from wx_coupon_channel where status=0 and tenant_id=#{tenantId}),1=1)
            </if>
            <if test=" null != cardModelId and 0l != cardModelId">
                and if(type=100,put_apply_status =2 or c.id in(select coupon_id from wx_coupon_channel where status=0 and tenant_id=#{tenantId}),1=1)
            </if>
            <if test=" null != couponModelId and 0l != couponModelId">
                and if(type !=8 and type !=9 and type!=100,put_apply_status =2 or c.id in(select coupon_id from wx_coupon_channel where status=0 and tenant_id=#{tenantId}),1=1)
            </if>
        </if>

        <if test=" null != ids ">
            and c.id in
            <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
                #{idItem}
            </foreach>
        </if>

        <if test=" null != types ">
            and c.type in
            <foreach collection="types" index="index" item="typesItem" open="(" separator="," close=")">
                #{typesItem}
            </foreach>
        </if>

        <if test=" null != sortColumns">order by ${sortColumns}</if>
    </sql>

    <select id="findCouponList" parameterType="com.iformall.domain.po.WxCoupon" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from wx_coupon c
        <include refid="dynamicWhereConditionsForCoupon"/>

        <if test="null != limitStart and null != limitEnd">
            limit #{limitStart},#{limitEnd}
        </if>
    </select>

    <select id="findCouponListCount" parameterType="com.iformall.domain.po.WxCoupon" resultType="Integer">
        select
        count(*)
        from wx_coupon c
        <include refid="dynamicWhereConditionsForCoupon"/>
    </select>




    <resultMap id="WxCouponCVoMap" type="com.iformall.domain.vo.WxCouponCVo">
        <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="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="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="INTEGER" property="business"/>
        <result column="sub_business" jdbcType="INTEGER" property="subBusiness"/>
        <result column="support_transfer" jdbcType="INTEGER" property="supportTransfer"/>
        <result column="subsidy_type" jdbcType="INTEGER" property="subsidyType"/>
        <result column="subsidy_num" jdbcType="INTEGER" property="subsidyNum"/>
        <result column="press_limit_num" jdbcType="INTEGER" property="pressLimitNum"/>
        <result column="press_limit_hours" jdbcType="INTEGER" property="pressLimitHours"/>
        <result column="conditions" jdbcType="VARCHAR" property="conditions"/>

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

    <resultMap id="WxCouponCVoMapL" type="com.iformall.domain.vo.WxCouponCVo">
        <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="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="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="INTEGER" property="business"/>
        <result column="sub_business" jdbcType="INTEGER" property="subBusiness"/>
        <result column="support_transfer" jdbcType="INTEGER" property="supportTransfer"/>
        <result column="subsidy_type" jdbcType="INTEGER" property="subsidyType"/>
        <result column="subsidy_num" jdbcType="INTEGER" property="subsidyNum"/>
        <result column="press_limit_num" jdbcType="INTEGER" property="pressLimitNum"/>
        <result column="press_limit_hours" jdbcType="INTEGER" property="pressLimitHours"/>
        <result column="conditions" jdbcType="VARCHAR" property="conditions"/>
    </resultMap>

    <select id="findVoDetail" parameterType="java.lang.Long" resultMap="WxCouponCVoMap">
        select
        <include refid="allColumns"/>
        from wx_coupon
        where id = #{id}
    </select>

    <select id="findVoStatusDetail" parameterType="java.lang.Long" resultMap="WxCouponCVoMapL">
        select id, remain_inventory, status
        from wx_coupon
        where id = #{id}
    </select>

    <update id="reduceInventory">
        update wx_coupon SET remain_inventory = remain_inventory - #{number} where id = #{id} and remain_inventory - #{number} &gt;= 0
    </update>

    <update id="backInventory">
        update wx_coupon SET remain_inventory = remain_inventory + #{number} where id = #{id} and remain_inventory + #{number} &lt;= inventory
    </update>

    <update id="offExpiriedCouponByValidDate">
        update wx_coupon SET status = 1, update_date = now()
        where status = 0 and valid_type = 1 and valid_end_date &lt; now()
    </update>

    <update id="offExpiriedCouponByMerchantStatus">
        update wx_coupon c
        SET c.status = 1, c.update_date = now()
        where c.status = 0 and
        (select count(*) from wx_coupon_merchant cm, wx_merchant m
        where m.id = cm.merchant_id
        and c.id = cm.product_id
        and cm.status = 0
        and m.status = 1) = 0
    </update>

    <select id="findSaleMoneyByDate" parameterType="com.iformall.domain.po.WxCoupon"
            resultType="com.iformall.domain.vo.WxCouponStatisVo">
        select DATE_FORMAT(ci.create_date,'%Y-%m-%d') xtime,sum(round(ci.sale_amount/100,2)) xcount from wx_card_info ci
        left join wx_coupon c on c.id = ci.coupon_id
        where c.type = 100
        and ci.create_date between #{startdate} and #{enddate}
        group by xtime
    </select>

    <select id="findPaymentByDate" parameterType="com.iformall.domain.po.WxCoupon"
            resultType="com.iformall.domain.vo.WxCouponStatisVo">
        select DATE_FORMAT(wcs.create_date,'%Y-%m-%d') xtime,sum(round(wcs.real_payment/100,2)) xcount from wx_card_info ci
        left join wx_coupon c on c.id = ci.coupon_id
        left join wx_card_spend wcs on wcs.card_id = ci.id
        where c.type = 100
        and wcs.create_date between #{startdate} and #{enddate}
        group by xtime
    </select>

    <select id="findSaleCardCount" parameterType="com.iformall.domain.po.WxCoupon" resultType="Long">
        select count(co.id)
        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
        where c.type = 100
        and co.create_date between #{startdate} and #{enddate}
    </select>

    <select id="findTranCardCount" parameterType="com.iformall.domain.po.WxCoupon" resultType="Long">
        select count(distinct wcs.coupon_order_id) from wx_card_info ci
        left join wx_coupon c on c.id = ci.coupon_id
        left join wx_card_transfer_info wcs on wcs.coupon_order_id = ci.id
        where c.type = 100 and wcs.create_date between #{startdate} and #{enddate}
    </select>

    <select id="findSubsidyMoney" parameterType="com.iformall.domain.po.WxCoupon" resultType="String">
        select sum(round(sc.subsidy/100,2)) from wx_card_info ci
        left join wx_coupon c on c.id = ci.coupon_id
        left join wx_merchant_subsidy sc on sc.coupon_order_id = ci.id
        where c.type = 100 and sc.create_date between #{startdate} and #{enddate}
    </select>

    <select id="findCountData" parameterType="com.iformall.domain.po.WxCoupon" resultMap="statisMap">
        select c.*,
        IFNULL(round((select sum(wcs.real_payment) from wx_card_info ci
        left join wx_coupon wc on wc.id = ci.coupon_id
        left join wx_card_spend wcs on wcs.card_id = ci.id
        where wc.id = c.id)/(select sum(ci.sale_amount) from wx_card_info ci
        left join wx_coupon wc on wc.id = ci.coupon_id
        where wc.id = c.id)*100,2),0) payment_rate,

        (select count(wco.id) from wx_coupon wc
        left join wx_coupon_order wco on wc.id = wco.coupon_id
        where wc.id = c.id) as sale_count,

        (select count(distinct wcs.coupon_order_id) from wx_card_info ci
        left join wx_coupon wc on wc.id = ci.coupon_id
        left join wx_card_transfer_info wcs on wcs.coupon_order_id = ci.id
        where wc.id = c.id) as transfer_count,

        (select sum(ci.sale_amount) from wx_card_info ci
        left join wx_coupon wc on wc.id = ci.coupon_id
        where wc.id = c.id) as sale_amount,

        (select sum(wcs.real_payment) from wx_card_info ci
        left join wx_coupon wc on wc.id = ci.coupon_id
        left join wx_card_spend wcs on wcs.card_id = ci.id
        where wc.id = c.id) as sum_payment,

        (select sum(sc.subsidy) from wx_card_info ci
        left join wx_coupon wc on wc.id = ci.coupon_id
        left join wx_merchant_subsidy sc on sc.coupon_order_id = ci.id
        where wc.id = c.id) as sum_subsidy

        from wx_coupon c where c.type = 100
        <if test="title != null and title !=''">
            and c.title like concat('%', #{title},'%')
        </if>
        <if test="id != null">
            and c.id = #{id}
        </if>
        <if test=" null != sortColumns">order by ${sortColumns}</if>
        <if test=" null == sortColumns">order by c.status asc,c.create_date desc</if>

    </select>

    <select id="findCouponData" parameterType="com.iformall.domain.po.WxCoupon" resultMap="statisMap">
        select c.*,
        IFNULL(round((select count(wco.id) from wx_coupon wc,wx_coupon_order wco where wco.coupon_id = wc.id and
        wco.coupon_order_status = 1 and wc.id = c.id)/(select count(wco.id) from wx_coupon_order wco where wco.coupon_id
        = c.id)*100,2),0) payment_rate,
        (select count(wco.id) from wx_coupon_order wco where wco.coupon_id = c.id) as sale_count,
        (select count(wco.id) from wx_coupon wc,wx_coupon_order wco where wco.coupon_id = wc.id and
        wco.coupon_order_status = 1 and wc.id = c.id) as sum_payment,
        (select count(wco.id) from wx_coupon wc,wx_coupon_order wco where wco.coupon_id = wc.id and
        wco.coupon_order_status = 3 and wc.id = c.id) as backpay_count,
        (select sum(sc.subsidy) from wx_coupon wc
        left join wx_coupon_order wo on wo.coupon_id = wc.id
        left join wx_merchant_subsidy sc on sc.coupon_order_id = wo.id
        where wc.id = c.id) as sum_subsidy
        from wx_coupon c where c.type not in(8,9,100)
        <if test=" 1 == type ">
            and c.sale_price > 0
        </if>
        <if test=" 2 == type ">
            and c.sale_price = 0
        </if>
        <if test="title != null and title != ''">
            and c.title like concat('%', #{title},'%')
        </if>
        <if test="id != null">
            and c.id = #{id}
        </if>
        <if test=" null != sortColumns">order by ${sortColumns}</if>
        <if test=" null == sortColumns">order by c.status asc,c.create_date desc</if>
    </select>

    <select id="findPressData" parameterType="com.iformall.domain.po.WxCoupon" resultMap="statisMap">
        select c.id, c.`cover_img`,c.`title`,c.`sale_price`,c.`price`,
        ifnull(op.press_count,0) as press_count,
        ifnull(o.press_succ_count,0) as press_succ_count,
        ifnull(op2.join_count,0) as join_count,
        ifnull(round(o.press_succ_count/op.press_count*100,2),0) as change_rate,
        ifnull(round(cop.xCount/op.press_count*100,2),0) as payment_rate
        FROM wx_coupon c
        left join (SELECT count(id) as press_count, coupon_id FROM wx_order_press WHERE `tenant_id` = #{tenantId} AND `first` = 1 group by coupon_id) op on op.coupon_id = c.id
        left join (SELECT count(id) as press_succ_count, product_id FROM wx_order WHERE `tenant_id` = #{tenantId} AND order_status = 1 group by product_id) o on o.product_id = c.id
        left join (SELECT count(id) as join_count, coupon_id FROM wx_order_press WHERE `tenant_id` = #{tenantId} group by coupon_id) op2 on op2.coupon_id = c.id
        left join (SELECT count(id) as xCount, coupon_id FROM wx_coupon_order  WHERE `tenant_id` = #{tenantId} AND coupon_order_status = 1 group by coupon_id) cop on cop.coupon_id = c.id
        where c.type = 8
        <if test="tenantId != null">
            and c.tenant_id=#{tenantId}
        </if>
        <if test="id != null">
            and c.id = #{id}
        </if>
        <if test="title != null and title != ''">
            and c.title like concat('%', #{title},'%')
        </if>
        <if test=" null != startdate and  null!=enddate">
            and c.`create_date` between #{startdate} and #{enddate}
        </if>

        <if test=" null != sortColumns">order by ${sortColumns}</if>
        <if test=" null == sortColumns">order by c.status asc,c.create_date desc</if>
    </select>

    <select id="findCouponOrderCount" parameterType="com.iformall.domain.po.WxCoupon"
            resultType="com.iformall.domain.vo.WxCouponStatisVo">
        select DATE_FORMAT(wco.create_date,'%Y-%m-%d') xtime,count(wco.id) xcount from wx_coupon c
        left join wx_coupon_order wco on c.id = wco.coupon_id
        where c.type not in(8,9,100)
        and wco.create_date between #{startdate} and #{enddate}
        group by xtime
    </select>


    <select id="findCouponPaymentCount" parameterType="com.iformall.domain.po.WxCoupon"
            resultType="com.iformall.domain.vo.WxCouponStatisVo">
        select DATE_FORMAT(wco.create_date,'%Y-%m-%d') xtime,count(wco.id) xcount from wx_coupon c,wx_coupon_order wco
        where c.type not in(8,9,100)
        and wco.coupon_id = c.id and wco.coupon_order_status = 1
        and wco.update_date between #{startdate} and #{enddate}
        group by xtime
    </select>

    <select id="priceCouponSaleCount" parameterType="com.iformall.domain.po.WxCoupon"
            resultType="com.iformall.domain.vo.WxCouponStatisVo">
        select DATE_FORMAT(wco.create_date,'%Y-%m-%d') xtime,count(wco.id) xcount from wx_coupon c,wx_coupon_order wco
        where c.type not in(8,9,100)
        and wco.coupon_id = c.id and wco.coupon_price > 0
        and wco.create_date between #{startdate} and #{enddate}
        group by xtime
    </select>

    <select id="priceCouponPaymentCount" parameterType="com.iformall.domain.po.WxCoupon"
            resultType="com.iformall.domain.vo.WxCouponStatisVo">
        select DATE_FORMAT(wco.create_date,'%Y-%m-%d') xtime,count(wco.id) xcount from wx_coupon c,wx_coupon_order wco
        where c.type not in(8,9,100)
        and wco.coupon_id = c.id and wco.coupon_order_status = 1 and wco.coupon_price > 0
        and wco.update_date between #{startdate} and #{enddate}
        group by xtime
    </select>

    <select id="findSumPriceSaleMoney" parameterType="com.iformall.domain.po.WxCoupon" resultType="String">
        select sum(round(c.sale_price/100,2)) from wx_coupon c,wx_coupon_order wco
        where wco.coupon_id = c.id and wco.coupon_price > 0
        and wco.create_date between #{startdate} and #{enddate}
    </select>

    <select id="findSumPricePayMent" parameterType="com.iformall.domain.po.WxCoupon" resultType="String">
        select sum(round(c.sale_price/100,2)) from wx_coupon c,wx_coupon_order wco
        where wco.coupon_id = c.id and wco.coupon_price > 0 and wco.coupon_order_status = 1
        and wco.update_date between #{startdate} and #{enddate}
    </select>

    <select id="findSumSubsidyMoney" parameterType="com.iformall.domain.po.WxCoupon" resultType="String">
        select sum(round(sc.subsidy/100,2)) from wx_coupon c
        left join wx_coupon_order wo on wo.coupon_id = c.id
        left join wx_merchant_subsidy sc on sc.coupon_order_id = wo.id
        where c.type not in(8,9,100) and wo.coupon_order_status = 1
        and sc.create_date between #{startdate} and #{enddate}
    </select>


    <!--砍价-->
    <select id="pressSendHistory" parameterType="com.iformall.domain.po.WxCoupon"
            resultType="com.iformall.domain.vo.WxCouponStatisVo">
        select DATE_FORMAT(wop.create_date,'%Y-%m-%d') xtime,count(wop.id) xcount from wx_coupon wc
        left join wx_order wo on wo.product_id = wc.id
        left join wx_order_press wop on wop.order_id = wo.id
        where wc.type = 8 and wop.first = 1 and
        wop.create_date between #{startdate} and #{enddate}
        group by xtime
    </select>
    <select id="pressSuccHistory" parameterType="com.iformall.domain.po.WxCoupon"
            resultType="com.iformall.domain.vo.WxCouponStatisVo">
        select DATE_FORMAT(wo.create_date,'%Y-%m-%d') xtime,count(wo.id) xcount from wx_coupon wc
        left join wx_order wo on wo.product_id = wc.id
        where wc.type = 8 and wo.order_status = 1
        and wo.create_date between #{startdate} and #{enddate}
        group by xtime
    </select>
    <select id="pressPaymentHistory" parameterType="com.iformall.domain.po.WxCoupon"
            resultType="com.iformall.domain.vo.WxCouponStatisVo">
        select DATE_FORMAT(wco.create_date,'%Y-%m-%d') xtime,count(wco.id) xcount from wx_coupon wc
        left join wx_coupon_order wco on wco.coupon_id = wc.id
        where wc.type = 8 and wco.coupon_order_status = 1
        and wco.create_date between #{startdate} and #{enddate}
        group by xtime
    </select>
    <select id="pressSuccRate" parameterType="com.iformall.domain.po.WxCoupon" resultType="java.lang.String">
        select
        round(
        (select count(wo.id) from wx_coupon wc
        left join wx_order wo on wo.product_id = wc.id
        where wo.order_status = 1 and wc.type = 8
        and wo.create_date between #{startdate} and #{enddate}
        and wo.tenant_id = #{tenantId}
        )
        /
        (select count(wop.id) from wx_coupon wc
        left join wx_order wo on wo.product_id = wc.id
        left join wx_order_press wop on wop.order_id = wo.id
        where wop.first = 1 and wc.type = 8
        and wop.create_date between #{startdate} and #{enddate}
        and wo.tenant_id = #{tenantId} and wop.tenant_id = #{tenantId})
        * 100
        ,2)
    </select>
    <select id="sumJoin" parameterType="com.iformall.domain.po.WxCoupon" resultType="Long">
        select count(wop.id) from wx_coupon wc
        left join wx_order wo on wo.product_id = wc.id
        left join wx_order_press wop on wop.order_id = wo.id
        where wc.type = 8
        and wop.create_date between #{startdate} and #{enddate}
    </select>

    <select id="priceCouponCancelCount" parameterType="com.iformall.domain.po.WxCoupon" resultType="Long">
        select count(c.id) from wx_coupon c
        left join wx_coupon_order wco on c.id = wco.coupon_id
        where c.type not in(8,9,100)
        and wco.create_date between #{startdate} and #{enddate}
        and wco.coupon_order_status = 3
    </select>

    <select id="findSaleCardCountHistory" parameterType="com.iformall.domain.po.WxCoupon"
            resultType="com.iformall.domain.vo.WxCouponStatisVo">
        select DATE_FORMAT(co.create_date,'%Y-%m-%d') xtime,count(co.id) xcount
        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
        where c.type = 100
        and co.create_date between #{startdate} and #{enddate}
        group by xtime
    </select>

    <select id="findCouponByTopic" parameterType="com.iformall.domain.po.WxTopic"
            resultType="com.iformall.domain.po.WxCoupon">
       select ct.id as id,wc.id as couponId,
        wc.tenant_id,wc.`type`,wc.`cover_img`,wc.`cover_picture`,wc.`detail_picture`,wc.`title`,wc.`sub_title`,wc.`sale_price`,wc.`use_price`,wc.`use_limit_quantity`,wc.`send_type`,
        wc.`valid_type`,wc.`valid_start_date`,wc.`valid_end_date`,wc.`valid_days`,wc.`detail`,wc.`price`,wc.`unit`,wc.`remain_inventory`,wc.`inventory`,
        wc.`remark`,wc.`status`,wc.`create_date`,wc.`update_date`,wc.`business`,wc.`sub_business`,wc.`support_transfer`,wc.`subsidy_num`,wc.`subsidy_type`,
        wc.`press_limit_num`, wc.`press_limit_hours`, wc.`auto_refund`,wc.`credit_price`,wc.`credit_refund`,wc.`conditions`,
        wc.title as couponName,wc.`cover_img` as couponPic,wc.`cover_picture` as couponPicture,wc.`detail_picture` as detailPicture,wc.id as couponId
       from wx_coupon_channel ct
       left join wx_coupon wc on wc.id = ct.coupon_id
	   where ct.`sub_target_id` = #{id} and ct.status = 0
    </select>

    <select id="findCouponModel" parameterType="com.iformall.domain.po.WxCoupon" resultType="com.iformall.domain.po.WxCoupon">
        select
        max(case type when 9 then model_id else 0 end) couponModelId,
        max(case type when 10 then model_id else 0 end) cardModelId,
        max(case type when 11 then model_id else 0 end) groupModelId,
        max(case type when 12 then model_id else 0 end) pressModelId
        from wx_flow_config where type in(9,10,11,12) and tenant_id=#{tenantId}
    </select>
</mapper>
