package market.Slm.DeliveryManagement.mapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import market.Lms.Logistics.vo.SendToCxSystem;
import market.Slm.DeliveryManagement.vo.SlmLadingBillDetail;
import market.Slm.DeliveryManagement.vo.SlmLadingBillHeat;
import market.Slm.DeliveryManagement.vo.SlmLadingBillLine;
import market.Slm.OrderManagement.vo.SlmOrderInfo;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.hnshituo.orm.GenericMapper;

@SuppressWarnings("rawtypes")
public interface SlmLadingBillHeatMapper extends GenericMapper<SlmLadingBillHeat,Object> {
	public List<SlmLadingBillHeat> doQueryWebBySQlSlm(int beginindex,int endindex,String startTime,String endTime,String billno,String state,@Param("coilno")String coilno,@Param("matcoilno")String materailcoilno,@Param("conno")String contract_no,@Param("storageattr")String storageattr);
	
	public List<SlmLadingBillHeat> doQueryWebBySQlSlmF(int beginindex,int endindex,String startTime,String endTime,String billno,String state,@Param("coilno")String coilno,@Param("matcoilno")String materailcoilno,@Param("conno")String contract_no,@Param("storageattr")String storageattr);
	
	public int doQueryWebBySQlSlmCount(String startTime,String endTime,String billno,String state);

	public int doQueryWebBySQlSlmCountF(String startTime,String endTime,String billno,String state);
	
	public void doRecovery(SlmLadingBillHeat heat);

	public void doCancelReceive(SlmLadingBillHeat heat);

	public void doEnable(SlmLadingBillHeat heat);

	public void doInvalidOverTime();

	public Integer findMaxId(String date);

	public List<SlmLadingBillHeat> doQueryAdjust(SlmLadingBillHeat heat,String coilno, String jY);
	
	public List<SlmLadingBillHeat> doQueryWebBySQlSpm(int beginindex,int endindex,String startTime,String endTime,String billno,String state,@Param("coilno")String coilno,@Param("matcoilno")String materailcoilno,@Param("conno")String contract_no);

	public int CountfindBillHeat(SlmLadingBillHeat slmLadingBillHeat,
			String startTime, String endTime);

	public List<SlmLadingBillHeat> findBillHeats(
			SlmLadingBillHeat slmLadingBillHeat, int pageIndex, int pageSize,
			String startTime, String endTime);

	public List<SlmLadingBillHeat> doQueryAlign(SlmLadingBillHeat heat,	String sWhere);

	public List<SlmLadingBillHeat> findContractNo(String settle_bill_no);

	public List<SlmLadingBillHeat> doQueryMonthSettle(String date);

	public double getMoney(String bill_no, String contract_no);

	public List<SlmLadingBillHeat> getBillNo(String settle_bill_no);

	public double getMoneyChild(SlmLadingBillLine line);

	public List<SlmLadingBillHeat> doQueryTran(SlmLadingBillHeat heat, String sWhere, String sOrder, int i,
			int pageSize);

	public int doQueryTranCount(SlmLadingBillHeat heat, String sWhere);

	public List<SlmOrderInfo> doQuaryOrderNatureByBillno(String billno);//根据提货单号获取订单属性
	public List<SlmLadingBillDetail> doQuaryYpdHInfo(SlmLadingBillHeat heat);
	
	public List<SlmLadingBillHeat> doQueryBillWebNew(String condition);
	
	//查询未结算提单明细
	public List<HashMap> doQueryUnSettleFundDetail(SlmLadingBillDetail heat,String condition,int start,int end);
	public int doQueryUnSettleFundDetailCount(SlmLadingBillDetail heat,String condition);
	
	
	//doQIfYCJS
	@Select("select distinct t.* from SLM_LADING_BILL_HEAT t\n" +
      "left join SLM_LADING_BILL_DETAIL sd on t.BILL_NO = sd.BILL_NO\n" +
      "where sd.COILNO like concat(concat('%', #{coilNo}), '%') and t.MEMO = '一次结算' and nvl(t.VALID_FLAG, '0') = '1' order by t.CREATE_TIME ")
	List<SlmLadingBillHeat> doQIfYCJS(@Param("coilNo")String coilNo);
	
