package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.offlineprint;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.offlineprint.IMesOfflinePrintService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.BarCodeUtils;
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.MesLabelTemplate;
import cn.estsh.i3plus.pojo.mes.bean.MesLabelTemplateParam;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrder;
import cn.estsh.i3plus.pojo.mes.repository.MesLabelTemplateParamRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesLabelTemplateRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesWorkOrderRepository;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.*;

@Service
@Slf4j
public class MesOfflinePrintServiceImpl implements IMesOfflinePrintService {
    @Autowired
    private MesWorkOrderRepository workOrderRepository;

    @Autowired
    private MesLabelTemplateRepository labelTemplateRepository;
    
    @Autowired
    private MesLabelTemplateParamRepository labelTemplateParamRepository;

    @Override
    public Map<String, Object> getCustSnOfflineInfo(String custSn,String templateCode,String organizeCode) {
        List<Map<String, Object>> printDate = new ArrayList<>();
        Map<String, Object> data = new HashMap<>();
        if (custSn.length() != MesPcnExtConstWords.CUSTOMER_SN_LENGTH_GM) {
            String a = custSn.substring(0, 3);
            String b = custSn.substring(3, 5);
            String c = custSn.substring(5, 20);
            String d = custSn.substring(20, 29);
            String e = custSn.substring(29, 41);
            String f = custSn.substring(41, 58);
            custSn= a + (char)30 + b + (char)29 + c + (char)29 + d + (char)29 + e + (char)29 + f + (char)30 + (char)04;
        }
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(custSn,"custSn",ddlPackBean);
        List<MesWorkOrder> workOrderList = workOrderRepository.findByHqlWhere(ddlPackBean);
        if (workOrderList != null && !workOrderList.isEmpty()) {
            MesWorkOrder mesWorkOrder = workOrderList.get(0);
            String vpps = custSn.substring(8, 23);
            String custPartNo = mesWorkOrder.getCustPartNo();
            String duns = custSn.substring(36, 46);
            String custSnAfterContent = custSn.substring(47, custSn.length() - 2);
            data.put("custSn", custSn);
            data.put("custPartNoPrefix", custPartNo.substring(0, custPartNo.length() - 4));
            data.put("custPartNoAfterFour", custPartNo.substring(custPartNo.length() - 4).toUpperCase());
            data.put("custSnVPPS", vpps);
            data.put("custSnDUNS", duns);
            data.put("custSnAfterContent", custSnAfterContent);
        }

        try {
            ByteArrayOutputStream dataMatrixCode = BarCodeUtils.createDataMatrixCode(custSn);
            data.put("custSnDataMatrix", Base64.getEncoder().encodeToString(dataMatrixCode.toByteArray()));
            printDate.add(data);
        } catch (Exception e) {
            throw new ImppBusiException().setErrorDetail("生成条码出错!");
        }
        MesLabelTemplate mesLabelTemplate = getMesLabelTemplate(templateCode, organizeCode);
        Map<String, Object> templateMap = new HashMap<>();
        templateMap.put("labelTemplate", mesLabelTemplate);
        templateMap.put("templateData", printDate);
        return templateMap;
    }

    private MesLabelTemplate getMesLabelTemplate(String templateCode,String organizeCode) {

        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(templateCode,"templateCode",ddlPackBean);
        List<MesLabelTemplate> mesLabelTemplates = labelTemplateRepository.findByHqlWhere(ddlPackBean);
        if (mesLabelTemplates == null ||mesLabelTemplates.isEmpty()){
            log.error(String.format("标签模板代码【%s】在标签模板表不存在！", templateCode));
            throw ImppExceptionBuilder.newInstance().setErrorDetail(String.format("标签模板代码【%s】在标签模板表不存在！", templateCode)).build();
        }
        DdlPackBean templateParamPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(templateCode,"templateCode",templateParamPackBean);
        List<MesLabelTemplateParam>  mesLabelTemplateParams = labelTemplateParamRepository.findByHqlWhere(templateParamPackBean);
        if (mesLabelTemplateParams == null || mesLabelTemplateParams.isEmpty()){
            log.error(String.format("标签模板代码【%s】在标签模板明细表不存在！", templateCode));
            throw ImppExceptionBuilder.newInstance().setErrorDetail(String.format("标签模板代码【%s】在标签模板明细表不存在！", templateCode)).build();
        }
        MesLabelTemplate mesLabelTemplate = mesLabelTemplates.get(0);
        mesLabelTemplate.setLabelTemplateParamList(mesLabelTemplateParams);
        return mesLabelTemplate;
    }
}
