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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesConfigService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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.mes.bean.MesConfig;
import cn.estsh.i3plus.pojo.mes.repository.MesConfigRepository;
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 org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @CreateDate: 2021/9/9 下午3:25
 * @Author: simon.song
 */
@Service
@Slf4j
public class MesConfigService implements IMesConfigService {
    @Autowired
    private MesConfigRepository configRepository;

    @Override
    public String getCfgValue(String org, String key) {
        MesConfig config = configRepository.getByProperty(
                new String[]{"cfgKey", "organizeCode", "isValid", "isDeleted"},
                new Object[]{key, org, CommonEnumUtil.IS_VAILD.VAILD.getValue(), CommonEnumUtil.IS_DEAL.NO.getValue()});

        if (config == null) {
            MesPcnException.throwMesBusiException("配置表数据不存在，请维护key【%s】", key);
        }
        return config.getCfgValue();
    }

    @Override
    public MesConfig getMesConfig(String organizeCode, String key) {
        MesConfig config = configRepository.getByProperty(
                new String[]{"cfgKey", "organizeCode", "isValid", "isDeleted"},
                new Object[]{key, organizeCode, CommonEnumUtil.IS_VAILD.VAILD.getValue(), CommonEnumUtil.IS_DEAL.NO.getValue()});

        if (config == null) {
            MesPcnException.throwMesBusiException("配置表数据不存在，请维护key【%s】", key);
        }
        return config;
    }

    @Override
    public MesConfig getMesConfigNoError(String organizeCode, String cfgKey) {
        return configRepository.getByProperty(
                new String[]{"cfgKey", "organizeCode", "isValid", "isDeleted"},
                new Object[]{cfgKey, organizeCode, CommonEnumUtil.IS_VAILD.VAILD.getValue(), CommonEnumUtil.IS_DEAL.NO.getValue()});
    }

    @Override
    public List<MesConfig> getConfigList(String organizeCode, String cfgCode, String cfgKey, String cfgType) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(cfgCode, MesPcnExtConstWords.CFG_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(cfgKey, MesPcnExtConstWords.CFG_KEY, packBean);
        DdlPreparedPack.getStringEqualPack(cfgType, MesPcnExtConstWords.CFG_TYPE, packBean);
        return configRepository.findByHqlWhere(packBean);
    }

    @Override
    public Boolean checkCfgValueIsContainItem(String organizeCode, String cfgCode, String cfgKey, String cfgType, String splitStr, String item) {
        String cfgValue = getAndMergeCfgValue(organizeCode, cfgCode, cfgKey, cfgType, splitStr);
        if (StringUtils.isEmpty(cfgValue)) return false;
        List<String> cfgValueList = Arrays.asList(cfgValue.split(splitStr));
        return (!CollectionUtils.isEmpty(cfgValueList) && cfgValueList.contains(item)) ? true : false;
    }

    private String getAndMergeCfgValue(String organizeCode, String cfgCode, String cfgKey, String cfgType, String splitStr) {
        List<MesConfig> configList = getConfigList(organizeCode, cfgCode, cfgKey, cfgType);
        if (CollectionUtils.isEmpty(configList)) return null;
        return configList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getCfgValue()))).map(MesConfig::getCfgValue).collect(Collectors.joining(splitStr));
    }

    @Override
    public Boolean checkShardingTablesObjectCfg(String organizeCode, String cfgKey, String item) {
        return checkCfgValueIsContainItem(organizeCode, MesPcnExtConstWords.MES_SHARDING_TABLES_OBJECT_CFG, cfgKey, CommonEnumUtil.SOFT_TYPE.MES_PCN.name(), MesPcnExtConstWords.COMMA, item);
    }

    @Override
    public List<String> findCfgValueContainItem(String organizeCode, String cfgCode, String cfgKey, String cfgType, String splitStr, List<String> itemList) {
        String cfgValue = getAndMergeCfgValue(organizeCode, cfgCode, cfgKey, cfgType, splitStr);
        if (StringUtils.isEmpty(cfgValue)) return null;
        List<String> cfgValueList = Arrays.asList(cfgValue.split(splitStr));
        if (CollectionUtils.isEmpty(cfgValueList)) return null;
        return itemList.stream().filter(cfgValueList::contains).collect(Collectors.toList());
    }

    @Override
    public List<String> findShardingTablesObjectCfg(String organizeCode, String cfgKey, List<String> itemList) {
        if(CollectionUtils.isEmpty(itemList)) return null;
        return findCfgValueContainItem(organizeCode, MesPcnExtConstWords.MES_SHARDING_TABLES_OBJECT_CFG, cfgKey, CommonEnumUtil.SOFT_TYPE.MES_PCN.name(), MesPcnExtConstWords.COMMA, itemList);
    }

    @Override
    public List<MesConfig> findMesConfigList(String organizeCode, String cfgCode) {
        List<MesConfig> configList = getConfigList(organizeCode, cfgCode, null, null);
        if(CollectionUtils.isEmpty(configList)){
            MesPcnException.throwMesBusiException("配置表数据不存在，请维护key【%s】", cfgCode);
        }
        return configList;
    }

    @Override
    public MesConfig getCfgValueByCode(String organizeCode,String cfgCode) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(cfgCode)) return null;
        return configRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.CFG_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), cfgCode});
    }


}
