package com.cpop.oam.business.service.impl;

import com.mybatisflex.core.row.DbChain;
import com.cpop.common.utils.StringUtils;
import com.cpop.common.utils.bean.BeanUtils;
import com.cpop.core.base.exception.ServiceException;
import com.cpop.core.base.table.SysConfig;
import com.cpop.core.service.CoreService;
import com.cpop.core.service.RedisService;
import com.cpop.core.utils.SpringUtils;
import com.cpop.oam.business.bo.SysConfigInfoBo;
import com.cpop.oam.business.service.CommonService;
import com.cpop.oam.business.vo.ConfigInfoVo;
import com.cpop.oam.framework.constant.OamConfigKey;
import com.cpop.oam.framework.enums.OamConfigEnum;
import org.apache.commons.lang3.ArrayUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.util.*;

import static com.cpop.core.base.table.table.SysConfigTableDef.SYS_CONFIG;

/**
 * @author DB
 * @createTime 2023/09/13 12:45
 * @description
 */
@Service("oamCommonService")
public class CommonServiceImpl implements CommonService {

    /**
     * 项目启动时，初始化参数到缓存
     */
    @PostConstruct
    private void init() {
        loadingConfigCache();
    }

    /**
     * 加载参数缓存数据
     */
    @Override
    public void loadingConfigCache() {
        List<String> keys = new ArrayList<>(Arrays.asList(OamConfigKey.WORK_WX_INFO));
        List<String> demandToTask = Arrays.asList(OamConfigKey.DEMAND_TO_TASK);
        keys.addAll(demandToTask);
        List<String> signGoalList = Arrays.asList(OamConfigKey.SIGN_GOAL);
        keys.addAll(signGoalList);
        // 加载OAM配置参数
        List<SysConfig> configsList = DbChain.table(SYS_CONFIG)
            .select(SYS_CONFIG.ALL_COLUMNS)
            .where(SYS_CONFIG.CONFIG_KEY.in(keys))
            .listAs(SysConfig.class);
        if (!configsList.isEmpty()) {
            for (SysConfig config : configsList) {
                SpringUtils.getBean(RedisService.class)
                    .setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
            }
        }
    }

    /**
     * 重置参数缓存数据
     */
    @Override
    public void resetConfigCache() {
        clearConfigCache();
        loadingConfigCache();
    }

    /**
     * 清空参数缓存数据
     */
    @Override
    public void clearConfigCache() {
        RedisService redisService = SpringUtils.getBean(RedisService.class);
        Collection<String> keys = redisService.keys(OamConfigKey.OAM_CONFIG_KEY + "*");
        redisService.deleteObject(keys);
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    @Override
    public String getCacheKey(String configKey) {
        return OamConfigKey.OAM_CONFIG_KEY + configKey;
    }

    /**
     * @Description: 获取系统配置信息
     * @return R<OpenPlatformConfigVo>
     * @author DB
     * @Date: 2023/7/7 0007 16:27
     */
    @Override
    public ConfigInfoVo getSysConfigInfo() {
        RedisService redisService = SpringUtils.getBean(RedisService.class);
        CoreService coreService = SpringUtils.getBean(CoreService.class);
        // 获取企业微信信息
        Map<String, String> configMap = new HashMap<>(8);
        Arrays.asList(OamConfigKey.WORK_WX_INFO).forEach(item -> {
            String cacheInfo = redisService.getCacheObject(getCacheKey(item));
            if (StringUtils.isNotBlank(cacheInfo)) {
                // 获取枚举
                OamConfigEnum sysConfigEnum = OamConfigEnum.matchKey(item);
                configMap.put(sysConfigEnum.getField(), cacheInfo);
            } else {
                // 查询系统
                SysConfig sysConfig = coreService.selectConfigByKey(item);
                if (null != sysConfig) {
                    // 获取枚举
                    OamConfigEnum sysConfigEnum = OamConfigEnum.matchKey(item);
                    configMap.put(sysConfigEnum.getField(), sysConfig.getConfigValue());
                }
            }
        });
        ConfigInfoVo sysConfigInfoVo = new ConfigInfoVo();
        BeanUtils.mapToObj(configMap, sysConfigInfoVo);
        return sysConfigInfoVo;
    }

    /**
     * @param bo 请求参数
     * @Description: 设置系统配置信息
     * @return: R<Void>
     * @Author: DB
     * @Date: 2023/5/11 15:48
     **/
    @Override
    public void setSysConfigInfo(SysConfigInfoBo bo) {
        CoreService coreService = SpringUtils.getBean(CoreService.class);
        // 遍历属性名与值
        try {
            for (Field field : bo.getClass().getDeclaredFields()) {
                field.setAccessible(true);
                // 获取枚举
                OamConfigEnum sysConfigEnum = OamConfigEnum.matchField(field.getName());
                if (null != sysConfigEnum) {
                    SysConfig sysConfig = new SysConfig();
                    sysConfig.setConfigName(sysConfigEnum.getName())
                        .setConfigKey(sysConfigEnum.getKey())
                        .setConfigValue(field.get(bo).toString());
                    // 修改
                    if (DbChain.table(SYS_CONFIG)
                        .where(SYS_CONFIG.CONFIG_KEY.eq(sysConfig.getConfigKey()))
                        .count() > 0) {
                        coreService.updateConfig(sysConfig);
                    } else {
                        coreService.insertConfig(sysConfig);
                    }
                    // 更新缓存
                    SpringUtils.getBean(RedisService.class)
                        .setCacheObject(getCacheKey(sysConfigEnum.getKey()), sysConfig.getConfigValue());
                }
            }
        } catch (IllegalAccessException e) {
            throw new ServiceException("更新系统配置失败,请联系相关人员!");
        }
    }
}
