<?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.WxOrderMapper">
    <resultMap id="BaseResultMap" type="com.iformall.domain.po.WxOrder">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="order_number" jdbcType="BIGINT" property="orderNumber"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="c_user_id" jdbcType="BIGINT" property="cUserId"/>
        <result column="coupon_channel_id" jdbcType="BIGINT" property="couponChannelId"/>
        <result column="product_id" jdbcType="BIGINT" property="productId"/>
        <result column="type" jdbcType="TINYINT" property="type"/>
        <result column="payment_type" jdbcType="INTEGER" property="paymentType"/>
        <result column="payment" jdbcType="INTEGER" property="payment"/>
        <result column="payment_time" jdbcType="TIMESTAMP" property="paymentTime"/>
        <result column="order_status" jdbcType="INTEGER" property="orderStatus"/>
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="update_date" jdbcType="TIMESTAMP" property="updateDate"/>
        <result column="detail" jdbcType="VARCHAR" property="detail"/>
        <result column="press_end_date" jdbcType="TIMESTAMP" property="pressEndDate"/>
        <result column="press_current_num" jdbcType="INTEGER" property="pressCurrentNum"/>
        <result column="press_current_value" jdbcType="INTEGER" property="pressCurrentValue"/>
        <result column="order_group_id" jdbcType="BIGINT" property="orderGroupId"/>

    </resultMap>

    <sql id="allColumns">
	    `id`,`order_number`,`tenant_id`,`c_user_id`,`coupon_channel_id`,`product_id`,`type`,`payment_type`,`payment`,`payment_time`,`order_status`,`create_date`,`update_date`,`detail`,
	    `press_end_date`,`press_current_num`,`press_current_value`,`order_group_id`
	</sql>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        <if test=" null != pressCurrentValue ">
            and `press_current_value` = #{pressCurrentValue}
        </if>
    
        <if test=" null != orderGroupId ">
            and `order_group_id` = #{orderGroupId}
        </if>
    
        <if test=" null != statusS ">
            and `order_status` in
            <foreach collection="statusS" index="index" item="sItem" open="(" separator="," close=")">
                #{sItem}
            </foreach>
        </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.WxOrder" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from wx_order
        <include refid="dynamicWhereConditions"/>
    </select>

    <select id="countList" parameterType="com.iformall.domain.po.WxOrder" resultType="java.lang.Integer">
        select count(*) from wx_order
        <include refid="dynamicWhereConditions"/>
    </select>


    <select id="findListOfUnpaidOrderByDate" parameterType="map" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from wx_order
        where order_status = '0' and press_end_date is null
        <if test="type != null">
            AND type = #{type,jdbcType=TINYINT}
        </if>
        <if test="startDate != null">
            AND unix_timestamp(`create_date`) &gt; unix_timestamp(#{startDate,jdbcType=TIMESTAMP})
        </if>
        <if test="endDate != null">
            AND unix_timestamp(`create_date`) &lt; unix_timestamp(#{endDate,jdbcType=TIMESTAMP})
        </if>
    </select>

    <select id="findListOfUnpaidPressOrderByDate" parameterType="map" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from wx_order
        where press_end_date is not null and (order_status = 6 or order_status = 7)
        <if test="type != null">
            AND type = #{type,jdbcType=TINYINT}
        </if>
        <if test="startDate != null">
            AND unix_timestamp(`create_date`) &gt; unix_timestamp(#{startDate,jdbcType=TIMESTAMP})
        </if>
        <if test="endDate != null">
            AND unix_timestamp(`press_end_date`) &lt; unix_timestamp(#{endDate,jdbcType=TIMESTAMP})
        </if>
    </select>

    <select id="findListOfCompleteUnPaidPressOrderByDate" parameterType="map" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from wx_order
        where press_end_date is not null and order_status = 7
        <if test="type != null">
            AND type = #{type,jdbcType=TINYINT}
        </if>
        <if test="startDate != null">
            AND unix_timestamp(`create_date`) &gt; unix_timestamp(#{startDate,jdbcType=TIMESTAMP})
        </if>
        <if test="endDate != null">
            AND unix_timestamp(`update_date`) &lt; unix_timestamp(#{endDate,jdbcType=TIMESTAMP})
        </if>
    </select>


    <sql id="allCUserOrderListColumns">
		o.id,o.order_number,o.tenant_id,o.c_user_id,o.coupon_channel_id,o.product_id,o.payment_type,o.payment,o.payment_time,o.order_status,o.create_date,o.update_date,
		c.type,c.cover_img,c.title,c.sub_title,c.sale_price,c.use_price,c.price,c.unit,c.valid_start_date,c.valid_end_date,
		cal.channel_type as send_channel_type
	</sql>


    <sql id="allCUserOrderDetailColumns">
		o.id,o.order_number,o.tenant_id,o.c_user_id,o.coupon_channel_id,o.product_id,o.payment_type,o.payment,o.payment_time,o.order_status,o.create_date,o.update_date,
		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_start_date,c.valid_end_date,
		co.id as coupon_order_id,co.coupon_order_status,c.support_transfer
	</sql>

    <update id="updateStatusByPressCouponId" parameterType="com.iformall.domain.po.WxOrder">
		update wx_order set order_status=#{orderStatus}
		where product_id=#{productId}
		and press_end_date is not null and order_status in (6,7)
        <if test=" null != tenantId ">
            and `tenant_id` = #{tenantId}
        </if>
    </update>

    <resultMap id="CVoResultMap" type="com.iformall.domain.vo.WxOrderCouponVo">

        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="order_number" jdbcType="BIGINT" property="orderNumber"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="c_user_id" jdbcType="BIGINT" property="cUserId"/>
        <result column="coupon_channel_id" jdbcType="BIGINT" property="couponChannelId"/>
        <result column="product_id" jdbcType="BIGINT" property="productId"/>
        <result column="payment_type" jdbcType="INTEGER" property="paymentType"/>
        <result column="payment" jdbcType="INTEGER" property="payment"/>
        <result column="payment_time" jdbcType="TIMESTAMP" property="paymentTime"/>
        <result column="order_status" jdbcType="INTEGER" property="orderStatus"/>
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="update_date" jdbcType="TIMESTAMP" property="updateDate"/>

        <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_start_date" jdbcType="TIMESTAMP" property="validStartDate"/>
        <result column="valid_end_date" jdbcType="TIMESTAMP" property="validEndDate"/>

        <result column="coupon_order_id" jdbcType="BIGINT" property="couponOrderId"/>
        <result column="coupon_order_status" jdbcType="INTEGER" property="couponOrderStatus"/>
        <result column="send_channel_type" jdbcType="INTEGER" property="sendChannelType" />
	    <result column="support_transfer" jdbcType="INTEGER" property="supportTransfer"/>

        <collection column="{productId=product_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.po.WxOrder" resultMap="CVoResultMap">
        select
        <include refid="allCUserOrderListColumns"/>
        from wx_coupon c,wx_order o
        left join wx_coupon_order co on co.order_id = o.id
        left join wx_coupon_action_log cal on cal.coupon_order_id = co.id
        where o.product_id = c.id and c.type!=9
        <if test="cUserId != null">
            and o.c_user_id = #{cUserId}
        </if>
        <if test="tenantId != null">
            and o.tenant_id = #{tenantId}
        </if>
        <if test="orderStatus != null">
            AND o.order_status = #{orderStatus}
        </if>
        <if test=" null != sortColumns">order by ${sortColumns}</if>
    </select>

    <select id="selectDetailOfCUser" parameterType="com.iformall.domain.po.WxOrder" resultMap="CVoResultMap">
        select
        <include refid="allCUserOrderDetailColumns"/>
        FROM wx_order o
        left join wx_coupon c ON o.product_id = c.id
        left join wx_coupon_order co on o.id = co.order_id
        where o.product_id = c.id
        <if test="cUserId != null">
            and o.c_user_id = #{cUserId}
        </if>
        <if test="tenantId != null">
            and o.tenant_id = #{tenantId}
        </if>
        <if test="id != null">
            and o.id = #{id}
        </if>

    </select>


    <select id="dayMicroPayAmountList" parameterType="map" resultType="map">
        select count(*) as count, IFNULL(sum(o.payment), 0) as total_price
        from wx_order o
        inner join wx_merchant_b_user bu on bu.id = product_id
        inner join wx_merchant m on m.id = bu.merchant_id
        where o.`type` = 1 and o.`order_status` = 1
        <if test=" null != tenantId ">
            and o.`tenant_id` = #{tenantId}
        </if>

        <if test=" null != merchantId ">
            and bu.`merchant_id` = #{merchantId}
        </if>

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

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

        <if test=" null != couponChannelId ">
            and o.`coupon_channel_id` = #{couponChannelId}
        </if>

        <if test=" null != productId ">
            and o.`product_id` = #{productId}
        </if>

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

        <if test="endDate != null">
            AND o.create_date &lt;  #{endDate,jdbcType=TIMESTAMP}
        </if>
    </select>

    <resultMap id="PayVoResultMap" type="com.iformall.domain.vo.WxOrderPayVo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="order_number" jdbcType="BIGINT" property="orderNumber"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="c_user_id" jdbcType="BIGINT" property="cUserId"/>
        <result column="coupon_channel_id" jdbcType="BIGINT" property="couponChannelId"/>
        <result column="product_id" jdbcType="BIGINT" property="productId"/>
        <result column="type" jdbcType="TINYINT" property="type"/>
        <result column="payment_type" jdbcType="INTEGER" property="paymentType"/>
        <result column="payment" jdbcType="INTEGER" property="payment"/>
        <result column="payment_time" jdbcType="TIMESTAMP" property="paymentTime"/>
        <result column="order_status" jdbcType="INTEGER" property="orderStatus"/>
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="update_date" jdbcType="TIMESTAMP" property="updateDate"/>
        <result column="detail" jdbcType="VARCHAR" property="detail"/>

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

    </resultMap>

    <sql id="allPayOrderColumns">
		o.`id`,o.`order_number`,o.`tenant_id`,o.`c_user_id`,o.`coupon_channel_id`,o.`product_id`,o.`type`,
		o.`payment_type`,o.`payment`,o.`payment_time`,o.`order_status`,o.`create_date`,o.`update_date`,o.`detail`,
        m.name as merchant_name, bu.name, bu.phone as bphone,
        cu.phone, cu.nick_name, cubi.name c_user_name
	</sql>

    <select id="findListOfMicroPayOrderByDate" parameterType="map" resultMap="PayVoResultMap">
        select
        <include refid="allPayOrderColumns"/>
        from wx_order o
        inner join wx_merchant_b_user bu on bu.id = o.product_id
        inner join wx_merchant m on m.id = bu.merchant_id
        LEFT JOIN wx_c_user cu on cu.id = o.c_user_id
        LEFT JOIN wx_c_user_basic_info cubi on cubi.id = o.c_user_id
        where o.order_status = '1' and o.`type` = 1
        <if test=" null != tenantId ">
            and o.`tenant_id` = #{tenantId}
        </if>
        <if test=" null != merchantId ">
            and bu.`merchant_id` = #{merchantId}
        </if>
        <if test=" null != bUserId ">
            and bu.`id` = #{bUserId}
        </if>
        <if test="startDate != null">
            AND o.create_date &gt; #{startDate,jdbcType=TIMESTAMP}
        </if>
        <if test="endDate != null">
            AND o.create_date &lt; #{endDate,jdbcType=TIMESTAMP}
        </if>
        order by o.create_date desc
    </select>

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

    <select id="listOrderReportByDate" resultType="com.iformall.domain.vo.WxOrderReportVo" parameterType="hashmap">
        select DATE_FORMAT(o.create_date,'%Y-%m-%d') xTime,
            count(o.id) count,
            IFNULL (SUM(o.payment),0) amount
        from wx_order o
        where o.`tenant_id` = #{tenantId}
        <if test=" null != tenantId">
            and o.`tenant_id` = #{tenantId}
        </if>
        <if test=" null != type">
            and o.`type` = #{type}
        </if>
        <if test=" null != orderStatus">
            and o.`order_status` = #{orderStatus}
        </if>
        <if test=" null != startTime and null != endTime">
            and o.`create_date` BETWEEN #{startTime} and #{endTime}
        </if>
        group by xTime
    </select>

    <select id="listMicroPayOrderReportByDateAndMerchant" resultType="com.iformall.domain.vo.WxOrderReportVo" parameterType="hashmap">
        select DATE_FORMAT(o.create_date,'%Y-%m-%d') xTime, count(o.id) count, IFNULL (SUM(o.payment),0) amount
        from wx_order o
        inner join wx_merchant_b_user bu on bu.id = o.product_id
        inner join wx_merchant m on m.id = bu.merchant_id
        where m.`id` = #{merchantId} and o.`type` = 1
        <if test=" null != startTime and null != endTime">
            and o.`create_date` BETWEEN #{startTime} and #{endTime}
        </if>
        group by xTime
    </select>


    <resultMap id="WxMerchantMicroPayVo" type="com.iformall.domain.vo.WxMerchantMicroPayVo">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId" />
        <result column="trade_amt" jdbcType="DECIMAL" property="tradeAmt" />
        <result column="report_date" jdbcType="VARCHAR" property="reportDate" />
        <result column="merchant_phone" jdbcType="VARCHAR" property="merchantPhone" />
        <result column="merchant_name" jdbcType="VARCHAR" property="merchantName" />
    </resultMap>

    <sql id="WxMerchantMicroPayColumnsVo">
        wm.id as id,
	    wm.tenant_id as tenant_id,
        wm.name as merchant_name,
        wm.link_phone as merchant_phone,
        mo.xTime as report_date,
	    (case when mo.amount > 0 then mo.amount else 0 end) trade_amt
	</sql>

    <select id="findListOfMerchantMicroPayOrderByDate" parameterType="map" resultMap="WxMerchantMicroPayVo">
        select
        <include refid="WxMerchantMicroPayColumnsVo"/>
        from wx_merchant wm
        left join
            (select m.id as mid, IFNULL(sum(o.payment),0) amount,DATE_FORMAT(o.create_date,'%Y-%m-%d') xTime
              from wx_merchant m, wx_order o, wx_merchant_b_user bu
                where bu.id = o.product_id
                  and m.status = 1
                  and m.id = bu.merchant_id
                  and o.order_status = '1'
                  and o.type = 1
                    <if test="startTime != null">
                        AND o.create_date &gt; #{startTime}
                    </if>
                    <if test="endTime != null">
                        AND o.create_date &lt; #{endTime}
                    </if>
              group by m.id,xTime) mo on mo.mid = wm.id
        where wm.status = 1
        <if test=" null != merchantName ">
            and wm.name like concat('%', #{merchantName},'%')
        </if>
        order by wm.create_date desc
    </select>
    
    <resultMap id="orderMap" type="com.iformall.domain.po.WxOrder">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="order_number" jdbcType="BIGINT" property="orderNumber"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="c_user_id" jdbcType="BIGINT" property="cUserId"/>
        <result column="coupon_channel_id" jdbcType="BIGINT" property="couponChannelId"/>
        <result column="product_id" jdbcType="BIGINT" property="productId"/>
        <result column="type" jdbcType="TINYINT" property="type"/>
        <result column="payment_type" jdbcType="INTEGER" property="paymentType"/>
        <result column="payment" jdbcType="INTEGER" property="payment"/>
        <result column="payment_time" jdbcType="TIMESTAMP" property="paymentTime"/>
        <result column="order_status" jdbcType="INTEGER" property="orderStatus"/>
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="update_date" jdbcType="TIMESTAMP" property="updateDate"/>
        <result column="detail" jdbcType="VARCHAR" property="detail"/>
        <result column="merchant_name" jdbcType="VARCHAR" property="merchantName"/>
        <result column="c_user_name" jdbcType="VARCHAR" property="cUserName"/>
        <result column="phone" jdbcType="VARCHAR" property="phone"/>

    </resultMap>
    
    <select id="findListOrder" resultMap="orderMap" parameterType="com.iformall.domain.vo.WxOrderQueryVo">
  
        select o.* from (
        select o.id,o.order_number,o.tenant_id,o.c_user_id,o.coupon_channel_id,o.product_id,o.type,o.payment_type,o.payment,o.payment_time,
        o.order_status,o.create_date,o.update_date,o.detail,'[多商户交易]' merchant_name,cubi.`name` c_user_name,cubi.phone
        from
        (
        select o.* from wx_order o where o.type=0 and o.order_status in (1,4) and o.id in(
        select o.id from wx_order o left join wx_coupon_merchant cm
        on o.product_id=cm.product_id group by o.id having count(o.id)>1)) o
        left join wx_c_user_basic_info cubi on o.c_user_id=cubi.id
        union all
        select o.id,o.order_number,o.tenant_id,o.c_user_id,o.coupon_channel_id,o.product_id,o.type,o.payment_type,o.payment,o.payment_time,
        o.order_status,o.create_date,o.update_date,o.detail,m.`name` merchant_name,cubi.`name` c_user_name,cubi.phone
        from(
        select o.*,cm.merchant_id from (
        select o.* from wx_order o where o.type=0 and o.order_status in (1,4) and o.id not in(
        select o.id from wx_order o left join wx_coupon_merchant cm
        on o.product_id=cm.product_id group by o.id having count(o.id)>1)
        ) o
        left join wx_coupon_merchant cm on o.product_id=cm.product_id
        union all
        select o.*,mbu.merchant_id from wx_order o
        left join wx_merchant_b_user mbu on o.product_id=mbu.id
        where o.type in (1,2) and o.order_status in (1,4) ) o
        left join wx_merchant m on o.merchant_id=m.id
        left join wx_c_user_basic_info cubi on o.c_user_id=cubi.id
        ) o
        <where>
            <if test="tenantId!=null">
                and o.tenant_id =#{tenantId}
            </if>
            <if test="merchantName!=null and merchantName!=''">
              and o.`merchant_name` like concat('%',#{merchantName},'%')
            </if>
            <if test="startdate!=null and enddate!=null">
                and o.create_date between #{startdate} and #{enddate}
            </if>
        </where>
        order by o.id desc
    </select>
    
    
    <select id="queryOrderForMerchantCount" resultType="Long" parameterType="hashmap">
        select count(*) merchantCount from (
        select distinct merchant_id from (
        select o.*, m.`name` merchantName from (
        select o.*,cm.merchant_id from wx_order o
        left join wx_coupon_merchant cm on o.product_id=cm.product_id
        where o.type = 0 and o.order_status in (1,4)) o
        left join wx_merchant m on o.merchant_id=m.id
        union all
        select o.*,m.`name` merchantName from (
        select o.*,mbu.merchant_id from wx_order o
        left join wx_merchant_b_user mbu on o.product_id=mbu.id
        where o.type in (1,2) and o.order_status in (1,4)) o
        left join wx_merchant m on o.merchant_id=m.id
        ) o
        <where>
            <if test="tenantId!=null">
                and o.tenant_id =#{tenantId}
            </if>
            <if test="startdate!=null and enddate!=null">
                and o.create_date between #{startdate} and #{enddate}
            </if>
        </where>
        ) o
    </select>
    
    <select id="queryOrderForSum" resultType="Long" parameterType="hashmap">
        select ifnull(sum(o.payment),0) payment from (
        select o.* from wx_order o
        where o.type in (0,1,2) and o.order_status = #{order_status}
        ) o
        <where>
            <if test="tenantId!=null">
                and o.tenant_id =#{tenantId}
            </if>
            <if test="startdate!=null and enddate!=null">
                and o.create_date between #{startdate} and #{enddate}
            </if>
        </where>
    </select>

    <resultMap id="PressVoResultMap" type="com.iformall.domain.vo.WxOrderCouponPressVo">

        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="order_number" jdbcType="BIGINT" property="orderNumber"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="c_user_id" jdbcType="BIGINT" property="cUserId"/>
        <result column="coupon_channel_id" jdbcType="BIGINT" property="couponChannelId"/>
        <result column="product_id" jdbcType="BIGINT" property="productId"/>
        <result column="type" jdbcType="INTEGER" property="type"/>
        <result column="payment_type" jdbcType="INTEGER" property="paymentType"/>
        <result column="payment" jdbcType="INTEGER" property="payment"/>
        <result column="payment_time" jdbcType="TIMESTAMP" property="paymentTime"/>
        <result column="order_status" jdbcType="INTEGER" property="orderStatus"/>
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="update_date" jdbcType="TIMESTAMP" property="updateDate"/>
        <result column="press_end_date" jdbcType="TIMESTAMP" property="pressEndDate"/>
        <result column="press_current_num" jdbcType="INTEGER" property="pressCurrentNum"/>
        <result column="press_current_value" jdbcType="INTEGER" property="pressCurrentValue"/>

        <result column="coupon_type" jdbcType="INTEGER" property="couponType"/>
        <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="press_limit_num" jdbcType="INTEGER" property="pressLimitNum"/>

        <result column="coupon_order_id" jdbcType="BIGINT" property="couponOrderId"/>
        <result column="coupon_order_status" jdbcType="INTEGER" property="couponOrderStatus"/>
    </resultMap>

    <resultMap id="PressVoDetailResultMap" type="com.iformall.domain.vo.WxOrderCouponPressVo">

        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="order_number" jdbcType="BIGINT" property="orderNumber"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="c_user_id" jdbcType="BIGINT" property="cUserId"/>
        <result column="coupon_channel_id" jdbcType="BIGINT" property="couponChannelId"/>
        <result column="product_id" jdbcType="BIGINT" property="productId"/>
        <result column="type" jdbcType="INTEGER" property="type"/>
        <result column="payment_type" jdbcType="INTEGER" property="paymentType"/>
        <result column="payment" jdbcType="INTEGER" property="payment"/>
        <result column="payment_time" jdbcType="TIMESTAMP" property="paymentTime"/>
        <result column="order_status" jdbcType="INTEGER" property="orderStatus"/>
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="update_date" jdbcType="TIMESTAMP" property="updateDate"/>
        <result column="press_end_date" jdbcType="TIMESTAMP" property="pressEndDate"/>
        <result column="press_current_num" jdbcType="INTEGER" property="pressCurrentNum"/>
        <result column="press_current_value" jdbcType="INTEGER" property="pressCurrentValue"/>

        <result column="coupon_type" jdbcType="INTEGER" property="couponType"/>
        <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="press_limit_num" jdbcType="INTEGER" property="pressLimitNum"/>

        <result column="coupon_order_id" jdbcType="BIGINT" property="couponOrderId"/>
        <result column="coupon_order_status" jdbcType="INTEGER" property="couponOrderStatus"/>

        <collection column="{orderId=order_number}" property="orderPressList"
                    ofType="com.iformall.domain.vo.WxOrderPressVo"
                    javaType="java.util.List"
                    select="com.iformall.mapper.WxOrderPressMapper.findPressList" >
        </collection>
    </resultMap>

    <sql id="allOrderPressListColumns">
		o.id,o.order_number,o.tenant_id,o.c_user_id,o.coupon_channel_id,o.product_id,o.payment_type,o.payment,o.payment_time,o.order_status,
		o.create_date,o.update_date,o.press_end_date,o.press_current_num,o.press_current_value,
		c.cover_img,c.title,c.sub_title,c.sale_price,c.use_price,c.price,c.unit,c.press_limit_num
	</sql>

    <sql id="allOrderPressDetailColumns">
		o.id,o.order_number,o.tenant_id,o.c_user_id,o.coupon_channel_id,o.product_id,o.payment_type,o.payment,o.payment_time,o.order_status,
		o.create_date,o.update_date,o.press_end_date,o.press_current_num,o.press_current_value,
		c.type as coupon_type,c.cover_img,c.title,c.sub_title,c.sale_price,c.use_price,c.price,c.unit,c.detail,c.remark,c.press_limit_num,
		co.id as coupon_order_id,co.coupon_order_status
	</sql>

    <select id="findListOfPress" parameterType="com.iformall.domain.po.WxOrder" resultMap="PressVoResultMap">
        select
        <include refid="allOrderPressListColumns"/>
        from wx_coupon c,wx_order o
        left join wx_coupon_order co on co.order_id = o.id
        where o.product_id = c.id and c.type = 8 and o.order_status in (6,7,8,9)
        <if test="cUserId != null">
            and o.c_user_id = #{cUserId}
        </if>
        <if test="tenantId != null">
            and o.tenant_id = #{tenantId}
        </if>
        <if test="orderStatus != null">
            AND o.order_status = #{orderStatus}
        </if>
        <if test=" null != sortColumns">order by ${sortColumns}</if>
    </select>

    <select id="selectDetailOfPress" parameterType="com.iformall.domain.po.WxOrder" resultMap="PressVoDetailResultMap">
        select
        <include refid="allOrderPressDetailColumns"/>
        FROM wx_order o
        left join wx_coupon c ON o.product_id = c.id
        left join wx_coupon_order co on o.id = co.order_id
        where o.product_id = c.id
        <if test="cUserId != null">
            and o.c_user_id = #{cUserId}
        </if>
        <if test="tenantId != null">
            and o.tenant_id = #{tenantId}
        </if>
        <if test="id != null">
            and o.id = #{id}
        </if>

    </select>
    
    <update id="updateStatusByOrderGroupId" parameterType="com.iformall.domain.po.WxOrder">
        update wx_order set order_status=#{orderStatus} where tenant_id=#{tenantId} and order_group_id=#{orderGroupId} and order_status=10
    </update>

</mapper>
