package com.huiju.estateagents.property.controller;


import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.entity.TaUser;
import com.huiju.estateagents.property.common.Constant;
import com.huiju.estateagents.property.common.UserElement;
import com.huiju.estateagents.property.service.IMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * <p>
 * 物业端消息表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-03-05
 */
@RestController
@RequestMapping("/api")
@Api(value = "消息通知API", tags = "消息通知API")
public class MessageController extends BaseController {

    @Autowired
    private IMessageService messageService;
    // @ApiOperation(value = "消息待办", notes = "消息待办")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "messageType:待办消息类型(-1:论坛，-2:工单，-3:缴费),status:(0:无效，1:有效)，createDate：时间，messageContent:内容 ")
    })

    @RequestMapping(value = "/messageLiset", method = RequestMethod.POST)
    public ResponseBean messageLiset(@RequestBody String parameter, HttpServletRequest request){
        TaUser userElement = getTaUser(request);
        ResponseBean  responseBean = messageService.messageLiset(parameter,userElement.getOrgId(),userElement.getUserId());
        return responseBean;
    }

    // @ApiOperation(value = "我的消息", notes = "我的消息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "messageType:消息类型(-1:论坛，-2:工单，-3:缴费),readStatus:(0:未读，1:已读)，createDate：时间，messageContent:内容 ")
    })

    @RequestMapping(value = "/myMessageList", method = RequestMethod.POST)
    public ResponseBean myMessageList(@RequestBody String parameter, HttpServletRequest request){
        TaUser userElement = getTaUser(request);
        ResponseBean  responseBean = messageService.myMessageList(parameter,userElement.getOrgId(),userElement.getUserId());
        return responseBean;
    }

    // @ApiOperation(value = "消息已读", notes = "消息已读")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "id:(多个) ")
    })

    @RequestMapping(value = "/delectRead", method = RequestMethod.POST)
    public ResponseBean delectRead(@RequestBody String parameter, HttpServletRequest request){
        TaUser userElement = getTaUser(request);
        ResponseBean  responseBean = messageService.delectRead(parameter,userElement.getUserId());
        return responseBean;
    }


    @ApiOperation(value = "按小区获取消息总数", notes = "按小区获取个人消息总数")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "String", name = "orgId", value = "小区Id")})
    @RequestMapping(value = "/wx/message/{orgId}/total",method = RequestMethod.GET)
    public ResponseBean getMessageTotal(@PathVariable Integer orgId, HttpServletRequest request){
        TaUser userElement = getTaUser(request);
        ResponseBean responseBean = new ResponseBean();
        Map<String,Object> totalMap = messageService.getMessageTotal(orgId, userElement.getUserId());
        responseBean.addSuccess(totalMap);
        return responseBean;
    }

    @ApiOperation(value = "按小区获取分页消息", notes = "按小区获取分页消息")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "String", name = "orgId", value = "小区Id"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageNum", value = "分页第几页"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageSize", value = "分页每页长度")})
    @RequestMapping(value = "/wx/message/{orgId}/news",method = RequestMethod.GET)
    public ResponseBean getNews(@PathVariable Integer orgId,
                                @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                @RequestParam(value ="pageSize",defaultValue = "5") Integer pageSize, HttpServletRequest request){
        TaUser userElement = getTaUser(request);
        ResponseBean responseBean = new ResponseBean();

        Map<String,Object> map = messageService.getMessages(orgId,pageNum,pageSize, Constant.MODEL_TYPE_NEWS, userElement.getUserId());
        responseBean.addSuccess(map);
        return responseBean;
    }

    @ApiOperation(value = "按小区获取分页待办", notes = "按小区获取分页待办")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "String", name = "orgId", value = "小区Id"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageNum", value = "分页第几页"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageSize", value = "分页每页长度")})
    @RequestMapping(value = "/wx/message/{orgId}/upcoming",method = RequestMethod.GET)
    public ResponseBean getUpcoming(@PathVariable Integer orgId,
                                    @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                    @RequestParam(value ="pageSize",defaultValue = "5") Integer pageSize, HttpServletRequest request){
        TaUser userElement = getTaUser(request);
        Integer userId = userElement.getUserId();
        ResponseBean responseBean = new ResponseBean();

        Map<String,Object> map = messageService.getMessages(orgId,pageNum,pageSize,Constant.MODEL_TYPE_UPCOMING, userId);
        responseBean.addSuccess(map);
        return responseBean;
    }

    @ApiOperation(value = "消息已读接口", notes = "消息已读接口")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "String", name = "orgId", value = "小区Id"),
            })
    @RequestMapping(value = "/wx/readStatus/{orgId}",method = RequestMethod.POST)
    public ResponseBean allMessageRead(@PathVariable("orgId") Integer orgId, HttpServletRequest request){
        TaUser userElement = getTaUser(request);
        Integer userId = userElement.getUserId();
        ResponseBean responseBean = new ResponseBean();

        messageService.updateMessageReadStatus(orgId, userId);
        return responseBean;
    }


    @ApiOperation(value = "根据消息id进行已读", notes = "根据消息id进行已读")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "String", name = "messageId", value = "消息id"),
            })
    @RequestMapping(value = "/wx/messageIdreadStatus/{messageId}",method = RequestMethod.PUT)
    public ResponseBean updateMessageByIdReadStatus(HttpServletRequest request, @PathVariable("messageId") Integer messageId) {
        ResponseBean responseBean = new ResponseBean();
        TaUser userElement = getTaUser(request);
        responseBean = messageService.updateMessageByIdReadStatus(userElement, messageId);
        return responseBean;
    }

    @ApiOperation(value = "根据消息id获取详细数据", notes = "根据消息id获取详细数据")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "String", name = "messageId", value = "消息id"),
            })
    @RequestMapping(value = "/wx/message/get/{messageId}",method = RequestMethod.GET)
    public ResponseBean getMessageById(HttpServletRequest request, @PathVariable("messageId") Integer messageId) {
        ResponseBean responseBean = new ResponseBean();
        TaUser userElement = getTaUser(request);
        responseBean = messageService.getMessageById(userElement.getOrgId(), messageId);
        return responseBean;
    }
}


