<?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.younike.system.mapper.OrderGoodsMapper">

    <resultMap type="com.younike.system.domain.vo.OrderGoodsVo" id="OrderGoodsVoResult" extends="OrderGoodsResult">
        <result property="totalAmount"    column="total_amount"    />
        <result property="orderAmount"    column="order_amount"    />
        <result property="orderTimeSum"    column="order_time_sum"    />
        <result property="orderNum"    column="order_num"    />
        <result property="orderType"    column="order_type"    />
        <result property="maintenanceAmount"    column="maintenance_amount"    />
        <result property="maintenanceRemark"    column="maintenance_remark"    />
        <result property="addPayStatus"    column="add_pay_status"    />
        <result property="appointmentStartTime"    column="appointment_start_time"    />
        <result property="goodsCounts"    column="goods_counts"    />
        <result property="address"    column="address"    />
        <result property="detailed"    column="detailed"    />
        <result property="name"    column="name"    />
        <result property="phone"    column="phone"    />
        <result property="orderState"    column="order_state"    />
        <result property="unit"    column="unit"    />
        <result property="custName"    column="cust_name"    />
        <result property="categoryName"    column="category_name"    />
        <result property="avatar"    column="avatar"    />
        <result property="addCounts"    column="add_counts"    />
        <result property="addAmount"    column="add_amount"    />


        <result property="price"    column="price"    />
        <result property="couponAmount"    column="coupon_amount"    />
        <result property="beanAmount"    column="bean_amount"    />
        <result property="orderIndex"    column="order_index"    />
        <result property="refundMoney"    column="refund_money"    />
        <result property="refundReason"    column="refund_reason"    />
        <result property="refundResult"    column="refund_result"    />
        <result property="publishId"    column="publish_id"    />
        <result property="goodsSpecificationId"    column="goods_specification_id"    />
        <result property="status"    column="status"    />
        <result property="refundMoney"    column="refund_money"    />


    </resultMap>
    <select id="getNowWeekDate"  resultMap="OrderGoodsVoResult">
        select IFNULL(SUM(o.total_amount),0)  AS total_amount ,IFNULL(SUM((og.goods_counts + og.add_counts) * single_time),0) as order_time_sum,count(1) as order_num
        from order_goods og
        left join `order` o on o.order_id = og.order_id and o.del_flag = 0
        left join category ca on ca.category_id = og.cate_id and ca.del_flag = 0
        where og.server_id = #{serverId}
        and og.del_flag = 0
        <if test="startTime != null ">
            and to_days(o.appointment_start_time) >= to_days(#{startTime})
        </if>
        <if test="endTime != null ">
            and to_days(o.appointment_start_time) &lt;= to_days(#{endTime})
        </if>
        <if test="categoryId != null and categoryId != ''">
            and (ca.category_id =  #{categoryId} or ca.parent_id =  #{categoryId} )
        </if>
        <if test="orderState != null and orderState != ''">
            and o.order_state in (${orderState})
        </if>

    </select>

    <select id="getOrderList"  resultMap="OrderGoodsVoResult">
        select
            og.name,
            o.appointment_start_time,
            og.goods_counts,
            oa.address,
            oa.detailed,
            oa.name as cust_name,
            oa.phone,
            o.order_state,
            IFNULL(o.total_amount,0)  AS total_amount,
            ca.category_name,
            m.avatar,
            og.add_counts,
            o.add_amount,
            o.order_id,
            og.publish_id,
            o.order_type,
            o.maintenance_amount,
            o.add_pay_status,
            orr.status,
            orr.refund_money,
            CASE WHEN og.goods_unit != '999'
            THEN sc1.dict_label
            ELSE og.other_unit END as unit
        from `order` o
                 left join order_goods og on o.order_id = og.order_id and og.del_flag = 0
                 left join sys_dict_data sc1 on og.goods_unit = sc1.dict_value and sc1.dict_type = 'goods_unit'
                 left join order_address oa on o.order_id = oa.order_id and oa.del_flag = 0
                 left join category ca on ca.category_id = og.cate_id and ca.del_flag = 0
                 left join member m on m.member_id = o.member_id and m.del_flag = 0
                 Left JOIN order_refund orr on o.order_id = orr.order_id and orr.is_del=0
          where og.server_id = #{serverId}
          and o.del_flag = 0

        <if test="startTime != null ">
            and to_days(o.appointment_start_time) >= to_days(#{startTime})
        </if>
        <if test="endTime != null ">
            and to_days(o.appointment_start_time) &lt;= to_days(#{endTime})
        </if>
        <if test="categoryId != null and categoryId != ''">
            and (ca.category_id =  #{categoryId} or ca.parent_id =  #{categoryId} )
        </if>
        <if test="orderState != null and orderState != ''">
            and o.order_state in (${orderState})
            <if test=" orderState == '001' or orderState == '002' ">
                order by o.appointment_start_time desc
            </if>
            <if test=" orderState == '003,004,007' or orderState == '005,006' ">
                order by o.appointment_start_time desc
            </if>
        </if>

    </select>
    <select id="getOrderInfo"  resultMap="OrderGoodsVoResult">
        select
            og.name,
            o.appointment_start_time,
            og.goods_counts,
            oa.address,
            oa.detailed,
            oa.name as cust_name,
            oa.phone,
            o.order_state,
            IFNULL(o.total_amount,0)  AS total_amount,
            ca.category_name,
            m.avatar,
            og.add_counts,
            o.add_amount,
            o.order_id,
            o.order_amount,
            o.order_type,
            o.maintenance_amount,
            o.maintenance_remark,
            o.add_pay_status,
            og.publish_id,
            CASE WHEN og.goods_unit != '999'
            THEN sc1.dict_label
            ELSE og.other_unit END as unit,
            og.price,IFNULL(o.coupon_amount,0) as coupon_amount,IFNULL(o.bean_amount,0) as bean_amount,o.order_index,o.create_time,
            IFNULL(or1.refund_money,0) as refund_money ,or1.refund_reason,or1.refund_result
        from `order` o
             left join order_goods og on o.order_id = og.order_id and og.del_flag = 0
             left join sys_dict_data sc1 on og.goods_unit = sc1.dict_value and sc1.dict_type = 'goods_unit'
             left join order_address oa on o.order_id = oa.order_id and oa.del_flag = 0
             left join category ca on ca.category_id = og.cate_id and ca.del_flag = 0
             left join member m on m.member_id = o.member_id and m.del_flag = 0
             left join order_refund or1 on or1.order_id = o.order_id and or1.is_del = 0
          where og.order_id = #{orderId}
          and o.del_flag = 0
        <if test="orderState != null and orderState != ''">
            and o.order_state in (${orderState})
        </if>
    </select>

    <!-- 查询商家某商品订单数 -->
    <select id="getOrderCountByGoodsIdAndMerchantId" resultType="java.lang.Integer">
        SELECT
        COUNT(*)
        FROM order_goods og
        LEFT JOIN `order` o ON o.order_id = og.order_id
        WHERE og.del_flag = '0'
        AND o.del_flag = '0'
        AND o.order_state = '004'
        AND og.goods_id = #{goodsId}
        AND o.merchant_id = #{merchantId}
    </select>

    <!-- 查询服务人员某商品订单数 -->
    <select id="getOrderCountByGoodsIdAndServerId" resultType="java.lang.Integer">
        SELECT
        COUNT(*)
        FROM order_goods og
        LEFT JOIN `order` o ON o.order_id = og.order_id
        WHERE og.del_flag = '0'
        AND o.del_flag = '0'
        AND o.order_state = '004'
        AND og.goods_id = #{goodsId}
        AND og.server_id = #{serverId}
    </select>

<!--    查询已退款，并且是部分退款部分的订单-->
    <select id="getRefundNowWeekDate"  resultMap="OrderGoodsVoResult">
        select IFNULL(SUM(o.total_amount-orr.refund_money),0)  AS total_amount ,IFNULL(SUM((og.goods_counts + og.add_counts) * single_time),0) as order_time_sum,count(1) as order_num
        from order_goods og
        left join `order` o on o.order_id = og.order_id and o.del_flag = 0
        left join category ca on ca.category_id = og.cate_id and ca.del_flag = 0
        Left JOIN order_refund orr on o.order_id = orr.order_id
        where og.server_id = #{serverId}
        and og.del_flag = 0
        and o.order_state = '006'
        and orr.is_del =0
        and orr.refund_money &lt; o.total_amount
         and (orr.status ='002' OR  orr.status ='004')
        <if test="startTime != null ">
            and to_days(o.appointment_start_time) >= to_days(#{startTime})
        </if>
        <if test="endTime != null ">
            and to_days(o.appointment_start_time) &lt;= to_days(#{endTime})
        </if>
        <if test="categoryId != null and categoryId != ''">
            and (ca.category_id =  #{categoryId} or ca.parent_id =  #{categoryId} )
        </if>
    </select>

<!--    （003，004，007，（006的部分退款）） 获取订单列表(包含部分退款的订单)-->
    <select id="getRefundOrderList"  resultMap="OrderGoodsVoResult">
        select
        og.name,
        o.appointment_start_time,
        og.goods_counts,
        oa.address,
        oa.detailed,
        oa.name as cust_name,
        oa.phone,
        o.order_state,
        IFNULL(o.total_amount,0)  AS total_amount,
        ca.category_name,
        m.avatar,
        og.add_counts,
        o.add_amount,
        o.order_id,
        og.publish_id,
        o.order_type,
        o.maintenance_amount,
        o.add_pay_status,
        CASE WHEN og.goods_unit != '999'
        THEN sc1.dict_label
        ELSE og.other_unit END as unit
        from `order` o
        left join order_goods og on o.order_id = og.order_id and og.del_flag = 0
        left join sys_dict_data sc1 on og.goods_unit = sc1.dict_value and sc1.dict_type = 'goods_unit'
        left join order_address oa on o.order_id = oa.order_id and oa.del_flag = 0
        left join category ca on ca.category_id = og.cate_id and ca.del_flag = 0
        left join member m on m.member_id = o.member_id and m.del_flag = 0
        left join ( SELECT
        o.*
        FROM
        `order` o
        Left JOIN order_refund orr on o.order_id = orr.order_id
        left join order_goods og on o.order_id = og.order_id and og.del_flag = 0
        left join category ca on ca.category_id = og.cate_id and ca.del_flag = 0
        WHERE
        o.order_state = '006'
        and o.del_flag = 0
        and  og.server_id = #{serverId}
        and orr.is_del =0
        and orr.refund_money &lt; o.total_amount
        and (orr.status ='002' OR  orr.status ='004')
        AND ( o.order_type = '001' OR o.order_type='002')
        <if test="startTime != null ">
            and to_days(o.appointment_start_time) >= to_days(#{startTime})
        </if>
        <if test="endTime != null ">
            and to_days(o.appointment_start_time) &lt;= to_days(#{endTime})
        </if>
        <if test="categoryId != null and categoryId != ''">
            and (ca.category_id =  #{categoryId} or ca.parent_id =  #{categoryId} )
        </if>
        ) orr on orr.order_id = o.order_id
        where og.server_id = #{serverId}
        and o.del_flag = 0
        <if test="startTime != null ">
            and to_days(o.appointment_start_time) >= to_days(#{startTime})
        </if>
        <if test="endTime != null ">
            and to_days(o.appointment_start_time) &lt;= to_days(#{endTime})
        </if>
        <if test="categoryId != null and categoryId != ''">
            and (ca.category_id =  #{categoryId} or ca.parent_id =  #{categoryId} )
        </if>
        and  (o.order_state = '003' or  o.order_state = '004' or o.order_state = '007'  or orr.order_state='006')
        order by o.appointment_start_time desc
    </select>
</mapper>
