package me.zhengjie.modules.dm.statement.repository;

import me.zhengjie.modules.dm.service.domain.DmServicePj;
import me.zhengjie.modules.dm.statement.domain.DmEvaluate;
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.Query;
import org.springframework.data.repository.query.Param;

import java.sql.Timestamp;

public interface DmEvaluateRepository extends JpaRepository<DmEvaluate,Integer>, JpaSpecificationExecutor<DmEvaluate> {
    /**
     * 年整体评价报表
     * @param
     * @param pageable
     * @return
     */
    @Query(nativeQuery =true,value = "SELECT id,format(wd_point,0) wd_point,format(pz_point,0) pz_point,format(fl_point,0) fl_point,format(fwtd_point,0) fwtd_point,format(hjws_point,0) hjws_point,format(zhfw_point,0) zhfw_point,format(cxgj_point,0) cxgj_point,format(total_point,0) total_point,CONCAT( pj_time, '-01-01 00:00:00' ) pj_time from ( " +
            "SELECT @id \\:= @id+1 as id,AVG(wd_point) wd_point,AVG(pz_point) pz_point,AVG(fl_point) fl_point,AVG(fwtd_point) fwtd_point,AVG(hjws_point) hjws_point,AVG(zhfw_point) zhfw_point,AVG(cxgj_point) cxgj_point,AVG(wd_point + pz_point + fl_point + fwtd_point + hjws_point + zhfw_point + cxgj_point)/7 total_point," +
            " DATE_FORMAT(pj_time,'%Y') pj_time FROM (SELECT @id \\:=0) r,dm_service_pj WHERE if(:stateDate != '' and :endDate !='',pj_time between :endDate and :stateDate,1=1) GROUP BY  DATE_FORMAT(pj_time,'%Y')  "+
            ") a ",
            countQuery =  "SELECT count(*) from ( " +
                    "SELECT 0 as id,AVG(wd_point) wd_point,AVG(pz_point) pz_point,AVG(fl_point) fl_point,AVG(fwtd_point) fwtd_point,AVG(hjws_point) hjws_point,AVG(zhfw_point) zhfw_point,AVG(cxgj_point) cxgj_point,AVG(wd_point + pz_point + fl_point + fwtd_point + hjws_point + zhfw_point + cxgj_point)/7 total_point," +
                    " DATE_FORMAT(pj_time,'%Y') pj_time FROM dm_service_pj WHERE if(:stateDate != '' and :endDate !='',pj_time between :endDate and :stateDate,1=1) GROUP BY  DATE_FORMAT(pj_time,'%Y')  "+
                    ") a ")
    Page<DmEvaluate> selectYearServicePj (@Param("stateDate") String stateDate, @Param("endDate") String endDate, Pageable pageable);

    /**
     * 月整体评价报表
     * @param
     * @param pageable
     * @return
     */
    @Query(nativeQuery =true,value = " SELECT id,format(wd_point,0) wd_point,format(pz_point,0) pz_point,format(fl_point,0) fl_point,format(fwtd_point,0) fwtd_point,format(hjws_point,0) hjws_point,format(zhfw_point,0) zhfw_point,format(cxgj_point,0) cxgj_point,format(total_point,0) total_point,CONCAT( pj_time, '-01 00:00:00' ) pj_time from ( " +
            " SELECT @id \\:= @id+1 as id,AVG(wd_point) wd_point,AVG(pz_point) pz_point,AVG(fl_point) fl_point,AVG(fwtd_point) fwtd_point,AVG(hjws_point) hjws_point,AVG(zhfw_point) zhfw_point,AVG(cxgj_point) cxgj_point,AVG(wd_point + pz_point + fl_point + fwtd_point + hjws_point + zhfw_point + cxgj_point)/7 total_point," +
            " DATE_FORMAT(pj_time,'%Y-%m') pj_time FROM (SELECT @id \\:=0) r,dm_service_pj WHERE if(:stateDate != '' and :endDate !='',pj_time between :stateDate and :endDate,1=1) GROUP BY DATE_FORMAT(pj_time,'%Y-%m')  "+
            ") a ",
            countQuery =  "SELECT count(*) from ( " +
                    "SELECT 0 as id,AVG(wd_point) wd_point,AVG(pz_point) pz_point,AVG(fl_point) fl_point,AVG(fwtd_point) fwtd_point,AVG(hjws_point) hjws_point,AVG(zhfw_point) zhfw_point,AVG(cxgj_point) cxgj_point,AVG(wd_point + pz_point + fl_point + fwtd_point + hjws_point + zhfw_point + cxgj_point)/7 total_point," +
                    " DATE_FORMAT(pj_time,'%Y-%m') pj_time FROM dm_service_pj WHERE if(:stateDate != '' and :endDate !='',pj_time between :endDate and :stateDate,1=1) GROUP BY  DATE_FORMAT(pj_time,'%Y-%m')  "+
                    ") a ")
    Page<DmEvaluate> selectMonthServicePj (@Param("stateDate") String stateDate, @Param("endDate") String endDate, Pageable pageable);
}
