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

import me.zhengjie.modules.dm.statement.domain.DmHaveDinner;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.*;
import org.springframework.transaction.annotation.Transactional;

public interface DmHaveDinnerRepository extends JpaRepository<DmHaveDinner,Integer>, JpaSpecificationExecutor<DmHaveDinner> {
    /**
     * todo 是否存在视图
     */
    @Transactional
    @Modifying
    @Query(value = "DROP VIEW IF EXISTS `zgmonth`",nativeQuery = true)
    void dropByZgMonth();
    @Transactional
    @Modifying
    @Query(value = "DROP VIEW IF EXISTS `zdmonth`",nativeQuery = true)
    void dropByZdMonth();

    @Transactional
    @Modifying
    @Query(value = "DROP VIEW IF EXISTS zgyear",nativeQuery = true)
    void dropByZgYear();
    @Transactional
    @Modifying
    @Query(value = "DROP VIEW if IF EXISTS zdyear",nativeQuery = true)
    void dropByZdYear();

    @Transactional
    @Modifying
    @Query(value = "DROP VIEW if IF EXISTS `zgday`",nativeQuery = true)
    void dropByZgDay();
    @Transactional
    @Modifying
    @Query(value = "DROP VIEW IF EXISTS `zdday`",nativeQuery = true)
    void dropByZdDay();

    @Transactional
    @Modifying
    @Query(value = "DROP VIEW IF EXISTS `zgweek`",nativeQuery = true)
    void dropByZgWeek();
    @Transactional
    @Modifying
    @Query(value = "DROP VIEW IF EXISTS `zdweek`",nativeQuery = true)
    void dropByZdWeek();

    /**
     * todo 创建视图
     */
    @Modifying
    @Query(value = "CREATE VIEW  zgmonth as " +
            "SELECT  i.name as zg_name,SUM(i.num) as zg_num,DATE_FORMAT(create_date,'%Y-%m') time  FROM `dm_order_item` i " +
            "LEFT JOIN `dm_order_record` r ON  i.bh = r.bh " +
            "WHERE if(:stateDate != '' and :endDate !='',create_date between :stateDate and :endDate,1=1) " +
            "GROUP BY time,`zg_name` " +
            "ORDER BY time DESC,SUM(num) DESC ",nativeQuery = true)
    void addZgMonth(String stateDate,String endDate);

    @Modifying
    @Query(value = "CREATE VIEW  zdmonth as " +
            "SELECT i.name as zd_name,SUM(i.num) as zd_num,SUM(i.money) as money,DATE_FORMAT(create_date,'%Y-%m') time FROM `dm_order_item` i " +
            "LEFT JOIN `dm_order_record` r ON  i.bh = r.bh " +
            "WHERE if(:stateDate != '' and :endDate !='',create_date between :stateDate and :endDate,1=1) " +
            "GROUP BY time,`zd_name` " +
            "ORDER BY time DESC,SUM(num) ASC ",nativeQuery = true)
    void addZdMonth(String stateDate,String endDate);


    @Modifying
    @Query(value = "CREATE VIEW  zgyear as " +
            "SELECT  i.name as zg_name,SUM(i.num) as zg_num,DATE_FORMAT(create_date,'%Y') time  FROM `dm_order_item` i " +
            "LEFT JOIN `dm_order_record` r ON  i.bh = r.bh " +
            "WHERE if(:stateDate != '' and :endDate !='',create_date between :stateDate and :endDate,1=1) " +
            "GROUP BY time,`zg_name` " +
            "ORDER BY time DESC,SUM(num) DESC ",nativeQuery = true)
    void addZgYear(String stateDate,String endDate);

    @Modifying
    @Query(value = "CREATE VIEW  zdyear as " +
            "SELECT i.name as zd_name,SUM(i.num) as zd_num,SUM(i.money) as money,DATE_FORMAT(create_date,'%Y') time FROM `dm_order_item` i " +
            "LEFT JOIN `dm_order_record` r ON  i.bh = r.bh " +
            "WHERE if(:stateDate != '' and :endDate !='',create_date between :stateDate and :endDate,1=1) " +
            "GROUP BY time,`zd_name` " +
            "ORDER BY time DESC,SUM(num) ASC ",nativeQuery = true)
    void addZdYear(String stateDate,String endDate);

    @Modifying
    @Query(value = "CREATE VIEW  zgday as " +
            "SELECT  i.name as zg_name,SUM(i.num) as zg_num,DATE_FORMAT(create_date,'%Y-%m-%d') time  FROM `dm_order_item` i " +
            "LEFT JOIN `dm_order_record` r ON  i.bh = r.bh " +
            "WHERE if(:stateDate != '' and :endDate !='',create_date between :stateDate and :endDate,1=1) " +
            "GROUP BY time,`zg_name` " +
            "ORDER BY time DESC,SUM(num) DESC ",nativeQuery = true)
    void addZgDay(String stateDate,String endDate);

