package com.sw.mes.sap.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sw.mes.core.annotation.Jinko;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.sap.dao.MesReconciliationDao;
import com.sw.mes.sap.entity.MesReconciliation;
import com.sw.mes.sap.entity.WarehouseLocation;
import com.sw.mes.sap.service.LogSapStockService;
import com.sw.mes.sap.service.MesReconciliationService;
import com.sw.mes.sap.service.ReportSapGoodsmvtService;
import com.sw.mes.sap.service.SAPOutputServiceService;
import com.sw.mes.sap.service.WarehouseLocationService;
import com.sw.mes.wms.entity.LogWmsMesInteractive;
import com.sw.mes.wms.service.LogWmsMesInteractiveService;


/**
 * 晶科SAP下发生产工单通信记录Service业务层处理
 * 
 * @author liru
 * @date 2023-12-29
 */
@Service
@Transactional
@Jinko(value="WMS发料解析",author = "liru")
public class MesReconciliationServiceImpl extends BaseServiceImpl<MesReconciliation> implements MesReconciliationService
{
	
//	@Autowired
//	private LogSapMesInteractiveService logWmsMesInteractiveService;
	@Autowired
	private LogWmsMesInteractiveService logWmsMesInteractiveService;
	@Autowired
	private SAPOutputServiceService sAPOutputServiceService;
	
	@Autowired
	private LogSapStockService logSapStockService;
	
	@Autowired
	private WarehouseLocationService warehouseLocationService;
	@Autowired
	private ReportSapGoodsmvtService reportSapGoodsmvtService;
	
    private MesReconciliationDao dao;

    @Autowired
    public void setDao(MesReconciliationDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }
    
    
    @Override
    public void startReconciliation(String createTimeStart, String createTimeEnd) throws Exception {
//    	try {
//    		
//    		List<WarehouseLocation> warehouseLocations = warehouseLocationService.findAllPWarehouse();
//            for (WarehouseLocation warehouseLocation : warehouseLocations) {
//	    		//同步sapStock
//	    		sAPOutputServiceService.getSapStock(warehouseLocation.getLgort(),null);
//            }
//    	}catch (Exception e) {
//		}
    	
    	
    	//检查WMS发送原辅料对账信息
    	checkWMSReconciliation(createTimeStart,createTimeEnd);
    	
    	//绑定对账和sap库存
//    	buildSAPStockAndReconciliation();
    	
    	
    	
    	
    }
    
    
    private void buildSAPStockAndReconciliation() {
		dao.buildSAPStockAndReconciliation();
	}


	/**1. 检查WMS发送原辅料对账信息**/
    private void checkWMSReconciliation(String createTimeStart, String createTimeEnd) {
    	dao.deleteAll();
    	
    	List<LogWmsMesInteractive> logWmsMesInteractiveList = logWmsMesInteractiveService.findByControllerNameAndTimeRange("晶科WMS原辅料发线边仓",createTimeStart,createTimeEnd);
    	for (LogWmsMesInteractive logWmsMesInteractive : logWmsMesInteractiveList) {
    		JSONObject item = JSONObject.parseObject(logWmsMesInteractive.getContext());
    		JSONObject bcInfo =item.getJSONObject("IS_BC_INFO");
    		if(bcInfo.getString("TYPE").equals("S")) {
    			JSONObject data = item.getJSONObject("IT_DATA");
    			JSONArray details = data.getJSONArray("details");
    			for(int i=0;i<details.size();i++) {
    				JSONObject obj = details.getJSONObject(i);
    				String sku = obj.getString("sku");//物料号
    				String qty = obj.getString("qty");//数量
    				String lotatt04 = obj.getString("lotatt04");//批次号
    				
    				MesReconciliation reconciliation = null;
    				
    				List<MesReconciliation> list = findByLotAndSku(lotatt04,sku);
    				if(list.size()>0) {
    					reconciliation = list.get(0);
    				}else {
    					reconciliation = new MesReconciliation();
    					reconciliation.setLot(lotatt04);
        				reconciliation.setSku(sku);
        				reconciliation.setQty(0d);
        				reconciliation.setMesReceiveTime(logWmsMesInteractive.getCreateTime());
    				}
    				
    				//物料 批次 总数量
    				reconciliation.setQty(reconciliation.getQty()+Double.valueOf(qty));
    				
    				saveOrUpdate(reconciliation);
    			}
    		}
		}
    }
    
    private List<MesReconciliation> findByLotAndSku(String lotatt04, String sku) {
		return dao.findByLotAndSku(lotatt04,sku);
	}

}