	@Select("select sum(t.FACTWEIGHT) from SLM_LADING_BILL_DETAIL t right join SLM_LADING_BILL_HEAT sh on t.BILL_NO = sh.BILL_NO where t.ORDER_NO = #{orderNo} and nvl(sh.VALID_FLAG, '0') = '1' and nvl(sh.MEMO, 'a') = '一次结算'")
	Double getYCW (@Param("orderNo")String orderNo);
	
	
	List<HashMap> getTrainBill(@Param("billNo")String billNo, @Param("startTime")String startTime, @Param("endTime")String endTime);
	
	@Select("select count(1) from SLM_LADING_BILL_DETAIL t left join SLM_LADING_BILL_HEAT sh on t.BILL_NO = sh.BILL_NO where t.COILNO = #{coilNo} and sh.MEMO = '一次结算' and nvl(sh.VALID_FLAG, '0') = '1'")
	int isJSNum (@Param("coilNo")String coilNo);
	
	@Select("select sh.CREATE_TIME,sh.BILL_NO,sh.CONTRACT_NO,sh.CUSTOMER_NAME, y.JUDGESTEELCNAME,y.PROD_NAME, t.LOAD_THEORY_WEIGHT,t.LOAD_FACT_WEIGHT,t.QUANTITY,t.CARNO from spm_distrbute_main t join SLM_LADING_BILL_HEAT sh on t.DELIVERYNO = sh.BILL_NO left join ( select row_number() over (partition by d.BILL_NO order by CREATE_TIME )rn,d.JUDGESTEELCNAME,d.BILL_NO,d.PROD_NAME from SLM_LADING_BILL_DETAIL d )y on y.BILL_NO = sh.BILL_NO where t.DELIVERYNO = #{billNo} and y.rn = '1' and t.STATE = '2'")
	SendToCxSystem getSendToCxSystem(@Param("billNo")String billNo);
	
	// 写入前删除所有相同提单号的数据防止主键冲突：delete from YTG_CARTOTRAIN@link_sq where ASKPLAN_ID = '202406010232'
	@Delete("delete from YTG_CARTOTRAIN@link_sq where ASKPLAN_ID = #{bill_no}")
	void deleteCx(@Param("bill_no")String bill_no);
	
	// 往产销系统写入装车
	@Insert("insert into YTG_CARTOTRAIN@link_sq ( DLIV_DIR_DATE,ASKPLAN_ID,PACTNO,INCEPTCORPNAME,TRANSTYPE,STEELCODE,CATEGORY, PLANQUANTITY,WEIGHT,KS,SENDLL,SENDWEIGHT,TRAINWAGON_NZ ) values (to_char(#{create_time}, 'yyyyMMdd'),#{bill_no},#{contract_no},#{customer_name},#{transtype},#{judgesteelcname},#{prod_name},#{pcs},#{load_fact_weight},#{quantity},#{load_theory_weight},#{load_fact_weight},#{carno} )")
	void sendToCx(@Param("create_time")Date create_time, @Param("bill_no")String bill_no,
			@Param("contract_no")String contract_no, @Param("customer_name")String customer_name, 
			@Param("judgesteelcname")String judgesteelcname, @Param("prod_name")String prod_name, 
			@Param("load_theory_weight")Double load_theory_weight, @Param("load_fact_weight")Double load_fact_weight, 
			@Param("quantity")int quantity, @Param("carno")String carno, @Param("transtype")String transtype, @Param("pcs")String pcs);
	
	
	// 写入前删除所有相同提单号的数据防止主键冲突：delete from YTG_CARTOTRAIN@link_sq where ASKPLAN_ID = '202406010232'
	@Delete("delete from KCY_TURNOFFSENDLIST@link_sq where planvehicleid = #{bill_no}")
	void deleteCxDetail(@Param("bill_no")String bill_no);
	
	// 往产销系统写入装车
	@Insert("insert into KCY_TURNOFFSENDLIST@link_sq "
			+ "( pactno,orderformnumber,planvehicleid,trainwagon,vehicletype,serialnumber,stoveno,steeltype,ply,width,weight,addweight,ORDERBUYER,PRODUCT_NAME,CREATTIME,DETERMINANTRESULT ) "
			+ "values ( #{CONTRACT_NO},#{ORDER_NO},#{BILL_NO},#{boxno},'2',#{COILNO},#{memo},#{JUDGESTEELCNAME},#{THICK},#{WIDTH},#{THEORYWEIGHT},#{FACTWEIGHT},#{CUSTOMER_NAME},#{PROD_NAME},#{create_time},#{MATERAILCOILNO} )")
	void sendToCxDetail(
		@Param("create_time")Date create_time, 
		@Param("CONTRACT_NO")String CONTRACT_NO,
		@Param("ORDER_NO")String ORDER_NO,
		@Param("BILL_NO")String BILL_NO,
		@Param("boxno")String boxno, // 车号
		@Param("COILNO")String COILNO,
		@Param("memo")String memo, // 炉号
		@Param("JUDGESTEELCNAME")String JUDGESTEELCNAME,
		@Param("THICK")Double THICK,
		@Param("WIDTH")Double WIDTH,
		@Param("LEN")Double LEN,
		@Param("THEORYWEIGHT")Double THEORYWEIGHT,
		@Param("FACTWEIGHT")Double FACTWEIGHT,
		@Param("CUSTOMER_NAME")String CUSTOMER_NAME,
		@Param("PROD_NAME")String PROD_NAME,
		@Param("MATERAILCOILNO")String MATERAILCOILNO // 综判结果
	);
	
