package com.ssj.sys.controller;

import com.ssj.bean.common.framework.core.domain.Response;
import com.ssj.bean.sys.message.domain.Message;
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.conch.common.service.AppCommonService;
import com.ssj.service.sys.message.service.MessageService;
import com.ssj.service.sys.sort.service.SortService;
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.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/sys/message")
public class MessageController extends BaseController {
	
	@Autowired
	private AppCommonService appCommonService;
	
	@Autowired
	private MessageService messageService;

	@Autowired
	private SortService sortService;

	@RequestMapping("/messageList")
	public String messageList(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_captions", request.getParameter("LIKE_captions")==null?"":request.getParameter("LIKE_captions"));
		SplitPage sp = new SplitPage();
		sp.setAction(request.getRequestURI().toString());
		sp.setPageNo(pageNo);
		sp.setPageSize(pageSize);
		sp.setParams(params);
		Page<Message> page= messageService.findByPage(params, initPage(pageNo,pageSize));
		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/message/messageList";
	}
	@RequestMapping("/messageAdd")
	public String messageAdd(Model model,@RequestParam( defaultValue = "0")String id) throws Exception {
		Message message= messageService.getById(id);
		if(message==null) {
			message = new Message();
		}
		model.addAttribute("ws", message);
		model.addAttribute("push_terminal", sortService.getParamValByCode("push_terminal"));
		return "sys/message/messageAdd";
	}
	@RequestMapping(value = "/messageSave", method = RequestMethod.POST)
	@ResponseBody
	public Response messageSave(Message message){
		logger.info(message.toString());
		Response response = new Response();
		Date now = new Date();
		message.setLastUpdated(now);
		if(StringUtil.isBlank(message.getId())){
			message.setCreated(now);
		}
		messageService.save(message);
		//环信推送
		appCommonService.pushAllBySystemMessage(message.getType(), message.getCaptions());
		response.success();
		return response;
	}
	@RequestMapping(value = "/messageDelete", method = RequestMethod.POST)
	@ResponseBody
	public Response messageDelete(String id){
		Response response = new  Response();
		messageService.delete(id);
		response.success();
		return response;
	}
}
