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

import io.swagger.models.auth.In;
import me.zhengjie.modules.dm.foodCate.domain.DmFoodCate;
import me.zhengjie.modules.system.domain.Menu;
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 javax.persistence.criteria.CriteriaBuilder;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @author sunmz
* @date 2021-09-15
**/
public interface DmFoodCateRepository extends JpaRepository<DmFoodCate, Integer>, JpaSpecificationExecutor<DmFoodCate> {

    @Query(value = "SELECT p.pcid,p.`name`,c.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(p.date,'%Y-%m-%d') = date_format(now(),'%Y-%m-%d') GROUP BY p.`name`",nativeQuery = true)
    List<Map<String,Object>> foodApp();

    /**
     * 获取节点数量
     * @param id /
     * @return /
     */
    int countByPid(Integer id);

    /**
     * 更新节点数目
     * @param count /
     * @param cateId /
     */
    @Modifying
    @Query(value = " update dm_food_cate set sub_count = ?1 where cate_id = ?2 ",nativeQuery = true)
    void updateSubCntById(int count, Integer cateId);

    /**
     * 根据菜单的 PID 查询
     * @param pid /
     * @return /
     */
    List<DmFoodCate> findByPid(Integer pid);

    @Query(value = "SELECT * FROM dm_food_cate WHERE pid = ?1",nativeQuery = true)//(SELECT cate_id FROM dm_food_cate WHERE id = ?1)
    List<DmFoodCate> findByPidUpdate(Integer cateId);

    @Query(value = "SELECT * FROM dm_food_cate WHERE cate_id = ?",nativeQuery = true)
    DmFoodCate findByCateId(Integer id);

    /**
     * 查询顶级菜单
     * @return /
     */
    List<DmFoodCate> findByPidIsNull();
}