    @Modifying
    @Query(value = "CREATE VIEW  zdday as " +
            "SELECT i.name as zd_name,SUM(i.num) as zd_num,SUM(i.money) as money,DATE_FORMAT(create_date,'%Y-%m-%d') time FROM `dm_order_item` i " +
            "LEFT JOIN `dm_order_record` r ON  i.bh = r.bh " +
            "WHERE if(:stateDate != '' and :endDate !='',create_date between :stateDate and :endDate,1=1) " +
            "GROUP BY time,`zd_name` " +
            "ORDER BY time DESC,SUM(num) ASC ",nativeQuery = true)
    void addZdDay(String stateDate,String endDate);

    @Modifying
    @Query(value = "CREATE VIEW  zgweek as " +
            "SELECT  i.name as zg_name,SUM(i.num) as zg_num,DATE_FORMAT(create_date,'%Y-%u') week_time  FROM `dm_order_item` i " +
            "LEFT JOIN `dm_order_record` r ON  i.bh = r.bh " +
            "WHERE if(:stateDate != '' and :endDate !='',create_date between :stateDate and :endDate,1=1)  AND create_date IS NOT NULL AND i.NAME IS NOT NULL " +
            "GROUP BY week_time,`zg_name` " +
            "ORDER BY week_time DESC,SUM(num) DESC ",nativeQuery = true)
    void addZgWeek(String stateDate,String endDate);

    @Modifying
    @Query(value = "CREATE VIEW  zdweek as " +
            "SELECT i.name as zd_name,SUM(i.num) as zd_num,SUM(i.money) as money,DATE_FORMAT(create_date,'%Y-%u') week_time FROM `dm_order_item` i " +
            "LEFT JOIN `dm_order_record` r ON  i.bh = r.bh " +
            "WHERE if(:stateDate != '' and :endDate !='',create_date between :stateDate and :endDate,1=1)  AND create_date IS NOT NULL AND i.NAME IS NOT NULL " +
            "GROUP BY week_time,`zd_name` " +
            "ORDER BY week_time DESC,SUM(num) ASC ",nativeQuery = true)
    void addZdWeek(String stateDate,String endDate);

    /**
     * todo 页面数据
     */
    @Query(value = "SELECT id,zg_name,zg_num,CONCAT( time, '-01 00:00:00' ) time,money,zd_name,zd_num,week_time FROM("+
            "SELECT @id \\:= @id+1 as id,zg_name,zg_num,time,null as money,null as zd_name,0 as zd_num,null as week_time from zgmonth a " +
            "WHERE 3> (SELECT COUNT(*) from (SELECT @id \\:=0) r,zgmonth b WHERE a.time = b.time and a.zg_num < b.zg_num))a ",
            countQuery="select count(*) from(" +
                    "SELECT @id \\:= @id+1 as id,zg_name,zg_num,time from (SELECT @id \\:=0) r,zgmonth a " +
                    "WHERE 3> (SELECT COUNT(*) from zgmonth b WHERE a.time = b.time and a.zg_num < b.zg_num)" +
                    ")a ",nativeQuery = true)
    Page<DmHaveDinner> selectByZgMonth(Pageable pageable);

    @Query(value = "SELECT id,zg_name,zg_num,CONCAT( time, '-01 00:00:00' ) time,CAST(money AS DECIMAL(15,2)) money,zd_name,zd_num,week_time FROM("+
            "SELECT @id \\:= @id+1 as id,null as zg_name,0 as zg_num,time,money,zd_name,zd_num,null as week_time from zdmonth a " +
            "WHERE 1> (SELECT COUNT(*) from (SELECT @id \\:=0) r,zdmonth b WHERE a.time = b.time and a.zd_num > b.zd_num))a ",
            countQuery="select count(*) from(" +
                    "SELECT @id \\:= @id+1 as id,zd_name,zd_num,time from (SELECT @id \\:=0) r,zdmonth a " +
                    "WHERE 1> (SELECT COUNT(*) from zdmonth b WHERE a.time = b.time and a.zd_num > b.zd_num)" +
                    ")a ",nativeQuery = true)
    Page<DmHaveDinner> selectByZdMonth(Pageable pageable);

