package cn.estsh.i3plus.ext.mes.pcn.apiservice.controller.busi;

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesTemplateService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
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 cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ResultBean;
import cn.estsh.impp.framework.boot.util.ValidatorBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Api("获取系统配置的模版信息")
@RestController
@RequestMapping(MesCommonConstant.MES_YANFEN)
public class MesLabelTemplateExtController {

    @Autowired
    private IConfigService configService;

    @Autowired
    private IMesTemplateService mesTemplateService;

    @GetMapping("/label-template/by-module-or-template/get")
    @ApiOperation(value = "获取系统配置的模版信息")
    public ResultBean queryMesLabelTemplate(String organizeCode, String moduleCode, String templateCode) {
        try {
            ValidatorBean.checkNotNull(organizeCode, "工厂代码不能为空");
            if (StringUtils.isEmpty(moduleCode) && StringUtils.isEmpty(templateCode)) {
                ValidatorBean.checkNotNull(moduleCode, "模版配置代码不能为空");
            }
            if (!StringUtils.isEmpty(moduleCode)) {
                Map<String, String> configMap = configService.getConfigMapByCfgCode(moduleCode, organizeCode);
                String templateCustomHtml = configMap.get(MesPcnExtConstWords.TEMPLATE_CUSTOM_HTML);
                if (!StringUtils.isEmpty(templateCustomHtml)) return ResultBean.success("查询成功").setResultObject(templateCustomHtml);
                String templateCfg = configMap.get(moduleCode);
                if (!StringUtils.isEmpty(templateCfg)) templateCode = templateCfg;
            }
            ValidatorBean.checkNotNull(templateCode, "模版代码不能为空");
            return ResultBean.success("查询成功").setResultObject(mesTemplateService.getMesLabelTemplate(templateCode, organizeCode));

        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/label-template/by-module-or-template/get-custom")
    @ApiOperation(value = "获取系统配置的模版信息")
    public ResultBean queryModuleCodeCfg(String organizeCode, String moduleCode, String chooseCodes, String templateCode) {
        try {
            ValidatorBean.checkNotNull(organizeCode, "工厂代码不能为空");
            if (StringUtils.isEmpty(moduleCode) && StringUtils.isEmpty(templateCode)) {
                ValidatorBean.checkNotNull(moduleCode, "模版配置代码不能为空");
            }

            if (StringUtils.isEmpty(chooseCodes)) return queryMesLabelTemplate(organizeCode, moduleCode, templateCode);

            List<String> chooseCodeList = Arrays.asList(chooseCodes.split(MesPcnExtConstWords.COMMA));

            List<MesConfig> configList = configService.getMesConfigListByCfgCodeKey(moduleCode, MesPcnExtConstWords.CUSTOM_CFG, organizeCode);
            Optional<MesConfig> optional = CollectionUtils.isEmpty(configList) ? null :
                    configList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getCfgValue())
                            && !StringUtils.isEmpty(o.getCfgType()) && chooseCodeList.contains(o.getCfgType()))).findFirst();

            return queryMesLabelTemplate(organizeCode, (null == optional || !optional.isPresent()) ? moduleCode : optional.get().getCfgValue(), templateCode);

        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
