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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.ISxPrintTemplateMethodService;
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 cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesCustomerPart;
import cn.estsh.i3plus.pojo.mes.repository.MesCustomerPartRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
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 SxPrintHasGoodsCodeAndReplaceCustPartNoService extends SxPrintDispatchService implements ISxPrintTemplateMethodService {

    @Autowired
    private MesCustomerPartRepository customerPartRepository;

    private static final String WP = "WP";
    private static final String WT = "WT";
    private static final String BN8 = " BN8";
    private static final String BW1 = " BW1";

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

        String customerPartNo = getCustomerPartNo(printModel.getOrganizeCode(), printModel.getPart().getPartNo());

        for (ProduceSnExtModel sn : snList) {
            if (null == sn) {
                continue;
            }
            if (CollectionUtils.isEmpty(resultMapList)) {
                resultMapList = new ArrayList<>();
            }
            Map<String, Object> resultMap = new HashMap<>();
            if (null != printModel.getPart()) {
                resultMap.put(MesPcnExtConstWords.PART_NO, getPartNo(customerPartNo));
                resultMap.put(MesPcnExtConstWords.GOODS_CODE, printModel.getPart().getGoodsCode());
                resultMap.put(MesPcnExtConstWords.BAR_CODE_2, printModel.getPart().getGoodsCode());
                resultMap.put(MesPcnExtConstWords.PREFIX, printModel.getPart().getPrefix());
            }
            resultMap.put(MesPcnExtConstWords.SERIAL_NUMBER, getSnWithOutPercent(sn.getSerialNumber()));
            resultMap.put(MesPcnExtConstWords.BAR_CODE, sn.getSerialNumber());
            resultMapList.add(resultMap);
        }
        return resultMapList;
    }

    private String getCustomerPartNo(String organizeCode, String partNo) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(partNo, MesPcnExtConstWords.PART_NO, packBean);
        List<MesCustomerPart> customerPartList = customerPartRepository.findByHqlWhere(packBean);
        customerPartList = CollectionUtils.isEmpty(customerPartList) ? null :
                customerPartList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getCustomerPartNo())))
                        .sorted(Comparator.comparing(MesCustomerPart::getModifyDatetime).reversed()).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(customerPartList)) {
            return null;
        }
        return customerPartList.get(0).getCustomerPartNo();
    }

    private String getPartNo(String partNo) {
        if (StringUtils.isEmpty(partNo) || partNo.length() < 2) {
            return partNo;
        }
        String lastChar = partNo.substring(partNo.length() - 2).toUpperCase();
        String beforeChar = partNo.substring(0, partNo.length() - 2);
        if (lastChar.equals(WP)) {
            partNo = beforeChar + BN8;
        } else if (lastChar.equals(WT)) {
            partNo = beforeChar + BW1;
        }
        return partNo;
    }

}
