package com.ssj.sys.controller;

import com.ssj.bean.common.framework.core.domain.Response;
import com.ssj.framework.basic.common.bean.SplitPage;
import com.ssj.framework.basic.config.bean.Config;
import com.ssj.framework.basic.config.service.ConfigService;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.framework.core.security.manager.TokenManager;
import com.ssj.framework.core.util.SystemResourceLocator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.bind.annotation.ResponseBody;

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


/**
 * 系统参数管理控制类
 */
@Controller
@RequestMapping("/sys/config")
public class ConfigController extends BaseController {
	@Autowired
    private ConfigService configService;
	@Autowired
	private TokenManager tokenManager;

	 @RequestMapping("/list")
	 public String list(Model model,
	    		@RequestParam(required = false, defaultValue = "10") int pageSize,
				@RequestParam(required = false, defaultValue = "1") int pageNo ) throws Exception {
		 Map<String, Object> params = new HashMap<String, Object>();
		params.put("LIKE_paramCode", request.getParameter("LIKE_paramCode")==null?"":request.getParameter("LIKE_paramCode"));
		SplitPage sp = new SplitPage();
	        sp.setAction(request.getRequestURI().toString());
	        sp.setPageNo(pageNo);
	        sp.setPageSize(pageSize);
	        sp.setParams(params);
	    Page<Config> page= configService.findByPage(params, initPage(pageNo,pageSize));
	    List<Config> list = page.getContent();
		sp.setRowCnt((int)page.getTotalElements());
		model.addAttribute("list", list);
		model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
        model.addAttribute("page", sp);
        model.addAttribute("search", params);
		 
	     return "sys/config/list";
	 }
    
    /**
     * 系统参数编辑页面
     * @param
     * @param
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Model model, @RequestParam( defaultValue = "id")String id) throws Exception{
         Config  config= configService.getById(id);
         if(config ==null) {config = new Config();}
         model.addAttribute("config", config); 
    	return "sys/config/edit";
    }
    
    @RequestMapping(value = "/saveConfig", method = RequestMethod.POST)
    @ResponseBody
    public Response saveConfig(Config  config) throws Exception{
    	Response response = new  Response();
    	config=configService.save(config);
    	configService.clearConfigCache();
		SystemResourceLocator.setValue(config.getParamCode(),config.getParamVal(),true);
    	response.success(config);
        return response;
    }
    
    /**
     * 删除系统参数
     * @param
     * @param
     * @return
     * @throws Exception 
     */
    @RequestMapping(value = "/deleteConfig", method = RequestMethod.POST)
    @ResponseBody
    public Response delete(String id) throws Exception{
    	configService.delete(id);
    	configService.clearConfigCache();
    	Response response = new  Response();
    	response.success();
        return response;
    }
}
