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.*;
import java.util.stream.Collectors;

/**
 * @Description 嘉兴现品票打印数据（工厂内部使用）
 * @Author: xinwang.yi
 * @CreateDate: 2023/11/29 9:38 AM
 * @Description:
 **/
@Slf4j
@Service
public class JxPrintPackageAndSpotTicketService 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;
            }
            startTimeMap.put(workOrderNo, workOrderExt.getStartTime().substring(0, 10).replaceAll("-", "/"));
        }

        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.PART_NAME, pack.getPartNameRdd());

            resultMap.put(MesPcnExtConstWords.PACKAGENO, pack.getPackageNo());

            if (!StringUtils.isEmpty(pack.getPrinterCode())) {
                resultMap.put(MesPcnExtConstWords.PRINTER_CODE, pack.getPrinterCode());
            }
            // 一维码
            resultMap.put(MesPcnExtConstWords.BAR_CODE, pack.getPackageNo());
            // 二维码
            resultMap.put(MesPcnExtConstWords.QR_CODE, pack.getPackageNo());

            // 生产日期
            resultMap.put(MesPcnExtConstWords.YEAR_MONTH_DAY, startTimeMap.get(pack.getLastWorkOrderNo()));
            resultMap.put(MesPcnExtConstWords.QTY, pack.getQty());

            resultMapList.add(resultMap);
        }
        return resultMapList;
    }

}
