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.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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 净水器本体条码打印数据
 * @Author: mingliang.li
 * @CreateDate: 2020/6/22 9:38 AM
 * @Description:
 **/
@Slf4j
@Service
public class SxPrintSmallPackageCodeService 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> snList = printModel.getPackList();

        List<String> workOrderNoList = snList.stream().filter(o -> null != o).map(PackageExtModel::getWorkOrderNo).collect(Collectors.toList());
        Map<String, String> startTimeMap = new HashMap<>();
        Map<String, String> startTimeDateMap = new HashMap<>();
        Map<String, String> startTimeDateSlashFormMap = 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, 10).split("-");
            String[] timeDateArr = workOrderExt.getStartTime().substring(2, 10).split("-");

            startTimeMap.put(workOrderNo, new StringBuilder().append(dateArr[0]).append(dateArr[1]).append(dateArr[2]).toString());
            startTimeDateMap.put(workOrderNo, new StringBuilder().append(timeDateArr[0]).append(".").append(dateArr[1]).append(".").append(dateArr[2]).toString());
            startTimeDateSlashFormMap.put(workOrderNo, new StringBuilder().append(timeDateArr[2]).append("/").append(dateArr[1]).append("/").append(dateArr[0]).toString());
        }

        for (PackageExtModel sn : snList) {
            if (null == sn) {
                continue;
            }
            if (CollectionUtils.isEmpty(resultMapList)) {
                resultMapList = new ArrayList<>();
            }
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put(MesPcnExtConstWords.SHI_XIANG_DI_CODE, printModel.getPart().getShiXiangDiCode());
            resultMap.put(MesPcnExtConstWords.COLOR_CODE, printModel.getPart().getColorCode());
            resultMap.put(MesPcnExtConstWords.MODEL, printModel.getPart().getModel());
            resultMap.put(MesPcnExtConstWords.PACKAGENO, getSnWithOutPercent(sn.getPackageNo()));
            resultMap.put(MesPcnExtConstWords.BAR_CODE, sn.getPackageNo());
            resultMap.put(MesPcnExtConstWords.ORDER_START_TIME, startTimeMap.get(sn.getWorkOrderNo()));
            resultMap.put(MesPcnExtConstWords.ORDER_START_TIME_DATE, startTimeDateMap.get(sn.getWorkOrderNo()));
            resultMap.put(MesPcnExtConstWords.ORDER_START_TIME_DATE_2, startTimeDateSlashFormMap.get(sn.getWorkOrderNo()));
            resultMap.put(MesPcnExtConstWords.PRODUCT_NUMBER, printModel.getPart().getProductNumber());
            resultMap.put(MesPcnExtConstWords.SHI_XIANG_DI_CODE_2, "-" + printModel.getPart().getShiXiangDiCode());
            resultMap.put(MesPcnExtConstWords.GOODS_CODE, printModel.getPart().getPrefix() + printModel.getPart().getGoodsCode());
            resultMap.put(MesPcnExtConstWords.GOODS_BAR_CODE, printModel.getPart().getGoodsCode());
            resultMapList.add(resultMap);
        }
        return resultMapList;
    }

}
