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

import me.zhengjie.modules.dm.order.domain.DmOrderRecord;
import me.zhengjie.modules.dm.statement.domain.DmStatement;
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;

public interface DmStatementRepository extends JpaRepository<DmStatement, Integer>, JpaSpecificationExecutor<DmStatement> {
    /**
     * 年部门消费
     * @param
     * @param pageable
     * @return
     */
    @Query(nativeQuery =true,value = " SELECT id,name,money,avg_money,CONCAT(order_time,'-01-01 00:00:00') order_time from ( " +
            " SELECT @id \\:= @id+1 as id,b.name,CAST(SUM(money) AS DECIMAL(15,2)) money,CAST(AVG(money) AS DECIMAL(15,2)) avg_money, DATE_FORMAT( create_date, '%Y' ) order_time FROM (SELECT @id \\:=0) r,dm_order_record a  LEFT JOIN sys_dept b ON a.dept_id=b.dept_id " +
            " WHERE  if(:stateDate != '' and :endDate !='',create_date between :stateDate and :endDate,1=1) GROUP BY DATE_FORMAT(create_date,'%Y'),b.name" +
            ") a ",
            countQuery =  "SELECT count(*) from ( " +
                    "SELECT 0 as id,b.name,SUM(money),AVG(money) avg_money,DATE_FORMAT( create_date, '%Y' ) order_time FROM dm_order_record a LEFT JOIN sys_dept b ON a.dept_id = b.dept_id " +
                    " WHERE if(:stateDate != '' and :endDate !='',DATE_FORMAT(create_date,'%Y') between :stateDate and :endDate,1=1) GROUP BY DATE_FORMAT(create_date,'%Y'),b.name" +
                    ") a ")
    Page<DmStatement> selectYearStatement (@Param("stateDate") String stateDate, @Param("endDate") String endDate, Pageable pageable);
    /**
     * 月部门消费
     * @param
     * @param pageable
     * @return
     */
    @Query(nativeQuery =true,value = " SELECT id,name,money,avg_money,CONCAT(order_time,'-01 00:00:00') order_time from ( " +
            " SELECT @id \\:= @id+1 as id,b.name,CAST(SUM(money) AS DECIMAL(15,2)) money,CAST(AVG(money) AS DECIMAL(15,2)) avg_money, DATE_FORMAT( create_date, '%Y-%m' ) order_time FROM (SELECT @id \\:=0) r,dm_order_record a  LEFT JOIN sys_dept b ON a.dept_id=b.dept_id " +
            " WHERE  if(:stateDate != '' and :endDate !='',create_date between :stateDate and :endDate,1=1) GROUP BY DATE_FORMAT(create_date,'%Y-%m'),b.name" +
            ") a ",
            countQuery =  "SELECT count(*) from ( " +
                    "SELECT 0 as id,b.name,SUM(money),AVG(money) avg_money,DATE_FORMAT( create_date, '%Y-%m' ) order_time FROM dm_order_record a LEFT JOIN sys_dept b ON a.dept_id=b.dept_id " +
                    " WHERE if(:stateDate != '' and :endDate !='',create_date between :stateDate and :endDate,1=1) GROUP BY DATE_FORMAT(create_date,'%Y-%m'),b.name" +
                    ") a ")
    Page<DmStatement> selectMonthStatement (@Param("stateDate") String stateDate, @Param("endDate") String endDate, Pageable pageable);
}
