package com.sw.mes.sap.dao;

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

import org.springframework.data.jpa.repository.Query;

import com.sw.mes.core.annotation.Jinko;
import com.sw.mes.core.base.BaseDao;
import com.sw.mes.sap.dto.statistics.StatisticsDto;
import com.sw.mes.sap.dto.statistics.StatisticsLjDto;
import com.sw.mes.sap.entity.ReportSapGoodsmvt;


/**
 * MES报工SAP货物移动接口Dao接口
 *
 * @author liru
 * @date 2024-01-06
 */
@Jinko(value = "MES报工SAP货物移动接口Service接口", author = "liru")
public interface ReportSapGoodsmvtDao extends BaseDao<ReportSapGoodsmvt, Long> {

    @Query(value = "SELECT * FROM report_sap_goodsmvt WHERE crystal_no=?1 AND bwart=?2 ORDER BY id DESC LIMIT 1 ", nativeQuery = true)
    ReportSapGoodsmvt findByCrystalNoAndBwart(String crystalNo, String bwart);

    @Query(value = "SELECT * FROM report_sap_goodsmvt WHERE proc_name like ?1 AND aufnr=?2 AND bwart in ?3", nativeQuery = true)
    List<ReportSapGoodsmvt> findByProcNameAndOrderNoAndBwart(String procName, String orderNo, List<String> bwart);

    @Query(value = "SELECT * FROM report_sap_goodsmvt WHERE proc_name like ?1 AND aufnr=?2 AND bwart in ?3 And remark = ?4", nativeQuery = true)
    List<ReportSapGoodsmvt> findByProcNameAndOrderNoAndBwartAndLotNo(String procName, String orderNo, List<String> bwart, String lotNo);

    /**
     * 根据物料号groupby，统计报工的总和
     *
     * @return
     */
    @Query(value = "SELECT id as id, matnr as code, SUM(menge) as quantity, proc_name as procName FROM report_sap_goodsmvt WHERE  status= 'true' AND bwart = 101 GROUP BY matnr", nativeQuery = true)
    List<Map<String,Object>> groupByMatnr();

    @Query(value = "SELECT * FROM report_sap_goodsmvt WHERE crystal_no=?1 ORDER BY id ASC", nativeQuery = true)
	List<ReportSapGoodsmvt> findByCrystalNo(String crystalNo);


    @Query(value = "SELECT * FROM report_sap_goodsmvt WHERE crystal_no=?1 AND bwart=?2 AND proc_name = ?3 ORDER BY id DESC LIMIT 1 ", nativeQuery = true)
    ReportSapGoodsmvt findByCrystalNoAndBwartAndProcName(String crystalNo, String bwart,String procName);
    
    @Query(value = "SELECT * FROM report_sap_goodsmvt WHERE crystal_no=?1 AND proc_name = ?2 ORDER BY id DESC LIMIT 1 ", nativeQuery = true)
    ReportSapGoodsmvt findByCrystalNoAndBwartAndProcName1(String crystalNo, String procName);


    public List statisticYbcshdb(StatisticsDto dto);



    @Query(value = "SELECT * FROM report_sap_goodsmvt WHERE proc_name = ?1 AND crystal_no IN (?2) AND status= ?3  ORDER BY id DESC  ", nativeQuery = true)
    List<ReportSapGoodsmvt> findByCrystalNoAndProcName(String procName,List<String> crystalNoList,Integer status);
    
    
    List<ReportSapGoodsmvt> findByBwartAndProcNameAndStatus(String bwart, String procName, Boolean status);
    
    public List<StatisticsLjDto> statisticLj(StatisticsLjDto dto);

    @Query(value = "SELECT * FROM sap_inventory_view", nativeQuery = true)
	List<Map<Object, Object>> ljStatistics();

    //
	List<Map<Object, Object>> djshStatistics(Date startDate,Date endDate, String aufnr, String matnr, String charg);
	
	List<Map<Object, Object>> jdxhStatistics(Date startDate,Date endDate, String aufnr, String matnr, String charg);

	List<Map<Object, Object>> jdshStatistics(Date startDate, Date endDate, String aufnr, String matnr, String charg);

	List<Map<Object, Object>> dpxhStatistics(Date startDate, Date endDate, String aufnr, String matnr, String charg);

    @Query(value = "SELECT ifnull(SUM(menge),0) as weight FROM `report_sap_goodsmvt` WHERE proc_name IN (?1) AND bwart=?2 AND aufnr = ?3 ", nativeQuery = true)
    BigDecimal findSumWeightByProcNameAndBwartAndOrderNo(String procName,String bwart,String orderNo);

    @Query(value = "SELECT * FROM report_sap_goodsmvt WHERE zuuid=?1 or mblnr=?1", nativeQuery = true)
	List<ReportSapGoodsmvt> findByZuuidOrMblnr(String mBLNR);

    @Query(value = "SELECT * FROM report_sap_goodsmvt WHERE md5value=?1 order by id desc limit 1", nativeQuery = true)
    ReportSapGoodsmvt findLastByMd5Value(String md5Value);

	List<ReportSapGoodsmvt> findByMblnr(String mblnr);

	@Query(value = "SELECT sum(menge) FROM report_sap_goodsmvt WHERE aufnr=?1 and `status`=1 and bwart in (?2)", nativeQuery = true)
	Double getSumAufnrAndInBwart(String aufnr, List<String> bwarts);

	@Query(value = "select sum(menge) from report_sap_goodsmvt where `status`=0 and aufnr=?1", nativeQuery = true)
	Double getSumNoSendSapLog(String aufnr);

	@Query(value = "select id,zuuid,budat,bldat,ernam,xblnr,frbnr,bwart,kostl,aufnr,vbeln,posnr,ebeln,ebelp,rsnum,rspos,werks,lgort,matnr,charg,sum(menge) menge,meins,umwrk,umlgo,umcha,sgtxt,zfcp_flag,other1,charg_item,`status`,crystal_no,proc_name,device_no,uuid_value,remark,mblnr,lgort_sapnum_ago,lgort_sapnum_after,umlgo_sapnum_ago,umlgo_sapnum_after,create_date,remark1,send_type,md5value,sapmblnr,error_info from report_sap_goodsmvt where aufnr=?1 GROUP BY matnr,charg,lgort", nativeQuery = true)
	List<ReportSapGoodsmvt> findAllOrderMatnrChargLgort(String aUFNR);

	@Query(value = "select count(id) from report_sap_goodsmvt where aufnr=?1 and length(charg)>10", nativeQuery = true)
	Long getChargLengthCount(String aUFNR);

	@Query(value = "select count(id) from report_sap_goodsmvt where aufnr=?1 and bwart in ('102','262','532') and menge>0", nativeQuery = true)
	Long getChargeOffMengeCount(String aUFNR);

	@Query(value = "select count(id) from report_sap_goodsmvt where aufnr=?1 and bwart in ('261','262') and (rsnum is null or rsnum='')", nativeQuery = true)
	Long getRsnumNullCount(String aUFNR);

	
	@Query(value = "select * from report_sap_goodsmvt where bwart=?1 and lgort=?2 and matnr=?3 and charg=?4 and menge=?5 and status=?6", nativeQuery = true)
	List<ReportSapGoodsmvt> findByBwartAndLgortAndMatnrAndChargAndMengeAndStatus(String bwart, String lgort,String matnr, String charg, String menge, boolean status);

}
