/**
 * @Title: ProcessYieldServiceImpl.java
 * @Package com.sw.mes.webservice.service.impl
 * @Description: TODO(用一句话描述该文件做什么)
 * @author 中环 keily
 * @date 2022年2月28日 上午10:34:15
 * @version V1.0
 */

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

import com.sw.mes.core.utils.SapUtil;
import com.sw.mes.core.utils.WbServiceUtils;
import com.sw.mes.third.entity.Gr;
import com.sw.mes.third.enums.SapStaticEnum;
import com.sw.mes.third.service.SapGrService;
import com.sw.mes.webservice.entity.SendSapWarehousingStatisticsInfoYb;
import com.sw.mes.webservice.service.GrService;
import com.sw.mes.webservice.service.SendGrToSapService;
import com.sw.mes.yb.turnstore.entity.WarehousingStatisticsYb;
import com.sw.mes.yb.turnstore.service.WarehousingStatisticsYbService;
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 java.math.BigDecimal;
import java.util.*;

@Service
@Transactional
public class GrServiceImpl implements GrService {

	@Autowired
	private WarehousingStatisticsYbService warehousingStatisticsYbService; // 圆棒入库确认统计service

	@Autowired
    private SapGrService sapGrService;

	@Autowired
	private SendGrToSapService sendGrToSapService; // 发送gr数据到sap service

	@Override
	public void sendGrData() {
		Date nowTime = new Date();

		//计算结束时间 开始时间
		Calendar caNow = Calendar.getInstance();
        caNow.setTime(nowTime);

        caNow.set(Calendar.MINUTE, 0);
        caNow.set(Calendar.SECOND, 0);
        caNow.set(Calendar.MILLISECOND, 0);
        Date endTime = caNow.getTime();

        caNow.add(Calendar.HOUR_OF_DAY, -1);
        caNow.set(Calendar.MINUTE, 0);
        caNow.set(Calendar.SECOND, 0);
        caNow.set(Calendar.MILLISECOND, 0);
        Date startTime = caNow.getTime();




		//1.查询数据
		List<WarehousingStatisticsYb> queryList = warehousingStatisticsYbService.findByConfirmTime(startTime,endTime);

		if (queryList.size() > 0) {
			// 2.组装数据
			String sapDate = WbServiceUtils.getSapDate(nowTime);
	        String sapTime = WbServiceUtils.getSapTime(nowTime);
			List<Gr> list = new ArrayList<>();
			List<SendSapWarehousingStatisticsInfoYb> sendRecordlist = new ArrayList<>();
			for (WarehousingStatisticsYb map : queryList) {
				if (map.getOrderNo() != null && map.getMaterialCode() != null && map.getTheoryWeight() != null
						&& map.getPlanGrade() != null
						&& StringUtils.isNotBlank(map.getOrderNo().toString()) // 订单编号
						&& StringUtils.isNotBlank(map.getMaterialCode().toString())  // 料号
						&& StringUtils.isNotBlank(map.getPlanGrade().toString()) // 产品等级
						&& new BigDecimal(map.getTheoryWeight().toString()).compareTo(new BigDecimal(0)) > 0 // 理论重量
						) {
					Gr grEntity = new Gr();
					grEntity.setZMESID(UUID.randomUUID().toString()); // MES DATA Key ID Number
					grEntity.setAUFNR(map.getOrderNo()); // SAP生产订单编码
					grEntity.setMATNR(map.getMaterialCode()); // 产品编码
					grEntity.setWERKS(SapStaticEnum.SapFactoryId.六期工厂ID.getValue()); // 工厂编码
					grEntity.setMENGE(map.getTheoryWeight().toString()); // 收货数量
					grEntity.setMEINS("KG"); // 基本单位
					grEntity.setLGORT("Q026"); // 收货库存地点
					grEntity.setGRDATE(sapDate);// 日期 yyyymmdd
					grEntity.setGRTIME(sapTime);// 时间 HHmmss
					grEntity.setGRADE(map.getPlanGrade().toString()); // 产品等级
					grEntity.setCHARG("9999999999"); // 产品批次信息
					grEntity.setBTY("1"); // 业务类型 1 GR ; 2 GR cancel
					list.add(grEntity);

					//圆棒发送记录
					SendSapWarehousingStatisticsInfoYb ybRecord = new SendSapWarehousingStatisticsInfoYb();
					ybRecord.setZmesid(grEntity.getZMESID());
					ybRecord.setOrderNo(grEntity.getAUFNR()); // SAP生产订单编码
					ybRecord.setMaterialCode(grEntity.getMATNR()); // 产品编码
					ybRecord.setWerks(grEntity.getWERKS()); // 工厂编码
					ybRecord.setTheoryWeight(new BigDecimal(grEntity.getMENGE().toString())); // 收货数量
					ybRecord.setMeins(grEntity.getMEINS()); // 基本单位
					ybRecord.setCharg(grEntity.getCHARG()); // 产品批次信息
					ybRecord.setGrdate(grEntity.getGRDATE()); // 日期 yyyymmdd
					ybRecord.setGrtime(grEntity.getGRTIME()); // 时间 HHmmss
					ybRecord.setGrade(grEntity.getGRADE());// 产品等级
					ybRecord.setBty(grEntity.getBTY()); // 业务类型 1 GR ; 2 GR cancel
					ybRecord.setLgort(grEntity.getLGORT()); //  收货库存地点
					ybRecord.setBatchval1(grEntity.getBATCHVAL1()); //
					ybRecord.setBatchval2(grEntity.getBATCHVAL2()); //
					ybRecord.setBatchval3(grEntity.getBATCHVAL3()); //
					ybRecord.setBatchval4(grEntity.getBATCHVAL4()); //
					ybRecord.setBatchval5(grEntity.getBATCHVAL5()); //
					ybRecord.setBatchval6(grEntity.getBATCHVAL6()); //
					ybRecord.setBatchval7(grEntity.getBATCHVAL7()); //
					ybRecord.setBatchval8(grEntity.getBATCHVAL8()); //
					ybRecord.setCreateTime(nowTime);
					ybRecord.setSectionNo(map.getSectionNo()); // 晶段编号(短棒)
					sendRecordlist.add(ybRecord);

				}
			}

			// 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);
						}
					}
					sendGrToSapService.mesToSap(list, 300000, sendRecordlist);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}
