package com.zfire.jiasm.syncdata.gree.recycle;

import com.aliyun.oss.common.utils.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zfire.jiasm.syncdata.constant.Constant;
import com.zfire.jiasm.syncdata.constant.GreeSynStatusEnum;
import com.zfire.jiasm.syncdata.constant.RecycleSynStatusEnum;
import com.zfire.jiasm.syncdata.gree.helper.GreeResponseHelper;
import com.zfire.jiasm.syncdata.gree.recycle.stock.RecycleStockBarcodeInfoUploadBean;
import com.zfire.jiasm.syncdata.plus.entity.*;
import com.zfire.jiasm.syncdata.plus.service.*;
import com.zfire.jiasm.syncdata.response.WebsitSH;
import com.zfire.jiasm.syncdata.response.WorderInfo;
import com.zfire.jiasm.syncdata.service.PublicService;
import com.zfire.jiasm.syncdata.utils.StringUtil;
import io.jsonwebtoken.lang.Collections;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;



@Slf4j
@Service
public class RecycleAcquisitionLogic {
	@Autowired
	GreeLogicT greeLogic;
	@Autowired
	ItfTblRecycleHsptJjcjDownloadService recycleHsptJjcjDownloadService;
	@Autowired
	ItfTblRecycleHsptJjcjFjDownloadService recycleHsptJjcjFjDownloadService;
	@Autowired
	ItfTblRecycleHsptJjcjTmmxDownloadService recycleHsptJjcjTmmxDownloadService;
	@Autowired
	ItfTblRecycleAssignBatchDownloadRecService recycleAssignBatchDownloadRecService;
	@Autowired
	ItfTblRecycleHsptJjcjBatchUploadRecService recycleHsptJjcjBatchUploadRecService;
	@Autowired
	ItfTblRecycleHsptJjcjUploadService recycleHsptJjcjUploadService;
	@Autowired
	ItfTblRecycleHsptJjcjFjUploadService recycleHsptJjcjFjUploadService;
	@Autowired
	ItfTblRecycleHsptJjcjTmmxUploadService recycleHsptJjcjTmmxUploadService;
	@Autowired
	ItfTblRecyleStockInfoUploadTaskListService recyleStockInfoUploadTaskListService;
	@Autowired
	OrderRecycleUploadEventService orderRecycleUploadEventService;
	@Autowired
	ItfTblRecycleStockBarcodeInfoUploadService recycleStockBarcodeInfoUploadService;
	@Autowired
	RedisLockRegistry redisLockRegistry;
	@Autowired
	OrderRecycleAssignFkmxService orderRecycleAssignFkmxService;
	@Autowired
	OrderRecycleAssignService orderRecycleAssignService;
	@Autowired
	OrderRecycleAssignCollectionService orderRecycleAssignCollectionService;
	@Autowired
	OrderRecycleAssignAnnexService orderRecycleAssignAnnexService;
	@Autowired
	OrderRecycleAssignMxService orderRecycleAssignMxService;
	@Autowired
	PublicService publicService;
	@Autowired
	private OrderBaseService orderBaseService;
	@Autowired
	ItfTblRecycleHsptJjcjUploadService itfTblRecycleHsptJjcjUploadService;
	@Autowired
	ItfTblRecycleHsptJjcjFjUploadService itfTblRecycleHsptJjcjFjUploadService;
	@Autowired
	ItfTblRecycleHsptJjcjTmmxUploadService itfTblRecycleHsptJjcjTmmxUploadService;
    private ObjectMapper mapper;

	
	@Autowired
	public RecycleAcquisitionLogic() {
		this.mapper = new ObjectMapper();
		SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		this.mapper.setDateFormat(smt);
		this.mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	}




	private void updateData(RecycleAssignBean bean, GreeResponseHelper response, ItfTblRecycleAssignBatchDownloadRec rec) {
		//回收工单采集数据下载失败
		// rec.setSynStatus(99);
		rec.setCjSynStatus(Constant.SynStatus.FAIL.getCode());
		rec.setCjSynTimes(rec.getCjSynTimes()+1);
		rec.setSynErrMsg(response.getMsg());
		rec.setSynReqTime(new Date());
		rec.setLastModifiedDate(new Date());
		recycleAssignBatchDownloadRecService.saveOrUpdate(rec);

		/*// 下载失败原表中一般也不会有数据 处理一下也不会有问题
		// 采集表
		List<ItfTblRecycleHsptJjcjDownload> jjcjList = recycleHsptJjcjDownloadService.lambdaQuery()
				.eq(ItfTblRecycleHsptJjcjDownload::getDownloadTaskNo, bean.getTaskNo()).list();
		// 采集附件表
		List<ItfTblRecycleHsptJjcjFjDownload> fjlist = recycleHsptJjcjFjDownloadService.lambdaQuery()
				.eq(ItfTblRecycleHsptJjcjFjDownload::getDownloadTaskNo, bean.getTaskNo()).list();
		// 采集条码明细表
		List<ItfTblRecycleHsptJjcjTmmxDownload> tmmxlist = recycleHsptJjcjTmmxDownloadService.lambdaQuery()
				.eq(ItfTblRecycleHsptJjcjTmmxDownload::getDownloadTaskNo, bean.getTaskNo()).list();
		if(CollectionUtils.isNotEmpty(jjcjList)) {
			jjcjList.stream().forEach(o->{
					o.setSynStatus(rec.getSynStatus());
					o.setSynErrMsg(rec.getSynErrMsg());
					o.setLastModifiedDate(new Date());
					o.setSynReqTime(new Date());
				});
			recycleHsptJjcjDownloadService.saveOrUpdateBatch(jjcjList);
			jjcjList.clear();
		}
		if(CollectionUtils.isNotEmpty(fjlist)) {
			fjlist.stream().forEach(o->{
					o.setSynStatus(rec.getSynStatus());
					o.setSynErrMsg(rec.getSynErrMsg());
					o.setLastModifiedDate(new Date());
					o.setSynReqTime(new Date());
				});
			recycleHsptJjcjFjDownloadService.saveOrUpdateBatch(fjlist);
			fjlist.clear();
		}
		if(CollectionUtils.isNotEmpty(tmmxlist)) {
			tmmxlist.stream().forEach(o->{
					o.setSynStatus(rec.getSynStatus());
					o.setSynErrMsg(rec.getSynErrMsg());
					o.setLastModifiedDate(new Date());
					o.setSynReqTime(new Date());
				});
			recycleHsptJjcjTmmxDownloadService.saveOrUpdateBatch(tmmxlist);
			tmmxlist.clear();
		}
		*/
	}

