/**   
 * @Title: SendSapWarehousingStatisticsInfoYbServiceImpl.java 
 * @Package com.sw.mes.webservice.service.impl 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author 中环 keily  
 * @date 2022年3月1日 下午4:28:51 
 * @version V1.0   
 */   
     
package com.sw.mes.webservice.service.impl;

import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.utils.SapUtil;
import com.sw.mes.third.entity.Gr;
import com.sw.mes.third.service.SapGrService;
import com.sw.mes.webservice.dao.SendSapWarehousingStatisticsInfoYbDao;
import com.sw.mes.webservice.entity.SendSapWarehousingStatisticsInfoYb;
import com.sw.mes.webservice.service.SendGrToSapService;
import com.sw.mes.webservice.service.SendSapWarehousingStatisticsInfoYbService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.namespace.QName;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/** 
 * @ClassName: SendSapWarehousingStatisticsInfoYbServiceImpl 
 * @Description: 向sap发送的gr圆棒入库数据记录表 service impl
 * @author 中环 keily
 * @date 2022年3月1日 下午4:28:51 
 *  
 */
@Service
@Transactional
public class SendSapWarehousingStatisticsInfoYbServiceImpl extends BaseServiceImpl<SendSapWarehousingStatisticsInfoYb>
		implements SendSapWarehousingStatisticsInfoYbService {
	
	@Autowired
    private SapGrService sapGrService;
	
	@Autowired
	private SendGrToSapService sendGrToSapService; // 发送gr数据到sap service

	SendSapWarehousingStatisticsInfoYbDao sendSapWarehousingStatisticsInfoYbDao;
	@Autowired
	public void setSendSapWarehousingStatisticsInfoYbDao(SendSapWarehousingStatisticsInfoYbDao dao) {
		super.setBaseDao(dao);
		this.sendSapWarehousingStatisticsInfoYbDao = dao;
	}
	
	@Override
	public String retrySend(Integer[] ids) {
		String msg = null;
		//1.查询数据
		List<Integer> idsList = Arrays.asList(ids);
		List<SendSapWarehousingStatisticsInfoYb> entityList = sendSapWarehousingStatisticsInfoYbDao.findAllById(idsList);
		//2.组装发送数据，请求发送sap
		List<Gr> list = new ArrayList<>();
		if(entityList.size() > 0) {
			for (SendSapWarehousingStatisticsInfoYb entity : entityList) {
				if (entity.getOrderNo() != null && entity.getMaterialCode() != null && entity.getTheoryWeight() != null
						&& entity.getGrade() != null 
						&& StringUtils.isNotBlank(entity.getOrderNo().toString()) // 订单编号
						&& StringUtils.isNotBlank(entity.getMaterialCode().toString())  // 料号
						&& StringUtils.isNotBlank(entity.getGrade().toString()) // 产品等级
						&& new BigDecimal(entity.getTheoryWeight().toString()).compareTo(new BigDecimal(0)) > 0 // 理论重量
						) {
					Gr grEntity = new Gr();
					grEntity.setZMESID(entity.getZmesid()); // MES DATA Key ID Number
					grEntity.setAUFNR(entity.getOrderNo()); // SAP生产订单编码
					grEntity.setMATNR(entity.getMaterialCode()); // 产品编码
					grEntity.setWERKS(entity.getWerks()); // 工厂编码	
					grEntity.setMENGE(entity.getTheoryWeight().toString()); // 收货数量
					grEntity.setMEINS("KG"); // 基本单位
					grEntity.setLGORT("Q026"); // 收货库存地点
					grEntity.setGRDATE(entity.getGrdate());// 日期 yyyymmdd
					grEntity.setGRTIME(entity.getGrtime());// 时间 HHmmss
					grEntity.setGRADE(entity.getGrade()); // 产品等级
					grEntity.setCHARG(entity.getCharg()); // 产品批次信息
					grEntity.setBTY(entity.getBty()); // 业务类型 1 GR ; 2 GR cancel
					list.add(grEntity);
				}
			}
		}
		
		//3.根据返回结果更新记录数据
		if (list.size() > 0) {
			try {
				String sapOrderDate = SapUtil.getDateNowForSap(540);
				for (Gr gr : list) {
					String newAUFNR = sapGrService.getNowSapOrderNo(gr.getAUFNR(), sapOrderDate);
					if (StringUtils.isNotBlank(newAUFNR)) {
						gr.setAUFNR(newAUFNR);
					}
				}
				Map map = sendGrToSapService.mesToSap(list, 300000);
				String sapResult =  map.get(new QName( "ZTYPE")).toString();
				if(sapResult.equals("S")){ // test
					//更新数据状态
					entityList.stream().forEach(entity -> entity.setStatus("1")); // 0:失败；1：成功；
					sendSapWarehousingStatisticsInfoYbDao.saveAll(entityList);
					msg ="发送sap成功，更新状态成功;";
				}
				else {
					msg = "发送sap失败;";
				}
			} catch (Exception e) {
				e.printStackTrace();
				msg = "发送sap发生异常;";
			}
		}
		return msg;
	}
	
	
}