	// 获取装车明细：
	@Select("select t.CONTRACT_NO,t.ORDER_NO,t.BILL_NO,main.CARNO boxno,t.COILNO,ypd.HEATNO memo,t.JUDGESTEELCNAME,t.THICK,t.WIDTH,t.LEN,t.THEORYWEIGHT*1000 THEORYWEIGHT,t.FACTWEIGHT*1000 FACTWEIGHT,t.CUSTOMER_NAME,t.PROD_NAME,decode(ypd.JUDGERESULT,'40740801','合格','40740802','不合格','40740803','判次','40740804','判废') MATERAILCOILNO from SLM_LADING_BILL_DETAIL t left join SPM_DISTRBUTE_MAIN main on t.BILL_NO = main.DELIVERYNO left join YDM_PRODUCT_DETAIL ypd on t.COILNO = ypd.COILNO where t.BILL_NO = #{billNo}")
	List<SlmLadingBillDetail> getSendToCxDetail(@Param("billNo")String billNo);
	
	// 检测提单明细卷是否满足要求：20240730刘侣
	@Select("select case when soi.PK_ASK_CODE = '201101' then 1 when soi.PK_ASK_CODE != '201101' and res.PK_RLT_CODE is null then 0 else 1 end as num from YDM_PRODUCT_DETAIL t left join SLM_ORDER_INFO soi on t.ORDERNO = soi.CONTRACT_NO||soi.CONTRACT_LINE_SEQ||soi.ORDER_SEQ left join MES_PACK_RESULT res on t.COILNO = res.COIL_NO where t.COILNO = #{coilNo}")
	int isMeetRequire(@Param("coilNo")String coilNo);
	
	@Select("select 'F' || to_char(sysdate, 'yy') || decode(to_char(sysdate, 'MM'),'10','A','11','B','12','C',to_number(to_char(sysdate, 'MM')))|| LPAD(NVL(max(TO_NUMBER(SUBSTR(COILNO,5,4))),0)+1,4,'0') || 'H0101'from SLM_LADING_BILL_DETAIL where COILNO like 'F' || to_char(sysdate, 'yy') ||decode(to_char(sysdate, 'MM'),'10','A','11','B','12','C',to_number(to_char(sysdate, 'MM'))) ||'%'order by COILNO desc")
	String getFCoil();
	
	@Select("select sd.PROD_NAME,cbs.STEEL_NAME,sd.STD_NAME,sd.COILNO,sd.THICK ||'*'||sd.WIDTH spec,sd.FACTWEIGHT * 1000 as FACTWEIGHT,''heatno,to_char(sysdate,'yyyy-MM-dd HH24:mi:ss')wrk_date,t.CONTRACT_NO, soi.LEVEL_NAME,soi.RECIVER_ADDRESS,soi.RECIVER_UNIT_NAME, 'CoilNo:' || sd.COILNO || ' SteelGrade:' || cbs.STEEL_NAME || ' Date:' || to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') || ' Weight:' || sd.FACTWEIGHT as barcode from SLM_LADING_BILL_HEAT t left join SLM_LADING_BILL_DETAIL sd on t.BILL_NO = sd.BILL_NO left join SLM_ORDER_INFO soi on sd.CONTRACT_NO||sd.CONTRACT_LINE_SEQ||sd.ORDER_SEQ = soi.CONTRACT_NO||soi.CONTRACT_LINE_SEQ||soi.ORDER_SEQ left join COM_BASE_STEEL cbs on cbs.STEEL_CODE = sd.JUDGESTEELCODE where t.BILL_NO = #{billNo}")
	List<HashMap> getPrintLabel(@Param("billNo")String billNo);
}
