/*
*  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.daypc.repository;

import me.zhengjie.modules.dm.daypc.domain.DmDayPc;
import me.zhengjie.modules.dm.food.domain.DmFood;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @author sunmz
* @date 2021-09-22
**/
public interface DmDayPcRepository extends JpaRepository<DmDayPc, Long>, JpaSpecificationExecutor<DmDayPc> {

//    @Query(value = "SELECT DISTINCT a.`pcid`, `pid`, a.`id`, a.`name`, a.`price`, `times`, `cate`, `cate_name`, a.`state`, " +
//            " `window`, `window_name`, a.`pstate`, a.`barcode`, a.`no`, a.`goods`, a.`pinyin`, a.`qty_book`, `seq`, `date`, `shopname`,0 as `food.id`,null as `food.code`," +
//            "null as `food.name`,null as `food.type`,null as `food.price`,null as `food.picture`,null as `food.remark`," +
//            "null as `food.create_by`,null as `food.update_by`,null as `food.create_time`,null as `food.update_time`," +
//            "0 as `food.num` FROM dm_day_pc a LEFT JOIN dm_food b ON a.pid=b.id " +
//            "WHERE date_format(date,'%Y-%m-%d') = date_format(now(),'%Y-%m-%d') ",nativeQuery = true)
//    List<DmDayPc> foodRepository();

//    @Query(value = "SELECT DISTINCT a.`pcid`, a.`pid`, a.`id`, a.`name`, a.`price`, `times`,a.`cate`, a.`cate_name`, a.`state`," +
//            " `window`,`window_name`, a.`pstate`, a.`barcode`, a.`no`, a.`goods`, a.`pinyin`, a.`qty_book`, `seq`, `date`," +
//            " `shopname`,0 as cate_id,0 as pid,NULL as cate_name,null as picture,null as create_by,null as update_by," +
//            "null as create_time,null as update_time,NULL AS `storage_id`, NULL AS `real_name`, NULL AS `name`, NULL AS `suffix`," +
//            " NULL AS `path`, NULL AS `url`, NULL AS `type`, NULL AS `size`, NULL AS `create_by`, NULL AS `update_by`," +
//            " NULL AS `create_time`, NULL AS `update_time` FROM dm_day_pc a LEFT JOIN dm_food_cate c ON a.cate = c.cate_id " +
//            "LEFT JOIN tool_local_storage s ON c.url = s.storage_id " +
//            "WHERE date_format(date,'%Y-%m-%d') = date_format(now(),'%Y-%m-%d')",nativeQuery = true)
//    List<DmDayPc> foodRepository();

    @Query(value = "SELECT p.pcid,p.`name`,cate_id,c.cate_name,t.url,p.price,COUNT(p.`name`) AS `no` FROM dm_day_pc p " +
            "LEFT JOIN dm_food_cate c ON c.cate_id = p.cate LEFT JOIN tool_local_storage t ON " +
            "c.url = t.storage_id WHERE date_format(date,'%Y-%m-%d') = date_format(now(),'%Y-%m-%d') GROUP BY p.`name`",nativeQuery = true)
    List<Map<String,Object>> foodRepository();

    /**
     * 今日菜品数量
     * @return
     */
    @Query(value = "SELECT COUNT(1) FROM dm_day_pc WHERE to_days(date) = to_days(now()) ",nativeQuery = true)
    Integer countDishesNumber();
}