<?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="orderTimeSum"    column="order_time_sum"    />
        <result property="orderNum"    column="order_num"    />

        <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"    />
    </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 og.create_time >= #{startTime}
        </if>
        <if test="endTime != null ">
          and og.create_time &lt;= #{endTime}
        </if>
        <if test="categoryId != null and categoryId != ''">
            and (ca.category_id =  #{categoryId} or ca.parent_id =  #{categoryId} )
        </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,
            CASE WHEN og.goods_unit != '099'
            THEN sc1.Decode_Value
            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_code sc1 on og.goods_unit = sc1.Decode_Id and sc1.Code_Id = '011'
                 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
          where og.server_id = #{serverId}
          and o.del_flag = 0
        <if test="orderState != null and orderState != ''">
            and o.order_state in (${orderState})
        </if>
        <if test="startTime != null ">
            and og.create_time >= #{startTime}
        </if>
        <if test="endTime != null ">
            and og.create_time &lt;= #{endTime}
        </if>
        <if test="categoryId != null and categoryId != ''">
            and (ca.category_id =  #{categoryId} or ca.parent_id =  #{categoryId} )
        </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,
            CASE WHEN og.goods_unit != '099'
            THEN sc1.Decode_Value
            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
        from `order` o
             left join order_goods og on o.order_id = og.order_id and og.del_flag = 0
             left join sys_code sc1 on og.goods_unit = sc1.Decode_Id and sc1.Code_Id = '011'
             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>


</mapper>