package com.yunzhi.marketing.controller;


import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.common.CommConstant;
import com.yunzhi.marketing.entity.TaChat;
import com.yunzhi.marketing.service.ITaChatService;
import com.yunzhi.marketing.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 聊天记录表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-05-10
 */
@RestController
@RequestMapping("/api")
public class TaChatController extends BaseController {
    @Autowired
    ITaChatService taChatService;

    /**
     * 获取聊天记录
     * @param chatWith
     * @param pageNumber
     * @param pageSize
     * @param request
     * @return
     */
    @GetMapping("/wx/chat/history")
    public ResponseBean getChatHistory(@RequestParam String chatWith, @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
        // 消息发送人
        String openid = getOpenId(request);

        return taChatService.getChatHistory(openid, chatWith, pageNumber, pageSize);
    }

    /**
     * 获取聊天的人员列表
     * @param request
     * @return
     */
    @GetMapping("/wx/chat/with")
    public ResponseBean getChatWithList( @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
        // 当前人员
        String openid = getOpenId(request);

        return taChatService.getChatWithList(openid, pageNumber, pageSize);
    }

    /**
     * 更新所有消息为已读
     * @param request
     * @return
     */
    @PutMapping("/wx/chat/message")
    public ResponseBean updateMessageReaded(HttpServletRequest request) {
        // 当前人员
        String openid = getOpenId(request);
        return taChatService.updateMessageReadedOf(openid);
    }

    /**
     * 更新当前消息为已读
     * @return
     */
    @PutMapping("/wx/chat/message/{chatId}")
    public ResponseBean updateMessageReadedById(@PathVariable("chatId") Integer chatId,HttpServletRequest request) {
        TaChat taChat = new TaChat();
        taChat.setChatId(chatId);
        taChat.setIsRead(CommConstant.MESSAGE_READED);
        if (taChatService.updateById(taChat)){
            return ResponseBean.success("更新成功");
        }
        return ResponseBean.error("更新失败",ResponseBean.ERROR_ILLEGAL_PARAMS);
    }
}
