package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.print.method;

import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.ISxPrintTemplateMethodService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkOrderExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.PackageExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ProduceSnExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.SxPrintTemplateModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

/**
 * @Description 包装条码打印数据
 * @Author: wangjie
 * @CreateDate: 2020/6/22 9:38 AM
 * @Description:
 **/
@Slf4j
@Service
public class SxPrintPackageAndOrderStartTimeService extends SxPrintDispatchService implements ISxPrintTemplateMethodService {

    @Autowired
    private ISxWorkOrderExtService sxWorkOrderExtService;

    @Override
    public List<Map<String, Object>> exec(SxPrintTemplateModel printModel) {
        List<Map<String, Object>> resultMapList = null;
        List<PackageExtModel> packList = printModel.getPackList();

        List<String> workOrderNoList = packList.stream().filter(o -> null != o).map(PackageExtModel::getWorkOrderNo).collect(Collectors.toList());
        Map<String, String> startTimeMap = new HashMap<>();
        for (String workOrderNo : workOrderNoList) {
            if (StringUtils.isEmpty(workOrderNo)) {
                continue;
            }
            MesWorkOrderExt workOrderExt = sxWorkOrderExtService.getWorkOrderExtByOrderNo(printModel.getOrganizeCode(), workOrderNo);
            if (null == workOrderExt) {
                continue;
            }
            String[] dateArr = workOrderExt.getStartTime().substring(2, 7).split("-");
            startTimeMap.put(workOrderNo, new StringJoiner(".").add(dateArr[1]).add(dateArr[0]).toString());
        }

        for (PackageExtModel pack : packList) {
            if (null == pack) {
                continue;
            }
            if (CollectionUtils.isEmpty(resultMapList)) {
                resultMapList = new ArrayList<>();
            }
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put(MesPcnExtConstWords.PART_NO, pack.getPartNo());
            resultMap.put(MesPcnExtConstWords.BAR_CODE_2, pack.getPartNo());
            resultMap.put(MesPcnExtConstWords.PACKAGENO, getSnWithOutPercent(pack.getPackageNo()));
            resultMap.put(MesPcnExtConstWords.BAR_CODE, pack.getPackageNo());
            if (null != printModel.getPart()) {
                resultMap.put(MesPcnExtConstWords.GOODS_CODE, printModel.getPart().getGoodsCode());
                resultMap.put(MesPcnExtConstWords.BAR_CODE_3, printModel.getPart().getGoodsCode());
            }
            if (!StringUtils.isEmpty(pack.getPrinterCode())) {
                resultMap.put(MesPcnExtConstWords.PRINTER_CODE, pack.getPrinterCode());
            }
            resultMap.put(MesPcnExtConstWords.QR_CODE, new StringBuffer().append("1P").append(pack.getPartNo()).append(";S").append(getSnWithOutPercent(pack.getPackageNo())).append(";").toString());
            resultMap.put(MesPcnExtConstWords.MONTH_YEAR, startTimeMap.get(pack.getWorkOrderNo()));
            resultMapList.add(resultMap);
        }
        return resultMapList;
    }

}
