<?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.willalp.canteen.mapper.HsCanteenOrderMapper">

    <resultMap type="HsCanteenOrder" id="HsCanteenOrderResult">
        <result property="orderId" column="order_id"/>
        <result property="orderNumber" column="order_number"/>
        <result property="userNumber" column="user_number"/>
        <result property="menuNumber" column="menu_number"/>
        <result property="orderTotal" column="order_total"/>
        <result property="deviceNumber" column="device_number"/>
        <result property="deviceName" column="device_name"/>
        <result property="paytime" column="paytime"/>
        <result property="zfsj" column="zfsj"/>
        <result property="zfbz" column="zfbz"/>
        <result property="jgid" column="jgid"/>
        <result property="isDelete" column="is_delete"/>
        <result property="consumeSign" column="consume_sign"/>
        <result property="nickName" column="nick_name"/>
        <result property="patternId" column="pattern_id"/>
        <result property="placeNumber" column="place_number"/>
        <result property="phonenumber" column="phonenumber"/>
        <result property="placeName" column="place_name"/>
        <result property="cardCode" column="card_code"/>
    </resultMap>

    <sql id="selectHsCanteenOrderVo">
        select a.order_id,
               a.order_number,
               a.user_number,
               a.menu_number,
               a.order_total,
               a.device_number,
               a.device_name,
               a.paytime,
               a.zfbz,
               a.jgid,
               a.is_delete,
               b.nick_name,
               a.consume_sign,
               c.pattern_id,
               a.place_number,
               a.zfsj,
               b.phonenumber,
               d.card_code,
               f.place_name
        from hs_canteen_order a
                 left join sys_user b on a.user_number = b.user_name
                 left join hs_canteen_menu c on a.menu_number = c.menu_number
                 left join sys_card_info d on a.user_number = d.user_id and a.jgid = d.organization_code
                 left join hs_canteen_dining_place f on a.place_number = f.place_number
    </sql>

    <select id="selectHsCanteenOrderList" parameterType="HsCanteenOrder" resultMap="HsCanteenOrderResult">
        <include refid="selectHsCanteenOrderVo"/>
        <where>
            a.is_delete = 0
            <if test="orderNumber != null  and orderNumber != ''">and a.order_number = #{orderNumber}</if>
            <if test="nickName != null  and nickName != ''">and b.nick_name = #{nickName}</if>
            <if test="userNumber != null  and userNumber != ''">and a.user_number = #{userNumber}</if>
            <if test="menuNumber != null  and menuNumber != ''">and a.menu_number = #{menuNumber}</if>
            <if test="deviceNumber != null  and deviceNumber != ''">and a.device_number = #{deviceNumber}</if>
            <if test="deviceName != null  and deviceName != ''">and a.device_name like concat('%', #{deviceName}, '%')
            </if>
            <!--            <if test="periodId != null "> and period_id = #{periodId}</if>-->
            <if test="patternId != null ">and c.pattern_id = #{patternId}</if>
            <!--            <if test="consumerType != null "> and consumer_type = #{consumerType}</if>-->
            <if test="params.beginPaytime != null and params.beginPaytime != '' and params.endPaytime != null and params.endPaytime != ''">
                and paytime between #{params.beginPaytime} and #{params.endPaytime}
            </if>
            <if test="zfbz != null ">and a.zfbz = #{zfbz}</if>
            <if test="jgid != null and jgid != ''">and a.jgid = #{jgid}</if>
            <if test="placeNumber != null and placeNumber != ''">and a.place_number = #{placeNumber}</if>
        </where>
        order by a.paytime desc
    </select>

    <select id="getOrderNow" parameterType="CustomGetOrderVo" resultType="CustomGetOrderResp">
        SELECT
        a.order_number as orderNumber,
        a.user_number as userNumber,
        a.order_total as orderTotal,
        a.paytime as paytime,
        a.jgid as jgid,
        b.period_id as periodId,
        b.use_date as useDate,
        a.zfbz as zfbz
        FROM
        hs_canteen_order a
        JOIN hs_canteen_menu b ON a.menu_number = b.menu_number
        WHERE
        Date(b.use_date) = #{useDate}
        and b.period_id = #{periodId}
        <if test="placeNumber != null and placeNumber != ''">and a.place_number = #{placeNumber}</if>
        <if test="userNumber != null and userNumber != ''">and a.user_number = #{userNumber}</if>
        <if test="jgid != null and jgid != ''">and a.jgid = #{jgid}</if>
    </select>

    <select id="getMenuNow" parameterType="CustomGetOrderVo" resultType="CustomGetOrderResp">
        SELECT b.menu_number menuNumber,
               b.menu_name   menuName
        FROM hs_canteen_menu b
        WHERE Date(b.use_date) = #{useDate}
          and b.period_id = #{periodId}
    </select>

    <update id="updateCanteenOrderZfpb">
        update hs_canteen_order
        set zfbz=1,
            zfsj = NOW()
        where order_id = #{orderid}
          and jgid = #{jgid}
    </update>

    <select id="selectHsCanteenOrderLists" parameterType="HsCanteenOrder" resultMap="HsCanteenOrderResult">
        select substr(a.paytime,1,10) as paytime ,count(a.order_id) as order_id ,sum(a.order_total) as order_total from
        (
        select paytime , order_id, order_total from hs_canteen_order
        <where>
            is_delete = 0 and zfbz=0
            <if test="jgid != null and jgid != ''">and jgid = #{jgid}</if>
        </where>
        ) a
        group by substr(paytime,1,10) order by paytime desc limit 30
    </select>

    <select id="getSumpay" parameterType="HsCanteenOrder" resultType="java.lang.String">
        select sum(order_total) as fee
        from hs_canteen_order
        where zfbz = 0

    </select>

    <select id="getSumOrderNum" parameterType="HsCanteenOrder" resultType="java.lang.String">
        select count(*) as total
        from hs_canteen_order
        where zfbz = 0

    </select>

    <select id="checkSfzf" parameterType="HsCanteenOrder" resultType="java.util.Map">
        select order_total
        from hs_canteen_order
        where order_number = #{orderNumber}
          and (zfbz = 0 or zfbz = 2)
          and is_delete = 0
        union
        select ifnull(sum(order_total), 0)
        from hs_canteen_order
        where zfbz = 3
          and order_number = #{orderNumber}
          and is_delete = 0
    </select>

    <select id="getConsumeReport" resultType="java.util.Map">
        select t.user_name                                   as list_number,
               t.nick_name                                   as list_name,
               date_format(t.paytime, '%Y-%m-%d %T')         as list_time,
               case
                   when t.sign = 0 then '积分消费'
                   when t.sign = 1 then '记账消费'
                   when t.sign = 2 then '因错退还'
                   when t.sign = 3 then '积分消费作废'
                   when t.sign = 4 then '记账消费作废'
                   end                                       as list_type,
               case when t.sign = 0 then total else 0 end    as list_czjf,
               case when t.sign in (2, 3) then tk else 0 end as list_tkjf,
               case when t.sign = 1 then total else 0 end    as list_czed,
               case when t.sign = 4 then tk else 0 end       as list_tked
        from (
                 SELECT b.user_name,
                        b.nick_name,
                        a.paytime,
                        CASE WHEN a.zfbz = 3 THEN 2 ELSE a.consume_sign END AS sign,
                        CASE WHEN a.zfbz != 3 THEN a.order_total ELSE 0 END AS total,
                        CASE WHEN a.zfbz = 3 THEN -a.order_total ELSE 0 END AS tk,
                        a.jgid
                 FROM hs_canteen_order a
                          JOIN sys_user b ON a.user_number = b.user_name
                 UNION ALL
                 SELECT b.user_name,
                        b.nick_name,
                        a.zfsj             as paytime,
                        a.consume_sign + 3 AS sign,
                        0                  AS total,
                        - a.order_total    AS tk,
                        a.jgid
                 FROM hs_canteen_order a
                          JOIN sys_user b ON a.user_number = b.user_name
                 where a.zfbz = 1
             ) t
        where date(t.paytime) >= #{begin}
          and date(t.paytime) &lt;= #{end}
          and t.jgid = #{organizationCode}
        order by t.paytime
    </select>
    <select id="getOrderDataBasedOnMenu"
            resultType="com.willalp.canteen.domain.vo.HsCanteenOrderInfoVo">
        SELECT hco.menu_number,
               hco.user_number,
               hcdp.place_name,
               su.nick_name,
               hco.order_number,
               hcod.cuisine_name,
               hcod.cuisine_number,
               hcod.price,
               hcod.amount,
               hcod.total
        FROM hs_canteen_order hco
                 LEFT JOIN hs_canteen_order_detail hcod ON hcod.menu_number = hco.menu_number
                 LEFT JOIN sys_user su ON su.user_name = hco.user_number
                 LEFT JOIN hs_canteen_dining_place hcdp on hcdp.place_number = hco.place_number
        WHERE hco.menu_number = #{menuCode}
          AND hco.is_delete = 0
          AND hco.zfbz = 0
        ORDER BY hco.order_number, hcod.cuisine_number
    </select>
    <select id="todayNotOrderUser" resultType="com.willalp.canteen.domain.HsCanteenOrder">
        SELECT user_number
        FROM `hs_canteen_order`
        WHERE DATE(paytime) != SUBSTR(NOW(), 1, 9)
        GROUP BY user_number;
    </select>

</mapper>
