/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.dm.order.repository;

import me.zhengjie.modules.dm.order.domain.DmOrderRecord;
import me.zhengjie.modules.dm.order.service.dto.DmExpenseCalendar;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @author sunmz
* @date 2021-09-15
**/
public interface DmOrderRecordRepository extends JpaRepository<DmOrderRecord, Long>, JpaSpecificationExecutor<DmOrderRecord> {
    /**
     * 昨日订单数量
     * @return
     */
    @Query(value = "SELECT COUNT(1) FROM dm_order_record WHERE create_date BETWEEN DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL 1 DAY),'%Y-%m-%d 00:00:00') AND DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL 1 DAY),'%Y-%m-%d 23:59:59') ",nativeQuery = true)
    Integer countOrderQuantity();

    /**
     * 昨日收入
     * @return
     */
    @Query(value = "SELECT IF(SUM(amt) is null,0,SUM(amt)) FROM dm_order_record WHERE create_date BETWEEN DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL 1 DAY),'%Y-%m-%d 00:00:00') AND DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL 1 DAY),'%Y-%m-%d 23:59:59') ",nativeQuery = true)
    Integer countIncome();

    /**
     * 昨日消费人数
     * @return
     */
    @Query(value = "SELECT count(1) from ( " +
            " SELECT DISTINCT mob FROM dm_order_record WHERE create_date BETWEEN DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL 1 DAY),'%Y-%m-%d 00:00:00') AND DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL 1 DAY),'%Y-%m-%d 23:59:59') " +
            " ) a ",nativeQuery = true)
    Integer countConsumerNumber();

    @Query(value = "SELECT a.`id`, a.`bh`, a.`money`, a.`mob`, a.`user_code`, a.`user_name`, a.`dept_id`, " +
            "a.`user_type`, a.`phone`, a.`create_date`, a.`total_money`, a.`zk_money`, a.`seq`, a.`card`, " +
            "a.`card_no`, a.`part`, a.`term`, a.`shop`, a.`term_name`, a.`shop_name`, a.`amt`, a.`balance`, " +
            "a.`account`, a.`type`,b.id as orderItemId,b.bh as orderItemBh,b.pid ," +
            "b.code ,b.name ,b.price,b.num ," +
            "b.money as orderItemMoney,b.seq as orderItemSeq,b.amt as orderItemAmt,b.pj_type as pjType  " +
            "FROM dm_order_record a LEFT JOIN dm_order_item b ON a.bh=b.bh " +
            "WHERE date_format(create_date,'%Y-%m-%d') = date_format(now(),'%Y-%m-%d') ",nativeQuery = true)
    List<Map<String,Object>> selectOrderRecord();

    /**
     * 通过seq流水号查询消费记录
     *
     * */
    @Query(value = "SELECT id, seq, bh from dm_order_record where seq = :seq",nativeQuery = true)
    List<Map<String, Object>> selectOrderRecordBySeq(@Param("seq") Long seq);

    @Modifying
    @Query(value = "INSERT INTO `dm_order_record`(`bh`, `money`, `mob`, `user_code`, `user_name`, `dept_id`, `user_type`, `phone`, `create_date`, " +
            "`total_money`, `zk_money`, `seq`, `card`, `card_no`, `part`, `term`, `shop`, `term_name`, `shop_name`, `amt`, `balance`, `account`, `type`) " +
            "VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20, ?21, ?22, ?23) ",nativeQuery = true)
    void insertOrderRecord(String bh, String money, String mob, String userCode, String userName, Long deptId, String userType, String phone, Timestamp createDate, String totalMoney, String zkMoney, Long seq, String card, String cardNo, Integer part, Integer term, Integer shop, String termName, String shopName, Double amt, Double balance, Integer account, Integer type);

    @Modifying
    @Query(value = "INSERT INTO `dm_order_item`(`bh`, `pid`, `code`, `name`, `price`, `num`, `money`, `seq`, `amt`, `pj_type`) " +
            "VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10) ",nativeQuery = true)
    void insertDmOrderItem(String bh,Long pid,String code,String name,String price,Integer num,String money,Long seq,Double amt,String pjType);

    /**
     * 食堂营业额
     * @return
     */
    @Query(value = "SELECT SUM(amt) FROM dm_order_record ",nativeQuery = true)
    Integer countOrderAmt();
    /**
     * 食堂营业额
     * @return
     */
    @Query(value = "SELECT SUM(amt) FROM dm_order_record where DATE_FORMAT(create_date,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') ",nativeQuery = true)
    BigDecimal countOrderAmtByNow();
    /**
     * 食堂消费人数
     * @return
     */
    @Query(value = "SELECT COUNT(DISTINCT(mob)) FROM dm_order_record where DATE_FORMAT(create_date,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') ",nativeQuery = true)
    Integer countOrderPersonByNow();
    /**
     * 查询浸提食品消耗
     * @return
     */
    @Query(value = "SELECT if(AVG(pj.pj_star) is null,0,AVG(pj.pj_star)) as grade, COUNT(DISTINCT(pj.user_code)) as code_num,count(pj.food_code) as food_num FROM dm_order_record as record INNER JOIN dm_order_item as item on item.bh = record.bh left join dm_food_pj as pj on pj.order_bh = item.bh where DATE_FORMAT(record.create_date,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') and pj.food_code = :cateId ",nativeQuery = true)
    Map<String,Object> selectOrderSumByCateId(@Param("cateId") String cateId);
}