<?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.bofeng.dao.OrderMapper">
    <select id="selectAll" resultType="com.bofeng.entity.Order">
        select * from tb_order where be_active='Y'
    </select>

    <select id="search" resultType="com.bofeng.entity.Order">
        select * from tb_order
        where
        be_active='Y'
        <if test="saleName != null and saleName != ''">
            and sale_name like concat('%', #{saleName}, '%')
        </if>
        <if test="orderStartAt != null and orderStartAt != ''">
            <![CDATA[
            and arrival_at >= #{orderStartAt}
            ]]>
        </if>
        <if test="orderEndAt != null and orderEndAt != ''">
            <![CDATA[
            and arrival_at < #{orderEndAt}
            ]]>
        </if>
        <if test="placeId != null and placeId != ''">
            and place_id = #{placeId}
        </if>
        <if test="roomName != null and roomName != ''">
            and room_name = #{roomName}
        </if>
        <if test="customerName != null and customerName != ''">
            and customer_name = #{customerName}
        </if>
        <if test="paid != null and paid != ''">
            and paid = #{paid}
        </if>
        order by arrival_at desc
    </select>


    <select id="selectBySale" resultType="com.bofeng.entity.Order">
select
  o.*,
  oc.cms_amount
from
  tb_order o
  inner join tb_order_cms oc on o.order_id=oc.order_id
where
  oc.sale_user_id = #{userId}
  and DATE_FORMAT(pay_at,'%Y-%m')=#{timeMonth}
  and o.be_active='Y'
  and o.paid=1
order by
  pay_at desc
    </select>

    <select id="groupByStatistics" resultType="com.bofeng.entity.StatisticsOrder">
        select
        <choose>
            <when test="statistics != null and 'W'.toString() eq statistics.toString() ">
                CONCAT(YEAR(pay_at),'-',DATE_FORMAT(pay_at,'%U')) times,
            </when>
            <when test="statistics != null and 'M'.toString() eq statistics.toString() ">
                CONCAT(YEAR(pay_at),'-',DATE_FORMAT(pay_at,'%m')) times,
            </when>
            <when test="statistics != null and 'Q'.toString() eq statistics.toString() ">
                CONCAT(YEAR(pay_at),'-',quarter(pay_at)) times,
            </when>
            <when test="statistics != null and 'Y'.toString() eq statistics.toString() ">
                DATE_FORMAT(pay_at,'%Y') times,
            </when>
            <otherwise>
                CONCAT(YEAR(pay_at),'-',DATE_FORMAT(pay_at,'%m'),'-',DATE_FORMAT(pay_at,'%d')) times,
            </otherwise>
        </choose>
        sum(o.pay_amount) as totalAmount,
        count(o.order_id) as orderCount,
        count(distinct room_id) as activeRoomCount,
        sum(oc.cms_amount) as totalCommission
        from
        tb_order o
        inner join tb_order_cms oc on o.order_id=oc.order_id
        <where>
            o.be_active='Y'
            and oc.be_active='Y'
            and o.paid=1
            <if test="saleName != null and saleName != ''">
                and oc.sale_name like concat('%', #{saleName}, '%')
            </if>
            <if test="orderStartAt != null and orderStartAt != ''">
                <![CDATA[
                and pay_at >= #{orderStartAt}
                ]]>
            </if>
            <if test="orderEndAt != null and orderEndAt != ''">
                <![CDATA[
                and pay_at < #{orderEndAt}
                ]]>
            </if>
            <if test="roomName != null and roomName != ''">
                and room_name = #{roomName}
            </if>
            <if test="customerName != null and customerName != ''">
                and customer_name = #{customerName}
            </if>

            <if test="userId != null and userId>0">
                and oc.sale_user_id = #{userId}
            </if>
        </where>
        group by times
    </select>

    <select id="serchCurrentMarkList" resultType="com.bofeng.entity.Order">
        select o.*,r.be_use as beUse from tb_order o left join tb_room r on o.room_id = r.room_id
        where
          o.sale_user_id = #{saleUserId}
          and o.paid = 0
          and o.be_active = 'Y'
    </select>


    <select id="selectCmsByUserId" resultType="com.bofeng.entity.Order">
select
  o.*,
  oc.cms_amount
from
  tb_order o
  inner join tb_order_cms oc on o.order_id=oc.order_id
where
  oc.sale_user_id =#{userId}
  and settle=0
  and o.be_active='Y'
  and oc.be_active='Y'
    </select>

    <update id="ordersSettle">
        update tb_order_cms set settle=1, settle_id=#{settleId}
        where cms_id in (
        <foreach collection="list" item="item" index="index" open="" close="" separator=",">
            #{item}
        </foreach>
        )
    </update>

    <update id="payFinish">
        update tb_order set
          pay_at=current_timestamp,
          pay_amount=#{payAmount},
          paid=1
        where
          order_id=#{orderId}
    </update>
    <update id="discard">
        update tb_order set be_active='D' where order_id=#{orderId}
    </update>

    <select id="selectByUserId" resultType="com.bofeng.entity.Order">
        select * from tb_order where (sale_id_1=#{userId} or sale_id_2=#{userId}) and be_active!='D' and be_paid='Y'
    </select>

    <select id="getSettleOrder" resultType="com.bofeng.entity.Settle">
        select
        t.*,
        u.staff_name as sale_name,
        p.place_name
        from
        (
        select
        o.place_id,
        oc.sale_user_id as sale_id,
        sum(oc.cms_amount) as commission,
        count(*) as order_count,
        max(o.pay_at) as order_start_at,
        min(o.pay_at) as order_end_at
        from
        tb_order o
        inner join tb_order_cms oc on o.order_id = oc.order_id
        where
        o.be_active='Y'
        and oc.be_active='Y'
        and o.paid=1
        and oc.settle=0
        <if test="placeId != null and placeId != ''">
            and o.place_id=#{placeId}
        </if>
        <if test="orderStartAt != null and orderStartAt != ''">
            <![CDATA[
        and o.pay_at >= #{orderStartAt}
            ]]>
        </if>
        <if test="orderEndAt != null and orderEndAt != ''">
            <![CDATA[
        and o.pay_at < #{orderEndAt}
            ]]>
        </if>
        group by
        o.place_id,
        oc.sale_user_id
        ) as t
        inner join tb_user u on t.sale_id = u.user_id
        inner join tb_place p on t.place_id = p.place_id
        where
        u.be_active='Y'
        <if test="saleName != null and saleName != ''">
            and u.staff_name like concat('%', #{saleName}, '%')
        </if>
    </select>

    <select id="selectOrderBySaleIdAndTimeSpace" resultType="com.bofeng.entity.SettleDetail">
        select
        oc.cms_id,
        o.pay_amount,
        oc.cms_amount,
        o.pay_at
        from
        tb_order o
        inner join tb_order_cms oc on o.order_id=oc.order_id
        where
        oc.be_active='Y'
        and oc.settle=0
        and oc.sale_user_id=#{saleId}
        and place_id=#{placeId}
        <if test="orderStartAt != null and orderStartAt != ''">
            <![CDATA[
    and o.pay_at >= #{orderStartAt}
        ]]>
        </if>
        <if test="orderEndAt != null and orderEndAt != ''">
            <![CDATA[
    and o.pay_at < #{orderEndAt}
        ]]>
        </if>
    </select>

    <select id="selectByRoomId" resultType="com.bofeng.entity.Order">
        select o.*,u.phone as salePhone from tb_order o left join tb_user u on o.sale_user_id=u.user_id where o.room_id=#{roomId} and o.paid=0 and o.be_active='Y'
    </select>

    <update id="discardByRoomId">
        update tb_order set be_active='D' where room_id=#{roomId}
    </update>

</mapper>
