package com.ssj.sys.controller;

import com.ssj.bean.common.framework.core.domain.Response;
import com.ssj.bean.sys.sort.domain.Sort;
import com.ssj.framework.basic.common.bean.SplitPage;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.service.sys.sort.service.SortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort.Direction;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/sys/sort")
public class SortController extends BaseController {
 
	@Autowired
    private SortService sortService;
	
	 @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_name", request.getParameter("LIKE_name")==null?"":request.getParameter("LIKE_name"));
		params.put("LIKE_code", request.getParameter("LIKE_code")==null?"":request.getParameter("LIKE_code"));
		params.put("EQ_pid", "0");
		SplitPage sp = new SplitPage();
	        sp.setAction(request.getRequestURI().toString());
	        sp.setPageNo(pageNo);
	        sp.setPageSize(pageSize);
	        sp.setParams(params);
	    Page<Sort> page= sortService.findByPage(params, initPage(pageNo,pageSize,Direction.DESC,"sort"));
	    List<Sort> 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/sort/list";
	 }
	 
	 
	 /**
	     * 系统参数编辑页面
	     * @param request
	     * @param response
	     * @return
	     */
	    @RequestMapping("/add")
	    public String add(Model model, @RequestParam( defaultValue = "id")String id) throws Exception{
	    	Sort  sort= sortService.getById(id);
	         if(sort ==null) {
	        	 sort = new Sort();
	        	 sort.setSort(sortService.getSortChildrenCount("0")+1);
	         }
	         model.addAttribute("sort", sort); 
	    	return "sys/sort/add";
	    }
	    /**
	     * 系统参数编辑页面
	     * @param request
	     * @param response
	     * @return
	     */
	    @RequestMapping("/childrenList")
	    public String childrenList(Model model, @RequestParam( defaultValue = "id")String id) throws Exception{
	    	model.addAttribute("id", id); 
	    	return "sys/sort/childrenList";
	    }
	    @RequestMapping(value = "/getSortList", method = RequestMethod.POST)
	    @ResponseBody
	    public List<Sort> getSortList(@RequestParam( defaultValue = "id")String id){
	    	List<Sort> list = new ArrayList<Sort>();
	    	Sort sort=sortService.findByIdAndChildren(id);
			list.add(sort);
			return list;
	    }
	    
	    @RequestMapping(value = "/saveSort", method = RequestMethod.POST)
	    @ResponseBody
	    public Response saveSort(Sort  sort) throws Exception{
	    	Response response = new  Response();
	    	sort=sortService.save(sort);
	    	sortService.clearConfigCache();
	    	response.success(sort);
	        return response;
	    }
	    /**
	     * 保存排序
	     * @param ids
	     * @return
	     */
	    @RequestMapping(value = "/sequenceSort", method = RequestMethod.POST)
	    @ResponseBody
	    public Response  sequenceSort(@RequestParam(value = "ids[]")String[] ids){
	    	Response response = new  Response();
	    	if(ids!=null && ids.length>0){
	    		for (int i = 0; i < ids.length; i++) {
	    			sortService.updateSequenceSort(ids[i], i+1);
	    		}
	    	}
	    	response.success();
	        return response;
	    }
	    
	    
	    /**
	     * 删除标签参数
	     * @param request
	     * @param response
	     * @return
	     * @throws Exception 
	     */
	    @RequestMapping(value = "/deleteSort", method = RequestMethod.POST)
	    @ResponseBody
	    public Response deleteSort(String id) throws Exception{
	    	deleteChildrenSort(id, 0);
	    	sortService.clearConfigCache();
	    	Response response = new  Response();
	    	response.success();
	        return response;
	    }
	    
		 //递归调用删除所有的子节点
	    private void deleteChildrenSort(String  pid, int callCount) throws Exception {
	        callCount ++;
	        //判断递归深度
	        if(callCount > 5){
	        	return ;
	        }
	        List<Sort> childrenList= sortService.findByPidAndIshot(pid, 1);
	        for (Sort sort : childrenList) {
	        	deleteChildrenSort(sort.getId(), callCount);
		    }
	        sortService.delete(pid);
	    }
	    
	    
	    @RequestMapping("/childrenAdd")
	    public String childrenAdd(Model model, @RequestParam( defaultValue = "id")String id,
	    		@RequestParam( defaultValue = "0")String edit,
	    		@RequestParam( defaultValue = "pid")String pid) {
	    	Sort parent = sortService.getById(pid);
	    	if(parent == null ){
	    		parent = new Sort();
	    	}
	    	Sort sort = sortService.getById(id);
	    	if(sort == null ){
	    		sort = new Sort();
	    		if(parent.getLevel()!=null){
	    			sort.setLevel(parent.getLevel()+1);
	    		}
	    		sort.setSort(sortService.getSortChildrenCount(pid)+1);
	    	}
	    	model.addAttribute("parent", parent); 
	    	model.addAttribute("sort", sort); 
	    	model.addAttribute("edit", edit); 
	    	return "sys/sort/childrenAdd";
	    }
	    @RequestMapping("/childrenSort")
	    public String childrenSort(Model model, 
	    		@RequestParam( defaultValue = "pid")String pid) {
	    	List<Sort> list = sortService.findByPid(pid);
	    	model.addAttribute("list", list); 
	    	return "sys/sort/childrenSort";
	    }
}
