package org.wcs.serve.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.wcs.factory.AppServeResponseFactory;
import org.wcs.helper.AppConfigHelper;
import org.wcs.mapper.intf.AppBaseConfigService;
import org.wcs.model.dto.serve.config.QueryConfigReq;
import org.wcs.model.dto.serve.config.UpdateConfigReq;
import org.wcs.model.po.app.AppBaseConfig;
import org.wcs.model.vo.serve.AppServeDataResponse;
import org.wcs.model.vo.serve.AppServeResponse;
import org.wcs.model.vo.serve.config.QueryConfigResp;
import org.wcs.serve.service.intf.IConfigService;
import org.wcs.utils.AppBeanUtils;
import org.wcs.utils.AppStringUtils;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class ConfigService implements IConfigService {


    private final AppBaseConfigService baseConfigService;
    private final AppConfigHelper appConfigHelper;

    /**
     * 查询配置
     * @param queryConfigReq 查询参数
     * @return 配置列表
     */
    @Override
    public AppServeDataResponse<List<QueryConfigResp>> queryConfig(QueryConfigReq queryConfigReq) {
        List<AppBaseConfig> configs = baseConfigService.queryIndistinct(queryConfigReq.getConfigKey(), queryConfigReq.getConfigName(), false);
        if(configs == null) {
            return AppServeResponseFactory.internalServerError("数据服务异常", null);
        }
        List<QueryConfigResp> returnConfigList = AppBeanUtils.copyListProperties(configs, QueryConfigResp::new);
        return AppServeResponseFactory.success("查询成功", returnConfigList);
    }

    /**
     * 更新配置
     * @param updateConfigReq 更新参数
     * @return 更新结果
     */
    @Override
    public AppServeResponse updateConfig(UpdateConfigReq updateConfigReq) {
        Object userIdObj = StpUtil.getLoginId();
        if(userIdObj == null) {
            return AppServeResponseFactory.unauthorized("用户未登录");
        }
        String userId = userIdObj.toString();
        if(AppStringUtils.isEmpty(userId)) {
            return AppServeResponseFactory.unauthorized("未检索到您的登录信息或者登录已经过期");
        }
        log.info("用户 {} 更新配置 {}", userId, AppStringUtils.objectToString(updateConfigReq));
        AppBaseConfig  updateConfig = new AppBaseConfig();
        updateConfig.setConfigKey(updateConfigReq.getConfigKey());
        updateConfig.setConfigName(updateConfigReq.getConfigName());
        updateConfig.setConfigType(updateConfigReq.getConfigType());
        updateConfig.setImmediately(updateConfigReq.getImmediately());
        updateConfig.setClientShow(updateConfigReq.getClientShow());
        updateConfig.setConfigValue(updateConfigReq.getConfigValue());
        updateConfig.setTagText(updateConfigReq.getTagText());
        updateConfig.setValueType(updateConfigReq.getValueType());
        updateConfig.setRemark(updateConfigReq.getRemark());
        int update = baseConfigService.update(updateConfig);
        if(update == 0) {
            return AppServeResponseFactory.internalServerError("数据服务异常", null);
        }
        return AppServeResponseFactory.success(String.format("更新成功，更新了 %d 条记录", update));
    }

    /**
     * 重载配置
     * @return 重载结果
     */
    @Override
    public AppServeResponse reloadConfig() {
        appConfigHelper.loadConfig();
        return AppServeResponseFactory.success("已经尝试重载");
    }

    /**
     * 查询配置值
     * @param key 配置键
     * @return 配置值
     */
    @Override
    public AppServeDataResponse<String> queryConfigValue(String key) {
        if(AppStringUtils.isEmpty(key)) {
            return AppServeResponseFactory.fail("参数错误", "");
        }
        String configValue = appConfigHelper.getConfig(key);
        return AppServeResponseFactory.success("查询成功", configValue);
    }
}
