package com.xunwang.ucenter.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xunwang.commonutils.JwtUtils;
import com.xunwang.commonutils.R;
import com.xunwang.ucenter.entity.InviteInfo;
import com.xunwang.ucenter.service.InviteInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Guan
 * @since 2023-01-03
 */
@RestController
@RequestMapping("/ucenter/invite-info")
public class InviteInfoController {

    @Autowired
    private InviteInfoService inviteInfoService;


    @ApiOperation("获取已读的邀请list")
    @GetMapping("/getSendedInvitationRead")
    public R getSendedInvitation(HttpServletRequest request){
        String id = JwtUtils.getUIdByJwtToken(request);
        QueryWrapper<InviteInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("from_uid",id);
        wrapper.eq("read_status","已读");
        List<InviteInfo> list = inviteInfoService.list(wrapper);
        return R.ok().data("list",list);
    }


    @ApiOperation("获取未读的邀请list")
    @GetMapping("/getSendedInvitationUnread")
    public R getSendedInvitationUnread(HttpServletRequest request){
        String id = JwtUtils.getUIdByJwtToken(request);
        QueryWrapper<InviteInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("from_uid",id);
        wrapper.eq("read_status","未读");
        List<InviteInfo> list = inviteInfoService.list(wrapper);
        return R.ok().data("list",list);
    }

    @ApiOperation("我收到的邀请list")
    @GetMapping("/getReceivedInvitationRead")
    public R getReceivedInvitationRead(HttpServletRequest request){
        String id = JwtUtils.getUIdByJwtToken(request);
        QueryWrapper<InviteInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("to_uid",id);
        wrapper.eq("read_status","已读");
        List<InviteInfo> list = inviteInfoService.list(wrapper);
        return R.ok().data("list",list);
    }

    @ApiOperation("我收到的邀请list")
    @GetMapping("/getReceivedInvitationUnread")
    public R getReceivedInvitationUnread(HttpServletRequest request){
        String id = JwtUtils.getUIdByJwtToken(request);
        QueryWrapper<InviteInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("to_uid",id);
        wrapper.eq("read_status","未读");
        List<InviteInfo> list = inviteInfoService.list(wrapper);
        return R.ok().data("list",list);
    }


    //点击进入我收到的邀请详情，改变我方状态从未读变为待定，如果状态为非未读状态则不做修改
    @ApiOperation("修改我方操作状态为待定,阅读状态为已读")
    @GetMapping("/modifyStatusToHold")
    public R modifyStatusToHold(HttpServletRequest request){
        String require_id = request.getParameter("requireId");
        InviteInfo inviteInfo = new InviteInfo();
        inviteInfo.setReadStatus("已读");
        QueryWrapper<InviteInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("require_id",require_id);
        //修改阅读状态
        inviteInfoService.update(inviteInfo,wrapper);
        //修改操作状态
        inviteInfoService.modifyStatusToHold(require_id);
        return R.ok();
    }

    @ApiOperation("拒绝邀请")
    @GetMapping("/refuse")
    public R refuse(HttpServletRequest request){
        String require_id = request.getParameter("requireId");
        InviteInfo inviteInfo = new InviteInfo();
        inviteInfo.setOperateStatus("拒绝");
        QueryWrapper<InviteInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("require_id",require_id);
        inviteInfoService.update(inviteInfo,wrapper);
        return R.ok();
    }



}

