package com.liang.controller;

import com.liang.common.base.BaseController;
import com.liang.common.base.BaseResult;
import com.liang.entity.Config;
import com.liang.service.ConfigService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author
 * @date 2023-04-21-13:09
 **/
@Api(tags = "系统配置")
@Controller
@RequestMapping("/config")
public class ConfigController extends BaseController {
    final static Logger logger = LoggerFactory.getLogger(ConfigController.class);

    @Autowired
    private ConfigService configService;

    /**
     * 系统配置修改页面
     *
     * @return
     */
    @RequestMapping(value = "/init", method = RequestMethod.GET)
    public String init(Model model) {
        Config config = configService.getSysConfig();
        model.addAttribute("config", config);
        return "config/index";
    }


    /**
     * 系统配置修改
     *
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult update(@RequestBody Config config) {
        config.setUpdateUser(getSysUserAccount());
        int num = configService.updateSysConfig(config);
        if (num > 0) {
            return BaseResult.success("系统配置更新成功！");
        } else {
            return BaseResult.failure("系统配置更新失败！");
        }
    }


}
