package com.ssj.sys.controller;

import com.ssj.bean.common.framework.core.domain.Response;
import com.ssj.bean.sys.shortcuts.domain.Shortcuts;
import com.ssj.framework.basic.common.bean.SplitPage;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.service.sys.shortcuts.service.ShortcutsService;
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.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Mount Liu
 * @date 2018/8/16
 */
@Controller
@RequestMapping("/sys/shortcuts")
public class ShortcutsController extends BaseController {

	@Autowired
	private ShortcutsService shortcutsService;

	@RequestMapping("/shortcutsList")
	public String shortcutsList(Model model,
							   @RequestParam(required = false, defaultValue = "10") int pageSize,
							   @RequestParam(required = false, defaultValue = "1") int pageNo)
			throws Exception {

		Map<String, Object> params = new HashMap<>(16);
		params.put("LIKE_content", request.getParameter("LIKE_content")==null?"":request.getParameter("LIKE_content"));
		SplitPage sp = new SplitPage();
		sp.setAction(request.getRequestURI().toString());
		sp.setPageNo(pageNo);
		sp.setPageSize(pageSize);
		sp.setParams(params);
		Page<Shortcuts> page= shortcutsService.findByPage(params, initPage(pageNo,pageSize,Direction.ASC, "shortcutsOrder" ));
		sp.setRowCnt((int) page.getTotalElements());
		model.addAttribute("list", page.getContent());
		model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
		model.addAttribute("page", sp);
		model.addAttribute("search", params);

		return "sys/shortcuts/shortcutsList";
	}
	@RequestMapping("/shortcutsAdd")
	public String shortcutsAdd(Model model,@RequestParam( defaultValue = "0")String id) throws Exception {
		Shortcuts entity= shortcutsService.getById(id);
		if(entity==null) {
			entity = new Shortcuts();
		}
		model.addAttribute("entity", entity);
		return "sys/shortcuts/shortcutsAdd";
	}
	@RequestMapping(value = "/shortcutsSave", method = RequestMethod.POST)
	@ResponseBody
	public Response shortcutsSave(Shortcuts entity){
		Response response = new Response();
		if(StringUtil.isBlank(entity.getId())){
			entity.setCreatedTime(new Date());
		}
		shortcutsService.save(entity);
		response.success();
		return response;
	}
	@RequestMapping(value = "/shortcutsDelete", method = RequestMethod.POST)
	@ResponseBody
	public Response shortcutsDelete(String id){
		Response response = new  Response();
		shortcutsService.delete(id);
		response.success();
		return response;
	}
	@RequestMapping(value = "/updateOrder", method = RequestMethod.POST)
	@ResponseBody
	public Response  updateOrder(String id,Integer shortcutsOrder){
		Response response = new  Response();
		shortcutsService.updateOrder(id, shortcutsOrder);
		response.success();
		return response;
	}
}
