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.mes.pcn.api.iservice.base.IConfigService;
import cn.estsh.i3plus.pojo.mes.bean.MesConfig;
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 SxPrintHasGoodsCodeAndWebService extends SxPrintDispatchService implements ISxPrintTemplateMethodService {

    @Autowired
    private IConfigService configService;

    private static final String h = "h=";
    private static final String s = "s=";

    @Override
    public List<Map<String, Object>> exec(SxPrintTemplateModel printModel) {
        List<Map<String, Object>> resultMapList = null;
        List<ProduceSnExtModel> snList = printModel.getSnList();
        MesConfig config = configService.getMesConfigByCfgCode(MesPcnExtConstWords.PART_URL, printModel.getOrganizeCode());
        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, printModel.getPart().getPartNo());
                resultMap.put(MesPcnExtConstWords.GOODS_CODE, printModel.getPart().getGoodsCode());
                resultMap.put(MesPcnExtConstWords.BAR_CODE_2, printModel.getPart().getGoodsCode());
                resultMap.put(MesPcnExtConstWords.PREFIX, printModel.getPart().getPrefix());
                if (null != config && !StringUtils.isEmpty(config.getCfgValue())) {
                    StringJoiner sj = new StringJoiner(MesPcnExtConstWords.AND);
                    sj.add(config.getCfgValue())
                            .add(new StringBuilder().append(h).append(printModel.getPart().getPartNo()).toString())
                            .add(new StringBuilder().append(s).append(getSnWithOutPercent(sn.getSerialNumber())).toString());
                    resultMap.put(MesPcnExtConstWords.QR_CODE, sj.toString());
                }
            }
            resultMap.put(MesPcnExtConstWords.SERIAL_NUMBER, getSnWithOutPercent(sn.getSerialNumber()));
            resultMap.put(MesPcnExtConstWords.BAR_CODE, sn.getSerialNumber());
            resultMapList.add(resultMap);
        }
        return resultMapList;
    }

}
