package com.yonge.cooleshow.teacher.controller;


import com.yonge.cooleshow.biz.dal.dto.ImGroupResultDto;
import com.yonge.cooleshow.biz.dal.dto.ImGroupSearchDto;
import com.yonge.cooleshow.biz.dal.entity.ImGroup;
import com.yonge.cooleshow.biz.dal.service.ImGroupService;
import com.yonge.cooleshow.biz.dal.service.SysUserService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.utils.validator.ValidationKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 即时通讯群组(ImGroup)表控制层
 *
 * @author zx
 * @since 2022-03-22 10:45:56
 */
@Api(tags = "即时通讯群组")
@RestController
@RequestMapping("/imGroup")
public class ImGroupController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private ImGroupService imGroupService;
    @Resource
    private SysUserService sysUserService;

    @ApiOperation("创建群聊")
    @PostMapping(value = "/create")
    public HttpResponseResult create(@Valid @RequestBody ImGroup imGroup, BindingResult bindingResult) throws Exception {
        ValidationKit.ignoreFields(bindingResult,"id");
        imGroup.setCreateBy(sysUserService.getUserId());
        imGroupService.create(imGroup);
        return succeed();
    }

    @ApiOperation("解散群聊")
    @PostMapping(value = "/dismiss/{groupId}")
    public HttpResponseResult dismiss(@ApiParam(value = "群编号", required = true) @PathVariable("groupId") String groupId) throws Exception {
        imGroupService.dismiss(groupId);
        return succeed();
    }

    @ApiOperation("退出群聊")
    @PostMapping(value = "/quit/{groupId}")
    public HttpResponseResult quit(@ApiParam(value = "群编号", required = true) @PathVariable("groupId") String groupId) throws Exception {
        imGroupService.quit(groupId,sysUserService.getUserId());
        return succeed();
    }

    @ApiOperation("获取群详情")
    @PostMapping(value = "/getDetail/{groupId}")
    public HttpResponseResult<ImGroup> getDetail(@ApiParam(value = "群编号", required = true) @PathVariable("groupId") String groupId) throws Exception {
        ImGroup group = imGroupService.getById(groupId);
        if (group == null) {
            return failed(HttpStatus.NO_CONTENT, "群组不存在");
        }
        return succeed(group);
    }

    @ApiOperation("群列表")
    @PostMapping(value = "/queryAll")
    public HttpResponseResult<List<ImGroup>> queryAll(@Valid @RequestBody ImGroupSearchDto imGroupSearchDto, BindingResult bindingResult) throws Exception {
        ValidationKit.ignoreFields(bindingResult,"createUserId");
        imGroupSearchDto.setUserId(sysUserService.getUserId());
        return succeed(imGroupService.queryAll(imGroupSearchDto));
    }

    @ApiOperation("修改群信息")
    @PostMapping(value = "/update")
    public HttpResponseResult<Boolean> update(@Valid @RequestBody ImGroup imGroup) throws Exception {
        return succeed(imGroupService.updateById(imGroup));
    }

    @ApiOperation("获取指定用户的群列表")
    @PostMapping(value = "/queryTeacherGroup")
    public HttpResponseResult<List<ImGroupResultDto>> queryTeacherGroup(@RequestBody ImGroupSearchDto imGroupSearchDto) throws Exception {
        if (StringUtils.isEmpty(imGroupSearchDto.getType())) {
            imGroupSearchDto.setType("FAN");
        }
        return succeed(imGroupService.queryTeacherFun(imGroupSearchDto));
    }
}

