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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesTemplateService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.util.MesPcnConstWords;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.base.util.StringUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesLabelTemplate;
import cn.estsh.i3plus.pojo.mes.bean.MesLabelTemplateFieldInfo;
import cn.estsh.i3plus.pojo.mes.bean.MesLabelTemplateFieldValue;
import cn.estsh.i3plus.pojo.mes.bean.MesLabelTemplateParam;
import cn.estsh.i3plus.pojo.mes.repository.MesLabelTemplateFieldInfoRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesLabelTemplateFieldValueRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesLabelTemplateParamRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesLabelTemplateRepository;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import cn.hutool.core.util.ObjectUtil;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Description : 打印模板
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/6/13 17:51
 * @Modify:
 **/
@Service
public class MesTemplateServiceImpl implements IMesTemplateService {

    @Autowired
    private MesLabelTemplateRepository mesLabelTemplateRDao;

    @Autowired
    private MesLabelTemplateParamRepository mesLabelTemplateParamRDao;

    @Autowired
    private MesLabelTemplateFieldInfoRepository labelTemplateFieldInfoRDao;

    @Autowired
    private MesLabelTemplateFieldValueRepository labelTemplateFieldValueRDao;

    @Override
    public MesLabelTemplate getMesLabelTemplate(String templateCode,String organizeCode) {
        // 根据标签模板代码，查询【MES_标签模板表】，获取对应的方法代码
        MesLabelTemplate labelTemplate = mesLabelTemplateRDao.getByProperty(
                new String[]{MesPcnConstWords.IS_VALID, MesPcnConstWords.IS_DELETED, MesPcnExtConstWords.TEMPLATE_CODE, MesPcnExtConstWords.ORGANIZE_CODE},
                new Object[]{CommonEnumUtil.IS_VAILD.VAILD.getValue(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), templateCode,organizeCode});

        if (labelTemplate == null) {
            MesPcnException.throwMesBusiException("标签模板代码【%s】在标签模板表不存在！", templateCode);
        }

        // 根据模板代码查询模板参数信息
        List<MesLabelTemplateParam> templateParamList = mesLabelTemplateParamRDao.findByProperty(new String[]{MesPcnExtConstWords.TEMPLATE_CODE, MesPcnConstWords.IS_VALID, MesPcnConstWords.IS_DELETED,MesPcnExtConstWords.ORGANIZE_CODE},
                new Object[]{templateCode, CommonEnumUtil.IS_VAILD.VAILD.getValue(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(),organizeCode});

        if (CollectionUtils.isEmpty(templateParamList)) {
            MesPcnException.throwMesBusiException("标签模板代码【%s】在标签模板明细表不存在！", templateCode);
        }
        labelTemplate.setLabelTemplateParamList(templateParamList);

        return labelTemplate;
    }

    @Override
    public MesLabelTemplate getLabelTemplate(String templateCode, String organizeCode) {
        DdlPackBean templatePackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(templateCode, MesPcnExtConstWords.TEMPLATE_CODE, templatePackBean);
        MesLabelTemplate mesLabelTemplate = mesLabelTemplateRDao.getByProperty(templatePackBean);
        if (mesLabelTemplate != null) {
            //级联获取labelTemplateParam
            DdlPackBean templateParamPackBean = DdlPackBean.getDdlPackBean(organizeCode);
            DdlPreparedPack.getStringEqualPack(mesLabelTemplate.getTemplateCode(),  MesPcnExtConstWords.TEMPLATE_CODE, templateParamPackBean);
            List<MesLabelTemplateParam> params = mesLabelTemplateParamRDao.findByHqlWhere(templateParamPackBean);
            mesLabelTemplate.setLabelTemplateParamList(params);
        }
        return mesLabelTemplate;
    }

    @Override
    public Map<String, Object> queryMesLabelTemplateParamPartValue(String partNo, String labelTemplate, String organizeCode) {
        if (StringUtil.isEmpty(labelTemplate)) {
            MesPcnException.throwMesBusiException("零件号【%s】未维护\"零件条码匹配模板\"字段值", partNo);
        }

        Map<String, Object> result = new HashMap<>();
        List<MesLabelTemplateFieldInfo> templateFieldInfos = labelTemplateFieldInfoRDao.findByProperty(
                new String[]{"templateCode", "templateFieldType", MesPcnConstWords.IS_VALID, MesPcnConstWords.IS_DELETED, MesPcnConstWords.ORGANIZE_CODE},
                new Object[]{labelTemplate, MesExtEnumUtil.LABEL_TEMPLATE_FIELD_TYPE.CUSTOM.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                        CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), organizeCode});
        Map<String, MesLabelTemplateFieldInfo> templateFieldInfoMap = new TreeMap<>();
        for (MesLabelTemplateFieldInfo info : templateFieldInfos) {
            templateFieldInfoMap.putIfAbsent(info.getTemplateParam(), info);
        }

        List<MesLabelTemplateFieldValue> fieldValueList = labelTemplateFieldValueRDao.findByProperty(
                new String[]{"partNo", "templateCode", MesPcnConstWords.IS_VALID, MesPcnConstWords.IS_DELETED, MesPcnConstWords.ORGANIZE_CODE},
                new Object[]{partNo, labelTemplate, CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                        CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), organizeCode});
        if (!CollectionUtils.isEmpty(fieldValueList)) {
            for (MesLabelTemplateFieldValue fieldValue : fieldValueList) {
                if (StringUtil.isEmpty(fieldValue.getTemplateParam())) {
                    continue;
                }
                MesLabelTemplateFieldInfo templateFieldInfo = templateFieldInfoMap.remove(fieldValue.getTemplateParam());
                if (templateFieldInfo != null) {
                    result.put(templateFieldInfo.getTemplateParam(), ObjectUtil.clone(fieldValue.getFieldValue()));
                }
            }
        }

        for (Map.Entry<String, MesLabelTemplateFieldInfo> entry : templateFieldInfoMap.entrySet()) {
            MesLabelTemplateFieldInfo fieldInfo = entry.getValue();
            result.put(fieldInfo.getTemplateParam(), null);
        }
        return result;
    }
}
