package cn.pluss.platform.controller.systemConfig;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entity.SystemConfig;
import cn.pluss.platform.systemConfig.SystemConfigService;
import cn.pluss.platform.util.OssOperatUtil;
import cn.pluss.platform.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("systemconfig")
public class SystemConfigController extends BaseNoModelController {

    @Autowired
    private SystemConfigService systemConfigService;

    //引导页
    @GetMapping("/index")
    public String index() {
        return "systemConfig/systemconfigList";
    }

    //分页查询
    @GetMapping("/querySystemconfigByPage")
    @ResponseBody
    public Map<String, Object> querySystemconfigByPage(@RequestParam(defaultValue = "1") Integer currPage, Integer size) {
        Map<String, Object> map = new HashMap<>();
        map.put("pageSize", size);
        map.put("offset", (currPage - 1) * size);
        List<SystemConfig> systemconfigList = systemConfigService.querySystemConfigPage(map);
        Integer count = systemConfigService.querySystemConfigPageCount(map);
        Map<String, Object> result = new HashMap<>();
        result.put("systemconfigList", systemconfigList);
        result.put("count", count);
        result.put("code", 200);
        result.put("pageCount", StringUtil.getPageCount(count, size));
        result.put("currPage", currPage);
        return result;
    }


    /**
     * 查询详情
     */
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/querySystemconfigBaseInfo/{id}", "/querySystemconfigBaseInfo"})
    public ModelAndView querySystemConfigBaseInfo(@PathVariable(value = "id", required = false) Integer id) {
        ModelAndView view = new ModelAndView("systemConfig/systemconfigBaseInfo");
        if (id != null && id != 0) {
            SystemConfig systemconfig = new SystemConfig();
            systemconfig.setId(id);
            systemconfig = systemConfigService.querySystemConfig(systemconfig);
            view.addObject("systemconfig", systemconfig);
        }
        Map<String, String> uploadParam = OssOperatUtil.getUploadParam();
        view.addObject("uploadParam", uploadParam);
        return view;
    }

    /**
     * 保存对象
     */
    @PostMapping("/saveSystemconfig")
    @ResponseBody
    public Result<SystemConfig> saveSystemConfig(@RequestBody SystemConfig systemconfig) {
        if (systemconfig.getId() != null && systemconfig.getId() != 0) {
            systemConfigService.updateSystemConfig(systemconfig);
        } else {
            systemConfigService.saveSystemConfig(systemconfig);
        }
        return ResultGenerator.genSuccessResult("保存成功!",null);
    }

    /**
     * 删除对象
     */
    @PostMapping("/deleteSystemconfig")
    @ResponseBody
    public Result<SystemConfig> deleteSystemConfig(@RequestBody SystemConfig systemConfig) {
        SystemConfig systemconfig = new SystemConfig();
        systemconfig.setId(systemConfig.getId());
        systemConfigService.deleteSystemConfig(systemconfig);
        return ResultGenerator.genSuccessResult("删除成功！",null);
    }
}
