package com.ym.mec.teacher.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.ImGroup;
import com.ym.mec.biz.dal.entity.ImSendGroupMessage;
import com.ym.mec.biz.dal.page.ImGroupQueryInfo;
import com.ym.mec.biz.dal.page.ImSendGroupMessageQueryInfo;
import com.ym.mec.biz.service.ImGroupService;
import com.ym.mec.biz.service.ImSendGroupMessageService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@Api(tags = "群消息定时发送")
@RequestMapping("imSendGroupMessage")
@RestController
public class ImSendGroupMessageController extends BaseController {

    @Autowired
    private ImSendGroupMessageService imSendGroupMessageService;
    @Autowired
    private ImGroupService imGroupService;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation("获取群消息列表")
    @PostMapping(value = "/queryPage")
    public HttpResponseResult<PageInfo<ImSendGroupMessage>> queryPage(ImSendGroupMessageQueryInfo queryInfo) throws Exception {
        SysUser sysUser = Optional.ofNullable(sysUserFeignService.queryUserInfo()).
                orElseThrow(() -> new Exception("请登录"));
        queryInfo.setSenderId(sysUser.getId().toString());
        return succeed(imSendGroupMessageService.queryPage(queryInfo));
    }

    @ApiOperation("发送群聊消息")
    @PostMapping(value = "/send")
    public HttpResponseResult send(@RequestBody ImSendGroupMessage imSendGroupMessage) throws Exception {
        imSendGroupMessageService.sendGroupMessage(imSendGroupMessage);
        return succeed();
    }

    @ApiOperation("修改未发送的群消息")
    @PostMapping(value = "/update")
    public HttpResponseResult update(@RequestBody ImSendGroupMessage imSendGroupMessage) throws Exception {
        imSendGroupMessageService.updateGroupMessage(imSendGroupMessage);
        return succeed();
    }

    @ApiOperation("删除未发送的群消息")
    @PostMapping(value = "/delete")
    public HttpResponseResult delete(Long id) throws Exception {
        imSendGroupMessageService.deleteGroupMessage(id);
        return succeed();
    }
    @ApiOperation("删除未发送消息的群列表")
    @PostMapping(value = "/deleteGroupList")
    public HttpResponseResult deleteGroupList(Long imSendGroupMessageId,String groupIds) throws Exception {
        Optional.ofNullable(groupIds).orElseThrow(() -> new BizException("群组id不能为空"));
        Optional.ofNullable(imSendGroupMessageId).orElseThrow(() -> new BizException("群消息id不能为空"));
        imSendGroupMessageService.deleteGroupList(imSendGroupMessageId,groupIds);
        return succeed();
    }
    @ApiOperation("添加未发送消息的群列表")
    @PostMapping(value = "/addGroupList")
    public HttpResponseResult addGroupList(Long imSendGroupMessageId,String groupIds) throws Exception {
        Optional.ofNullable(groupIds).orElseThrow(() -> new BizException("群组id不能为空"));
        Optional.ofNullable(imSendGroupMessageId).orElseThrow(() -> new BizException("群消息id不能为空"));
        imSendGroupMessageService.addGroupList(imSendGroupMessageId,groupIds);
        return succeed();
    }

    @ApiOperation("查询群列表（没有在消息列表的）")
    @PostMapping(value = "/queryGroupPage")
    public HttpResponseResult<PageInfo<ImGroup>> queryGroupPage(ImGroupQueryInfo imGroupQueryInfo) throws Exception {
        SysUser sysUser = Optional.ofNullable(sysUserFeignService.queryUserInfo()).
                orElseThrow(() -> new Exception("请登录"));
        imGroupQueryInfo.setUserId(sysUser.getId());
        if(imGroupQueryInfo.getImSendGroupMessageId() != null){
            ImSendGroupMessage imSendGroupMessage = Optional.ofNullable(imSendGroupMessageService.get(imGroupQueryInfo.getImSendGroupMessageId())).
                    orElseThrow(()->new BizException("消息不存在"));
            imGroupQueryInfo.setIgnoreIds(imSendGroupMessage.getTargetIds());
        }
        return succeed(imGroupService.queryPage(imGroupQueryInfo));
    }

    @ApiOperation("查询群列表（在消息列表的）")
    @PostMapping(value = "/queryGroupPage1")
    public HttpResponseResult<PageInfo<ImGroup>> queryGroupPage1(ImGroupQueryInfo imGroupQueryInfo) throws Exception {
        SysUser sysUser = Optional.ofNullable(sysUserFeignService.queryUserInfo()).
                orElseThrow(() -> new Exception("请登录"));
        imGroupQueryInfo.setUserId(sysUser.getId());
        if(imGroupQueryInfo.getImSendGroupMessageId() != null){
            ImSendGroupMessage imSendGroupMessage = Optional.ofNullable(imSendGroupMessageService.get(imGroupQueryInfo.getImSendGroupMessageId())).
                    orElseThrow(()->new BizException("消息不存在"));
            imGroupQueryInfo.setTargetIds(imSendGroupMessage.getTargetIds());
        }
        return succeed(imGroupService.queryPage(imGroupQueryInfo));
    }
}
