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

/**
 * @Description 含有日文的条码打印数据
 * @Author: wangjie
 * @CreateDate: 2020/6/22 9:38 AM
 * @Description:
 **/
@Slf4j
@Service
public class SxPrintHasJapanCodeService extends SxPrintDispatchService implements ISxPrintTemplateMethodService {

    @Autowired
    private ISxWorkOrderExtService sxWorkOrderExtService;

    @Override
    public List<Map<String, Object>> exec(SxPrintTemplateModel printModel) {
        List<Map<String, Object>> resultMapList = null;
        List<ProduceSnExtModel> snList = printModel.getSnList();

        List<String> workOrderNoList = snList.stream().filter(o -> null != o).map(ProduceSnExtModel::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).replace("-", ""));
        }

        for (ProduceSnExtModel sn : snList) {
            if (null == sn) {
                continue;
            }
            if (CollectionUtils.isEmpty(resultMapList)) {
                resultMapList = new ArrayList<>();
            }
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put(MesPcnExtConstWords.SERIAL_NUMBER, getSnWithOutPercent(sn.getSerialNumber()));
            resultMap.put(MesPcnExtConstWords.QR_CODE, sn.getSerialNumber());
            resultMap.put(MesPcnExtConstWords.SHORT_DATE, startTimeMap.get(sn.getWorkOrderNo()));
            if (null != printModel.getPart()) {
                resultMap.put(MesPcnExtConstWords.JAPAN_DESC, printModel.getPart().getJapanDesc());
                resultMap.put(MesPcnExtConstWords.IDENTIFY_CODE, printModel.getPart().getIdentifyCode());
            }
            resultMap.put(MesPcnExtConstWords.SHORT_SN_5, sn.getSerialNumber().substring(sn.getSerialNumber().length() - 5));
            resultMap.put(MesPcnExtConstWords.SHORT_SN_8, sn.getSerialNumber().substring(sn.getSerialNumber().length() - 8));
            resultMapList.add(resultMap);
        }
        return resultMapList;
    }

}