    @Query(value = "SELECT id,zg_name,zg_num,CONCAT( time, '-01-01 00:00:00' ) time,money,zd_name,zd_num,week_time FROM("+
            "SELECT @id \\:= @id+1 as id,zg_name,zg_num,time,null as money,null as zd_name,0 as zd_num,null as week_time from zgyear a " +
            "WHERE 3> (SELECT COUNT(*) from (SELECT @id \\:=0) r,zgyear b WHERE a.time = b.time and a.zg_num < b.zg_num))a ",
            countQuery="select count(*) from(" +
                    "SELECT @id \\:= @id+1 as id,zg_name,zg_num,time from (SELECT @id \\:=0) r,zgyear a " +
                    "WHERE 3> (SELECT COUNT(*) from zgyear b WHERE a.time = b.time and a.zg_num < b.zg_num)" +
                    ")a ",nativeQuery = true)
    Page<DmHaveDinner> selectByZgYear(Pageable pageable);
    @Query(value = "SELECT id,zg_name,zg_num,CONCAT( time, '-01-01 00:00:00' ) time,CAST(money AS DECIMAL(15,2)) money,zd_name,zd_num,week_time FROM("+
            "SELECT @id \\:= @id+1 as id,zd_name,zd_num,time,money,null as zg_name,0 as zg_num,null as week_time from zdyear a " +
            "WHERE 1> (SELECT COUNT(*) from (SELECT @id \\:=0) r,zdyear b WHERE a.time = b.time and a.zd_num > b.zd_num))a ",
            countQuery="select count(*) from(" +
                    "SELECT @id \\:= @id+1 as id,zd_name,zd_num,time from (SELECT @id \\:=0) r,zdyear a " +
                    "WHERE 1> (SELECT COUNT(*) from zdyear b WHERE a.time = b.time and a.zd_num > b.zd_num)" +
                    ")a ",nativeQuery = true)
    Page<DmHaveDinner> selectByZdYear(Pageable pageable);

    @Query(value = "SELECT id,zg_name,zg_num,CONCAT( time, ' 00:00:00' ) time,money,zd_name,zd_num,week_time FROM("+
            "SELECT @id \\:= @id+1 as id,zg_name,zg_num,time,null as money,null as zd_name,0 as zd_num,null as week_time from zgday a " +
            "WHERE 3> (SELECT COUNT(*) from (SELECT @id \\:=0) r,zgday b WHERE a.time = b.time and a.zg_num < b.zg_num))a ",
            countQuery="select count(*) from(" +
                    "SELECT @id \\:= @id+1 as id,zg_name,zg_num,time from (SELECT @id \\:=0) r,zgday a " +
                    "WHERE 3> (SELECT COUNT(*) from zgday b WHERE a.time = b.time and a.zg_num < b.zg_num)" +
                    ")a ",nativeQuery = true)
    Page<DmHaveDinner> selectByZgDay(Pageable pageable);
    @Query(value = "SELECT id,zg_name,zg_num,CONCAT( time, ' 00:00:00' ) time,CAST(money AS DECIMAL(15,2)) money,zd_name,zd_num,week_time FROM("+
            "SELECT @id \\:= @id+1 as id,zd_name,zd_num,time,money,null as zg_name,0 as zg_num,null as week_time from zdday a " +
            "WHERE 1> (SELECT COUNT(*) from (SELECT @id \\:=0) r,zdday b WHERE a.time = b.time and a.zd_num > b.zd_num))a ",
            countQuery="select count(*) from(" +
                    "SELECT @id \\:= @id+1 as id,zd_name,zd_num,time from (SELECT @id \\:=0) r,zdday a " +
                    "WHERE 1> (SELECT COUNT(*) from zdday b WHERE a.time = b.time and a.zd_num > b.zd_num)" +
                    ")a ",nativeQuery = true)
    Page<DmHaveDinner> selectByZdDay(Pageable pageable);

    @Query(value = "SELECT id,zg_name,zg_num,time,money,zd_name,zd_num,week_time FROM("+
            "SELECT @id \\:= @id+1 as id,zg_name,zg_num,null as time,null as money,null as zd_name,0 as zd_num,week_time from zgweek a " +
            "WHERE 3> (SELECT COUNT(*) from (SELECT @id \\:=0) r,zgweek b WHERE a.week_time = b.week_time and a.zg_num < b.zg_num))a ",
            countQuery="select count(*) from(" +
                    "SELECT @id \\:= @id+1 as id,zg_name,zg_num,time from (SELECT @id \\:=0) r,zgweek a " +
                    "WHERE 3> (SELECT COUNT(*) from zgweek b WHERE a.time = b.time and a.zg_num < b.zg_num)" +
                    ")a ",nativeQuery = true)
    Page<DmHaveDinner> selectByZgWeek(Pageable pageable);
    @Query(value = "SELECT id,zg_name,zg_num, time,CAST(money AS DECIMAL(15,2)) money,zd_name,zd_num,week_time FROM("+//CONCAT( time, '-00 00:00:00' )
            "SELECT @id \\:= @id+1 as id,zd_name,zd_num,null as time,money,null as zg_name,0 as zg_num,week_time from zdweek a " +
            "WHERE 1> (SELECT COUNT(*) from (SELECT @id \\:=0) r,zdweek b WHERE a.week_time = b.week_time and a.zd_num > b.zd_num))a ",
            countQuery="select count(*) from(" +
                    "SELECT @id \\:= @id+1 as id,zd_name,zd_num,time from (SELECT @id \\:=0) r,zdweek a " +
                    "WHERE 1> (SELECT COUNT(*) from zdweek b WHERE a.time = b.time and a.zd_num > b.zd_num)" +
                    ")a ",nativeQuery = true)
    Page<DmHaveDinner> selectByZdWeek(Pageable pageable);
}
