package com.yonge.cooleshow.student.controller;


import com.alibaba.fastjson.JSON;
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.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.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.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
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 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("${app-config.url.student:}/imUserFriend")
public class ImUserFriendController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private ImUserFriendService imUserFriendService;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private ImGroupService imGroupService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "search", dataType = "String", value = "根据用户编号、昵称模糊查询")
    })
    @ApiOperation("获取通讯录成员列表")
    @PostMapping(value = "/queryAll")
    public HttpResponseResult<List<ImUserWrapper.ImUserFriend>> queryAll(@RequestBody Map<String,Object> params) throws Exception {

        // 用户ID
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (Objects.isNull(sysUser)) {
            return failed("请登录");
        }

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

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

        for (ImUserFriend item : userFriends) {
            // 学生目前添加好友都为老师
            item.clientType(ClientEnum.TEACHER)
                    .setImFriendId(MessageFormat.format("{0}", String.valueOf(item.getFriendId())));
        }*/

        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.STUDENT);
        if (Objects.isNull(userFriend)) {
            return failed("当前好友不存在");
        }

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

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

