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

import me.zhengjie.modules.dm.device.domain.DmDevice;
import me.zhengjie.modules.dm.service.domain.DmServicePj;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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 org.springframework.transaction.annotation.Transactional;

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

/**
* @website https://el-admin.vip
* @author sunmz
* @date 2021-09-15
**/
public interface DmServicePjRepository extends JpaRepository<DmServicePj, Integer>, JpaSpecificationExecutor<DmServicePj> {
    /**
     * 状态修改
     * @param enabled
     */
    @Transactional
    @Modifying
    @Query(value = " update dm_service_pj set enabled = ?1 ",nativeQuery = true)
    void updatePj(String enabled);

    @Query(value = "SELECT COUNT(1) FROM dm_service_pj WHERE to_days(pj_time) = to_days(now()) AND user_code = :userCode",nativeQuery = true)
    int servicePjApp(@Param("userCode") String userCode);

    @Query(value = "SELECT DISTINCT enabled FROM dm_service_pj",nativeQuery = true)
    Map<String,Object> enableApp();

    /**
     * 用户服务评价详情
     * @param userCode
     * @return
     */
    @Query(value = "SELECT `id`, `user_name`, `user_code`, `user_number`, `total_point`, `pj_time`, `wd_point`, `pz_point`," +
            " `fl_point`, `fwtd_point`, `hjws_point`, `zhfw_point`, `cxgj_point`, `remark`, `enabled` FROM dm_service_pj " +
            "WHERE user_code = ?1 and date_format( pj_time, '%Y-%m-%d' ) = date_format(now(),'%Y-%m-%d')",nativeQuery = true)
    Map<String,Object> getDmServicePjByUserCodeAndPjTime(String userCode);

    @Query(value = "SELECT COUNT(DISTINCT(user_code)) as num,if(AVG(zhfw_point) is null,0,AVG(zhfw_point)) as point FROM dm_service_pj WHERE DATE_FORMAT(pj_time,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')",nativeQuery = true)
    Map<String,Object> getDmServicePjByNow();

    @Query(value = "SELECT * FROM dm_service_pj WHERE DATE_FORMAT(pj_time,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')",nativeQuery = true)
    List<DmServicePj> getDmServicePjContentByNow();
}