package com.ym.mec.web.controller.education;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.service.ClassGroupService;
import com.ym.mec.biz.service.SysMessageService;
import com.ym.mec.biz.service.TeacherService;
import com.ym.mec.common.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "即时通讯相关服务")
@RequestMapping("im")
@RestController
public class ImController extends BaseController {

    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private ClassGroupService classGroupService;
    @Autowired
    private SysMessageService sysMessageService;
    @Autowired
    private TeacherService teacherService;

    @ApiOperation(value = "获取教务所有聊天群组")
    @GetMapping("/queryEmployeeGroups")
    @PreAuthorize("@pcs.hasPermissions('im/queryEmployeeGroups','system')")
    public Object queryEmployeeGroups(String search){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("获取用户信息失败");
        }
        return succeed(teacherService.queryTeacherGroups(sysUser.getId(),search));
    }

    @ApiOperation(value = "获取当前教务通讯录列表")
    @GetMapping("/queryGroupStudents")
    @PreAuthorize("@pcs.hasPermissions('im/queryGroupStudents','system')")
    public Object queryGroupStudents(String search){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("获取用户信息失败");
        }
        return succeed(teacherService.queryEducationIpa(sysUser.getId(),search));
    }

    @ApiOperation(value = "根据群编号，获取群组基本信息")
    @GetMapping("/findGroupById")
    @PreAuthorize("@pcs.hasPermissions('im/findGroupById','system')")
    public Object findGroupById(Integer groupId){
        if(null == groupId){
            return failed("参数校验错误");
        }
        return succeed(classGroupService.findGroupById(groupId));
    }

    @ApiOperation(value = "根据群编号，修改群名称")
    @GetMapping("/updateGroupName")
    public Object updateGroupName(Integer groupId,String groupName){
        if(null == groupId || StringUtils.isEmpty(groupName)){
            return failed("参数校验错误");
        }
        classGroupService.updateGroupName(groupId,groupName);
        return succeed();
    }

    @ApiOperation(value = "根据群编号，获取群组所有成员基本信息")
    @GetMapping("/findGroupUsers")
    @PreAuthorize("@pcs.hasPermissions('im/findGroupUsers','system')")
    public Object findGroupUsers(Integer groupId,String search) {
        if (groupId == null) {
            return failed("参数校验错误");
        }
        return succeed(classGroupService.findGroupUsers(groupId,search));
    }

    @ApiOperation(value = "根据群编号，获取用户群名片")
    @GetMapping("/getUserInfoWithUserId")
    public Object getUserInfoWithUserId(Integer groupId,Integer userId) {
        if (groupId == null || userId == null) {
            return failed("参数校验错误");
        }
        return succeed(classGroupService.getUserInfoWithUserId(groupId,userId));
    }

    @ApiOperation(value = "发送私聊消息")
    @GetMapping("/sendPrivateMessage")
    public Object sendPrivateMessage(String targetId,String content) {
        if (StringUtils.isEmpty(targetId) || StringUtils.isEmpty(content)) {
            return failed("参数校验错误");
        }
        sysMessageService.sendPrivateMessage(targetId,content);
        return succeed();
    }
}
