package com.yonge.cooleshow.teacher.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.ImUserFriend;
import com.yonge.cooleshow.biz.dal.service.ImUserFriendService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 用户通讯录表(ImUserFriend)表控制层
 *
 * @author zx
 * @since 2022-03-22 10:45:59
 */
@Api(tags = "用户通讯录表")
@RestController
@RequestMapping("/imUserFriend")
public class ImUserFriendController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private ImUserFriendService imUserFriendService;
    @Resource
    private SysUserFeignService sysUserFeignService;

    @SuppressWarnings("unchecked")//因为返回是集合，这里提示你集合内容较多排序可能回耗费性能
    @ApiImplicitParams({
            @ApiImplicitParam(name = "search", dataType = "String", value = "根据用户编号、昵称模糊查询")
    })
    @ApiOperation("获取通讯录成员列表")
    @PostMapping(value = "/queryAll")
    public HttpResponseResult<List<ImUserFriend>> queryAll(@RequestBody Map<String, Object> params) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        Object search = params.get("search");
        List<ImUserFriend> record = imUserFriendService.list(Wrappers.<ImUserFriend>query().lambda()
                .eq(ImUserFriend::getUserId, sysUser.getId())
                .and(Objects.nonNull(search) && StringUtils.isNotEmpty(search.toString()),
                        e -> e.eq(ImUserFriend::getFriendId, search)
                                .or().like(ImUserFriend::getFriendNickname, search))
                .orderByDesc(ImUserFriend::getId));
        return succeed(record);
    }

    @ApiOperation("获取好友详情")
    @PostMapping(value = "/getDetail/{userId}")
    public HttpResponseResult<ImUserFriend> getDetail(@ApiParam(value = "用户编号", required = true) @PathVariable("userId") Long userId) throws Exception {
        return succeed(imUserFriendService.getDetail(userId));
    }
}

