package cn.com.qmth.stmms.admin.config;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.com.qmth.stmms.biz.config.model.SystemConfig;
import cn.com.qmth.stmms.biz.config.service.SystemConfigService;
import cn.com.qmth.stmms.common.annotation.Logging;
import cn.com.qmth.stmms.common.annotation.RoleRequire;
import cn.com.qmth.stmms.common.controller.BaseController;
import cn.com.qmth.stmms.common.enums.LogType;
import cn.com.qmth.stmms.common.enums.Role;

@Controller
@RequestMapping("/admin/sys/config")
public class SystemConfigController extends BaseController {

    @Autowired
    private SystemConfigService configService;

    @Logging(menu = "查询", type = LogType.QUERY)
    @RoleRequire(Role.SYS_ADMIN)
    @RequestMapping
    public ModelAndView list(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("modules/sys/configList");
        List<SystemConfig> list = configService.findAll();
        view.addObject("list", list);
        return view;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView editInit(HttpServletRequest request, @RequestParam Integer id) {
        SystemConfig config = configService.findById(id);
        if (config != null) {
            ModelAndView view = new ModelAndView("modules/sys/configEdit");
            view.addObject("config", config);
            return view;
        } else {
            return new ModelAndView("redirect:/admin/sys/config");
        }
    }

    @Logging(menu = "修改配置", type = LogType.UPDATE)
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String save(HttpServletRequest request, SystemConfig config) {
        configService.update(config.getId(), config.getDescription());
        return "redirect:/admin/sys/config";
    }

}
