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

import me.zhengjie.modules.dmExpenseCalendar.domain.DmExpenseCalendar;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
* @website https://el-admin.vip
* @author zhirong
* @date 2021-11-23
**/
public interface DmExpenseCalendarRepository extends JpaRepository<DmExpenseCalendar, Long>, JpaSpecificationExecutor<DmExpenseCalendar> {

    //今日消费 总人数
    @Query(value = "  SELECT COUNT(1) FROM (SELECT COUNT(1) FROM dm_expense_calendar WHERE TO_DAYS(recharge_time) = TO_DAYS(now()) group by card_number) tmp ",nativeQuery = true)
    Integer getSumExpense();

    //今日消费 总金额
    @Query(value = " SELECT SUM(recharge_amount) FROM dm_expense_calendar WHERE TO_DAYS(recharge_time) = TO_DAYS(now()) ",nativeQuery = true)
    String getSumAmOut();

    //今日消费 餐厅
    @Query(value = " SELECT COUNT(1) FROM (SELECT COUNT(1) FROM dm_expense_calendar WHERE content = '餐厅' AND TO_DAYS(recharge_time) = TO_DAYS(now()) group by card_number) tmp ",nativeQuery = true)
    Integer getContentCt();

    //今日消费 其他
    @Query(value = " SELECT COUNT(1) FROM dm_expense_calendar WHERE content = '其他' AND TO_DAYS(recharge_time) = TO_DAYS(now()) ",nativeQuery = true)
    Integer getContentQt();

    //今日消费 人脸识别
    @Query(value = " SELECT COUNT(1) FROM dm_expense_calendar WHERE pattern_payment = '人脸识别' AND TO_DAYS(recharge_time) = TO_DAYS(now()) ",nativeQuery = true)
    Integer getPatternRl();

    //今日消费 园区卡
    @Query(value = " SELECT COUNT(1) FROM dm_expense_calendar WHERE pattern_payment = '园区卡' AND TO_DAYS(recharge_time) = TO_DAYS(now()) ",nativeQuery = true)
    Integer getPatternCt();

    //今日消费 其他
    @Query(value = " SELECT COUNT(1) FROM dm_expense_calendar WHERE pattern_payment = '其他' AND TO_DAYS(recharge_time) = TO_DAYS(now()) ",nativeQuery = true)
    Integer getPatternQt();

    /**
     * 根据seq查询
     *
     * @param seq
     * @return
     */
    @Query(value = "SELECT r.* FROM dm_expense_calendar r WHERE r.seq = :seq", nativeQuery = true)
    List<DmExpenseCalendar> findBySeq(@Param("seq") String seq);
}