package com.sifang.modules.sys.web;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.entity.CrmPlatformAnnouncement;
import com.sifang.modules.sys.service.CrmPlatformAnnouncementService;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.TrimStrMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("${adminPath}/msg/center/")
public class MessageCenterController extends DjxBaseController {

    @Autowired
    CrmPlatformAnnouncementService crmPlatformAnnouncementService;

    @RequestMapping(value = "list")
    public String list(Model model) {
        return "modules/yangguang/msgcenter/list";
    }

    @RequestMapping(value = "listData")
    @ResponseBody
    public String listData(HttpServletRequest request) {
        LayuiPage<Record> page = new LayuiPage<>(request);
        TrimStrMap parasMap = new TrimStrMap(getParasMap(request));
        parasMap.put("objType","2");
        crmPlatformAnnouncementService.listMessageData(parasMap, page);
        return page.renderJson();
    }
    
    @RequestMapping(value = "markMsgAsRead")
    @ResponseBody
    public Result<Void> markMsgAsRead(HttpServletRequest request) {
        String ids = request.getParameter("id");
        return crmPlatformAnnouncementService.markMsgAsRead(ids);
    }

    @RequestMapping(value = "showDetail")
    public String showDetail(HttpServletRequest request, Model model) {
        model.addAttribute("id", request.getParameter("id"));
        CrmPlatformAnnouncement announcement = crmPlatformAnnouncementService.get(request.getParameter("id"));
        model.addAttribute("announcement", announcement);
        model.addAttribute("hasRead", crmPlatformAnnouncementService.hasRead(request.getParameter("id")));
        return "modules/yangguang/msgcenter/show";
    }
    @RequestMapping(value="platemShowDetail")
    public String platemShowDetail(HttpServletRequest request, Model model){
        String id=request.getParameter("id");
        model.addAttribute("id", request.getParameter("id"));
        CrmPlatformAnnouncement announcement = crmPlatformAnnouncementService.get(request.getParameter("id"));
        model.addAttribute("announcement", announcement);
        model.addAttribute("hasRead", crmPlatformAnnouncementService.hasRead(request.getParameter("id")));
        //标记已读
        User user = UserUtils.getUser();
        crmPlatformAnnouncementService.markAnnounceById(id,user);
        return "modules/yangguang/msgcenter/platemShowDetail";
    }

    @RequestMapping(value="announceShowDetail")
    public String announceShowDetail(HttpServletRequest request, Model model){
        String id=request.getParameter("id");
        model.addAttribute("id", request.getParameter("id"));
        CrmPlatformAnnouncement announcement = crmPlatformAnnouncementService.get(request.getParameter("id"));
        model.addAttribute("announcement", announcement);
        model.addAttribute("hasRead", crmPlatformAnnouncementService.hasRead(request.getParameter("id")));
        //标记已读
        User user = UserUtils.getUser();
        crmPlatformAnnouncementService.markAnnounceById(id,user);
        return "modules/yangguang/msgcenter/announceShowDetail";
    }

    @RequestMapping(value="supplyShowDetail")
    public String supplyShowDetail(HttpServletRequest request, Model model){
        model.addAttribute("id", request.getParameter("id"));
        CrmPlatformAnnouncement announcement = crmPlatformAnnouncementService.get(request.getParameter("id"));
        model.addAttribute("announcement", announcement);
        model.addAttribute("hasRead", crmPlatformAnnouncementService.hasRead(request.getParameter("id")));
        return "modules/yangguang/msgcenter/supplyShowDetail";
    }

    @RequestMapping(value = "show")
    @ResponseBody
    public Object show(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        model.addAttribute("id", id);
        return crmPlatformAnnouncementService.get(id);
}

    /**
     * 记录未读消息的数量。
     */
    @RequestMapping(value = "unReadMsgCount")
    @ResponseBody
    public Result<Long> unReadMsgCount(HttpServletRequest request) {
        long unRead = crmPlatformAnnouncementService.unReadMsgCount();
        Result<Long> ret = new Result<>();
        ret.setOk();
        ret.setData(unRead);
        return ret;
    }

}
