package com.sw.mes.sap.reconciliation.sap.dao;

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

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

/**
 * sap native sql
 *
 * @Author：wanjia
 * @Date：2024/3/5 23:29
 */
public interface SAPNativeSQL extends BaseDao<LogSapMesInteractive, Long> {

    /**
     * 装罐料投料查询
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @Query(value = "SELECT tid,did,raw_type,sap_no,equipment_code,lot_no,again_num,material_code,batch_no,weight,fill_time,confirm_time \n" +
            "FROM\n" +
            "        (\n" +
            "SELECT\n" +
            "tid,\n" +
            "CONCAT( '0-', did ) AS did,\n" +
            "        '初始装料' AS raw_type,\n" +
            "        sap_no,\n" +
            "        equipment_code,\n" +
            "        lot_no,\n" +
            "        again_num,\n" +
            "        material_code,\n" +
            "        batch_no,\n" +
            "        weight,\n" +
            "        fill_time,\n" +
            "        confirm_time \n" +
            "FROM\n" +
            "        (\n" +
            "        SELECT\n" +
            "                lt.id AS tid,\n" +
            "                fd.id AS did,\n" +
            "                lt.sap_no,\n" +
            "                lt.equipment_code,\n" +
            "                lt.lot_no,\n" +
            "                0 AS again_num,\n" +
            "                fd.material_code,\n" +
            "                fd.batch_no,\n" +
            "                fd.weight,\n" +
            "                fd.install_time AS fill_time,\n" +
            "                lt.confirm_time \n" +
            "        FROM\n" +
            "                production_blast_furnace_detail AS fd\n" +
            "                INNER JOIN production_loading_task AS lt ON fd.blast_furnace_id = lt.id \n" +
            "        WHERE\n" +
            "                lt.raw_status = 4 \n" +
            "                AND lt.confirm = 1 \n" +
            "                AND lt.confirm_time BETWEEN ?1 AND ?2 \n" +
            "        ) AS t1 UNION ALL\n" +
            "SELECT\n" +
            "        tid,\n" +
            "        CONCAT( '1-', did ) AS did,\n" +
            "        '复投灌料' AS raw_type,\n" +
            "        sap_no,\n" +
            "        equipment_code,\n" +
            "        lot_no,\n" +
            "        again_num,\n" +
            "        material_code,\n" +
            "        batch_no,\n" +
            "        weight,\n" +
            "        fill_time,\n" +
            "        confirm_time \n" +
            "FROM\n" +
            "        (\n" +
            "        SELECT\n" +
            "                atd.id AS tid,\n" +
            "                fd.id AS did,\n" +
            "                atd.sap_no,\n" +
            "                atd.equipment_code,\n" +
            "                atd.lot_no,\n" +
            "                atd.again_num,\n" +
            "                fd.material_code,\n" +
            "                fd.batch_no,\n" +
            "                fd.weight,\n" +
            "                ifb.start_time AS fill_time,\n" +
            "                ifb.confirm_time \n" +
            "        FROM\n" +
            "                production_input_furnace_detail AS fd\n" +
            "                INNER JOIN production_input_furnace_burden AS ifb ON fd.input_furnace_id = ifb.id\n" +
            "                INNER JOIN production_again_task_detail AS atd ON fd.again_task_detail_id = atd.id \n" +
            "        WHERE\n" +
            "                atd.raw_status = 4 \n" +
            "                AND ifb.confirm = 1 \n" +
            "                AND ifb.confirm_time BETWEEN ?1 AND ?2 \n" +
            "        ) AS t2 \n" +
            "        ) AS t3 \n" +
            "ORDER BY\n" +
            "        confirm_time,\n" +
            "        equipment_code,\n" +
            "        material_code,\n" +
            "        weight", nativeQuery = true)
    List<Map<String, Object>> queryAgainTask(String startTime, String endTime);


    @Query(value = "SELECT\n" +
            "        device_no,\n" +
            "        other1,\n" +
            "        matnr,\n" +
            "        charg,\n" +
            "        menge,\n" +
            "        ernam,\n" +
            "        budat,\n" +
            "        proc_name,\n" +
            "        bwart,\n" +
            "        remark\n" +
            "FROM\n" +
            "        report_sap_goodsmvt \n" +
            "WHERE\n" +
            "  ernam is null and\n" +
            "        bwart = '261' \n" +
            "        AND proc_name = '拉晶' \n" +
            "        AND budat BETWEEN ?1 \n" +
            "AND ?2", nativeQuery = true)
    List<Map<String, Object>> queryAgainTaskInSAP(String startTime, String endTime);

    //MES毛棒检验
    @Query(value = "SELECT crystal_no ,weight,line_start_time  from production_long_crystal WHERE crystal_status = 5 AND line_start_time BETWEEN ?1 AND ?2", nativeQuery = true)
    List<Map<String, Object>> queryCrystalCheck(String startTime, String endTime);

    //单晶收货
    @Query(value = "SELECT  crystal_no ,menge ,bldat   FROM report_sap_goodsmvt WHERE proc_name IN('单晶收货') AND bwart = 101 AND bldat BETWEEN ?1 AND ?2", nativeQuery = true)
    List<Map<String, Object>> queryCrystalCutSAP(String startTime, String endTime);

    //毛棒调拨
    @Query(value = "SELECT  crystal_no,menge,bldat FROM report_sap_goodsmvt WHERE proc_name IN ('毛棒调拨') AND bwart = 311 AND bldat BETWEEN ?1 AND ?2", nativeQuery = true)
    List<Map<String, Object>> queryCrystalAllotSAP(String startTime, String endTime);


    //截断消耗
    @Query(value = "SELECT  crystal_no ,menge,bldat FROM report_sap_goodsmvt WHERE proc_name IN('截断消耗') AND bwart = 261 AND bldat BETWEEN ?1 AND ?2", nativeQuery = true)
    List<Map<String, Object>> queryCutCostSAP(String startTime, String endTime);


    //截断收货
    @Query(value = "SELECT  crystal_no,menge,bldat FROM report_sap_goodsmvt WHERE proc_name IN('截断收货') AND bwart = 101 AND bldat BETWEEN ?1  AND ?2", nativeQuery = true)
    List<Map<String, Object>> cutInputSAP(String startTime, String endTime);

    //截断调拨
    @Query(value = "SELECT  crystal_no,menge,bldat FROM report_sap_goodsmvt WHERE proc_name IN('截断调拨') AND bwart = 311 AND bldat BETWEEN ?1 AND ?2", nativeQuery = true)
    List<Map<String, Object>> cutAllotSAP(String startTime, String endTime);

    //刀剖消耗
    @Query(value = "SELECT crystal_no,menge,bldat FROM report_sap_goodsmvt WHERE proc_name IN('刀剖消耗') AND bwart = 261 AND bldat BETWEEN ?1 AND ?2", nativeQuery = true)
    List<Map<String, Object>> kfCostSAP(String startTime, String endTime);

    //刀剖截断
    @Query(value = "SELECT cmr.section_no ,yid.theory_weight as weight ,cmr.gmt_create FROM crystal_material_relation cmr left JOIN yb_inspect_data yid ON yid.section_no = cmr.section_no WHERE cmr.gmt_create  BETWEEN ?1 AND ?2", nativeQuery = true)
    List<Map<String, Object>> kfCut(String startTime, String endTime);

    //刀剖收货
    @Query(value = "SELECT  crystal_no,menge,bldat FROM report_sap_goodsmvt WHERE proc_name IN('刀剖收货') AND bwart = 101 AND bldat BETWEEN ?1 AND ?2", nativeQuery = true)
    List<Map<String, Object>> kfInputSAP(String startTime, String endTime);

    //mes方棒检验
    @Query(value = "SELECT fb_section_no,theory_weight_fb as weight ,check_time FROM zhxx_operate_check_out where check_time BETWEEN ?1 AND ?2", nativeQuery = true)
    List<Map<String, Object>> fbCheck(String startTime, String endTime);

    //余料数据
    @Query(value = "select weight,receive_time  from recycled_material_manage rmm where  receive_time BETWEEN ?1 AND ?2", nativeQuery = true)
    List<Map<String, Object>> recycledMaterial(String startTime, String endTime);

    //余料SAP
    @Query(value = "select menge from  report_sap_goodsmvt rsg where proc_name IN ('余料','拉晶余料','截断余料','刀剖余料','籽晶余料','硅料清洗余料') and bwart  =531   and status =0 AND budat BETWEEN ?1 AND ?2", nativeQuery = true)
    List<Map<String, Object>> recycledMaterialSAP(String startTime, String endTime);

    //硅料sap
    @Query(value = "select menge from  report_sap_goodsmvt rsg where proc_name IN ('硅料清洗审核调拨','拉晶余料调拨','截断余料调拨','刀剖余料调拨','籽晶余料调拨','硅料清洗余料调拨')  and bwart  =311   and status =0 AND budat BETWEEN ?1 AND ?2", nativeQuery = true)
    List<Map<String, Object>> storeSignSAP(String startTime, String endTime);
}