	private void addData(RecycleAssignBean bean, GreeResponseHelper response, ItfTblRecycleAssignBatchDownloadRec rec) throws JsonProcessingException {
		List<RecycleAcquisitionDownloadBean> responsebeanList = mapper.readValue((String) response.getData(), new TypeReference<List<RecycleAcquisitionDownloadBean>>() {});
		if(CollectionUtils.isNotEmpty(responsebeanList)) {
			responsebeanList.stream().forEach(responseBean->{
				// 采集主表
				List<ItfTblRecycleHsptJjcjDownload> jjcjList = new ArrayList<>();
				// 采集附件表
				List<ItfTblRecycleHsptJjcjFjDownload> fjList = new ArrayList<>();
				// 采集条码明细表
				List<ItfTblRecycleHsptJjcjTmmxDownload> tmmxList = new ArrayList<>();
				try {
					// 提取采集主表数据
					getJjcjList(bean, responseBean, jjcjList);
					// 提取采集附件表表数据
					getFjList(bean, responseBean, fjList);
					// 提取采集条码明细表数据
					getTmmxList(bean, responseBean, tmmxList);
					// 保存数据
					inputData(jjcjList, fjList, tmmxList);
				} catch (JsonProcessingException ex) {
					log.error("复制RecycleHsptJjcjDownloadBean bean到RecycleHsptJjcjFjDownload entity异常: {}", ex.getMessage());
				}
			});
		}
		//更新状态为已同步 增加一个中间状态，代表数据采集已完成
		rec.setSynStatus(Constant.SynStatus.SYN_ACQUISITION.getCode());
		rec.setCjSynStatus(Constant.SynStatus.SYN.getCode());
		rec.setSynErrMsg("");
		rec.setSynReqTime(new Date());
		rec.setLastModifiedDate(new Date());
		recycleAssignBatchDownloadRecService.saveOrUpdate(rec);
	}

	private void inputData(List<ItfTblRecycleHsptJjcjDownload> jjcjList, List<ItfTblRecycleHsptJjcjFjDownload> fjList, List<ItfTblRecycleHsptJjcjTmmxDownload> tmmxList) {
		if(null != jjcjList && !jjcjList.isEmpty()) {
			recycleHsptJjcjDownloadService.saveOrUpdateBatch(jjcjList);
			jjcjList.clear();
		}
		if(null != fjList && !fjList.isEmpty()) {
			recycleHsptJjcjFjDownloadService.saveOrUpdateBatch(fjList);
			fjList.clear();
		}
		if(null != tmmxList && !tmmxList.isEmpty()) {
			recycleHsptJjcjTmmxDownloadService.saveOrUpdateBatch(tmmxList);
			tmmxList.clear();
		}
	}

	private void getTmmxList(RecycleAssignBean bean, RecycleAcquisitionDownloadBean responseBean, List<ItfTblRecycleHsptJjcjTmmxDownload> tmmxList) {
		List<RecycleHsptJjcjTmmxDownloadBean> tmmxBeanList = responseBean.getTmmxlist();
		if(null != tmmxBeanList && !tmmxBeanList.isEmpty()) {
			tmmxBeanList.stream().forEach(tmmxBean->{
				try {
					ItfTblRecycleHsptJjcjTmmxDownload tmmx = mapper.readValue(mapper.writeValueAsString(tmmxBean), ItfTblRecycleHsptJjcjTmmxDownload.class);
					tmmx.setCreatedDate(new Date());
					tmmx.setLastModifiedDate(new Date());
					tmmx.setDownloadBatchNo(bean.getBatchNo());
					tmmx.setDownloadTaskNo(bean.getTaskNo());
					tmmx.setSynStatus(Constant.SynStatus.SYN_ACQUISITION.getCode());//增加一个中间状态，代表数据采集已完成
					tmmx.setSynReqTime(new Date());
					tmmxList.add(tmmx);
				} catch (JsonProcessingException e) {
					log.error("复制RecycleHsptJjcjTmmxDownload bean到RecycleHsptJjcjTmmxDownload entity异常: {}", e.getMessage());
				}
			});
		}
	}

	private void getFjList(RecycleAssignBean bean, RecycleAcquisitionDownloadBean responseBean, List<ItfTblRecycleHsptJjcjFjDownload> fjList) {
		List<RecycleHsptJjcjFjDownloadBean> fjBeanlist = responseBean.getFjlist();
		if( null != fjBeanlist && !fjBeanlist.isEmpty()) {
			fjBeanlist.stream().forEach(fjBean->{
				try {
					ItfTblRecycleHsptJjcjFjDownload fj = mapper.readValue(mapper.writeValueAsString(fjBean), ItfTblRecycleHsptJjcjFjDownload.class);
					fj.setCreatedDate(new Date());
					fj.setLastModifiedDate(new Date());
					fj.setDownloadBatchNo(bean.getBatchNo());
					fj.setDownloadTaskNo(bean.getTaskNo());
					fj.setSynStatus(Constant.SynStatus.SYN_ACQUISITION.getCode());//增加一个中间状态，代表数据采集已完成
					fj.setSynReqTime(new Date());
					fjList.add(fj);
				} catch (JsonProcessingException e) {
					log.error("复制RecycleHsptJjcjFjDownload bean到RecycleHsptJjcjFjDownload entity异常: {}", e.getMessage());
				}
			});
		}
	}

	private void getJjcjList(RecycleAssignBean bean, RecycleAcquisitionDownloadBean responseBean, List<ItfTblRecycleHsptJjcjDownload> jjcjList) throws JsonProcessingException {
		ItfTblRecycleHsptJjcjDownload newJjcj = mapper.readValue(mapper.writeValueAsString(responseBean.getJjcj() ), ItfTblRecycleHsptJjcjDownload.class);
		newJjcj.setCreatedDate(new Date());
		newJjcj.setLastModifiedDate(new Date());
		newJjcj.setDownloadBatchNo(bean.getBatchNo());
		newJjcj.setDownloadTaskNo(bean.getTaskNo());
		//增加一个中间状态，代表数据采集已完成
		newJjcj.setSynStatus(Constant.SynStatus.SYN_ACQUISITION.getCode());
		newJjcj.setSynReqTime(new Date());
		jjcjList.add(newJjcj);
	}



	public void syncDataCJCheck() {
		// 回收工单同步记录表提取待同步的过程反馈数据数据
		List<OrderRecycleUploadEvent> collectionEventList = orderRecycleUploadEventService.lambdaQuery()
				.eq(OrderRecycleUploadEvent::getSynStatus, Constant.SynStatus.UN_SYN.getCode())
				.eq(OrderRecycleUploadEvent::getTableName, Constant.RecycleOrderType.RECYCLE_ASSGIN_COLLECTION)
				.list();
		if(CollectionUtils.isNotEmpty(collectionEventList)) {
			for (OrderRecycleUploadEvent item:collectionEventList){
				OrderBase order = orderBaseService.lambdaQuery()
						.eq(OrderBase::getPgguid, item.getOrderNo())
						.last("limit 1")
						.one();
				// 工单号，名表，记录ID
				publicService.checkWebsitSyncFlag(order.getId(),"order_recycle_upload_event",item.getSynReqId());
			}
		}

	}

