package com.yonge.cooleshow.teacher.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yonge.cooleshow.biz.dal.entity.ImUserFriend;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.enums.MK;
import com.yonge.cooleshow.biz.dal.service.ImGroupService;
import com.yonge.cooleshow.biz.dal.service.ImUserFriendService;
import com.yonge.cooleshow.biz.dal.service.SysUserService;
import com.yonge.cooleshow.biz.dal.vo.im.ImUserFriendVO;
import com.yonge.cooleshow.biz.dal.wrapper.im.ImUserWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.MessageFormat;
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 SysUserService sysUserService;

    @Autowired
    private ImGroupService imGroupService;

    @SuppressWarnings("unchecked")//因为返回是集合，这里提示你集合内容较多排序可能回耗费性能
    @ApiImplicitParams({
            @ApiImplicitParam(name = "search", dataType = "String", value = "根据用户编号、昵称模糊查询")
    })
    @ApiOperation("获取通讯录成员列表")
    @PostMapping(value = "/queryAll")
    public HttpResponseResult<List<ImUserWrapper.ImUserFriend>> queryAll(@RequestBody Map<String, Object> params) {

        // 用户ID
        Long userId = sysUserService.getUserId();

        // 学生好友列表
        List<ImUserWrapper.ImUserFriend> userFriends = imUserFriendService.findUserAllImFriendInfo(ClientEnum.TEACHER, userId, params);

        /*Object search = params.get("search");
        List<ImUserFriend> record = imUserFriendService.list(Wrappers.<ImUserFriend>query().lambda()
                .eq(ImUserFriend::getUserId, sysUserService.getUserId())
                .and(Objects.nonNull(search) && StringUtils.isNotEmpty(search.toString()),
                        e -> e.eq(ImUserFriend::getFriendId, search)
                                .or().like(ImUserFriend::getFriendNickname, search))
                .orderByDesc(ImUserFriend::getId));

        for (ImUserFriend item : record) {

            // 老师端添加好友，可能为老师客服或者学生
            item.clientType(ClientEnum.STUDENT)
                    .setImFriendId(MessageFormat.format("{0}:{1}", String.valueOf(item.getFriendId()), ClientEnum.STUDENT.name()));
        }*/

        return succeed(userFriends);
    }

    @ApiOperation("获取好友详情")
    @PostMapping(value = "/getDetail/{userId}")
    public HttpResponseResult<ImUserFriendVO.ImUserFriend> getDetail(@ApiParam(value = "用户编号", required = true) @PathVariable("userId") String userId) {

        String ret = imGroupService.analysisImUserId(userId);
        if (!ret.matches(MK.EXP_INT)) {
            return failed("无效的用户ID");
        }

        ImUserFriend userFriend = imUserFriendService.getDetail(userId, ClientEnum.TEACHER);
        if (Objects.isNull(userFriend)) {
            return failed("当前好友不存在");
        }

        if (Objects.isNull(userFriend.getFriendType())) {
            userFriend.setFriendType(ClientEnum.STUDENT);
        }

        return succeed(ImUserFriendVO.ImUserFriend.from(JSON.toJSONString(userFriend)));
    }

}

