package com.sys.controller;

import com.cab.model.CabMymessage;
import com.cku.core.PageBeanResult;
import com.cku.core.RESTResponse;
import com.cku.sms.ZtSmsCode;
import com.cku.sms.ZtSmsUtil;
import com.cku.util.PageBean;
import com.cku.util.ServletUtils;
import com.sys.model.SysUser;
import com.sys.service.MessageServiceImpl;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * Created by user on 2016/5/30.
 */
@Controller
@RequestMapping("message")
public class MessageController {
    private static final Logger logger = Logger.getLogger(MessageController.class);

    @Autowired
    private MessageServiceImpl messageService;
    @RequestMapping("/getList")
    public ModelAndView getList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
        Integer type = ServletUtils.getParameterInt(request,"type",null);
        if(type!=null && type==-1){
            type=null;
        }
        String title = ServletUtils.getParameter(request, "title",null);
        PageBean pb = ServletUtils.getParameterBean(request);
        PageBeanResult<CabMymessage> s = messageService.getList(type,title,pb);
        modelMap.addAttribute("pageList",s);
        SysUser user = (SysUser)request.getSession().getAttribute("user");
        modelMap.addAttribute("user",user);
        ModelAndView mv = new ModelAndView("sys/message/messageList",modelMap);
        return mv;
    }
    @RequestMapping("/toUpdate")
    public ModelAndView toUpdate(HttpServletRequest request,HttpServletResponse response){
        String type = ServletUtils.getParameter(request,"type",null);
        Integer id = ServletUtils.getParameterInt(request,"id",null);
        CabMymessage message = null;
        if("add".equals(type)){
            message = new CabMymessage();
            message.setCreateTime(new Date());
        }else{
            message = messageService.getMessageById(id);
        }
        ModelAndView result = new ModelAndView("sys/message/updateMessage");
        result.addObject("item",message);
        return result;
    }
    @RequestMapping("/changeStatus")
    @ResponseBody
    public void changeStatus(HttpServletRequest request,HttpServletResponse response) throws IOException {
        RESTResponse result = null;
        try{
            Integer id = ServletUtils.getParameterInt(request,"id",null);
            int i = messageService.changeStatus(id);
            if(i>0){
                result = new RESTResponse(0,"审核成功！");
            }else{
                result = new RESTResponse(1,"审核失败！");
            }
        }catch (Exception e){
            result = new RESTResponse(e);
            logger.error(e);
        }
        ServletUtils.writeResponse(response, result);
    }
    @RequestMapping("deleteMessage")
    @ResponseBody
    public void deleteMessage(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap) throws IOException {
        RESTResponse result = null;
        try{
            Integer id = ServletUtils.getParameterInt(request,"id",null);
            int i = messageService.deleteMessage(id);
            if(i>0){
                result = new RESTResponse(0,"删除推荐消息成功！");
            }else{
                result = new RESTResponse(1,"删除推荐消息失败！");
            }
        }catch (Exception e){
            result = new RESTResponse(e);
            logger.error(e);
        }
        ServletUtils.writeResponse(response, result);
    }
    @RequestMapping("addMessage")
    public ModelAndView addMessage(HttpServletRequest request,HttpServletResponse response,CabMymessage message,ModelMap modelMap){
        SysUser sysUser = (SysUser) request.getSession().getAttribute("user");
        int i = messageService.addMessage(sysUser.getUserId(),message);
        String result = i>0?"success":"error";
        modelMap.addAttribute("toPage","message/getList.do");
        modelMap.addAttribute("message",i>0?"添加推荐消息成功!":"添加推荐消息失败!");
        ModelAndView mv = new ModelAndView(result,modelMap);
        return mv;
    }
    @RequestMapping("updateMessage")
    public ModelAndView updateMessage(HttpServletRequest request,HttpServletResponse response,CabMymessage message,ModelMap modelMap){
        SysUser sysUser = (SysUser) request.getSession().getAttribute("user");

        int i = messageService.updateMessage(sysUser.getUserId(),message);
        String result = i>0?"success":"error";
        modelMap.addAttribute("toPage","message/getList.do");
        modelMap.addAttribute("message",i>0?"修改推荐消息成功!":"修改推荐消息失败!");
        ModelAndView mv = new ModelAndView(result,modelMap);
        return mv;
    }
    @RequestMapping("/sendMessage")
    @ResponseBody
    public void sendMessage(HttpServletRequest request,HttpServletResponse response) throws IOException {
        RESTResponse result = null;
        try{
            String content = ServletUtils.getParameter(request,"content",null);
            Set<String> set = new HashSet<String>();
            set.add("13718355909");
            for(String phone:set){
//                ZtSmsCode.sendNote(content,phone);
                ZtSmsUtil.sendNote(content, phone);
            }
            result = new RESTResponse(0,"群发短信成功！");
        }catch (Exception e){
            result = new RESTResponse(e);
            logger.error(e);
        }
        ServletUtils.writeResponse(response, result);

    }
}
