<?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.yami.shop.dao.OrderMapper">
    <resultMap id="BaseResultMap" type="com.yami.shop.bean.model.Order">
        <id column="order_id" jdbcType="BIGINT" property="orderId"/>
        <result column="shop_id" jdbcType="BIGINT" property="shopId"/>
        <result column="shop_name" jdbcType="VARCHAR" property="shopName"/>
        <result column="channel_id" jdbcType="BIGINT" property="channelId"/>
        <result column="channel_name" jdbcType="VARCHAR" property="channelName"/>
        <result column="prod_name" jdbcType="VARCHAR" property="prodName"/>
        <result column="user_id" jdbcType="VARCHAR" property="userId"/>
        <result column="order_number" jdbcType="VARCHAR" property="orderNumber"/>
        <result column="total" jdbcType="DECIMAL" property="total"/>
        <result column="actual_total" jdbcType="DECIMAL" property="actualTotal"/>
        <result column="pay_type" jdbcType="INTEGER" property="payType"/>
        <result column="remarks" jdbcType="VARCHAR" property="remarks"/>
        <result column="hb_order_status" jdbcType="INTEGER" property="hbOrderStatus"/>
        <result column="dvy_status" jdbcType="VARCHAR" property="dvyStatus"/>
        <result column="dvy_type" jdbcType="VARCHAR" property="dvyType"/>
        <result column="dvy_id" jdbcType="BIGINT" property="dvyId"/>
        <result column="dvy_flow_id" jdbcType="VARCHAR" property="dvyFlowId"/>
        <result column="freight_amount" jdbcType="DECIMAL" property="freightAmount"/>
        <result column="addr_order_id" jdbcType="BIGINT" property="addrOrderId"/>
        <result column="product_nums" jdbcType="INTEGER" property="productNums"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="pay_time" jdbcType="TIMESTAMP" property="payTime"/>
        <result column="dvy_time" jdbcType="TIMESTAMP" property="dvyTime"/>
        <result column="finally_time" jdbcType="TIMESTAMP" property="finallyTime"/>
        <result column="cancel_time" jdbcType="TIMESTAMP" property="cancelTime"/>
        <result column="is_payed" jdbcType="BIT" property="isPayed"/>
        <result column="delete_status" jdbcType="INTEGER" property="deleteStatus"/>
        <result column="refund_status" jdbcType="INTEGER" property="refundStatus"/>
        <result column="member_discount_amount" jdbcType="DECIMAL" property="memberDiscountAmount"/>
        <result column="member_discount_desc" jdbcType="VARCHAR" property="memberDiscountDesc"/>
        <result column="close_type" jdbcType="INTEGER" property="closeType"/>
        <result column="user_mobile" jdbcType="VARCHAR" property="userMobile"/>
        <result column="receiver" jdbcType="VARCHAR" property="receiver"/>
        <result column="shop_name" jdbcType="VARCHAR" property="shopName"/>
        <result column="split_status" property="splitStatus"/>
        <result column="auto_split" property="autoSplit"/>
        <result column="order_level" property="orderLevel"/>
        <result column="order_split_number" property="orderSplitNumber"/>
        <result column="order_split_status" property="orderSplitStatus"/>
        <result column="consigneeAddress" property="consigneeAddress"/>
    </resultMap>

    <resultMap id="MyOrderMap" type="com.yami.shop.bean.app.dto.MyOrderDto">
        <result column="order_number" jdbcType="VARCHAR" property="orderNumber"/>
        <result column="actual_total" jdbcType="DECIMAL" property="actualTotal"/>
        <result column="order_type" jdbcType="VARCHAR" property="orderType"/>
        <result column="refund_type" jdbcType="INTEGER" property="refundType"/>
        <result column="return_money_sts" jdbcType="INTEGER" property="returnMoneySts"/>
        <result column="shop_id" jdbcType="BIGINT" property="shopId"/>
        <result column="shop_name" jdbcType="VARCHAR" property="shopName"/>
        <result column="latitude" property="latitude"/>
        <result column="longitude" property="longitude"/>
        <result column="create_time" property="createTime"/>
        <result column="hb_order_status" property="hbOrderStatus"/>
        <result column="hb_logistic_status" property="hbLogisticStatus"/>
        <result column="offset_points" property="offsetPoints"/>
        <result column="pay_time" property="payTime"/>
        <result column="dvy_type" property="dvyType"/>
        <result column="comm_sts" property="commSts"/>
        <collection property="orderItemDtos" ofType="com.yami.shop.bean.app.dto.MyOrderItemDto">
            <result column="order_number" jdbcType="VARCHAR" property="orderNumber"/>
            <result column="pic" jdbcType="VARCHAR" property="pic"/>
            <result column="prod_id" jdbcType="BIGINT" property="prodId"/>
            <result column="prod_name" jdbcType="VARCHAR" property="prodName"/>
            <result column="prod_count" jdbcType="INTEGER" property="prodCount"/>
            <result column="use_score" jdbcType="INTEGER" property="useScore"/>
            <result column="price" jdbcType="DECIMAL" property="price"/>
            <result column="sku_name" jdbcType="VARCHAR" property="skuName"/>
            <result column="order_item_id" property="orderItemId"/>
            <result column="comm_sts" property="commSts"/>
            <result column="rec_time" property="recTime"/>
        </collection>
    </resultMap>
    <resultMap id="MyOrderItemMap" type="com.yami.shop.bean.app.dto.MyOrderItemDto">
        <result column="order_number" jdbcType="VARCHAR" property="orderNumber"/>
        <result column="pic" jdbcType="VARCHAR" property="pic"/>
        <result column="prod_id" jdbcType="BIGINT" property="prodId"/>
        <result column="prod_name" jdbcType="VARCHAR" property="prodName"/>
        <result column="prod_count" jdbcType="INTEGER" property="prodCount"/>
        <result column="use_score" jdbcType="INTEGER" property="useScore"/>
        <result column="price" jdbcType="DECIMAL" property="price"/>
        <result column="sku_name" jdbcType="VARCHAR" property="skuName"/>
        <result column="order_item_id" property="orderItemId"/>
        <result column="comm_sts" property="commSts"/>
        <result column="rec_time" property="recTime"/>
    </resultMap>

    <resultMap type="com.yami.shop.bean.model.Order" id="orderAndOrderItemMap">
        <id column="order_id" jdbcType="BIGINT" property="orderId"/>
        <result column="shop_id" jdbcType="BIGINT" property="shopId"/>
        <result column="shop_name" jdbcType="VARCHAR" property="shopName"/>
        <result column="channel_id" jdbcType="BIGINT" property="channelId"/>
        <result column="channel_name" jdbcType="VARCHAR" property="channelName"/>
        <result column="prod_name" jdbcType="VARCHAR" property="prodName"/>
        <result column="user_id" jdbcType="VARCHAR" property="userId"/>
        <result column="order_number" jdbcType="VARCHAR" property="orderNumber"/>
        <result column="hb_order_status" jdbcType="VARCHAR" property="hbOrderStatus"/>
        <result column="dvy_status" jdbcType="VARCHAR" property="dvyStatus"/>
        <result column="total" jdbcType="DECIMAL" property="total"/>
        <result column="actual_total" jdbcType="DECIMAL" property="actualTotal"/>
        <result column="pay_type" jdbcType="INTEGER" property="payType"/>
        <result column="remarks" jdbcType="VARCHAR" property="remarks"/>
        <result column="dvy_type" jdbcType="VARCHAR" property="dvyType"/>
        <result column="dvy_id" jdbcType="BIGINT" property="dvyId"/>
        <result column="dvy_flow_id" jdbcType="VARCHAR" property="dvyFlowId"/>
        <result column="freight_amount" jdbcType="DECIMAL" property="freightAmount"/>
        <result column="platform_amount" jdbcType="DECIMAL" property="platformAmount"/>
        <result column="addr_order_id" jdbcType="BIGINT" property="addrOrderId"/>
        <result column="product_nums" jdbcType="INTEGER" property="productNums"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="pay_time" jdbcType="TIMESTAMP" property="payTime"/>
        <result column="dvy_time" jdbcType="TIMESTAMP" property="dvyTime"/>
        <result column="finally_time" jdbcType="TIMESTAMP" property="finallyTime"/>
        <result column="cancel_time" jdbcType="TIMESTAMP" property="cancelTime"/>
        <result column="is_payed" jdbcType="BIT" property="isPayed"/>
        <result column="hb_order_status" jdbcType="INTEGER" property="hbOrderStatus"/>
        <result column="delete_status" jdbcType="INTEGER" property="deleteStatus"/>
        <result column="refund_status" jdbcType="INTEGER" property="refundStatus"/>
        <result column="member_discount_amount" jdbcType="DECIMAL" property="memberDiscountAmount"/>
        <result column="member_discount_desc" jdbcType="VARCHAR" property="memberDiscountDesc"/>
        <result column="order_type" jdbcType="INTEGER" property="orderType"/>
        <result column="close_type" jdbcType="INTEGER" property="closeType"/>
        <result column="consignee_name" jdbcType="INTEGER" property="consigneeName"/>
        <result column="consignee_mobile" jdbcType="INTEGER" property="consigneeMobile"/>
        <result column="consignee_address" jdbcType="INTEGER" property="consigneeAddress"/>
        <result column="latitude" jdbcType="INTEGER" property="latitude"/>
        <result column="longitude" jdbcType="INTEGER" property="longitude"/>
        <result column="self_pick_code" jdbcType="VARCHAR" property="selfPickCode"/>
        <collection property="orderItems" ofType="com.yami.shop.bean.model.OrderItem">
            <id column="order_item_id" jdbcType="BIGINT" property="orderItemId"/>
            <result column="shop_id" jdbcType="BIGINT" property="shopId"/>
            <result column="order_number" jdbcType="VARCHAR" property="orderNumber"/>
            <result column="prod_id" jdbcType="BIGINT" property="prodId"/>
            <result column="sku_id" jdbcType="BIGINT" property="skuId"/>
            <result column="prod_count" jdbcType="INTEGER" property="prodCount"/>
            <result column="use_score" jdbcType="INTEGER" property="useScore"/>
            <result column="oi_prod_name" jdbcType="VARCHAR" property="prodName"/>
            <result column="pic" jdbcType="VARCHAR" property="pic"/>
            <result column="price" jdbcType="DECIMAL" property="price"/>
            <result column="user_id" jdbcType="VARCHAR" property="userId"/>
            <result column="product_total_amount" jdbcType="DECIMAL" property="productTotalAmount"/>
            <!--<result column="platform_amount" jdbcType="DECIMAL" property="platformAmount"/>-->
            <result column="rec_time" jdbcType="TIMESTAMP" property="recTime"/>
            <result column="comm_sts" jdbcType="INTEGER" property="commSts"/>
            <result column="distribution_card_no" property="distributionCardNo"/>
            <result column="basket_date" property="basketDate"/>
            <result column="share_reduce" property="shareReduce"/>
            <result column="member_discount_share_amount" property="memberDiscountShareAmount"/>
            <result column="distribution_amount" property="distributionAmount"/>
            <result column="distribution_parent_amount" property="distributionParentAmount"/>
            <result column="return_money_sts" property="returnMoneySts"/>
            <result column="oi_actual_total" property="actualTotal"/>
        </collection>
    </resultMap>

    <resultMap type="com.yami.shop.bean.model.Order" id="orderAndOrderItemAndUserAddrMap">
        <id column="order_id" jdbcType="BIGINT" property="orderId"/>
        <result column="shop_id" jdbcType="BIGINT" property="shopId"/>
        <result column="order_prod_name" jdbcType="VARCHAR" property="prodName"/>
        <result column="user_id" jdbcType="VARCHAR" property="userId"/>
        <result column="order_number" jdbcType="VARCHAR" property="orderNumber"/>
        <result column="total" jdbcType="DECIMAL" property="total"/>
        <result column="actual_total" jdbcType="DECIMAL" property="actualTotal"/>
        <result column="pay_type" jdbcType="INTEGER" property="payType"/>
        <result column="remarks" jdbcType="VARCHAR" property="remarks"/>
        <result column="hb_order_status" jdbcType="INTEGER" property="hbOrderStatus"/>
        <result column="dvy_type" jdbcType="VARCHAR" property="dvyType"/>
        <result column="dvy_id" jdbcType="BIGINT" property="dvyId"/>
        <result column="nick_name" jdbcType="BIGINT" property="nickName"/>
        <result column="dvy_flow_id" jdbcType="VARCHAR" property="dvyFlowId"/>
        <result column="freight_amount" jdbcType="DECIMAL" property="freightAmount"/>
        <result column="order_type" jdbcType="INTEGER" property="orderType"/>
        <result column="addr_order_id" jdbcType="BIGINT" property="addrOrderId"/>
        <result column="product_nums" jdbcType="INTEGER" property="productNums"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="pay_time" jdbcType="TIMESTAMP" property="payTime"/>
        <result column="dvy_time" jdbcType="TIMESTAMP" property="dvyTime"/>
        <result column="finally_time" jdbcType="TIMESTAMP" property="finallyTime"/>
        <result column="cancel_time" jdbcType="TIMESTAMP" property="cancelTime"/>
        <result column="is_payed" jdbcType="BIT" property="isPayed"/>
        <result column="delete_status" jdbcType="INTEGER" property="deleteStatus"/>
        <result column="pay_score" jdbcType="INTEGER" property="score"/>
        <result column="refund_status" jdbcType="INTEGER" property="refundStatus"/>
        <result column="member_discount_amount" jdbcType="DECIMAL" property="memberDiscountAmount"/>
        <result column="member_discount_desc" jdbcType="VARCHAR" property="memberDiscountDesc"/>
        <result column="close_type" jdbcType="INTEGER" property="closeType"/>
        <result column="shop_name" jdbcType="VARCHAR" property="shopName"/>
        <result column="pay_score" jdbcType="INTEGER" property="payScore"/>
        <result column="user_mobile" jdbcType="VARCHAR" property="userMobile"/>
        <result column="receiver" jdbcType="VARCHAR" property="receiver"/>
        <result column="qnh_order_id" jdbcType="VARCHAR" property="qnhOrderId"/>
        <result column="qnh_order_status" jdbcType="VARCHAR" property="qnhOrderStatus"/>
        <result column="dvy_status" jdbcType="INTEGER" property="dvyStatus"/>
        <association property="userAddrOrder" javaType="com.yami.shop.bean.model.UserAddrOrder">
            <id column="addr_order_id" jdbcType="BIGINT" property="addrOrderId"/>
            <result column="province" jdbcType="VARCHAR" property="province"/>
            <result column="city" jdbcType="VARCHAR" property="city"/>
            <result column="area" jdbcType="VARCHAR" property="area"/>
            <result column="addr" jdbcType="VARCHAR" property="addr"/>
            <result column="mobile" jdbcType="VARCHAR" property="mobile"/>
            <result column="receiver" jdbcType="VARCHAR" property="receiver"/>
            <result column="address" jdbcType="VARCHAR" property="address"/>
            <result column="address_name" jdbcType="VARCHAR" property="addressName"/>
            <result column="address_detail" jdbcType="VARCHAR" property="addrDetail"/>
        </association>
        <collection property="orderItems" ofType="com.yami.shop.bean.model.OrderItem">
            <id column="order_item_id" jdbcType="BIGINT" property="orderItemId"/>
            <result column="shop_id" jdbcType="BIGINT" property="shopId"/>
            <result column="order_number" jdbcType="VARCHAR" property="orderNumber"/>
            <result column="prod_id" jdbcType="BIGINT" property="prodId"/>
            <!--            <result column="prod_name" jdbcType="BIGINT" property="prodName"/>-->
            <result column="sku_id" jdbcType="BIGINT" property="skuId"/>
            <result column="prod_count" jdbcType="INTEGER" property="prodCount"/>
            <result column="use_score" jdbcType="INTEGER" property="useScore"/>
            <result column="prod_name" jdbcType="VARCHAR" property="prodName"/>
            <result column="pic" jdbcType="VARCHAR" property="pic"/>
            <result column="price" jdbcType="DECIMAL" property="price"/>
            <result column="user_id" jdbcType="VARCHAR" property="userId"/>
            <result column="oi_actual_total" jdbcType="DECIMAL" property="actualTotal"/>
            <result column="product_total_amount" jdbcType="DECIMAL" property="productTotalAmount"/>
            <result column="rec_time" jdbcType="TIMESTAMP" property="recTime"/>
            <result column="comm_sts" jdbcType="INTEGER" property="commSts"/>
            <result column="item_status" jdbcType="INTEGER" property="status"/>
            <result column="distribution_card_no" property="distributionCardNo"/>
            <result column="basket_date" property="basketDate"/>
            <result column="share_reduce" property="shareReduce"/>
            <result column="member_discount_share_amount" property="memberDiscountShareAmount"/>
            <result column="platform_share_reduce" property="platformShareReduce"/>
            <result column="oi_return_money_sts" property="returnMoneySts"/>
            <result column="oi_status" property="status"/>
        </collection>
    </resultMap>

    <insert id="addOrderPushLog">
        INSERT INTO tz_order_push_log(order_number,is_success,result,create_time) VALUES(#{orderNumber},#{isSuccess},#{result},NOW())
    </insert>

    <select id="getOrderByOrderNumber" resultType="com.yami.shop.bean.model.Order">
        select *
        from tz_order o
        where o.order_number = #{orderNumber}
    </select>

    <select id="listUnRefundOrderAndOrderItems" resultMap="orderAndOrderItemMap">
        select o.*, oi.*, tor.return_money_sts, oi.prod_name oi_prod_name, oi.actual_total as oi_actual_total
        from tz_order o
                 join tz_order_item oi on o.order_number = oi.order_number
                 left join tz_order_refund tor on tor.order_id = o.order_id
        where o.hb_order_status = #{orderStatus}
          and (refund_status IS NULL OR o.refund_status &lt;&gt; 1)
          and o.dvy_time &lt; #{lessThanUpdateTime}
    </select>

    <select id="selectCancelOrders" resultMap="orderAndOrderItemMap">
        select o.*, oi.*, oi.prod_name oi_prod_name, oi.actual_total as oi_actual_total
        from tz_order o
                 join tz_order_item oi on o.order_number = oi.order_number
        where o.hb_order_status = #{orderStatus}
          and o.create_time &lt; #{lessThanUpdateTime}
    </select>

    <update id="cancelOrders">
        update tz_order set `hb_order_status`=60,cancel_reason = #{cancelReason}, cancel_time =
        NOW(),update_time=NOW(),close_type=4 where order_id in
        <foreach collection="orders" item="order" open="(" close=")" separator=",">
            #{order.orderId}
        </foreach>
    </update>

    <update id="receiptOrder">
        update tz_order set `hb_order_status`= 80,finally_time = NOW(),update_time=NOW() where order_id in
        <foreach collection="orders" item="order" open="(" close=")" separator=",">
            #{order.orderId}
        </foreach>
    </update>

    <update id="updateByToPaySuccess">
        update tz_order set `hb_order_status` = 20,is_payed =1,update_time=NOW(),pay_time=NOW(),pay_type =#{payType}
        where dvy_type=3 and
        order_number in
        <foreach collection="orderNumbers" item="orderNumber" separator="," open="(" close=")">
            #{orderNumber}
        </foreach>
    </update>

    <update id="updateByToPaySuccessDelivery">
        update tz_order set `hb_order_status` = 1,is_payed =1,update_time=NOW(),pay_time=NOW(),pay_type =#{payType}
        where dvy_type=1 and
        order_number in
        <foreach collection="orderNumbers" item="orderNumber" separator="," open="(" close=")">
            #{orderNumber}
        </foreach>
    </update>


    <select id="listOrdersDetialByOrder" resultMap="orderAndOrderItemAndUserAddrMap">
        select o.*,oi.*,oi.prod_name as item_prod_name,o.prod_name as order_prod_name,uao.* ,o.hb_order_status as
        oi_status
        from tz_order o
        left join tz_order_item oi on o.order_number = oi.order_number
        left join tz_user_addr_order uao on o.addr_order_id = uao.addr_order_id
        where o.order_id in (
        select * from (
        select o.order_id from tz_order o
        left join tz_shop_detail sd on o.shop_id = sd.shop_id
        where 1=1
        <if test="order.orderNumber != null and order.orderNumber != ''">
            and o.order_number like concat('%',#{order.orderNumber},'%')
        </if>
        <if test="order.hb_order_status != null">
            and o.hb_order_status = #{order.orderStatus}
        </if>
        <if test="order.shopId != null">
            and o.shop_id = #{order.shopId}
        </if>
        <if test="order.isPayed != null">
            and o.is_payed = #{order.isPayed}
            and o.hb_order_status != 60
        </if>
        <if test="startTime != null">
            and o.create_time &gt; #{startTime}
        </if>
        <if test="endTime != null">
            and o.create_time &lt; #{endTime}
        </if>
        <if test="order.shopName != null">
            and sd.shop_name like concat("%",#{order.shopName},"%")
        </if>
        ORDER BY o.create_time DESC
        )
        AS limittable) ORDER BY o.create_time DESC
    </select>


    <select id="getOrderCountByShopId" resultType="com.yami.shop.bean.param.OrderPayParam">
        SELECT SUM(actual_total) as payActualTotal,COUNT(DISTINCT user_id) as payUserCount,
        COUNT(*) as payOrderCount
        FROM tz_order o
        <where>
            <if test="shopId != null">
                and o.shop_id = #{shopId}
            </if>
            <if test="startTime != null">
                and o.pay_time &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                and o.pay_time &lt;= #{endTime}
            </if>
            AND o.is_payed =1
        </where>
    </select>

    <select id="getPayOrderByTime" resultType="com.yami.shop.bean.param.OrderPayParam">
        SELECT dates, MAX(orderCount) AS payOrderCount,MAX(payActualTotal) as payActualTotal FROM(
        SELECT @cdate := DATE_ADD(@cdate,INTERVAL - 1 DAY) dates ,0 AS 'orderCount',0 AS 'payActualTotal'
        FROM (SELECT @cdate :=DATE_ADD(CURDATE(),INTERVAL + 1 DAY) FROM tz_user) t1
        <where>
            <if test="startTime != null">
                and @cdate >= #{startTime}
            </if>
        </where>
        UNION ALL
        SELECT DATE_FORMAT(o.pay_time, '%Y-%m-%d') AS dates,COUNT(*) AS orderCount,SUM(actual_total) AS payActualTotal
        FROM tz_order o
        <where>
            <if test="shopId != null">
                and o.shop_id = #{shopId}
            </if>
            <if test="startTime != null">
                and o.pay_time &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                and o.pay_time &lt;= #{endTime}
            </if>
            AND o.is_payed =1
        </where>
        GROUP BY dates) _tmpAllTable GROUP BY dates
        ORDER BY dates
    </select>

    <select id="getActualTotalByHour" resultType="com.yami.shop.bean.param.OrderPayParam">
        SELECT DATE_FORMAT(o.pay_time, '%k') AS dates,SUM(actual_total) as payActualTotal
        FROM tz_order o
        <where>
            <if test="shopId != null">
                and o.shop_id = #{shopId}
            </if>
            <if test="startTime != null">
                and o.pay_time &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                and o.pay_time &lt;= #{endTime}
            </if>
            AND o.is_payed =1
        </where>
        GROUP BY dates
    </select>

    <select id="getActualTotalByDay" resultType="com.yami.shop.bean.param.OrderPayParam">
        SELECT DATE_FORMAT(o.pay_time, '%Y-%m-%d') AS payDay,SUM(actual_total) as payActualTotal
        FROM tz_order o
        <where>
            <if test="shopId != null">
                and o.shop_id = #{shopId}
            </if>
            <if test="startTime != null">
                and o.pay_time &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                and o.pay_time &lt;= #{endTime}
            </if>
            AND o.is_payed =1
        </where>
        GROUP BY payDay
    </select>


    <select id="listOrdersDetialByOrderParam" resultMap="orderAndOrderItemAndUserAddrMap">
        SELECT *,oi.prod_name as item_prod_name,oi.actual_total as oi_actual_total,IF(r.refund_type =
        2,r.return_money_sts,IF(ar.refund_type = 1,ar.return_money_sts,NULL)) AS oi_return_money_sts,
        temp.prod_name as order_prod_name,temp.hb_order_status as oi_status, IF(os.pay_score IS NULL,0,os.pay_score) as
        pay_score
        FROM
        (
        SELECT o.*,sd.shop_name,uao.receiver,uao.mobile,u.user_mobile,u.nick_name FROM tz_order o
        LEFT JOIN tz_shop_detail sd ON o.shop_id = sd.shop_id
        LEFT JOIN tz_user_addr_order uao ON o.addr_order_id = uao.addr_order_id
        LEFT JOIN tz_user u on u.user_id=o.user_id
        <where>
            <!--            <if test="orderParam.type!=null and orderParam.type==1 and orderParam.content != null and orderParam.content != ''">-->
            <!--                and o.order_number = #{orderParam.content}-->
            <!--            </if>-->

            <if test="orderParam.parentOrderNumber != null and orderParam.parentOrderNumber != ''">
                and o.parent_order_number = #{orderParam.parentOrderNumber}
            </if>
            <if test="orderParam.orderNumber != null and orderParam.orderNumber != ''">
                and o.order_number = #{orderParam.orderNumber}
            </if>
            <if test="orderParam.hbOrderStatus != null">
                and o.hb_order_status = #{orderParam.hbOrderStatus}
            </if>
            <if test="orderParam.dvyType != null">
                and o.dvy_type = #{orderParam.dvyType}
            </if>
            <if test="orderParam.channelId != null">
                and o.channel_id = #{orderParam.channelId}
            </if>
            <if test="orderParam.shopId != null">
                and o.shop_id = #{orderParam.shopId}
            </if>
            <if test="orderParam.isPayed != null">
                and o.is_payed = #{orderParam.isPayed}
                and o.hb_order_status != 60
            </if>
            <if test="orderParam.startTime != null">
                and o.create_time &gt; #{orderParam.startTime}
            </if>
            <if test="orderParam.endTime != null">
                and o.create_time &lt; #{orderParam.endTime}
            </if>
            <if test="orderParam.orderType != null">
                and o.order_type = #{orderParam.orderType}
            </if>
            <if test="orderParam.orderType == null">
                and (o.order_type <![CDATA[ <> ]]> 3 OR o.order_type IS NULL)
            </if>
            <if test="orderParam.shopName != null">
                and sd.shop_name LIKE concat("%",#{orderParam.shopName},"%")
            </if>
            <if test="orderParam.refundStatus == 0">
                and o.refund_status IS NULL
            </if>
            <if test="orderParam.refundStatus != null and orderParam.refundStatus != 0">
                and o.refund_status = #{orderParam.refundStatus}
            </if>
            <if test="orderParam.prodName != null">
                and o.prod_name LIKE concat("%",#{orderParam.prodName},"%")
            </if>
        </where>
        ORDER BY o.create_time DESC
        LIMIT #{adapter.begin} , #{adapter.size}
        ) AS temp
        LEFT JOIN tz_order_item oi ON temp.order_number = oi.order_number
        <!--连接单个物品退款的退款信息-->
        LEFT JOIN tz_order_refund r ON r.order_item_id = oi.order_item_id AND r.refund_type = 2 AND r.return_money_sts
        &gt; 0 AND r.return_money_sts &lt; 6
        <!--连接整单退款的退款信息-->
        LEFT JOIN tz_order_refund ar ON ar.order_id = temp.order_id AND ar.refund_type = 1 AND ar.return_money_sts &gt;
        0 AND ar.return_money_sts &lt; 6
        LEFT JOIN tz_order_settlement os ON temp.order_number = os.order_number
        ORDER BY temp.create_time DESC
    </select>

    <select id="deliverList" resultMap="BaseResultMap">
        SELECT a.*,b.receiver,b.mobile user_mobile,c.shop_name FROM tz_order a
        LEFT JOIN tz_user_addr_order b on a.addr_order_id=b.addr_order_id
        LEFT JOIN tz_shop_detail c on a.shop_id=c.shop_id
        left join tz_user d on a.user_id = d.user_id
        <where>
            <if test="orderParam.orderNumber != null and orderParam.orderNumber != ''">
                and a.order_number = #{orderParam.orderNumber}
            </if>

            <if test="orderParam.parentOrderNumber != null and orderParam.parentOrderNumber != ''">
                and a.parent_order_number = #{orderParam.parentOrderNumber}
            </if>
            <if test="orderParam.channelIdList != null and !orderParam.channelIdList.isEmpty()">
                and a.channel_id in
                <foreach collection="orderParam.channelIdList" item="channelId" open="(" close=")" separator=",">
                    #{channelId}
                </foreach>
            </if>
            <if test="orderParam.dvyType != null and orderParam.dvyType != ''">
                and a.dvy_type = #{orderParam.dvyType}
            </if>
            <if test="orderParam.userAttrType != null and orderParam.userAttrType != '' and orderParam.userAttrType != 0">
                AND d.user_attr_type = #{orderParam.userAttrType}
            </if>
            <if test="orderParam.shopId != null">
                and a.shop_id = #{orderParam.shopId}
            </if>
            <if test="orderParam.startTime != null">
                and a.create_time &gt; #{orderParam.startTime}
            </if>
            <if test="orderParam.endTime != null">
                and a.create_time &lt; #{orderParam.endTime}
            </if>
            <if test="orderParam.refundStatus != null and orderParam.refundStatus != 0">
                and a.refund_status = #{orderParam.refundStatus}
            </if>
            <if test="orderParam.receiver != null">
                and b.receiver LIKE concat("%",#{orderParam.receiver},"%")
            </if>

            <if test="orderParam.userMobile != null">
                and b.mobile LIKE concat("%",#{orderParam.userMobile},"%")
            </if>
            <if test="orderParam.orderStatus != null and orderParam.orderStatus != ''">
                <if test="orderParam.orderStatus == 'all'">
                    AND a.hb_order_status in (0, 1,20,30,40,50,60,70,80)
                </if>
                <if test="orderParam.orderStatus == 'paddingPay'">
                    AND a.hb_order_status in (0)
                </if>

                <if test="orderParam.orderStatus == 'paddingShipped'">
                    AND a.hb_order_status in (1)
                </if>

                <if test="orderParam.orderStatus == 'paddingReceived'">
                    AND a.hb_order_status in (20,30,40,70)
                </if>
                <if test="orderParam.orderStatus == 'completed'">
                    AND a.hb_order_status in (80)
                </if>
                <if test="orderParam.orderStatus == 'cancel'">
                    AND a.hb_order_status in (50,60)
                </if>
                <if test="orderParam.orderStatus == 'split'">
                    AND a.hb_order_status in (1,20) and a.split_status in(2,3)
                </if>
            </if>

            <if test="orderParam.orderSplitStatus != null">
                and a.order_split_Status = #{orderParam.orderSplitStatus}
            </if>
            <if test="orderParam.autoSplit != null">
                and a.auto_split = #{orderParam.autoSplit}
            </if>
            <if test="orderParam.splitStatus != null">
                <if test="orderParam.splitStatus != 2">
                    and a.split_status = #{orderParam.splitStatus}
                </if>
            </if>
            <if test="orderParam.orderLevel != null">
                and a.order_level = #{orderParam.orderLevel}
            </if>
        </where>
        order by a.create_time desc,a.order_id desc
    </select>


    <select id="countOrderDetial" resultType="long">
        SELECT count(0)
        FROM tz_order o
        LEFT JOIN tz_shop_detail sd ON o.shop_id = sd.shop_id
        JOIN tz_user_addr_order uao
        ON o.addr_order_id = uao.addr_order_id
        <where>
            o.delete_status = 0
            <if test="orderParam.orderNumber != null and orderParam.orderNumber != ''">
                and o.order_number = #{orderParam.orderNumber}
            </if>
            <if test="orderParam.hbOrderStatus != null">
                and o.hb_order_status = #{orderParam.hbOrderStatus}
            </if>
            <if test="orderParam.shopId != null">
                and o.shop_id = #{orderParam.shopId}
            </if>
            <if test="orderParam.channelId != null">
                and o.channel_id = #{orderParam.channelId}
            </if>
            <if test="orderParam.dvyType != null">
                and o.dvy_type = #{orderParam.dvyType}
            </if>
            <if test="orderParam.isPayed != null">
                and o.is_payed = #{orderParam.isPayed}
                and o.hb_order_status != 60
            </if>
            <if test="orderParam.startTime != null">
                and o.create_time &gt; #{orderParam.startTime}
            </if>
            <if test="orderParam.endTime != null">
                and o.create_time &lt; #{orderParam.endTime}
            </if>
            <if test="orderParam.orderType != null">
                and o.order_type = #{orderParam.orderType}
            </if>
            <if test="orderParam.orderType == null">
                and (o.order_type <![CDATA[ <> ]]> 3 OR o.order_type IS NULL)
            </if>
            <if test="orderParam.shopName != null">
                and sd.shop_name LIKE concat("%",#{orderParam.shopName},"%")
            </if>
            <if test="orderParam.refundStatus == 0">
                and o.refund_status IS NULL
            </if>
            <if test="orderParam.refundStatus != null and orderParam.refundStatus != 0">
                and o.refund_status = #{orderParam.refundStatus}
            </if>
            <if test="orderParam.prodName != null">
                and o.prod_name LIKE concat("%",#{orderParam.prodName},"%")
            </if>
            <if test="orderParam.receiver != null">
                and uao.receiver LIKE concat("%",#{orderParam.receiver},"%")
            </if>
            <if test="orderParam.mobile != null">
                and uao.mobile LIKE concat("%",#{orderParam.mobile},"%")
            </if>
        </where>
    </select>


    <select id="calculateUserInShopData" resultType="com.yami.shop.bean.distribution.UserShoppingDataDto">
        select count(o.order_number)          as expense_number,
               ifnull(SUM(o.actual_total), 0) as expense_amount
        from tz_order o
        where o.user_id = #{userId}
          and o.shop_id = #{shopId}
          and (o.hb_order_status = 45 or o.hb_order_status = 80)
    </select>

    <select id="listMyOrderByUserIdAndStatus" resultMap="MyOrderMap">
        SELECT
        o.order_type,o.actual_total,o.order_number,o.freight_amount,o.create_time,o.order_id,
        o.hb_order_status,
        o.hb_logistic_status,
        o.offset_points,
        o.pay_time,
        o.dvy_type,
        o.comm_sts,
        (SELECT r.refund_type FROM tz_order_refund r WHERE r.order_id =o.order_id ORDER BY r.update_time DESC LIMIT 0,1)
        AS refund_type,
        (SELECT r.return_money_sts FROM tz_order_refund r WHERE r.order_id =o.order_id ORDER BY r.update_time DESC LIMIT
        0,1) AS return_money_sts,
        oi.pic,oi.price,oi.prod_name,oi.pic,oi.sku_name,ifnull(oi.use_score,0) as
        use_score,oi.prod_id,oi.rec_time,oi.prod_count,oi.order_item_id,oi.comm_sts,
        sd.shop_id,sd.shop_name,oi.actual_total as oi_actual_total,ad.longitude,ad.latitude
        FROM (
        SELECT temp.order_type,
        temp.hb_order_status,
        temp.hb_logistic_status,
        temp.offset_points,
        temp.pay_time,
        temp.dvy_type,
        temp.comm_sts,
        temp.order_id, temp.create_time,
        temp.freight_amount,temp.order_number,temp.actual_total,temp.shop_id,temp.addr_order_id FROM tz_order temp
        WHERE temp.user_id = #{userId} and temp.delete_status=0
        and (temp.order_level != 0 OR temp.split_status != 4) and (temp.order_level != 1 OR temp.split_status != 3)
        <if test="status != null">
            AND
            <choose>
                <when test="20 == status">
                    temp.hb_order_status IN (1,20,30,40,50,70)
                </when>
                <otherwise>
                    temp.hb_order_status = #{status}
                </otherwise>
            </choose>
        </if>

        <if test="dvyType != null">
            AND temp.dvy_type = #{dvyType}
        </if>
        ORDER BY temp.create_time DESC
        LIMIT #{adapter.begin} , #{adapter.size}
        )AS o
        JOIN tz_order_item oi ON o.order_number = oi.order_number
        left join tz_shop_detail sd on o.shop_id = sd.shop_id
        left join tz_order_refund tor on o.order_id = tor.order_id
        left join tz_user_addr_order ad on o.addr_order_id = ad.addr_order_id
        ORDER BY o.create_time DESC
    </select>


    <select id="countMyOrderByUserIdAndStatus" resultType="Long">
        SELECT count(temp.order_id) FROM tz_order temp
        WHERE temp.user_id = #{userId} and temp.delete_status = 0
        <if test="status != null">
            <if test="20 == status">
                and temp.hb_order_status IN (1,20,30,40,50,70)

            </if>
            <if test="20 != status">
                AND temp.hb_order_status = #{status}
            </if>
        </if>
        and (temp.order_level != 0 OR temp.split_status != 4) and (temp.order_level != 1 OR temp.split_status != 3)
    </select>


    <update id="deleteOrders">
        UPDATE tz_order SET `delete_status`=2,update_time=NOW()
        WHERE order_id IN
        <foreach collection="orders" item="order" open="(" close=")" separator=",">
            #{order.orderId}
        </foreach>
    </update>

    <select id="getOrderCount" resultType="com.yami.shop.bean.app.dto.OrderCountData">
        SELECT COUNT(o.order_id)                                                                              all_count,
               COUNT(CASE WHEN o.hb_order_status = 0 THEN o.order_id ELSE NULL END)                        AS unPay,
               COUNT(CASE WHEN o.hb_order_status = 1 THEN o.order_id ELSE NULL END)                        AS payed,
               COUNT(CASE
                         WHEN o.hb_order_status in (1, 20, 30, 40, 50, 70) THEN o.order_id
                         ELSE NULL END)                                                                    AS consignment,
               COUNT(CASE WHEN o.hb_order_status = 40 THEN o.order_id ELSE NULL END)                       AS confirm,
               COUNT(CASE WHEN o.hb_order_status = 80 THEN o.order_id ELSE NULL END)                       AS success,
               COUNT(CASE WHEN o.hb_order_status = 60 THEN o.order_id ELSE NULL END)                       AS `close`
        FROM tz_order o
        WHERE o.user_id = #{userId}
          AND o.delete_status = 0
          and (o.order_level != 0 OR o.split_status != 4)
          and (o.order_level != 1 OR o.split_status != 3)
    </select>
    <update id="cancelSeckillOrderByTime">
        UPDATE tz_order o JOIN (
            SELECT so.order_number FROM tz_seckill_order so JOIN
            tz_seckill s ON s.seckill_id = so.seckill_id
            AND (UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(so.`create_time`))/60 &gt; s.`max_cancel_time` and so.state = 0 ) t
        ON o.order_number = t.order_number
            SET o.`hb_order_status`=60, o.cancel_time = NOW(), o.update_time=NOW()
    </update>

    <update id="updateToWaitGroup">
        UPDATE tz_order
        SET hb_order_status = 7,
            update_time=NOW()
        WHERE order_number = #{orderNumber}
    </update>

    <update id="updateToWaitDelivery">
        UPDATE tz_order
        SET `hb_order_status` = 20,
            `update_time`     = NOW()
        WHERE `order_number` IN
              (SELECT order_number
               FROM (SELECT o.`order_number`
                     FROM tz_order o
                              LEFT JOIN tz_group_order go
                     ON go.`order_number` = o.`order_number`
                         LEFT JOIN tz_group_team gt ON gt.`group_team_id` = go.`group_team_id`
                     WHERE gt.`group_team_id` = #{groupTeamId}) temp)
    </update>
    <update id="syncOrderList">
        SELECT DISTINCT a.order_number FROM `tz_order` a
        LEFT JOIN tz_order_push_log b on a.order_number=b.order_number
        WHERE a.create_time >'2026-05-09 12:00:00' and ((ISNULL(b.order_number)) or (b.is_success=0))

    </update>
    <update id="updateOrderPushLog">
        update tz_order_push_log set is_success=#{isSuccess},result=#{result} WHERE order_number=#{orderNumber}
    </update>

    <select id="getOrderByOrderNumberAndUserId" resultType="com.yami.shop.bean.model.Order">
        select o.*
        from tz_order o
        where o.order_number = #{orderNumber}
          and o.user_id = #{userId}
    </select>

    <select id="getOrderAndOrderItemByOrderNumber" resultMap="orderAndOrderItemMap">
        select o.*, oi.*, oi.prod_name oi_prod_name, oi.actual_total as oi_actual_total
        from tz_order o
                 left join tz_order_item oi on o.order_number = oi.order_number
        where o.order_number = #{orderNumber}
    </select>

    <select id="hasBuySuccessProd" resultType="java.lang.Integer">
        select count(*)
        from tz_order_item oi
                 join tz_order o on o.order_number = oi.order_number
        where o.user_id = #{userId}
          and o.hb_order_status = 50
          and oi.prod_id = #{prodId}
    </select>

    <select id="listMyOrderByParams" resultMap="MyOrderMap">
        SELECT
        o.order_type,o.actual_total,
        oi.pic,oi.price,oi.prod_id,oi.prod_name,oi.sku_name,ifnull(oi.use_score,0) as
        use_score,oi.prod_count,oi.order_item_id,o.hb_order_status,o.order_number,oi.comm_sts,
        sd.shop_id,sd.shop_name
        FROM (
        SELECT temp.order_type, temp.create_time,
        temp.order_number,temp.actual_total,temp.hb_order_status,temp.shop_id,prod_name
        FROM tz_order temp
        WHERE temp.user_id = #{userId} and temp.delete_status = 0
        <if test="status != null and status != 0">
            AND temp.hb_order_status = #{status}
        </if>
        <if test="orderNumber != null and orderNumber != ''">
            AND temp.order_number LIKE CONCAT("%",#{orderNumber},"%")
        </if>
        <if test="orderType == 0">
            AND (temp.order_type = 0 or temp.order_type is null)
        </if>
        <if test="orderType != null and orderType !='' and orderType != 0">
            AND temp.order_type = #{orderType}
        </if>
        <if test="orderTimeStatus == 1 or orderTimeStatus == 2">
            AND temp.create_time &gt;= #{preTime}
        </if>
        <if test="orderTimeStatus == 3">
            AND temp.create_time &lt;= #{preTime}
        </if>
        <if test="orderName != null and orderName != ''">
            AND temp.prod_name LIKE concat('%',#{orderName},'%')
        </if>
        ORDER BY temp.create_time DESC
        LIMIT #{adapter.begin} , #{adapter.size}
        )AS o
        JOIN tz_order_item oi ON o.order_number = oi.order_number
        LEFT JOIN tz_shop_detail sd on o.shop_id = sd.shop_id
        ORDER BY o.create_time DESC
    </select>

    <select id="countMyOrderByParams" resultType="Long">
        SELECT COUNT(*) FROM(
        SELECT
        count(1)
        FROM tz_order o
        JOIN tz_order_item oi ON o.order_number = oi.order_number
        join tz_shop_detail sd on o.shop_id = sd.shop_id
        WHERE o.user_id = #{userId} and o.delete_status = 0
        <if test="status != null and status != 0">
            AND o.hb_order_status = #{status}
        </if>
        <if test="orderNumber != null and orderNumber != ''">
            AND o.order_number LIKE CONCAT("%",#{orderNumber},"%")
        </if>
        <if test="orderType == 0">
            AND (o.order_type = 0 or o.order_type is null)
        </if>
        <if test="orderType != null and orderType !='' and orderType != 0">
            AND o.order_type = #{orderType}
        </if>
        <if test="orderTimeStatus == 1 or orderTimeStatus == 2">
            AND o.create_time &gt;= #{preTime}
        </if>
        <if test="orderTimeStatus == 3">
            AND o.create_time &lt;= #{preTime}
        </if>
        <if test="orderName != null and orderName != ''">
            AND (o.prod_name LIKE concat('%',#{orderName},'%') OR oi.prod_name LIKE concat('%',#{orderName},'%'))
        </if>
        GROUP BY o.order_number
        ) AS temp
    </select>
    <select id="listOrdersDetialByOrderInfo" resultMap="orderAndOrderItemAndUserAddrMap">
        SELECT *,oi.prod_name,uao.address,uao.address_name,uao.addr_detail as item_prod_name,oi.actual_total as
        oi_actual_total,temp.prod_name as order_prod_name,temp.hb_order_status as oi_status
        FROM
        (
        SELECT o.*,sd.shop_name FROM tz_order o
        LEFT JOIN tz_shop_detail sd ON o.shop_id = sd.shop_id
        <where>
            <if test="orderParam.orderNumber != null and orderParam.orderNumber != ''">
                and o.order_number = #{orderParam.orderNumber}
            </if>
            <if test="orderParam.status != null">
                and o.hb_order_status = #{orderParam.status}
            </if>
            <if test="orderParam.shopId != null">
                and o.shop_id = #{orderParam.shopId}
            </if>
            <if test="orderParam.isPayed != null">
                and o.is_payed = #{orderParam.isPayed}
                and o.hb_order_status != 60
            </if>
            <if test="orderParam.startTime != null">
                and o.create_time &gt; #{orderParam.startTime}
            </if>
            <if test="orderParam.endTime != null">
                and o.create_time &lt; #{orderParam.endTime}
            </if>
            <if test="orderParam.orderType != null">
                and o.order_type = #{orderParam.orderType}
            </if>
            <if test="orderParam.orderType == null">
                and (o.order_type <![CDATA[ <> ]]> 3 OR o.order_type IS NULL)
            </if>
            <if test="orderParam.shopName != null">
                and sd.shop_name LIKE concat("%",#{orderParam.shopName},"%")
            </if>
            <if test="orderParam.refundStatus == 0">
                and o.refund_status IS NULL
            </if>
            <if test="orderParam.refundStatus != null and orderParam.refundStatus != 0">
                and o.refund_status = #{orderParam.refundStatus}
            </if>
            <if test="orderParam.prodName != null">
                and o.prod_name LIKE concat("%",#{orderParam.prodName},"%")
            </if>
        </where>
        ORDER BY o.create_time DESC
        ) AS temp
        LEFT JOIN tz_order_item oi
        ON temp.order_number = oi.order_number
        LEFT JOIN tz_user_addr_order uao
        ON temp.addr_order_id = uao.addr_order_id
        <where>
            <if test="orderParam.receiver != null">
                and uao.receiver LIKE concat("%",#{orderParam.receiver},"%")
            </if>
            <if test="orderParam.mobile != null">
                and uao.mobile LIKE concat("%",#{orderParam.mobile},"%")
            </if>
        </where>
        ORDER BY temp.create_time DESC
    </select>
    <select id="getOrderPayInfoByOrderNumber" resultMap="orderAndOrderItemMap">
        SELECT o.*,oi.prod_name as oi_prod_name,oi.actual_total as oi_actual_total,oi.use_score
        FROM tz_order o
        LEFT JOIN tz_order_item oi ON oi.order_number = o.order_number
        WHERE o.order_number IN
        <foreach collection="orderNumberList" item="orderNumber" separator="," open="(" close=")">
            #{orderNumber}
        </foreach>
    </select>
    <select id="getOrderDetailByOrderNumberAndShopId" resultMap="orderAndOrderItemAndUserAddrMap">
        SELECT o.*,oi.prod_name as item_prod_name,oi.*,uao.*,u.`nick_name`,u.user_mobile,o.hb_order_status as oi_status,
        IF(o.refund_type = 1,
        o.rms,r.return_money_sts) AS oi_return_money_sts
        FROM (
        SELECT temp.*,tr.refund_type,tr.return_money_sts rms
        FROM tz_order temp
        LEFT JOIN tz_order_refund tr ON tr.order_id = temp.order_id AND tr.return_money_sts &gt; 0 AND
        tr.return_money_sts &lt; 6
        WHERE temp.order_number =#{orderNumber}
        <if test="shopId != null">
            and temp.shop_id =#{shopId}
        </if>
        ) as o
        LEFT JOIN tz_order_item oi ON oi.order_number = o.order_number
        LEFT JOIN tz_order_refund r ON r.order_item_id = oi.order_item_id AND r.return_money_sts &gt; 0 AND
        r.return_money_sts &lt; 6
        LEFT JOIN tz_user_addr_order uao ON o.addr_order_id = uao.addr_order_id
        LEFT JOIN tz_user u ON u.user_id = o.user_id
    </select>
    <select id="OrderCommentByUserIdAndStatus" resultMap="MyOrderMap">
        SELECT o.order_type,
               o.actual_total,
               o.hb_order_status,
               o.order_number,
               o.freight_amount,
               o.create_time,
               pc.rec_time,
               oi.pic,
               oi.price,
               oi.prod_name,
               oi.pic,
               oi.sku_name,
               oi.use_score,
               oi.prod_id,
               oi.rec_time,
               oi.prod_count,
               oi.order_item_id,
               oi.comm_sts,
               sd.shop_id,
               sd.shop_name,
               oi.actual_total as oi_actual_total
        FROM (SELECT DISTINCT temp.order_type,
                              temp.create_time,
                              temp.freight_amount,
                              temp.order_number,
                              temp.actual_total,
                              temp.hb_order_status,
                              temp.shop_id
              FROM tz_order temp
                       JOIN tz_order_item toi ON temp.order_number = toi.order_number AND toi.comm_sts = #{isComm}
              WHERE temp.user_id = #{userId}
                and temp.delete_status = 0
                AND temp.hb_order_status = 80
              ORDER BY temp.create_time DESC
                  LIMIT #{adapter.begin}, #{adapter.size}) AS o
                 JOIN tz_order_item oi ON o.order_number = oi.order_number AND oi.comm_sts = #{isComm}
                 left JOIN tz_prod_comm pc ON pc.order_item_id = oi.order_item_id
                 left join tz_shop_detail sd on o.shop_id = sd.shop_id
        ORDER BY o.create_time DESC
    </select>
    <select id="orderItemCommentByUserIdAndStatus" resultMap="MyOrderItemMap">
        SELECT
        pc.rec_time,oi.pic,oi.price,oi.prod_name,oi.order_number,oi.pic,oi.sku_name,oi.use_score,oi.prod_id,oi.rec_time,oi.prod_count,oi.order_item_id,oi.comm_sts,
        oi.actual_total as oi_actual_total
        FROM tz_order o
        left JOIN tz_order_item oi ON o.order_number = oi.order_number
        left JOIN tz_prod_comm pc ON pc.order_item_id = oi.order_item_id
        where oi.comm_sts = #{isComm} and o.user_id = #{userId}
        <if test="isComm == 1">
            AND o.hb_order_status = 80
        </if>
        ORDER BY pc.rec_time DESC
        LIMIT #{adapter.begin} , #{adapter.size}
    </select>

    <select id="countOrderItemComment" resultType="long">
        SELECT count(*)
        FROM tz_order o
        left JOIN tz_order_item oi ON o.order_number = oi.order_number
        where oi.comm_sts = #{isComm} and o.user_id = #{userId}
        <if test="isComm == 1">
            AND o.hb_order_status = 80
        </if>
    </select>

    <select id="statusCount" resultType="integer">
        SELECT count(a.order_id) FROM `tz_order` a
        LEFT JOIN tz_user_addr_order b on a.addr_order_id=b.addr_order_id
        LEFT JOIN tz_shop_detail c on a.shop_id=c.shop_id
        left join tz_user d on a.user_id = d.user_id
        <where>
            hb_order_status in
            <foreach collection="status" item="status" separator="," open="(" close=")">
                #{status}
            </foreach>
            <if test="orderParam.orderNumber != null and orderParam.orderNumber != ''">
                and a.order_number = #{orderParam.orderNumber}
            </if>
            <if test="orderParam.parentOrderNumber != null and orderParam.parentOrderNumber != ''">
                and a.parent_order_number = #{orderParam.parentOrderNumber}
            </if>
            <if test="orderParam.channelIdList != null and !orderParam.channelIdList.isEmpty()">
                and a.channel_id in
                <foreach collection="orderParam.channelIdList" item="channelId" open="(" close=")" separator=",">
                    #{channelId}
                </foreach>
            </if>
            <if test="orderParam.dvyType != null and orderParam.dvyType != ''">
                and a.dvy_type = #{orderParam.dvyType}
            </if>
            <if test="orderParam.userAttrType != null and orderParam.userAttrType != '' and orderParam.userAttrType != 0">
                AND d.user_attr_type = #{orderParam.userAttrType}
            </if>
            <if test="orderParam.shopId != null">
                and a.shop_id = #{orderParam.shopId}
            </if>
            <if test="orderParam.startTime != null">
                and a.create_time &gt; #{orderParam.startTime}
            </if>
            <if test="orderParam.endTime != null">
                and a.create_time &lt; #{orderParam.endTime}
            </if>
            <if test="orderParam.refundStatus != null and orderParam.refundStatus != 0">
                and a.refund_status = #{orderParam.refundStatus}
            </if>
            <if test="orderParam.receiver != null">
                and b.receiver LIKE concat("%",#{orderParam.receiver},"%")
            </if>
            <if test="orderParam.userMobile != null">
                and b.mobile LIKE concat("%",#{orderParam.userMobile},"%")
            </if>

            <if test="orderParam.orderStatus != null and orderParam.orderStatus != ''">
                <if test="orderParam.orderStatus == 'all'">
                    AND a.hb_order_status in (0, 1,20,30,40,50,60,70,80)
                </if>
                <if test="orderParam.orderStatus == 'paddingPay'">
                    AND a.hb_order_status in (0)
                </if>

                <if test="orderParam.orderStatus == 'paddingShipped'">
                    AND a.hb_order_status in (1)
                </if>

                <if test="orderParam.orderStatus == 'paddingReceived'">
                    AND a.hb_order_status in (20,30,40,70)
                </if>
                <if test="orderParam.orderStatus == 'completed'">
                    AND a.hb_order_status in (80)
                </if>
                <if test="orderParam.orderStatus == 'cancel'">
                    AND a.hb_order_status in (50,60)
                </if>
            </if>


            <if test="orderParam.orderSplitStatus != null">
                and a.order_split_Status = #{orderParam.orderSplitStatus}
            </if>
            <if test="orderParam.autoSplit != null">
                and a.auto_split = #{orderParam.autoSplit}
            </if>
            <if test="orderParam.splitStatus != null">
                <if test="orderParam.splitStatus == 2">
                    and a.split_status in (2,3)
                </if>
                <if test="orderParam.splitStatus != 2">
                    and a.split_status = #{orderParam.splitStatus}
                </if>
            </if>
            <if test="orderParam.orderLevel != null">
                and a.order_level = #{orderParam.orderLevel}
            </if>
        </where>
    </select>
    <select id="selectOrderCount" resultType="com.yami.shop.bean.param.OrderCountParam">
        SELECT
        count(1) as allCount,
        count(case when hb_order_status = 0 then 1 end) as payedCount,
        count(case when hb_order_status = 20 then 1 end) as readyCount,
        count(case when hb_order_status = 30 then 1 end) as deliveryCount,
        count(case when hb_order_status = 40 then 1 end) as shippingCount,
        count(case when hb_order_status = 50 then 1 end) as cancelAuditCount,
        count(case when hb_order_status = 60 then 1 end) as canceledCount,
        count(case when hb_order_status = 70 then 1 end) as arrivedCount,
        count(case when hb_order_status = 80 then 1 end) as completedCount
        FROM tz_order
        <where>
            delete_status = 0
            <if test="orderParam.orderNumber != null and orderParam.orderNumber != ''">
                and o.order_number = #{orderParam.orderNumber}
            </if>
            <if test="orderParam.hbOrderStatus != null">
                and o.hb_order_status = #{orderParam.hbOrderStatus}
            </if>
            <if test="orderParam.dvyType != null">
                and o.dvy_type = #{orderParam.dvyType}
            </if>
            <if test="orderParam.channelId != null">
                and o.channel_id = #{orderParam.channelId}
            </if>
            <if test="orderParam.shopId != null">
                and o.shop_id = #{orderParam.shopId}
            </if>
            <if test="orderParam.startTime != null">
                and o.create_time &gt; #{orderParam.startTime}
            </if>
            <if test="orderParam.endTime != null">
                and o.create_time &lt; #{orderParam.endTime}
            </if>
            <if test="orderParam.orderType != null">
                and o.order_type = #{orderParam.orderType}
            </if>

            <if test="orderParam.refundStatus == 0">
                and o.refund_status IS NULL
            </if>
        </where>
    </select>

    <select id="deliveryOrder" resultMap="BaseResultMap">
        SELECT o.*, sd.shop_name,ch.channel_name
        FROM tz_order o
        LEFT JOIN tz_shop_detail sd ON o.shop_id = sd.shop_id
        LEFT JOIN tz_channel ch ON o.channel_id = ch.id
        join tz_user_addr_order uao ON o.addr_order_id = uao.addr_order_id
        <where>
            o.delete_status = 0 AND o.dvy_type = #{orderParam.dvyType}
            <if test="orderParam.orderNumber != null and orderParam.orderNumber != ''">
                AND o.order_number LIKE concat('%', #{orderParam.orderNumber}, '%')
            </if>
            <if test="orderParam.mobile != null and orderParam.mobile != ''">
                AND uao.mobile LIKE concat('%', #{orderParam.mobile}, '%')
            </if>
            <if test="orderParam.receiver != null and orderParam.receiver != ''">
                AND uao.receiver LIKE concat('%', #{orderParam.receiver}, '%')
            </if>
            <if test="orderParam.startTime != null">
                AND o.create_time &gt;= #{orderParam.startTime}
            </if>
            <if test="orderParam.endTime != null">
                AND o.create_time &lt;= #{orderParam.endTime}
            </if>
            <if test="orderParam.channelId != null">
                AND o.channel_id = #{orderParam.channelId}
            </if>
            <if test="orderParam.shopId != null">
                AND o.shopId = #{orderParam.shopId}
            </if>
            <if test="orderParam.hbOrderStatus != null">
                <if test="orderParam.hbOrderStatus == 0">
                    AND o.hb_order_status = 0
                </if>
                <if test="orderParam.hbOrderStatus == 20">
                    AND o.hb_order_status = 20
                </if>
                <if test="orderParam.hbOrderStatus == 70">
                    AND o.hb_order_status in (30,40,70)
                </if>
                <if test="orderParam.hbOrderStatus == 80">
                    AND o.hb_order_status = 80
                </if>
                <if test="orderParam.hbOrderStatus == 60">
                    AND o.hb_order_status in (50, 60)
                </if>
            </if>
        </where>
        ORDER BY o.create_time DESC
    </select>

    <select id="findList" resultMap="BaseResultMap">
        SELECT a.*,b.receiver,b.mobile user_mobile,c.shop_name,d.channel_name, CONCAT(b.address,b.address_name, b.addr_detail) as consigneeAddress FROM tz_order a
        LEFT JOIN tz_user_addr_order b on a.addr_order_id=b.addr_order_id
        LEFT JOIN tz_shop_detail c on a.shop_id=c.shop_id
        LEFT JOIN tz_channel d on a.channel_id=d.id
        left join tz_user e on a.user_id = e.user_id
        <where>
            <if test="orderParam.orderNumber != null and orderParam.orderNumber != ''">
                and a.order_number = #{orderParam.orderNumber}
            </if>
            <if test="orderParam.channelIdList != null and !orderParam.channelIdList.isEmpty()">
                and a.channel_id in
                <foreach collection="orderParam.channelIdList" item="channelId" open="(" close=")" separator=",">
                    #{channelId}
                </foreach>
            </if>
            <if test="orderParam.userAttrType != null and orderParam.userAttrType != '' and orderParam.userAttrType != 0">
                AND e.user_attr_type = #{orderParam.userAttrType}
            </if>
            <if test="orderParam.dvyType != null and orderParam.dvyType != ''">
                and a.dvy_type = #{orderParam.dvyType}
            </if>
            <if test="orderParam.shopId != null">
                and a.shop_id = #{orderParam.shopId}
            </if>
            <if test="orderParam.startTime != null">
                and a.create_time &gt; #{orderParam.startTime}
            </if>
            <if test="orderParam.endTime != null">
                and a.create_time &lt; #{orderParam.endTime}
            </if>
            <if test="orderParam.refundStatus != null and orderParam.refundStatus != 0">
                and a.refund_status = #{orderParam.refundStatus}
            </if>
            <if test="orderParam.receiver != null">
                and b.receiver LIKE concat("%",#{orderParam.receiver},"%")
            </if>
            <if test="orderParam.userMobile != null">
                and b.mobile LIKE concat("%",#{orderParam.userMobile},"%")
            </if>

            <if test="orderParam.orderStatus != null and orderParam.orderStatus != ''">
                <if test="orderParam.orderStatus == 'all'">
                    AND a.hb_order_status in (0, 1,20,30,40,50,60,70,80)
                </if>
                <if test="orderParam.orderStatus == 'paddingPay'">
                    AND a.hb_order_status in (0)
                </if>

                <if test="orderParam.orderStatus == 'paddingShipped'">
                    AND a.hb_order_status in (1)
                </if>

                <if test="orderParam.orderStatus == 'paddingReceived'">
                    AND a.hb_order_status in (20,30,40,70)
                </if>
                <if test="orderParam.orderStatus == 'completed'">
                    AND a.hb_order_status in (80)
                </if>
                <if test="orderParam.orderStatus == 'cancel'">
                    AND a.hb_order_status in (50,60)
                </if>
            </if>
        </where>
        order by a.create_time desc,a.order_id desc
    </select>

    <select id="orderCount" resultType="com.yami.shop.bean.vo.OrderCountVo">
        select count(1) as orderTotal,
        sum(orderMoney) as orderMoneyTotal,
        sum(shopMoney) as shopMoneyTotal,
        sum(carriageMoney) as carriageMoneyTotal,
        sum(orderMoney) as payMoneyTotal,
        ROUND(sum(pointsMoney) / 100,2) as pointsMoneyTotal,
        sum(money) as moneyTotal
        from (
        select
        a.order_number,
        a.total as orderMoney,
        sum(c.product_total_amount) as shopMoney,
        IFNUll(a.freight_amount,0) as carriageMoney,
        IFNUll(a.offset_points,0) as pointsMoney,
        IFNUll(a.actual_total,0) as money
        FROM tz_order a
        LEFT JOIN tz_user_addr_order b on a.addr_order_id=b.addr_order_id
        LEFT JOIN tz_order_item c on a.order_number =c.order_number
        left join tz_user e on a.user_id = e.user_id
        <where>
            and a.delete_status =0
            <if test="orderParam.orderNumber != null and orderParam.orderNumber != ''">
                and a.order_number = #{orderParam.orderNumber}
            </if>

            <if test="orderParam.parentOrderNumber != null and orderParam.parentOrderNumber != ''">
                and a.parent_order_number = #{orderParam.parentOrderNumber}
            </if>
            <if test="orderParam.channelIdList != null and !orderParam.channelIdList.isEmpty()">
                and a.channel_id in
                <foreach collection="orderParam.channelIdList" item="channelId" open="(" close=")" separator=",">
                    #{channelId}
                </foreach>
            </if>
            <if test="orderParam.userAttrType != null and orderParam.userAttrType != '' and orderParam.userAttrType != 0">
                AND e.user_attr_type = #{orderParam.userAttrType}
            </if>
            <if test="orderParam.dvyType != null and orderParam.dvyType != ''">
                and a.dvy_type = #{orderParam.dvyType}
            </if>
            <if test="orderParam.shopId != null">
                and a.shop_id = #{orderParam.shopId}
            </if>
            <if test="orderParam.startTime != null">
                and a.create_time &gt; #{orderParam.startTime}
            </if>
            <if test="orderParam.endTime != null">
                and a.create_time &lt; #{orderParam.endTime}
            </if>
            <if test="orderParam.refundStatus != null and orderParam.refundStatus != 0">
                and a.refund_status = #{orderParam.refundStatus}
            </if>
            <if test="orderParam.receiver != null">
                and b.receiver LIKE concat("%",#{orderParam.receiver},"%")
            </if>
            <if test="orderParam.userMobile != null">
                and b.mobile LIKE concat("%",#{orderParam.userMobile},"%")
            </if>

            <if test="orderParam.orderStatus != null and orderParam.orderStatus != ''">
                <if test="orderParam.orderStatus == 'all'">
                    AND a.hb_order_status in (0, 1,20,30,40,50,60,70,80)
                </if>
                <if test="orderParam.orderStatus == 'paddingPay'">
                    AND a.hb_order_status in (0)
                </if>

                <if test="orderParam.orderStatus == 'paddingShipped'">
                    AND a.hb_order_status in (1)
                </if>

                <if test="orderParam.orderStatus == 'paddingReceived'">
                    AND a.hb_order_status in (20,30,40,70)
                </if>
                <if test="orderParam.orderStatus == 'completed'">
                    AND a.hb_order_status in (80)
                </if>
                <if test="orderParam.orderStatus == 'cancel'">
                    AND a.hb_order_status in (50,60)
                </if>
            </if>
        </where>
        group by a.order_number
        ) m
    </select>
    <select id="getRiderInfo" resultType="java.lang.String">
        SELECT CONCAT('骑手：',courier_name,'，电话：', SUBSTRING_INDEX(courier_phone, ',', 1)) info
        FROM `tz_order_rider_location` WHERE order_number=#{orderNumber}
    </select>
    <select id="getOrderPushLog" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM tz_order_push_log WHERE order_number=#{orderNumber}
    </select>
</mapper>
