<?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="org.jeecg.modules.system.app.mapper.AppOrderMapper">

    <select id="queryPage" resultType="org.jeecg.modules.system.app.vo.OrderPageVO">
        SELECT b.* FROM (SELECT
        o.id,
        o.order_code,
        o.order_type,
        o.create_time,
        s.`name` siteName,
        f.depart_name departName,
        o.amount,
        o.price,
        GROUP_CONCAT( DISTINCT opi.user_phone ) userPhone
        FROM
        nm_order o
        LEFT JOIN nm_site s ON s.id = o.address_site_id
        LEFT JOIN sys_depart f ON f.org_code = s.org_code
        LEFT JOIN nm_order_pro_info opi ON opi.order_id = o.id
        <where>
            o.del_flag = 0
            <if test="appOrderPageForm.orgCode != null and appOrderPageForm.orgCode !=''">
                and o.org_code like CONCAT(#{appOrderPageForm.orgCode},'%')
            </if>
            <if test="appOrderPageForm.userPhone != null and appOrderPageForm.userPhone !=''">
                and opi.user_phone LIKE CONCAT('%',#{appOrderPageForm.userPhone},'%')
            </if>
            <if test="appOrderPageForm.orderCode != null and appOrderPageForm.orderCode !=''">
                and o.order_code LIKE CONCAT('%',#{appOrderPageForm.orderCode},'%')
            </if>
            <if test="appOrderPageForm.orderStatus != null">
                and o.order_status = #{appOrderPageForm.orderStatus}
            </if>
            <if test="appOrderPageForm.afterSaleStatus != null">
                and o.after_sale_status = #{appOrderPageForm.afterSaleStatus}
            </if>
            <if test="appOrderPageForm.startTime != null">
                and DATE_FORMAT(o.create_time,'%Y-%m-%d %H:%i:%s') <![CDATA[ >= ]]> #{appOrderPageForm.startTime}
            </if>
            <if test="appOrderPageForm.endTime != null">
                and DATE_FORMAT(o.create_time,'%Y-%m-%d %H:%i:%s') <![CDATA[ <= ]]> #{appOrderPageForm.endTime}
            </if>
        </where>
        GROUP BY o.id
        ORDER BY o.create_time DESC) AS b

    </select>
    <select id="countTypeAndProductIds" resultType="java.lang.Integer">
        select  count(1) from nm_order where product_ids in
        <foreach collection="productIds" item="productId" open="(" separator="," close=")">
            #{productId}
        </foreach>
        and `type`=#{type} and order_status in (1,2,3)
    </select>
    <select id="findByNewOrderInfo" resultType="org.jeecg.modules.app.vo.game.GameOrderVo">
        select b.avatar,b.username  from nm_order a
        left join  sys_user b on a.user_id = b.id
        where a.product_ids in
        <foreach collection="productIds" item="productId" open="(" separator="," close=")">
            #{productId}
        </foreach>
        and a.`type`=#{orderType} and a.order_status in (1,2,3)
        order by a.pay_time desc
        limit 20
    </select>
    <select id="findByTeachingCount" resultType="java.lang.Long">
        select count(1)  from nm_order_pro_info
        where  product_id in
        <foreach collection="productIds" item="productId" open="(" separator="," close=")">
            #{productId}
        </foreach>
    </select>
    <select id="findByOrderNumCount" resultType="java.lang.Long">
        SELECT COUNT(DISTINCT order_id)
        FROM nm_order_pro_info
        WHERE type = 5
        AND order_status IN (1, 2)
        <if test="productIds != null and !productIds.isEmpty()">
            AND product_id IN
            <foreach collection="productIds" item="productId" open="(" separator="," close=")">
                #{productId}
            </foreach>
        </if>
        <if test="productIds == null or productIds.isEmpty()">
            AND 1 = 0
        </if>
    </select>
    <select id="queryBookedCount" resultType="java.lang.Integer">
        SELECT COUNT(opi.id) AS bookedCount
        FROM nm_order a
                 JOIN nm_site_price_rules b ON a.product_ids = b.id
                 LEFT JOIN nm_order_pro_info opi ON a.id = opi.order_id
        WHERE b.site_place_id = #{placeId}
          AND b.date_of_sale = #{date}
          AND b.start_time = #{startTime}
          AND b.end_time = #{endTime}
          AND opi.type <![CDATA[!=]]> 6
          AND b.del_flag = 0;
    </select>
    <select id="pageOrders" resultType="org.jeecg.modules.app.vo.PageOrdersVO">
        SELECT
            o.id AS orderId,
            o.order_code,
        o.product_ids,
            o.order_status,
            o.order_type,
            o.original_price,
            o.price,
            o.amount,
            o.create_time,
            o.address_site_id
        FROM
            nm_order o
        <where>
            del_flag = 0
            <if test="pageOrdersForm.orAfterSale != null and pageOrdersForm.orAfterSale == 0">
                <if test="pageOrdersForm.orderStatus != null ">
                    and o.order_status = #{pageOrdersForm.orderStatus}
                </if>
            </if>
            <if test="pageOrdersForm.orAfterSale != null and pageOrdersForm.orAfterSale == 1">
                and (o.order_status = 5 or o.order_status = 6)
            </if>
            <if test="pageOrdersForm.userId != null and pageOrdersForm.userId != ''">
                and o.user_id = #{pageOrdersForm.userId}
            </if>
        </where>
          ORDER BY o.create_time DESC
    </select>
    <select id="findByJob" resultType="org.jeecg.modules.quartz.vo.JobClassNoticeVo">
        select a.id,a.user_id,c.user_open_id ,b.start_time,d.`name` as siteName
        from  nm_courses a
                  LEFT JOIN  nm_courses_price_rules b on a.id = b.courses_id and b.del_flag=0
                  LEFT JOIN nm_order c on  a.id =c.product_ids and order_status =1
                  LEFT JOIN nm_site d on a.address_site_id =d.id  and d.del_flag=0
        WHERE 1=1
        <if test="hoursBefore != null and hoursBefore==8">
            and c.notified8h = false
            <![CDATA[  and DATE_FORMAT(b.start_time, '%Y-%m-%d %H:%i') <= DATE_FORMAT(#{date}, '%Y-%m-%d %H:%i') ]]>
        </if>
        <if test="hoursBefore != null and hoursBefore==24">
            and c.notified24h = false
            <![CDATA[  and DATE_FORMAT(b.start_time, '%Y-%m-%d %H:%i') <= DATE_FORMAT(#{date}, '%Y-%m-%d %H:%i') ]]>
        </if>
        and notify_retry_count = #{maxRetry}
    </select>
    <select id="findByReservationSite" resultType="org.jeecg.modules.quartz.vo.JobReservationSiteVo">
        select b.user_open_id,e.`name`,e.id as siteId from nm_order_pro_info a
        INNER JOIN nm_order b on a.order_id =b.id and b.order_type =0
        INNER JOIN nm_site_price_rules c  on  a.product_id =c.id and  c.type =0
        INNER JOIN nm_site_place d on c.site_place_id =d.id
        INNER JOIN nm_site e on  d.site_id = e.id and e.del_flag =0  and e.type =0
        GROUP BY b.user_open_id,e.id
    </select>
    <select id="findByReservationSiteTicket" resultType="org.jeecg.modules.quartz.vo.JobReservationSiteVo">
        SELECT
            e.id as siteId,
            p.start_time,
            p.end_time,
            p.date_of_sale,
            p.inventory,
            e.`name` as siteName
        FROM
            nm_site_price_rules p
                INNER JOIN (
                SELECT
                    site_place_id,
                    MIN(start_time) AS min_start_time
                FROM
                    nm_site_price_rules
                WHERE
                    type = 0
                  AND view_status = 0
                  AND date_of_sale = DATE_ADD(CURDATE(), INTERVAL 8 HOUR)
                  AND start_time > DATE_ADD(CURRENT_TIME(), INTERVAL 8 HOUR)
                GROUP BY
                    site_place_id
            ) AS m ON p.site_place_id = m.site_place_id AND p.start_time = m.min_start_time
                INNER JOIN nm_site_place d on p.site_place_id =d.id
                INNER JOIN nm_site e on  d.site_id = e.id and e.del_flag =0 and e.type =0
        WHERE
            p.type = 0
          AND p.view_status = 0
          AND p.date_of_sale = DATE_ADD(CURDATE(), INTERVAL 8 HOUR)
          AND p.start_time > DATE_ADD(CURRENT_TIME(), INTERVAL 8 HOUR);
    </select>
    <select id="findByShopSumList" resultType="org.jeecg.modules.system.app.vo.statistics.FindByShopSumVO">
        <![CDATA[
        SELECT b.product_name AS shop_name, COALESCE(SUM(b.price), 0) AS total_price,
        COALESCE(COUNT(b.product_id), 0) AS product_count from nm_order a
        LEFT JOIN nm_order_pro_info b on a.id = b.order_id
        where b.type in (1,3)  and a.order_status in (1,2,3) and a.org_code like CONCAT('',#{orgCode},'%')  and a.pay_time >=#{startTime} and a.pay_time <=#{endTime}
        GROUP BY b.product_name
        order by product_count desc
        ]]>
    </select>
    <select id="findByProductIds" resultType="java.lang.Long">
        select count(1) from  nm_order where order_status in (0,1) and type =1 and product_ids in
        <foreach collection="productIds" item="productId" open="(" separator="," close=")">
            #{productId.id}
        </foreach>
    </select>
</mapper>