package com.xunwang.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xunwang.servicebase.utils.JwtUtils;
import com.xunwang.servicebase.utils.R;
import com.xunwang.education.entity.MpInnerMsg;
import com.xunwang.education.entity.socket.Commucation;
import com.xunwang.education.entity.socket.Type;
import com.xunwang.education.feign.PayFeign;
import com.xunwang.education.handler.ChatEndpoint;
import com.xunwang.education.service.MpInnerMsgService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Long
 * @since 2023-03-05
 */
@RestController
@RequestMapping("/education/mp-inner-msg")
public class MpInnerMsgController {

    @Resource
    private MpInnerMsgService mpInnerMsgService;

    @Resource
    private ChatEndpoint chatEndpoint;

    @Resource
    private PayFeign payFeign;

    @ApiOperation("查询个人消息未读数")
    @GetMapping("/queryUnreadCount/{uid}")
    public int queryUnreadCount(@PathVariable String uid) {
        return mpInnerMsgService.count(new LambdaQueryWrapper<MpInnerMsg>().eq(MpInnerMsg::getUid, uid).eq(MpInnerMsg::getStatus, "未读"));
    }

    @ApiOperation("个人消息总数")
    @GetMapping("/queryMsg")
    public R queryMsg(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        if (StringUtils.isAnyBlank(uid, pageNum, pageSize)) {
            return R.error().message("请求参数为空");
        }
        HashMap<String, Object> hashMap = mpInnerMsgService.selectMsgPage(uid, pageNum, pageSize);
        return R.ok().data(hashMap);
    }

    @ApiOperation("修改状态为已读")
    @GetMapping("/updateMsg")
    public R updateMsg(HttpServletRequest request) {
        String id = request.getParameter("id");
        UpdateWrapper<MpInnerMsg> wrapper = new UpdateWrapper<MpInnerMsg>().eq("id", id);
        MpInnerMsg mpInnerMsg = new MpInnerMsg().setStatus("已读");
        boolean update = mpInnerMsgService.update(mpInnerMsg, wrapper);
        return update ? R.ok() : R.error();
    }

    @ApiOperation("Feign支付完成发送消息")
    @GetMapping("/notify/paymentSuccess/{uid}/{orderId}")
    public void notifyPaymentSuccess(@PathVariable String uid, @PathVariable String orderId) throws IOException {
        // 5.向客户端(被邀请方)主动推送消息
        chatEndpoint.sendInvited(uid, orderId, Commucation.NOTICE.getMessage(), Type.PAYSUCCESS.getType());
    }

    @ApiOperation("发送申请退款消息")
    @GetMapping("/notify/refund")
    public R notifyRefund(HttpServletRequest request) throws IOException {
        String studentId = request.getParameter("studentId");
        String orderId = request.getParameter("orderId");
        boolean b = payFeign.updateIsRefund(orderId);
        if (b) {
            // 没超过5天
            chatEndpoint.sendInvited(studentId, orderId, Commucation.REFUND.getMessage(), Type.REFUND.getType());
            return R.ok().message("退款需要对方学员确认，已通知该学员，请耐心等待");
        }
        return R.ok().message("超过5天不能退款");
    }
}

