package com.prolog.cs.biz.outbound.service.impl.tms;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.prolog.cs.biz.outbound.convert.TmsConvert;
import com.prolog.cs.biz.outbound.dao.BarcodeHMapper;
import com.prolog.cs.biz.outbound.model.dto.BillPackageDTO;
import com.prolog.cs.biz.outbound.model.tms.TmsShipBillPackage;
import com.prolog.cs.biz.outbound.model.tms.TmsShipBillPackageItem;
import com.prolog.cs.biz.outbound.service.TmsService;
import com.prolog.cs.biz.outbound.service.impl.platform.InterfacePlatform;
import com.prolog.cs.biz.outbound.utils.TransactionHelper;
import com.prolog.framework.common.message.RestMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.prolog.cs.biz.outbound.model.tms.TmsConstant.SHIP_BILL_PACKAGE;

@Slf4j
@Service
public class TmsServiceImpl implements TmsService {

	@Resource
	private BarcodeHMapper barcodeHMapper;

	@Resource
	private TmsConvert convert;

	@Autowired
	private InterfacePlatform ip;

	@Override
	public void pushPackage() {
		List<BillPackageDTO> list = barcodeHMapper.getPushPackageList();
		if (CollUtil.isEmpty(list)) {
			return;
		}
		pushPackageAndUpdateStatus(list);
	}

	public void pushPackageAndUpdateStatus(List<BillPackageDTO> list) {
		Map<String, List<BillPackageDTO>> map = list.stream().collect(Collectors.groupingBy(BillPackageDTO::getBillNo));
		for (Map.Entry<String, List<BillPackageDTO>> entry : map.entrySet()) {
			boolean match = entry.getValue().stream().anyMatch(bp -> !"5".equals(bp.getCheckStatus()) || bp.getWeight() == null);
			if (match) {
				continue;
			}
			TmsShipBillPackage dto = warpTmsParams(entry);

			try {
				RestMessage result = ip.call(SHIP_BILL_PACKAGE, JSONObject.toJSONString(dto));
				if (!result.isSuccess()) {
					barcodeHMapper.updaPushCount(entry.getKey());
					continue;
				}
			} catch (Exception e) {
				log.error("", e);
				continue;
			}

			TransactionHelper.persistenceErrorThrow(() -> {
				barcodeHMapper.updateCheckStatus2EndByBillNo(entry.getKey());
			});
		}
	}

	private TmsShipBillPackage warpTmsParams(Map.Entry<String, List<BillPackageDTO>> entry) {
		List<BillPackageDTO> values = entry.getValue();
		TmsShipBillPackage dto = convert.parse2TmsShipBillPackage(values.get(0));
		List<TmsShipBillPackageItem> packageItems = warpTmsShipBillPackageItems(values);
		dto.setHeaders(packageItems);
		return dto;
	}

	private List<TmsShipBillPackageItem> warpTmsShipBillPackageItems(List<BillPackageDTO> list) {
		int size = list.size();
		List<TmsShipBillPackageItem> packageItems = new ArrayList<>(size);

		int i = 1;
		for (BillPackageDTO dto : list) {
			TmsShipBillPackageItem packageItem = convert.parse2TmsShipBillPackageItem(dto, i);
			packageItem.setLastPackage("0");
			if (size == i) {
				packageItem.setLastPackage("1");
			}
			packageItems.add(packageItem);
			i++;
		}

		return packageItems;
	}
}
