<?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.pz.system.mapper.TotalOrderMapper">

    <sql id="OrderCommon">
        select total_order.id                           as orderId,
               services.name                            as project,
               business.name                            as businessName,
               CONVERT((1 - services.fenmo / 100), decimal (10, 2)) *
               CONVERT(services.price, decimal (10, 2)) as commission,
               total_order.business_id,
               total_order.remark                       as description,
               total_order.evaluation_content,
               total_order.is_satisfaction              as evaluation_flag,
               total_order.uid                          as uid,
               total_order.status                       as orderStatus
        from total_order
                 left join services on total_order.service_id = services.id
                 left join business on total_order.business_id = business.id
    </sql>

    <select id="selectUndistributedTotalOrder" resultType="com.pz.system.domain.vo.AccompanyDemandVo">
        <include refid="OrderCommon"/>
        <where>
            <if test="ew.emptyOfWhere == false">
                and ${ew.sqlSegment}
            </if>
        </where>
    </select>

    <select id="selectTotalOrderById" resultType="com.pz.system.domain.vo.AccompanyDemandVo">
        <include refid="OrderCommon"/>
        where total_order.id = #{id}
    </select>
    <select id="selectAppOrder" resultType="com.pz.system.domain.vo.TotalOrderVo">

    </select>

    <select id="selectByOrderId" resultType="com.pz.system.domain.vo.TotalOrderVo">
        select t.id,
               t.order_sn,
               t.uid,
               t.em_id,
               t.city_id,
               t.business_id,
               t.service_id,
               t.pay_money,
               t.status,
               t.lng,
               t.lat,
               t.is_satisfaction,
               t.evaluation_content,
               t.refund_reason,
               t.refund_amount,
               t.pay_cs,
               t.del_flag,
               t.create_by,
               t.create_time,
               t.update_by,
               t.update_time,
               t.remark,
               e.name as emName,
               b.name as business,
               s.name as service
        from total_order t
                 left join business b on b.id = t.business_id
                 left join services s on s.id = t.service_id
                 left join employees e on e.id = t.em_id
        where t.id = #{id}
    </select>
    <select id="selectSaleroom" resultType="com.pz.system.domain.vo.DataOrderVo">
        SELECT IFNULL(ROUND(SUM(pay_money), 2),0) as dataSum FROM total_order WHERE `status` = 1 and refund_amount = 0
        <if test="dataViewBo.condition == 2">
            and finish_time >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) -- 近7天
        </if>
        <if test="dataViewBo.condition == 3">
            and finish_time >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) -- 近30天
        </if>
        <if test="dataViewBo.condition == 1">
            and DATE(finish_time) = CURDATE()
        </if>
    </select>
    <select id="selectPlatformRevenue" resultType="com.pz.system.domain.vo.DataOrderVo">
        SELECT IFNULL(ROUND(SUM(employees_divide.order_price - employees_divide.divide_price), 2),0) as dataSum FROM
        employees_divide
        left join total_order on total_order.order_sn = employees_divide.order_id WHERE total_order.`status` = 2 and
        refund_amount = 0
        <if test="dataViewBo.condition == 2">
            and employees_divide.create_time >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) -- 近7天
        </if>
        <if test="dataViewBo.condition == 3">
            and employees_divide.create_time >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) -- 近30天
        </if>
        <if test="dataViewBo.condition == 1">
            and DATE(employees_divide.create_time) = CURDATE()
        </if>
    </select>
    <select id="selectOrderQuantity" resultType="com.pz.system.domain.vo.DataOrderVo">
        SELECT count(*) as dataSum FROM total_order WHERE `status` != 0 and refund_amount = 0
        <if test="dataViewBo.condition == 2">
            and finish_time >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) -- 近7天
        </if>
        <if test="dataViewBo.condition == 3">
            and finish_time >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) -- 近30天
        </if>
        <if test="dataViewBo.condition == 1">
            and DATE(finish_time) = CURDATE()
        </if>
    </select>

    <select id="selectDataToMessageById" resultType="com.pz.system.domain.OrderMessage">
        select total_order.id     as totalOrderId,
               total_order.city_id,
               business.name      as businessName,
               services.name      as serviceName,
               sys_user.nick_name as userName
        from total_order
                 left join business on total_order.business_id = business.id
                 left join services on total_order.service_id = services.id
                 left join sys_user on sys_user.user_id = total_order.uid
        where total_order.id = #{id}
    </select>
    <select id="findOrderColumnarVoList" resultType="com.pz.system.domain.vo.OrderColumnarVo">
        SELECT b.`name` name,DATE (#{time}) AS time, COUNT(t.business_id) AS num
        FROM business b
        LEFT JOIN total_order t ON t.business_id = b.id
        AND t.`status` = 2
        AND DATE (t.create_time) = DATE (#{time})
        <if test="emIds != null and emIds.size() > 0">
            AND t.em_id IN(emIds)
        </if>
        GROUP BY b.id;
    </select>
    <select id="findStoreOrderVoList" resultType="com.pz.system.domain.vo.OrderColumnarVo">
        SELECT '商城订单' name,DATE (#{time}) AS time,COUNT(1) num FROM total_order
        WHERE business_id = 0 AND `status` = 2
        AND DATE (create_time) = DATE (#{time})
        <if test="emIds != null and emIds.size() > 0">
            AND em_id IN(emIds)
        </if>
    </select>
</mapper>