package ink.wgink.login.base.controller.api.config;

import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.login.base.consts.IUserCenterConst;
import ink.wgink.login.base.pojo.vos.config.ConfigVO;
import ink.wgink.login.base.service.config.IConfigService;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: ConfigController
 * @Description: 系统配置
 * @Author: WangGeng
 * @Date: 2019/11/20 10:28
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "配置")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/config")
public class ConfigController {

    @Autowired
    private IConfigService configService;

    @ApiOperation(value = "保存配置", notes = "保存配置接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updateconfig")
    @CheckRequestBodyAnnotation
    public SuccessResult updateConfig(@RequestBody ConfigVO configVO) throws Exception {
        if (!StringUtils.isBlank(configVO.getuKeyLogin())) {
            if (!StringUtils.equals(ISystemConstant.IS_TRUE, configVO.getuKeyLogin()) && !StringUtils.equals(ISystemConstant.IS_FALSE, configVO.getuKeyLogin())) {
                throw new ParamsException("uKey登录只能是true和false");
            }
        }
        if (!StringUtils.isBlank(configVO.getScanCodeLogin())) {
            if (!StringUtils.equals(ISystemConstant.IS_FALSE, configVO.getScanCodeLogin()) && !StringUtils.equals(IUserCenterConst.DING_DING_SCAN_CODE, configVO.getScanCodeLogin())) {
                throw new ParamsException("uKey登录只能是false和dingDingScanCode");
            }
        }
        configService.updateConfig(configVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "获取配置", notes = "获取配置接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getconfig")
    public Map<String, Object> getConfig() {
        return configService.getConfig();
    }

}
