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.bean.MesPrintPartParameterExt;
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.repository.MesPrintPartParameterExtRepository;
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 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.*;

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

    @Autowired
    private MesPrintPartParameterExtRepository printPartParameterExtRepository;

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

        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.PACKAGENO, getSnWithOutPercent(pack.getPackageNo()));
            resultMap.put(MesPcnExtConstWords.BAR_CODE, pack.getPackageNo());
            String netWeight = null;
            String roughWeight = null;
            String packageSize = null;
            String colorName = null;
            log.info("模板携带的物料：[%s]",printModel.getPart());
            log.info("模板携带的工厂代码：[%s]",printModel.getOrganizeCode());
            if (null != printModel.getPart()) {
                resultMap.put(MesPcnExtConstWords.GOODS_CODE, printModel.getPart().getGoodsCode());
                resultMap.put(MesPcnExtConstWords.BAR_CODE_3, printModel.getPart().getGoodsCode());
                DdlPackBean packBean = DdlPackBean.getDdlPackBean(printModel.getOrganizeCode());
                DdlPreparedPack.getStringEqualPack(printModel.getPart().getPartNo(), MesPcnExtConstWords.PART_NO, packBean);
                MesPrintPartParameterExt printPartParameterExt = printPartParameterExtRepository.getByProperty(packBean);
                log.info("该物料关联的产品信息：[%s]",printPartParameterExt);
                if(printPartParameterExt != null){
                    netWeight = printPartParameterExt.getNetWeight();
                    roughWeight = printPartParameterExt.getRoughWeight();
                    packageSize = printPartParameterExt.getPackageSize();
                    colorName = printModel.getPart().getColorName();
                }
            }
            if (!StringUtils.isEmpty(pack.getPrinterCode())) {
                resultMap.put(MesPcnExtConstWords.PRINTER_CODE, pack.getPrinterCode());
            }

            resultMap.put(MesPcnExtConstWords.NET_WEIGHT, netWeight);
            resultMap.put(MesPcnExtConstWords.ROUGH_WEIGHT, roughWeight);
            resultMap.put(MesPcnExtConstWords.PACKAGE_SIZE, packageSize);
            resultMap.put(MesPcnExtConstWords.COLOR_NAME, colorName);
            resultMapList.add(resultMap);
        }
        return resultMapList;
    }

}
