package com.sw.mes.yb.report.dao;

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.report.entity.MaterialPreparation;
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 MaterialPreparationDao extends BaseDao<MaterialPreparation, Long> {

    MaterialPreparation findByDate(Date date);

        @Query(value = "select\n" +
                "ad.time,\n" +
                "(ifnull(ad.actual_qty,0)-ifnull(t.weight,0)) as weight,\n" +
                "tv.target_value\n" +
                "from\n" +
                "(select \n" +
                "DATE_FORMAT(DATE_SUB(ad.create_time,INTERVAL 510 MINUTE),'%m-%d') as time,\n" +
                "ad.material_model_code,\n" +
                "sum(ifnull(ad.actual_qty,0)) as actual_qty\n" +
                "from jinko_store.store_input_apply_detail ad \n" +
                "LEFT JOIN jinko_store.store_input_apply ia ON ad.input_apply_id=ia.id \n" +
                "where \n" +
                "ia.order_status_id=3\n" +
                "and ad.create_time>=CONCAT(DATE_ADD(curdate(),interval -day(curdate()) day),' 08:30:00')\n" +
                "and ad.create_time<CONCAT(last_day(curdate()),' 08:30:00')\n" +
                "GROUP BY ad.material_model_code,DATE_FORMAT(DATE_SUB(ad.create_time,INTERVAL 510 MINUTE),'%m-%d')\n" +
                ")ad#入库管理\n" +
                "LEFT JOIN(\n" +
                "select \n" +
                "time,#日期\n" +
                "material_code,\n" +
                "ifnull(sum(weight),0) as weight#实际产出\n" +
                "from\n" +
                "(select \n" +
                "DATE_FORMAT(DATE_SUB(install_time,INTERVAL 510 MINUTE),'%m-%d') as time,\n" +
                "material_code,\n" +
                "sum(ifnull(weight,0)) as weight\n" +
                "from production_blast_furnace_detail_log\n" +
                "WHERE \n" +
                "install_time>=CONCAT(DATE_ADD(curdate(),interval -day(curdate()) day),' 08:30:00')\n" +
                "and install_time<CONCAT(last_day(curdate()),' 08:30:00')\n" +
                "GROUP BY material_code,DATE_FORMAT(DATE_SUB(install_time,INTERVAL 510 MINUTE),'%m-%d')#配料\n" +
                "\n" +
                "UNION ALL\n" +
                "select \n" +
                "DATE_FORMAT(DATE_SUB(fb.end_time,INTERVAL 510 MINUTE),'%m-%d') as time,\n" +
                "fd.material_code,\n" +
                "sum(ifnull(fd.weight,0)) as weight\n" +
                "from production_input_furnace_detail fd \n" +
                "LEFT JOIN production_input_furnace_burden fb ON fd.again_task_detail_id=fb.id\n" +
                "WHERE \n" +
                "fb.end_time>=CONCAT(DATE_ADD(curdate(),interval -day(curdate()) day),' 08:30:00')\n" +
                "and fb.end_time<CONCAT(last_day(curdate()),' 08:30:00')\n" +
                "GROUP BY fd.material_code,DATE_FORMAT(DATE_SUB(fb.end_time,INTERVAL 510 MINUTE),'%m-%d')#灌料\n" +
                ")t#实际产出\n" +
                "GROUP BY material_code,time)t ON t.material_code=ad.material_model_code and t.time=ad.time\n" +
                "LEFT JOIN (\n" +
                "select DATE_FORMAT(date,'%m-%d') as date,target_value from material_preparation_target_value \n" +
                "WHERE date>=CONCAT(DATE_ADD(curdate(),interval -day(curdate()) day),' 08:30:00')\n" +
                "and date<CONCAT(last_day(curdate()),' 08:30:00')\n" +
                ") tv ON tv.date=ad.time\n",nativeQuery = true)
    List<Map<String, Object>> findAllData();
}