	/**
	 * 批量上传采集信息
	 */
	@Transactional(rollbackFor = Exception.class)
	public void acquisitionUpload() {
		syncDataCJCheck();
		// 回收工单同步记录表中提取待同步的 回收工单-采集主表
		List<OrderRecycleUploadEvent> collectionEventList = orderRecycleUploadEventService.lambdaQuery()
				.eq(OrderRecycleUploadEvent::getSynStatus, Constant.SynStatus.UN_SYN.getCode())
				.eq(OrderRecycleUploadEvent::getTableName, Constant.RecycleOrderType.RECYCLE_ASSGIN_COLLECTION)
				.list();

		if(CollectionUtils.isNotEmpty(collectionEventList)) {
			// 回收工单同步记录表中提取回 收工单-数据采集附件表
			List<OrderRecycleUploadEvent> annexEventList = orderRecycleUploadEventService.lambdaQuery()
					.eq(OrderRecycleUploadEvent::getSynStatus, Constant.SynStatus.UN_SYN.getCode())
					.eq(OrderRecycleUploadEvent::getTableName, Constant.RecycleOrderType.RECYCLE_ASSIGN_ANNEX).list();

			String batchNo = publicService.getUUID();
			final Map<String, List<OrderRecycleUploadEvent>> collectionEventMap = collectionEventList.stream().collect(Collectors.groupingBy(OrderRecycleUploadEvent::getId));
			final Map<String, List<OrderRecycleUploadEvent>> annexEventMap = (null == annexEventList || annexEventList.isEmpty()) ? null : annexEventList.stream().collect(Collectors.groupingBy(OrderRecycleUploadEvent::getId));
			LinkedHashSet<String> collectionIds = new LinkedHashSet<>();
			LinkedHashSet<String> annexIds = new LinkedHashSet<>();
			collectionEventList.stream().forEach(event->{
				collectionIds.add(event.getId());
			});
			if(null != annexEventList && !annexEventList.isEmpty()) {
				annexEventList.stream().forEach(annexEvent->{
					annexIds.add(annexEvent.getId());
				});
			}
			if(!collectionIds.isEmpty()) {
				// 提取待同步的采集主表数据
				List<OrderRecycleAssignCollection> collectionList = orderRecycleAssignCollectionService.lambdaQuery()
						.in(OrderRecycleAssignCollection::getId, collectionIds).list();
				if( null != collectionList && !collectionList.isEmpty()) {
					// 获取采集附件表数据
					final List<OrderRecycleAssignAnnex> annexList = annexIds.isEmpty() ? null :
							orderRecycleAssignAnnexService.lambdaQuery()
									.in(OrderRecycleAssignAnnex::getId, annexIds).list();
					// 采集附件表按工单主表主键分组
					final Map<Integer, List<OrderRecycleAssignAnnex>> annexMap = (null == annexList || annexList.isEmpty()) ? null :  annexList.stream().collect(Collectors.groupingBy(OrderRecycleAssignAnnex::getCollectionId));
					LinkedHashSet<Integer> orderIds = new LinkedHashSet<>();
					LinkedHashSet<Integer> mxIds = new LinkedHashSet<>();
					// 提取回收工单ID
					collectionList.stream().forEach(collection->{
						orderIds.add(collection.getOrderId());
						mxIds.add(collection.getMxId());
					});

					// 提取回收工单主表 数据明细 spmc (value = "品类名称拼接，空调、电视")
					List<OrderRecycleAssign> recycleList = orderRecycleAssignService.lambdaQuery()
							.in(OrderRecycleAssign::getId, orderIds).list();
					final Map<Integer, List<OrderRecycleAssign>> recycleMap = (null == recycleList || recycleList.isEmpty()) ? null : recycleList.stream().collect(Collectors.groupingBy(OrderRecycleAssign::getId));
					// 提取 回收明细表数据
					List<OrderRecycleAssignMx> mxList = orderRecycleAssignMxService.lambdaQuery()
							.in(OrderRecycleAssignMx::getId, mxIds).list();
					collectionList.stream().forEach(collection->{
						// 存同步记录表
						List<OrderRecycleUploadEvent> eventList = new ArrayList<>();
						final List<OrderRecycleUploadEvent> collectionEventGroupList =  collectionEventMap.get(collection.getId());
						eventList.addAll(collectionEventGroupList);

						RecycleAcquisitionUploadBean bean = new RecycleAcquisitionUploadBean();
						RecycleHsptJjcjUploadBean feedbackUploadBean = new RecycleHsptJjcjUploadBean();
						//完工和报废状态才上传
						if(null != recycleMap && recycleMap.containsKey(collection.getOrderId())
								&& null != recycleMap.get(collection.getOrderId())
								&& !recycleMap.get(collection.getOrderId()).isEmpty()) {
							if(null != recycleMap.get(collection.getOrderId()).get(0).getOrderState()
									&& 6 != recycleMap.get(collection.getOrderId()).get(0).getOrderState().intValue()
									&& 7 != recycleMap.get(collection.getOrderId()).get(0).getOrderState().intValue()
							) {
								return;
							}
						}

						feedbackUploadBean.setPgguid(recycleList.stream().filter(recycle->recycle.getId().intValue() ==collection.getOrderId().intValue() ).collect(Collectors.toList()).get(0).getPgguid());
						feedbackUploadBean.setPgmxid(mxList.stream().filter(mx->mx.getId().intValue() == collection.getMxId()).collect(Collectors.toList()).get(0).getPgmxid());
						//总部和广佛两边鉴定结果刚好相反
						feedbackUploadBean.setJdjg( (null == collection.getIdentificationResult()) ? null :(  0 == collection.getIdentificationResult().intValue() ? 1 : 0));
						feedbackUploadBean.setSfcj(collection.getDismantle());
						feedbackUploadBean.setLceng(collection.getFloor());
						feedbackUploadBean.setCcun(collection.getMachineSize());
						feedbackUploadBean.setJjpp(collection.getMachineBrand());
						feedbackUploadBean.setJjlx(collection.getMachineType());
						feedbackUploadBean.setJjqsqk(collection.getMachineDefectData());
						feedbackUploadBean.setPshu(collection.getMachineLmx());

						feedbackUploadBean.setSpid(mxList.stream().filter(mx->mx.getId().intValue() == collection.getMxId()).collect(Collectors.toList()).get(0).getSpid());
						feedbackUploadBean.setSpmc(mxList.stream().filter(mx->mx.getId().intValue() == collection.getMxId()).collect(Collectors.toList()).get(0).getSpmc());
						//gps经纬度
						feedbackUploadBean.setGps((!StringUtils.isNullOrEmpty(collection.getLat()) && !StringUtils.isNullOrEmpty(collection.getLng()) ) ? (collection.getLat()+","+collection.getLng()) : "");
						feedbackUploadBean.setSjcjsfen(collection.getProvince());
						feedbackUploadBean.setSjcjcshi(collection.getCity());
						feedbackUploadBean.setSjcjxian(collection.getArea());
						feedbackUploadBean.setGpsdizi(collection.getGpsAddress());

						WebsitSH wsh = null;
						if( null != collection.getMainResponsibleId() ) {
							// spmc (value = "品类名称拼接，空调、电视")
							wsh = publicService.ptshwdToshshwd(recycleMap.get(collection.getOrderId()).get(0).getWxwdno(),
									recycleMap.get(collection.getOrderId()).get(0).getSpmc());
							WorderInfo worderInfo = publicService.getWorkerInfo(String.valueOf(collection.getMainResponsibleId()),
									wsh.getWdId(),"1");
							if( null != worderInfo ) {
								feedbackUploadBean.setAzrenno(worderInfo.getBrandWorkerNumber());
								feedbackUploadBean.setAzren(worderInfo.getWorkerName());
								feedbackUploadBean.setAzrenyddh(worderInfo.getMobile());
							}
						}

						//网点编号,网点名称
						Websit websit = null;
						OrderRecycleAssign orderRecycleAssign = recycleMap.get(collection.getOrderId()).get(0);
						if(null != orderRecycleAssign.getAfterWebsitId()) {
							if( null != wsh) {
								feedbackUploadBean.setWdno(wsh.getWdId());
								feedbackUploadBean.setWdmc(wsh.getWdmc());
							}
						}

						bean.setTblHsptJjcjVO(feedbackUploadBean);
						bean.setBatchNo(batchNo);
						bean.setTaskNo(publicService.getUUID());
                        // 附件
						List<RecycleHsptJjcjFjUploadBean> tblHsptJjcjFjVO = new ArrayList<>();
						//条码
						List<RecycleHsptJjcjTmmxUploadBean> tblHsptJjcjTmmxVO = new ArrayList<>();

						if(!StringUtils.isNullOrEmpty(collection.getInsideTempCode())) {
							if(null != annexMap && annexMap.containsKey(collection.getId())
									&& null != annexMap.get(collection.getId()) && !annexMap.get(collection.getId()).isEmpty()) {
								List<OrderRecycleAssignAnnex> subAnnexList = annexMap.get(collection.getId());
								if( null != subAnnexList && !subAnnexList.isEmpty() ) {
									subAnnexList.stream().forEach(o->{
										if(null != o.getType() && 1 == o.getType().intValue()) {//内机条码
											RecycleHsptJjcjTmmxUploadBean tmmxBean = new RecycleHsptJjcjTmmxUploadBean();
											tmmxBean.setTmlx(0);
											tmmxBean.setJqtm(collection.getInsideTempCode());
											tmmxBean.setFjserverpath(o.getGreeImageId());
											tblHsptJjcjTmmxVO.add(tmmxBean);

											//图片id不为空才上传
											if( !StringUtil.isEmpty(o.getGreeImageId()) ) {
												RecycleHsptJjcjFjUploadBean fjBean = new RecycleHsptJjcjFjUploadBean();
												fjBean.setType(1);
												fjBean.setFjindex(1);
												fjBean.setFjname("内机条码图");
												fjBean.setFjserverpath(o.getGreeImageId());
												tblHsptJjcjFjVO.add(fjBean);
											}

											if(null != annexEventMap
													&& annexEventMap.containsKey(o.getId())
													&& null != annexEventMap.get(o.getId())
											) {
												List<OrderRecycleUploadEvent> annexEventGroupList = annexEventMap.get(o.getId());
												eventList.addAll(annexEventGroupList);
											}
										}else if(null != o.getType() && 2 == o.getType().intValue()) {//外机条码
											RecycleHsptJjcjTmmxUploadBean tmmxBean = new RecycleHsptJjcjTmmxUploadBean();
											tmmxBean.setTmlx(1);
											tmmxBean.setJqtm(collection.getOutTempCode());
											tmmxBean.setFjserverpath(o.getGreeImageId());
											tblHsptJjcjTmmxVO.add(tmmxBean);
//													图片id不为空才上传
											if(!StringUtil.isEmpty(o.getGreeImageId())) {
												RecycleHsptJjcjFjUploadBean fjBean = new RecycleHsptJjcjFjUploadBean();
												fjBean.setType(2);
												fjBean.setFjindex(2);
												fjBean.setFjname("外机条码图");
												fjBean.setFjserverpath(o.getGreeImageId());
												tblHsptJjcjFjVO.add(fjBean);
											}

											if(null != annexEventMap
													&& annexEventMap.containsKey(o.getId())
													&& null != annexEventMap.get(o.getId())
											) {
												List<OrderRecycleUploadEvent> annexEventGroupList = annexEventMap.get(o.getId());
												eventList.addAll(annexEventGroupList);
											}
										}
									});
								}
							}
						}
						bean.setTblHsptJjcjFjVO(tblHsptJjcjFjVO);
						bean.setTblHsptJjcjTmmxVO(tblHsptJjcjTmmxVO);
						GreeResponseHelper response = acquisitionUploadReq(bean, eventList);
					});
				}
			}
		}
	}

