package com.yonge.cooleshow.student.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yonge.cooleshow.biz.dal.entity.ImGroupMember;
import com.yonge.cooleshow.biz.dal.service.ImGroupMemberService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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 javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 即时通讯群组(ImGroupMember)表控制层
 *
 * @author zx
 * @since 2022-03-22 10:45:56
 */
@Api(tags = "即时通讯群组联系人")
@RestController
@RequestMapping("/imGroupMember")
public class ImGroupMemberController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private ImGroupMemberService imGroupMemberService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", dataType = "Long", value = "群编号",required = true),
            @ApiImplicitParam(name = "userId", dataType = "Long", value = "用户编号",required = true),
    })
    @ApiOperation("获取好友详情")
    @PostMapping(value = "/getUserDetail")
    public HttpResponseResult<ImGroupMember> getUserDetail(@RequestBody Map<String,Object> params) throws Exception {
        if(Objects.isNull(params.get("groupId")) || Objects.isNull(params.get("userId"))){
            throw new BizException("参数校验失败");
        }
        return succeed(imGroupMemberService.getUserDetail(Long.parseLong(params.get("userId").toString()),
                params.get("groupId").toString()));
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "search", dataType = "String", value = "根据用户编号、昵称模糊查询"),
            @ApiImplicitParam(name = "groupId", dataType = "Long", value = "群编号")
    })
    @ApiOperation("获取群成员列表")
    @PostMapping(value = "/queryAll")
    public HttpResponseResult<List<ImGroupMember>> queryAll(@RequestBody Map<String,Object> params) throws Exception {
        if(Objects.isNull(params.get("groupId"))){
            throw new BizException("参数校验失败");
        }
        Object search = params.get("search");
        List<ImGroupMember> iPage = imGroupMemberService.getBaseMapper().selectList(Wrappers.<ImGroupMember>query().lambda()
                .and(Objects.nonNull(search) && StringUtils.isNotEmpty(search.toString()),
                        e->e.eq(ImGroupMember::getUserId, search).or()
                        .like(ImGroupMember::getNickname, search))
                .eq(ImGroupMember::getGroupId,params.get("groupId")).orderByDesc(ImGroupMember::getId));
        return succeed(iPage);
    }
}

