package com.qmth.jkserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.jkserver.constant.SystemConstant;
import com.qmth.jkserver.core.exception.JkServerException;
import com.qmth.jkserver.dao.SysConfigDao;
import com.qmth.jkserver.model.SysConfig;
import com.qmth.jkserver.service.CommonCacheService;
import com.qmth.jkserver.service.SysConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
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 xf
 * @since 2021-03-23
 */
@Service
@Scope(proxyMode = ScopedProxyMode.INTERFACES)
public class SysConfigServiceImpl extends ServiceImpl<SysConfigDao, SysConfig> implements SysConfigService {

    @Resource
    CommonCacheService commonCacheService;

    @Override
    public SysConfig getByKey(String key) {
        if (StringUtils.isBlank(key)) {
            throw new JkServerException("key不能为空");
        }
        QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysConfig::getConfigKey, key);
        SysConfig sysConfig = this.baseMapper.selectOne(queryWrapper);
        return sysConfig;
    }

    /**
     * 查询所有系统配置项
     *
     * @return
     */
    @Override
    public List<SysConfig> selectAll() {
        List<SysConfig> sysConfigList = null;
        try {
            String platform = System.getProperties().getProperty(SystemConstant.OS_NAME);
            QueryWrapper<SysConfig> sysConfigQueryWrapper = new QueryWrapper<>();
            sysConfigQueryWrapper.lambda().isNull(SysConfig::getSchoolId);
            sysConfigList = this.list(sysConfigQueryWrapper);
            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;
    }
}