	//采集信息上传接口
	@Transactional
	public GreeResponseHelper acquisitionUploadReq(RecycleAcquisitionUploadBean acquisitionUploadBean,
												   List<OrderRecycleUploadEvent> eventList) {
		if(null != acquisitionUploadBean) {
			final String apiPath = Constant.GreeApiPath.RECYCLE_ACQUISITION_UPLOAD;
			//采集主表
			RecycleHsptJjcjUploadBean jjcjBean = acquisitionUploadBean.getTblHsptJjcjVO();
			//附件表
			List<RecycleHsptJjcjFjUploadBean> fjBeanList = acquisitionUploadBean.getTblHsptJjcjFjVO();
			//条码明细表
			List<RecycleHsptJjcjTmmxUploadBean> tmmxBeanList = acquisitionUploadBean.getTblHsptJjcjTmmxVO();
			Map<String,Object> map = new HashMap<>();
			map.put("tblHsptJjcjVO", jjcjBean);
			map.put("tblHsptJjcjFjVO", fjBeanList);
			map.put("tblHsptJjcjTmmxVO", tmmxBeanList);
			ItfTblRecycleHsptJjcjUpload jjcjUpload = new ItfTblRecycleHsptJjcjUpload();
			List<ItfTblRecycleHsptJjcjFjUpload> fjlist = new ArrayList<>();
			List<ItfTblRecycleHsptJjcjTmmxUpload> tmmxlist = new ArrayList<>();
			try {
				jjcjUpload = mapper.readValue(mapper.writeValueAsString(jjcjBean), ItfTblRecycleHsptJjcjUpload.class);
				jjcjUpload.setSynStatus(Constant.SynStatus.SYN.getCode());
				jjcjUpload.setSynTime(new Date());
				jjcjUpload.setCreatedDate(new Date());
				jjcjUpload.setLastModifiedDate(new Date());
				jjcjUpload.setUploadBatchNo(acquisitionUploadBean.getBatchNo());
				jjcjUpload.setUploadTaskNo(acquisitionUploadBean.getTaskNo());
				if(null != fjBeanList && !fjBeanList.isEmpty()) {
					fjBeanList.stream().forEach(fjBean->{
						ItfTblRecycleHsptJjcjFjUpload fjUpload = new ItfTblRecycleHsptJjcjFjUpload();
						try {
							fjUpload = mapper.readValue(mapper.writeValueAsString(fjBean), ItfTblRecycleHsptJjcjFjUpload.class);
							fjUpload.setSynStatus(Constant.SynStatus.SYN.getCode());
							fjUpload.setSynTime(new Date());
							fjUpload.setCreatedDate(new Date());
							fjUpload.setLastModifiedDate(new Date());
							fjUpload.setUploadBatchNo(acquisitionUploadBean.getBatchNo());
							fjUpload.setUploadTaskNo(acquisitionUploadBean.getTaskNo());
							fjlist.add(fjUpload);
						} catch (JsonProcessingException e) {
							fjUpload.setSynStatus(Constant.SynStatus.FAIL.getCode());
							fjUpload.setSynTime(new Date());
							fjUpload.setCreatedDate(new Date());
							fjUpload.setLastModifiedDate(new Date());
							fjUpload.setUploadBatchNo(acquisitionUploadBean.getBatchNo());
							fjUpload.setUploadTaskNo(acquisitionUploadBean.getTaskNo());
							fjlist.add(fjUpload);
						}
					});
				}
				if(null != tmmxBeanList && !tmmxBeanList.isEmpty()) {
					tmmxBeanList.stream().forEach(tmmxBean->{
						ItfTblRecycleHsptJjcjTmmxUpload tmmxUpload = new ItfTblRecycleHsptJjcjTmmxUpload();
						try {
							tmmxUpload = mapper.readValue(mapper.writeValueAsString(tmmxBean), ItfTblRecycleHsptJjcjTmmxUpload.class);
							tmmxUpload.setSynStatus(Constant.SynStatus.SYN.getCode());
							tmmxUpload.setSynTime(new Date());
							tmmxUpload.setCreatedDate(new Date());
							tmmxUpload.setLastModifiedDate(new Date());
							tmmxUpload.setUploadBatchNo(acquisitionUploadBean.getBatchNo());
							tmmxUpload.setUploadTaskNo(acquisitionUploadBean.getTaskNo());
							tmmxlist.add(tmmxUpload);
						} catch (JsonProcessingException e) {
							tmmxUpload.setSynStatus(Constant.SynStatus.FAIL.getCode());
							tmmxUpload.setSynTime(new Date());
							tmmxUpload.setCreatedDate(new Date());
							tmmxUpload.setLastModifiedDate(new Date());
							tmmxUpload.setUploadBatchNo(acquisitionUploadBean.getBatchNo());
							tmmxUpload.setUploadTaskNo(acquisitionUploadBean.getTaskNo());
							tmmxlist.add(tmmxUpload);
						}
					});
				}
				GreeResponseHelper response = greeLogic.greePost(apiPath, map, 1);
				if( 200 == response.getStatus()) {
					jjcjUpload.setGreeStatSynStatus(Constant.SynStatus.SYN.getCode());
					jjcjUpload.setGreeStatSynTime(new Date());
					if( null != eventList && !eventList.isEmpty() ) {
						eventList.stream().forEach(event->{
							event.setSynStatus(Constant.SynStatus.SYN.getCode());
							event.setSynTime(new Date());
						});
					}
					if(null != fjlist && !fjlist.isEmpty()) {
						fjlist.stream().forEach(fj->{
							fj.setGreeStatSynStatus(Constant.SynStatus.SYN.getCode());
							fj.setGreeStatSynTime(new Date());
						});
					}
					if(null != tmmxlist && !tmmxlist.isEmpty()) {
						tmmxlist.stream().forEach(tmmx->{
							tmmx.setGreeStatSynStatus(Constant.SynStatus.SYN.getCode());
							tmmx.setGreeStatSynTime(new Date());
						});
					}
				}else {
					jjcjUpload.setGreeStatSynStatus(Constant.SynStatus.FAIL.getCode());
					jjcjUpload.setGreeStatSynErrMsg(response.getMsg());
					jjcjUpload.setGreeStatSynTime(new Date());
					if( null != eventList && !eventList.isEmpty() ) {
						eventList.stream().forEach(event->{
							event.setSynStatus(Constant.SynStatus.FAIL.getCode());
							event.setSynTime(new Date());
							event.setSynErrMsg(response.getMsg());
						});
					}
					if(null != fjlist && !fjlist.isEmpty()) {
						fjlist.stream().forEach(fj->{
							fj.setGreeStatSynStatus(Constant.SynStatus.FAIL.getCode());
							fj.setGreeStatSynTime(new Date());
							fj.setSynErrMsg(response.getMsg());
						});
					}
					if(null != tmmxlist && !tmmxlist.isEmpty()) {
						tmmxlist.stream().forEach(tmmx->{
							tmmx.setGreeStatSynStatus(Constant.SynStatus.FAIL.getCode());
							tmmx.setGreeStatSynTime(new Date());
							tmmx.setSynErrMsg(response.getMsg());
						});
					}
				}
				itfTblRecycleHsptJjcjUploadService.saveOrUpdate(jjcjUpload);
				if( null != eventList && !eventList.isEmpty()) {
					orderRecycleUploadEventService.saveOrUpdateBatch(eventList);
				}
				if(null != fjlist && !fjlist.isEmpty()) {
					itfTblRecycleHsptJjcjFjUploadService.saveOrUpdateBatch(fjlist);
				}
				if(null != tmmxlist && !tmmxlist.isEmpty()) {
					itfTblRecycleHsptJjcjTmmxUploadService.saveOrUpdateBatch(tmmxlist);
				}
				return response;
			}catch(Exception e) {
				jjcjUpload.setGreeStatSynStatus(Constant.SynStatus.FAIL.getCode());
				jjcjUpload.setGreeStatSynTime(new Date());
				if( null != eventList && !eventList.isEmpty() ) {
					eventList.stream().forEach(event->{
						event.setSynStatus(Constant.SynStatus.FAIL.getCode());
						event.setSynTime(new Date());
					});
				}
				if(null != fjlist && !fjlist.isEmpty()) {
					fjlist.stream().forEach(fj->{
						fj.setGreeStatSynStatus(Constant.SynStatus.FAIL.getCode());
						fj.setGreeStatSynTime(new Date());
					});
				}
				if(null != tmmxlist && !tmmxlist.isEmpty()) {
					tmmxlist.stream().forEach(tmmx->{
						tmmx.setGreeStatSynStatus(Constant.SynStatus.FAIL.getCode());
						tmmx.setGreeStatSynTime(new Date());
					});
				}
				itfTblRecycleHsptJjcjUploadService.saveOrUpdate(jjcjUpload);
				if( null != eventList && !eventList.isEmpty()) {
					orderRecycleUploadEventService.saveOrUpdateBatch(eventList);
				}
				if(null != fjlist && !fjlist.isEmpty()) {
					itfTblRecycleHsptJjcjFjUploadService.saveOrUpdateBatch(fjlist);
				}
				if(null != tmmxlist && !tmmxlist.isEmpty()) {
					itfTblRecycleHsptJjcjTmmxUploadService.saveOrUpdateBatch(tmmxlist);
				}
			}
		}
		return null;
	}


