package com.sw.mes.kanban.dao;

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.kanban.entity.DailyProductionRecord;
import org.springframework.data.jpa.repository.Query;

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


/**
* @Author
* @Description：
* @Date:
* @Modified By:
*/
public interface DailyProductionRecordDao extends BaseDao<DailyProductionRecord, Integer>{

    @Query(value = "SELECT * FROM daily_production_record WHERE type = ?1 and date = ?2 LIMIT 1", nativeQuery = true)
    DailyProductionRecord findOldRecord(Integer type, Date date);

    @Query(value = "SELECT SUM(production) as total FROM daily_production_record WHERE date BETWEEN ?1 AND ?2  and type = ?3 ", nativeQuery = true)
    Map<String, Object> countByTypeAndTime(String startTime, String endTime, Integer type);

    @Query(value = "SELECT *  FROM daily_production_record WHERE date BETWEEN ?1 AND ?2  order by date ", nativeQuery = true)
    List<DailyProductionRecord> findByTypeAndTime(String startTime, String endTime);

    @Query(value = "select  IFNULL(sum(weight),0) as total\n" +
            "from crystal_material_relation where \n" +
            "device_type = 4 and\n" +
            "gmt_create between ?1 and ?2 ", nativeQuery = true)
    Map<String, Object> countDJByTime(String startTime, String endTime);

    @Query(value = "select  IFNULL(sum(weight),0) as total\n" +
            "from crystal_material_relation where \n" +
            "device_type = 3 and\n" +
            "gmt_create between ?1 and ?2 ", nativeQuery = true)
    Map<String, Object> countJDByTime(String startTime, String endTime);

    @Query(value = "select IFNULL(sum(theory_weight_fb),0)  as total\n" +
            "from fb_turnstore_tray_item \n" +
            "where  time between ?1 and ?2 ", nativeQuery = true)
    Map<String, Object> countDPByTime(String startTime, String endTime);

    @Query(value = "select  IFNULL(sum(weight),0) as production,1 as type,\n" +
            "DATE_FORMAT(gmt_create,'%Y-%m-%d') AS date\n" +
            "from crystal_material_relation where \n" +
            "device_type = 4 and\n" +
            "gmt_create between ?1 and ?2 \n" +
            "group by DATE_FORMAT(gmt_create,'%Y-%m-%d')\n" +
            "union all\n" +
            "select  IFNULL(sum(weight),0) as production,2 as type,\n" +
            "DATE_FORMAT(gmt_create,'%Y-%m-%d') AS date\n" +
            "from crystal_material_relation where \n" +
            "device_type = 3 and\n" +
            "gmt_create between ?1 and ?2 \n" +
            "group by DATE_FORMAT(gmt_create,'%Y-%m-%d')\n" +
            "union all\n" +
            "select IFNULL(sum(theory_weight_fb),0)  as production,3 as type,\n" +
            "DATE_FORMAT(time,'%Y-%m-%d') AS date\n" +
            "from fb_turnstore_tray_item \n" +
            "where  time between ?1 and ?2 \n" +
            "group by DATE_FORMAT(time,'%Y-%m-%d')", nativeQuery = true)
    List<Map<String, Object>> findDaysByTypeAndTime(String startTime, String endTime);
}