package com.njcn.gather.system.cfg.controller;

import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.LogUtil;
import com.njcn.gather.system.cfg.pojo.param.SysConfigParam;
import com.njcn.gather.system.cfg.pojo.po.SysConfig;
import com.njcn.gather.system.cfg.service.ISysConfigService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统配置接口。
 */
@Slf4j
@Api(tags = "系统配置")
@RestController
@RequestMapping("/sysConfig")
@RequiredArgsConstructor
public class SysConfigController extends BaseController {

    private final ISysConfigService sysConfigService;

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getConfig")
    @ApiOperation("获取系统配置")
    public HttpResult<SysConfig> getConfig() {
        String methodDescribe = getMethodDescribe("getConfig");
        LogUtil.njcnDebug(log, "{}", methodDescribe);
        SysConfig sysConfig = sysConfigService.getOneConfig();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, sysConfig, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改系统配置")
    @ApiImplicitParam(name = "sysConfig", value = "系统配置", required = true)
    public HttpResult<Boolean> update(@RequestBody @Validated SysConfigParam.UpdateParam sysConfig) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}", methodDescribe);
        boolean result = sysConfigService.updateConfig(sysConfig);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
    }
}