	/**
	 * 访问采集下载接口，把上传任务保存到条码信息上传任务表
	 * @return
	 */
	/*
	public void stockBarcodeInfoDonwlodToUpload() {
		List<ItfTblRecyleStockInfoUploadTaskList> recyleStockInfoUploadTaskList = recyleStockInfoUploadTaskListService.lambdaQuery()
				.eq(ItfTblRecyleStockInfoUploadTaskList::getSynStatus, RecycleSynStatusEnum.UN_SYN.getCode())
				.orderByAsc(ItfTblRecyleStockInfoUploadTaskList::getSynReqId).list();

		if( Collections.isEmpty(recyleStockInfoUploadTaskList)) {
			return ;
		}
		List<ItfTblRecycleStockBarcodeInfoUpload> recycleStockBarcodeInfoUploadList = new ArrayList<>();
		for(ItfTblRecyleStockInfoUploadTaskList recyleStockInfoUploadTask : recyleStockInfoUploadTaskList) {
			//含有上一条失败，本条不上传
			List<ItfTblRecyleStockInfoUploadTaskList> lastTaskList = recyleStockInfoUploadTaskListService.lambdaQuery().
					in(ItfTblRecyleStockInfoUploadTaskList::getSynStatus,
							new Integer[] { RecycleSynStatusEnum.FAIL.getCode(), RecycleSynStatusEnum.UN_SYN.getCode()}).
					lt(ItfTblRecyleStockInfoUploadTaskList::getSynReqId, recyleStockInfoUploadTask.getSynReqId()).
					eq(ItfTblRecyleStockInfoUploadTaskList::getPgguid, recyleStockInfoUploadTask.getPgguid()).
					orderByDesc(ItfTblRecyleStockInfoUploadTaskList::getSynReqId).list();
			if(!Collections.isEmpty(lastTaskList)) {
				ItfTblRecyleStockInfoUploadTaskList lastTask = lastTaskList.get(0);
				if( RecycleSynStatusEnum.UN_SYN.getCode().intValue() == lastTask.getSynStatus() ) {//若上一条未同步，则本条跳过
					continue;
				}else {//若上一条失败，则本条也不允许上传
					recyleStockInfoUploadTask.setSynStatus(RecycleSynStatusEnum.FAIL.getCode());
					recyleStockInfoUploadTask.setSynErrMsg("上一条数据没上传成功，本条数据不上传, pguuid="+recyleStockInfoUploadTask.getPgguid()+", pguuid="+recyleStockInfoUploadTask.getPgguid());
					recyleStockInfoUploadTask.setSynTime(new Date());
					continue;
				}
			}		
			//先请求采集下载接口
			final String acquisitionDownloadApiPath = Constant.GreeApiPath.RECYCLE_ACQUISITION;
			Map<String,Object> param = new HashMap<>();
			param.put("type","get");
			param.put("pgguid", recyleStockInfoUploadTask.getPgguid());
			try {
				GreeResponseHelper response = greeLogic.greePost(acquisitionDownloadApiPath, param, 1);
				if(200 == response.getStatus()) {
					List<RecycleAcquisitionDownloadBean> responsebeanList = mapper.readValue((String)response.getData()  , new TypeReference<List<RecycleAcquisitionDownloadBean>>() {});
					List<RecycleHsptJjcjDownloadBean> recycleHsptJjcjDownloadList = new ArrayList<>();
					List<RecycleHsptJjcjTmmxDownloadBean> recycleHsptJjcjTmmxDownloadList = new ArrayList<>();
					if( Collections.isEmpty(responsebeanList) ) {
						//请求次数+1
						recyleStockInfoUploadTask.setSynTimes(Objects.isNull(recyleStockInfoUploadTask.getSynTimes()) ? 1 : recyleStockInfoUploadTask.getSynTimes()+1);
						//请求次数达到6次，不再请求
						if(!Objects.isNull(recyleStockInfoUploadTask.getSynTimes() ) && recyleStockInfoUploadTask.getSynTimes() == 6) {
							recyleStockInfoUploadTask.setSynStatus(RecycleSynStatusEnum.SYN.getCode());
							recyleStockInfoUploadTask.setSynErrMsg("没有采集数据, pgguid="+recyleStockInfoUploadTask.getPgguid());
						}
						continue;
					}
					
					//根据pgguid获取回收工单主表数据
					OrderRecycleAssign orderRecycleAssign =  orderRecycleAssignService.lambdaQuery().eq(OrderRecycleAssign::getPgguid, recyleStockInfoUploadTask.getPgguid()).one();
					if(Objects.isNull(orderRecycleAssign)) {
						recyleStockInfoUploadTask.setSynTimes(Objects.isNull(recyleStockInfoUploadTask.getSynTimes()) ? 1 : recyleStockInfoUploadTask.getSynTimes()+1);
						//请求次数达到6次，不再请求
						if(!Objects.isNull(recyleStockInfoUploadTask.getSynTimes() ) && recyleStockInfoUploadTask.getSynTimes() == 6) {
							recyleStockInfoUploadTask.setSynStatus(RecycleSynStatusEnum.SYN.getCode());
							recyleStockInfoUploadTask.setSynErrMsg("没有回收工单数据, pgguid="+recyleStockInfoUploadTask.getPgguid());
						}
						continue;
					}
					
					for(RecycleAcquisitionDownloadBean recycleAcquisitionDownloadBean : responsebeanList) {
						recycleHsptJjcjDownloadList.add(recycleAcquisitionDownloadBean.getJjcj());
						recycleHsptJjcjTmmxDownloadList.addAll(recycleAcquisitionDownloadBean.getTmmxlist());
					}
					//时间倒序，拿第一条
					recycleHsptJjcjDownloadList.stream().sorted(Comparator.comparing(RecycleHsptJjcjDownloadBean::getLastModifiedDate, Comparator.reverseOrder()));
                    RecycleHsptJjcjDownloadBean jjcjDownload = recycleHsptJjcjDownloadList.get(0);
					final Map<String, List<RecycleHsptJjcjTmmxDownloadBean>> recycleHsptJjcjTmmxDownloadMap = Collections.isEmpty(recycleHsptJjcjTmmxDownloadList) ? null : recycleHsptJjcjTmmxDownloadList.stream().collect(Collectors.groupingBy(RecycleHsptJjcjTmmxDownloadBean::getPgwcmxid));
					final List<RecycleHsptJjcjTmmxDownloadBean> tmmxDownloadList = (Objects.isNull(recycleHsptJjcjTmmxDownloadMap) 
							|| !recycleHsptJjcjTmmxDownloadMap.containsKey(jjcjDownload.getPgwcmxid()) || Collections.isEmpty(recycleHsptJjcjTmmxDownloadMap)) ? 
									null :  recycleHsptJjcjTmmxDownloadMap.get(jjcjDownload.getPgwcmxid());
					if( Collections.isEmpty(tmmxDownloadList) ) {
						recyleStockInfoUploadTask.setSynStatus(GreeSynStatusEnum.SYN.getCode());
						recyleStockInfoUploadTask.setSynErrMsg("没有条码数据, pgguid="+recyleStockInfoUploadTask.getPgguid());
						continue;
					}
					
					String taskNo = java.util.UUID.randomUUID().toString();
					for( RecycleHsptJjcjTmmxDownloadBean tmmxDownload : tmmxDownloadList ) {
						ItfTblRecycleStockBarcodeInfoUpload recycleStockBarcodeInfoUpload = new ItfTblRecycleStockBarcodeInfoUpload();
						recycleStockBarcodeInfoUpload.setPgguid(jjcjDownload.getPgguid());
						recycleStockBarcodeInfoUpload.setUploadTaskNo(taskNo);
						recycleStockBarcodeInfoUpload.setJqtm(tmmxDownload.getJqtm());
						recycleStockBarcodeInfoUpload.setTmlx(tmmxDownload.getTmlx());
						recycleStockBarcodeInfoUpload.setSpid( Objects.isNull(jjcjDownload.getSpid()) ? null : Integer.parseInt(jjcjDownload.getSpid().toString()) );
						recycleStockBarcodeInfoUpload.setSpmc(jjcjDownload.getSpmc());
						recycleStockBarcodeInfoUpload.setPinp(jjcjDownload.getJjpp());
						recycleStockBarcodeInfoUpload.setJqlx(jjcjDownload.getJjlx());
						//匹数要传数字型，1点5匹，就填1.50 ，用数字填
						String pshuStr = jjcjDownload.getPshu();
						Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]*");
						Matcher matcher = pattern.matcher(pshuStr); 
						if(matcher.find()) {
							recycleStockBarcodeInfoUpload.setPshu( Float.parseFloat(matcher.group()) );
						}
						recycleStockBarcodeInfoUpload.setWdno(orderRecycleAssign.getWxwdno());
						recycleStockBarcodeInfoUpload.setDqcfid(orderRecycleAssign.getWxwdno());
						recycleStockBarcodeInfoUpload.setDqcfmc(orderRecycleAssign.getWxwdmc());
						recycleStockBarcodeInfoUpload.setYddh(orderRecycleAssign.getYddh());
						//工单类型:1:以旧换新;2:回收工单
						recycleStockBarcodeInfoUpload.setSrclx(2);
						//回收时间（师傅报完工时间）
						recycleStockBarcodeInfoUpload.setHssj(orderRecycleAssign.getFinishTime());
						recycleStockBarcodeInfoUpload.setHsfwwdno(orderRecycleAssign.getWxwdno());
						recycleStockBarcodeInfoUpload.setHsfwwdmc(orderRecycleAssign.getWxwdmc());
						recycleStockBarcodeInfoUpload.setSrcno(orderRecycleAssign.getSrcOrderNo());
						recycleStockBarcodeInfoUpload.setSrcId(orderRecycleAssign.getPgguid());
						recycleStockBarcodeInfoUpload.setPgwcmxid(jjcjDownload.getPgwcmxid());
						recycleStockBarcodeInfoUpload.setState(0);
						recycleStockBarcodeInfoUpload.setCreatedBy(jjcjDownload.getCreatedBy());
						recycleStockBarcodeInfoUpload.setCreatedDate(jjcjDownload.getCreatedDate());
						recycleStockBarcodeInfoUpload.setLastModifiedBy(jjcjDownload.getLastModifiedBy());
						recycleStockBarcodeInfoUpload.setLastModifiedDate(jjcjDownload.getLastModifiedDate());
						recycleStockBarcodeInfoUpload.setSrcxsdh(orderRecycleAssign.getSrcOrderNo());
						recycleStockBarcodeInfoUploadList.add(recycleStockBarcodeInfoUpload);
					}
					recyleStockInfoUploadTask.setSynStatus(RecycleSynStatusEnum.SYN.getCode());
					recyleStockInfoUploadTask.setSynTime(new Date());
				}else {
					//请求次数+1
					recyleStockInfoUploadTask.setSynTimes(Objects.isNull(recyleStockInfoUploadTask.getSynTimes()) ? 1 : recyleStockInfoUploadTask.getSynTimes()+1);
					//请求次数达到6次，不再请求
					if(!Objects.isNull(recyleStockInfoUploadTask.getSynTimes() ) && recyleStockInfoUploadTask.getSynTimes() == 6) {
						recyleStockInfoUploadTask.setSynStatus(GreeSynStatusEnum.FAIL.getCode());
						recyleStockInfoUploadTask.setSynErrMsg(response.getMsg());
						recyleStockInfoUploadTask.setSynTime(new Date());
					}
				}
			} catch (Exception e) {
				//请求次数+1
				recyleStockInfoUploadTask.setSynTimes(Objects.isNull(recyleStockInfoUploadTask.getSynTimes()) ? 1 : recyleStockInfoUploadTask.getSynTimes()+1);
				if(!Objects.isNull(recyleStockInfoUploadTask.getSynTimes() ) && recyleStockInfoUploadTask.getSynTimes() == 6) {
					recyleStockInfoUploadTask.setSynStatus(GreeSynStatusEnum.FAIL.getCode());
					recyleStockInfoUploadTask.setSynErrMsg(e.getMessage().length() > 100 ? e.getMessage().substring(0, 99) : e.getMessage());
					recyleStockInfoUploadTask.setSynTime(new Date());
				}
			}
		}
		
		recyleStockInfoUploadTaskListService.saveOrUpdateBatch(recyleStockInfoUploadTaskList);
		if(!Collections.isEmpty(recycleStockBarcodeInfoUploadList)) {
			recycleStockBarcodeInfoUploadService.saveOrUpdateBatch(recycleStockBarcodeInfoUploadList);
		}
	}
	*/
	/**
	 * 上传总部条码（需要上传了barcodeInfo主表数据，上传的stockin和stockout才有有效）
	 */
	public void stockBarcodeInfoUpload() {
		final List<ItfTblRecycleStockBarcodeInfoUpload> recycleStockBarcodeInfoUploadList = recycleStockBarcodeInfoUploadService.lambdaQuery()
				.eq(ItfTblRecycleStockBarcodeInfoUpload::getSynStatus, RecycleSynStatusEnum.UN_SYN.getCode()).list();
		if(Collections.isEmpty(recycleStockBarcodeInfoUploadList)) {
			return;
		}
		//按uploadTaskNo分组
		List<ItfTblRecyleStockInfoUploadTaskList> stockInfoUploadTaskList = new ArrayList<>();
		final Map<String, List<ItfTblRecycleStockBarcodeInfoUpload>> recycleStockBarcodeInfoUploadMap = Collections.isEmpty(recycleStockBarcodeInfoUploadList) ? null :recycleStockBarcodeInfoUploadList.stream().collect(Collectors.groupingBy(ItfTblRecycleStockBarcodeInfoUpload::getUploadTaskNo));
		for(Map.Entry<String,List<ItfTblRecycleStockBarcodeInfoUpload>> entry : recycleStockBarcodeInfoUploadMap.entrySet()){
			List<ItfTblRecycleStockBarcodeInfoUpload> barcodeInfoList = entry.getValue();
			String pgguid = barcodeInfoList.get(0).getPgguid();
			final String apiPath = Constant.GreeApiPath.RECYCLE_STOCK_STOCKINFOS;
			Integer synStatus = -1;
			String errMsg = "";
			List<RecycleStockBarcodeInfoUploadBean> barcodeInfoBeanList = new ArrayList<>();
			List<Map> infos = new ArrayList<>() ;
			try {
				barcodeInfoBeanList = this.mapper.readValue(this.mapper.writeValueAsString(barcodeInfoList), new TypeReference<List<RecycleStockBarcodeInfoUploadBean>>() {});
				infos = this.mapper.readValue(this.mapper.writeValueAsString(barcodeInfoBeanList), new TypeReference<List<Map>>() {});
			} catch (JsonMappingException e1) {
				synStatus = RecycleSynStatusEnum.FAIL.getCode();
				for(ItfTblRecycleStockBarcodeInfoUpload recycleStockBarcodeInfoUpload : recycleStockBarcodeInfoUploadList) {
					recycleStockBarcodeInfoUpload.setSynStatus(GreeSynStatusEnum.FAIL.getCode());
					recycleStockBarcodeInfoUpload.setSynErrMsg(e1.getMessage().length() > 100 ? e1.getMessage().substring(0, 99) : e1.getMessage());
					errMsg = recycleStockBarcodeInfoUpload.getSynErrMsg();
					recycleStockBarcodeInfoUpload.setSynTime(new Date());
					recycleStockBarcodeInfoUpload.setSynTimes( Objects.isNull(recycleStockBarcodeInfoUpload.getSynTimes()) ? 1 : recycleStockBarcodeInfoUpload.getSynTimes() + 1 );
				}
				continue;
			} catch (JsonProcessingException e1) {
				synStatus = RecycleSynStatusEnum.FAIL.getCode();
				for(ItfTblRecycleStockBarcodeInfoUpload recycleStockBarcodeInfoUpload : recycleStockBarcodeInfoUploadList) {
					recycleStockBarcodeInfoUpload.setSynStatus(GreeSynStatusEnum.FAIL.getCode());
					recycleStockBarcodeInfoUpload.setSynErrMsg(e1.getMessage().length() > 100 ? e1.getMessage().substring(0, 99) : e1.getMessage());
					errMsg = recycleStockBarcodeInfoUpload.getSynErrMsg();
					recycleStockBarcodeInfoUpload.setSynTime(new Date());
					recycleStockBarcodeInfoUpload.setSynTimes( Objects.isNull(recycleStockBarcodeInfoUpload.getSynTimes()) ? 1 : recycleStockBarcodeInfoUpload.getSynTimes() + 1 );
				}
				continue;
			}
			Map<String, Object> param = new HashMap<>();
			param.put("taskNo", entry.getKey());
			param.put("infos", infos);
			try {
				GreeResponseHelper response = greeLogic.greePost(apiPath, param, 1);
				if(200 == response.getStatus()) {
					synStatus = RecycleSynStatusEnum.SYN.getCode();
					for(ItfTblRecycleStockBarcodeInfoUpload recycleStockBarcodeInfoUpload : recycleStockBarcodeInfoUploadList) {
						recycleStockBarcodeInfoUpload.setSynStatus(GreeSynStatusEnum.SYN.getCode());
						recycleStockBarcodeInfoUpload.setSynTime(new Date());
						recycleStockBarcodeInfoUpload.setSynTimes( Objects.isNull(recycleStockBarcodeInfoUpload.getSynTimes()) ? 1 : recycleStockBarcodeInfoUpload.getSynTimes() + 1 );
					}
				}else {
					synStatus = RecycleSynStatusEnum.FAIL.getCode();
					for(ItfTblRecycleStockBarcodeInfoUpload recycleStockBarcodeInfoUpload : recycleStockBarcodeInfoUploadList) {
						recycleStockBarcodeInfoUpload.setSynStatus(GreeSynStatusEnum.FAIL.getCode());
						recycleStockBarcodeInfoUpload.setSynErrMsg(response.getMsg());
						errMsg = recycleStockBarcodeInfoUpload.getSynErrMsg();
						recycleStockBarcodeInfoUpload.setSynTime(new Date());
						recycleStockBarcodeInfoUpload.setSynTimes( Objects.isNull(recycleStockBarcodeInfoUpload.getSynTimes()) ? 1 : recycleStockBarcodeInfoUpload.getSynTimes() + 1 );
						pgguid = recycleStockBarcodeInfoUpload.getPgguid();
					}
				}
			}catch(Exception e) {
				e.printStackTrace();
				synStatus = RecycleSynStatusEnum.FAIL.getCode();
				for(ItfTblRecycleStockBarcodeInfoUpload recycleStockBarcodeInfoUpload : recycleStockBarcodeInfoUploadList) {
					recycleStockBarcodeInfoUpload.setSynStatus(GreeSynStatusEnum.FAIL.getCode());
					recycleStockBarcodeInfoUpload.setSynErrMsg(e.getMessage().length() > 100 ? e.getMessage().substring(0, 99) : e.getMessage());
					errMsg = recycleStockBarcodeInfoUpload.getSynErrMsg();
					recycleStockBarcodeInfoUpload.setSynTime(new Date());
					recycleStockBarcodeInfoUpload.setSynTimes( Objects.isNull(recycleStockBarcodeInfoUpload.getSynTimes()) ? 1 : recycleStockBarcodeInfoUpload.getSynTimes() + 1 );
				}
			}
			//上传条码信息任务列表
			List<ItfTblRecyleStockInfoUploadTaskList> recyleStockInfoUploadTaskList = recyleStockInfoUploadTaskListService.lambdaQuery()
					.eq(ItfTblRecyleStockInfoUploadTaskList::getPgguid, pgguid)
					.eq(ItfTblRecyleStockInfoUploadTaskList::getSynStatus, RecycleSynStatusEnum.UN_SYN.getCode())
					.orderByAsc(ItfTblRecyleStockInfoUploadTaskList::getSynReqId).list();
			if( Collections.isEmpty(recyleStockInfoUploadTaskList) ) {
				continue;
			}
			//按synReqId顺序更新
			ItfTblRecyleStockInfoUploadTaskList recyleStockInfoUploadTask = recyleStockInfoUploadTaskList.get(0);
			if( synStatus == RecycleSynStatusEnum.SYN.getCode() ) {
				recyleStockInfoUploadTask.setSynStatus(synStatus);
				recyleStockInfoUploadTask.setSynTime(new Date());
			}else {
				if( (Objects.isNull(recyleStockInfoUploadTask.getSynTimes()) ? 0 : recyleStockInfoUploadTask.getSynTimes()) >= 6 ) {
					recyleStockInfoUploadTask.setSynStatus(synStatus);
					recyleStockInfoUploadTask.setSynErrMsg(errMsg);
					recyleStockInfoUploadTask.setSynTime(new Date());
				}else {
					//请求失败，累加请求次数
					recyleStockInfoUploadTask.setSynTimes((Objects.isNull(recyleStockInfoUploadTask.getSynTimes()) ? 0 : recyleStockInfoUploadTask.getSynTimes()) + 1);
				}
			}
			stockInfoUploadTaskList.add(recyleStockInfoUploadTask);
		}
		if(!Collections.isEmpty(stockInfoUploadTaskList)) {
			recyleStockInfoUploadTaskListService.saveOrUpdateBatch(stockInfoUploadTaskList);
		}
		if(!Collections.isEmpty(recycleStockBarcodeInfoUploadList)) {
			recycleStockBarcodeInfoUploadService.saveOrUpdateBatch(recycleStockBarcodeInfoUploadList);
		}
	}
}

