package com.jd.lightapplication.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.jd.lightapplication.common.lang.R;
import com.jd.lightapplication.easemob.EasemobComtroller;
import com.jd.lightapplication.easemob.dto.CreateGroupDto;
import com.jd.lightapplication.model.MeetingInfo;
import com.jd.lightapplication.model.dto.MeetingInfoQueryDto;
import com.jd.lightapplication.service.MeetingInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.annotation.Resource;
import java.util.ArrayList;


@Slf4j
@RestController()
@RequestMapping("/api/meeting/info")
@Api(tags = "会议信息表")
public class MeetingInfoController {

    @Resource
    private MeetingInfoService meetingInfoService;
    @Resource
    private EasemobComtroller easemobComtroller;

    @GetMapping("/list")
    @ApiOperation(value = "1、分页查询")
    public R queryPageBean(Page page, MeetingInfoQueryDto meetingInfoQueryDto) {
        return R.success(meetingInfoService.page(page, meetingInfoQueryDto));
    }

    @PostMapping("/addBean")
    @ApiOperation(value = "2、新增")
    public R addBean(@RequestBody MeetingInfo meetingInfo) {
        //添加这次会议的群组
        CreateGroupDto createGroupDto = new CreateGroupDto()
                .setGroupname("【" + meetingInfo.getTitle() + "】群组")
                .setDesc(meetingInfo.getTitle() + "添加的默认群组")
                .setIspublic(true)
                .setMaxusers(1000)
                .setApproval(true)
                .setOwner("admin")
                .setMembers(new ArrayList<>());
        R result = easemobComtroller.createGroup(createGroupDto);
        if (((JSONObject) result.getData()).get("data") == null) {
            log.error("新建会议的群聊失败，会议信息：" + meetingInfo);
        }
        //新建会议
        meetingInfo.setStatus(10)
                .setCreateTime(DateUtil.now());
        meetingInfoService.save(meetingInfo);
        return R.success();
    }

    @PostMapping("/updateBean")
    @ApiOperation(value = "3、修改")
    public R updateBean(@RequestBody MeetingInfo meetingInfo) {
        meetingInfo.setUpdateTime(DateUtil.now());
        meetingInfoService.updateById(meetingInfo);
        return R.success();
    }

    @PostMapping("/deleteById")
    @ApiOperation(value = "4、删除")
    public R deleteById(long id) {
        meetingInfoService.removeById(id);
        return R.success();
    }

    @GetMapping("/getNextMeeting")
    @ApiOperation(value = "5、查询下一场会议")
    public R getNextMeeting() {
        return R.success(meetingInfoService.getNextMeeting());
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "6、修改会议状态")
    public R updateStatus(Long meetingId, Integer status) {
        return R.success(meetingInfoService.updateStatus(meetingId, status));
    }

    @GetMapping("/getByIdNotAuth")
    @ApiOperation(value = "7、根据id查询会议详情[无权限]")
    public R getNextMeeting(Long id) {
        return R.success(meetingInfoService.getById(id));
    }

}