<?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.gree.mall.manager.commonmapper.OrderMapper">


    <select id="selectByQueryMsg" resultType="com.gree.mall.manager.bean.order.OrderDetailListBean">
        select
        o.order_id as orderId,
        od.goods_name as goodsName,
        CONCAT(od.goods_spec_name,'(',od.goods_spec_value,')') as goodsSpecValue,
        od.num as num,
        od.price as price,
        od.pay_amount as payAmount,
        od.work_order_type,
        o.rece_user_name as receUserName,
        o.rece_phone as recePhone,
        CONCAT(o.province,o.city,o.area,o.street,o.rece_address,o.house_no) as address,
        o.order_status as orderStatus,
        o.refund_flag as refundFlag,
        od.refund_amount as refundAmount,
        od.refund_num as refundNum,
        o.remark as remark,
        o.create_time as createTime,
        o.pay_time,
        o.user_name as userName,
        o.worker_name as workerName,
        o.buyer_msg as buyerMsg,
        o.remark as remark,
        o.websit_name as websitName,
        CONCAT( o.worker_name,'/',o.websit_name) as workerNameWithWebsitName,
        od.share_amount as shareAmount ,
        u.nick_name as nickName,
        u.mobile as mobile,
        o.promotion_group_id as promotionGroupId,
        GROUP_CONCAT(odr.worker_order_no) workerOrderNo,
         o.comment_goods as commentGoods,
        o.comment_service as commentService,
        o.comment_express as commentExpress,
        o.worker_phone,
        o.promotion_package_goods,
        gs.goods_code,
        o.is_add_corp,
        o.buyer_name,
        o.buyer_id_card,
        o.coupon_merchant_contribute,
        gs.bar_code,
        o.transaction_id

        from order_detail od
         join order_info o  on od.order_id = o.order_id
        left join goods_spec gs on gs.goods_spec_id = od.goods_spec_id
        LEFT join `user` u  on o.promotion_group_user_id=u.user_id
        LEFT JOIN order_request odr on odr.order_id=o.order_id

        <where>
            <if test="userId != null and userId != ''">
                and o.user_id=#{userId}
            </if>
            <if test="orderId != null and orderId != ''">
                and o.order_id like  CONCAT('%',#{orderId},'%')
            </if>
            <if test="productName != null and productName != ''">
                and o.order_title like  CONCAT('%',#{productName},'%')
            </if>
            <if test="userName != null and userName != ''">
                and (o.rece_user_name like CONCAT('%',#{userName},'%') or o.user_name like CONCAT('%',#{userName},'%') )
            </if>
            <if test="phone != null and phone != ''">
                and (o.phone like  CONCAT('%',#{phone},'%') or o.rece_phone like CONCAT('%',#{phone},'%')  or o.worker_phone like CONCAT('%',#{phone},'%'))
            </if>
            <if test="exchangeCode != null and exchangeCode != ''">
                and o.exchange_code like  CONCAT('%',#{exchangeCode},'%')
            </if>
            <if test="orderStatus != null and orderStatus != ''">
                and o.order_status = #{orderStatus}
            </if>
            <if test="orderStatus != null and orderStatus=='DFH'">
                and od.refund_num = 0
            </if>

            <if test="startCreateTime != null and startCreateTime != '' and endCreateTime != null and endCreateTime != ''">
                and (o.create_time between #{startCreateTime} and #{endCreateTime})
            </if>

            <if test="startPayTime != null and startPayTime != '' and endPayTime != null and endPayTime != ''">
                and (o.pay_time between #{startPayTime} and #{endPayTime})
            </if>

            <if test="promotionGroupUserName != null and promotionGroupUserName != '' ">
                and pgu.nick_name   like  CONCAT('%',#{promotionGroupUserName},'%')
            </if>
            <if test="promotionGroupId != null and promotionGroupId != ''">
                and o.promotion_group_id=#{promotionGroupId}
            </if>
            <if test="promotionGroupUserId != null and promotionGroupUserId != ''">
                and o.promotion_group_user_id=#{promotionGroupUserId}
            </if>
            <if test="websitId != null and websitId !=''">
                and o.websit_id = #{websitId}
            </if>
            <if test="goodsId != null and goodsId !=''">
                and o.promotion_package_goods_id = #{goodsId}
            </if>
            <if test="companyWechatIds != null and companyWechatIds.size > 0">
                and o.company_wechat_id in
                <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="websitIds != null and websitIds.size > 0">
                and o.websit_id in
                <foreach item="item" index="index" collection="websitIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY od.order_detail_id
        order by o.create_time desc
    </select>


    <select id="abnormalDispatchOrder" resultType="com.gree.mall.manager.plus.entity.OrderInfo">
        SELECT DISTINCT * from (
            SELECT o.*
                from order_detail od
                    LEFT JOIN order_info o on od.order_id=o.order_id
                    LEFT JOIN order_request oq on oq.order_id=o.order_id
            WHERE o.work_order=1 and od.work_order_type='REPAIR' AND oq.order_request_id is NULL and (o.order_status='YFH' or o.order_status='OVER')
            UNION
            SELECT o.*
            from order_detail od
                LEFT JOIN order_info o on od.order_id=o.order_id
                LEFT JOIN order_request oq on oq.order_id=o.order_id
            WHERE o.work_order=1  and od.work_order_type='INSTALL' AND oq.order_request_id is NULL and o.order_status='OVER'
        ) a
        <where>
            <if test="userId != null and userId != ''">
                and user_id=#{userId}
            </if>
            <if test="orderId != null and orderId != ''">
                and order_id like  CONCAT('%',#{orderId},'%')
            </if>
            <if test="productName != null and productName != ''">
                and order_title like  CONCAT('%',#{productName},'%')
            </if>
            <if test="userName != null and userName != ''">
                and (rece_user_name like CONCAT('%',#{userName},'%') or user_name like CONCAT('%',#{userName},'%') )
            </if>
            <if test="phone != null and phone != ''">
                and (phone like  CONCAT('%',#{phone},'%') or rece_phone like CONCAT('%',#{phone},'%')  or worker_phone like CONCAT('%',#{phone},'%'))
            </if>
            <if test="exchangeCode != null and exchangeCode != ''">
                and exchange_code like  CONCAT('%',#{exchangeCode},'%')
            </if>
            <if test="orderStatus != null and orderStatus != ''">
                and order_status = #{orderStatus}
            </if>

            <if test="startCreateTime != null and startCreateTime != '' and endCreateTime != null and endCreateTime != ''">
                and (create_time between #{startCreateTime} and #{endCreateTime})
            </if>

            <if test="startPayTime != null and startPayTime != '' and endPayTime != null and endPayTime != ''">
                and (pay_time between #{startPayTime} and #{endPayTime})
            </if>

            <if test="isNegative == '2'.toString()">
                and comment_express &gt; 2 and comment_service &gt; 2 and comment_goods &gt; 2
            </if>
            <if test="isNegative == '0'.toString()">
                and comment_express = 0 and comment_service = 0 and comment_goods = 0
            </if>
            <if test="isNegative == '1'.toString()">
                and (comment_express &gt; 0 and comment_service &gt; 0 and comment_goods &gt; 0
                    and (
                        comment_express &lt; 3 or comment_service &lt; 3 or comment_goods &lt; 3
                    )
                )
            </if>
            <if test="isNegative == '3'.toString()">
                and comment_express &gt; 0 and comment_service &gt; 0 and comment_goods &gt; 0
            </if>
            <if test="companyWechatIds != null and companyWechatIds.size > 0">
                and company_wechat_id in
                <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="websitIds != null and websitIds.size > 0">
                and websit_id in
                <foreach item="item" index="index" collection="websitIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by create_time desc
    </select>
</mapper>