package com.qmth.sop.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.sop.business.cache.CommonCacheService;
import com.qmth.sop.business.entity.SysConfig;
import com.qmth.sop.business.mapper.SysConfigMapper;
import com.qmth.sop.business.service.SysConfigService;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.ExceptionResultEnum;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 参数表 服务实现类
 * </p>
 *
 * @author wangliang
 * @since 2023-07-17
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    @Resource
    CommonCacheService commonCacheService;

    /**
     * 查询所有系统配置项
     *
     * @return
     */
    @Override
    public List<SysConfig> selectAll() {
        List<SysConfig> sysConfigList = null;
        try {
            String platform = System.getProperties().getProperty(SystemConstant.OS_NAME);
            sysConfigList = this.list();
            if (!CollectionUtils.isEmpty(sysConfigList)) {
                for (SysConfig s : sysConfigList) {
                    if (s.getConfigKey().contains(SystemConstant.SYS_CONFIG_KEY_CHARSETS)) {
                        if (Objects.nonNull(platform) && platform.contains(SystemConstant.WINDOWS)) {
                            if (!Objects.equals(s.getConfigValue(), SystemConstant.CHARSET_GBK)) {
                                s.setConfigValue(SystemConstant.CHARSET_GBK);
                                this.updateById(s);
                            }
                        } else {
                            if (!Objects.equals(s.getConfigValue(), StandardCharsets.UTF_8.toString())) {
                                s.setConfigValue(StandardCharsets.UTF_8.toString());
                                this.updateById(s);
                            }
                        }
                    }
                    commonCacheService.updateSysConfigCacheForDb(s.getConfigKey(), s);
                }
            }
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
        }
        return sysConfigList;
    }

    @Override
    public SysConfig findByConfigKey(String configKey) {
        List<SysConfig> sysConfigList = this.list(new QueryWrapper<SysConfig>().lambda().eq(SysConfig::getConfigKey, configKey));
        if (CollectionUtils.isEmpty(sysConfigList)) {
            throw ExceptionResultEnum.ERROR.exception("未找到key[" + configKey + "]的系统变量");
        }
        if (sysConfigList.size() > 1) {
            throw ExceptionResultEnum.ERROR.exception(String.format("key为[%s]的系统变量有多个", configKey));
        }

        return sysConfigList.get(0);
    }
}
