package com.yihu.hos.system.controller;

import com.yihu.hos.system.model.SystemParam;
import com.yihu.hos.system.service.SystemParamManager;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.controller.BaseController;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统参数管理
 */
@RequestMapping("/param")
@Controller
public class ParamController extends BaseController {
    @Resource(name = SystemParamManager.BEAN_ID)
    private SystemParamManager systemParamManager;

    /**
     * 系统参数管理界面
     *
     * @param model
     * @return
     */
    @RequestMapping("/initial")
    public String roleInitial(Model model) {
        model.addAttribute("contentPage", "system/systemparam/systemParam");
        return "partView";
    }

    /**
     * 系统参数列表
     *
     * @param request
     * @return
     */
    @RequestMapping("getParamList")
    @ResponseBody
    public Result getParamList(HttpServletRequest request) {
        try {
            Map<String, Object> params = new HashMap<>();
            String name = request.getParameter("name");
            params.put("name", name);
            String page = StringUtils.isEmpty(request.getParameter("page")) ? "1" : request.getParameter("page");
            String rows = StringUtils.isEmpty(request.getParameter("rows")) ? "10" : request.getParameter("rows");

            params.put("page", page);
            params.put("rows", rows);
            return systemParamManager.getParamList(params);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }
    /**
     * 系统参数编辑界面
     */
    @RequestMapping("editorParam")
    public String editorParam(Model model, String id) {
        try {
            //是否编辑
            if (id != null && id.length() > 0) {
                //获取菜单信息
                SystemParam role = systemParamManager.getParamById(id);
                model.addAttribute("model", role);
            }
            model.addAttribute("contentPage", "system/systemparam/editorParam");
            return "pageView";
        } catch (Exception ex) {
            model.addAttribute("contentPage", "system/systemparam/editorParam");
            return "pageView";
        }
    }

    /**
     * 新增系统参数
     */
    @RequestMapping("addParam")
    @ResponseBody
    public Result addParam(HttpServletRequest request) {
        try {
            SystemParam obj = new SystemParam();
            BeanUtils.populate(obj, request.getParameterMap());

            return systemParamManager.addParam(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 修改系统参数
     */
    @RequestMapping("updateParam")
    @ResponseBody
    public Result updateParam(HttpServletRequest request) {
        try {
            SystemParam obj = new SystemParam();
            BeanUtils.populate(obj, request.getParameterMap());

            return systemParamManager.updateParam(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 删除系统参数
     */
    @RequestMapping("deleteParam")
    @ResponseBody
    public Result deleteParam(HttpServletRequest request) {
        try {
            String id=request.getParameter("id");

            return systemParamManager.deleteParam(id);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

}
