package com.yonge.cooleshow.student.controller;


import com.alibaba.fastjson.JSONObject;
import com.microsvc.toolkit.common.response.template.R;
import com.microsvc.toolkit.common.webportal.exception.BizException;
import com.microsvc.toolkit.config.jwt.utils.JwtUserInfo;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.ImUserStateSync;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.service.LiveRoomService;
import com.yonge.cooleshow.biz.dal.service.SysUserService;
import com.yonge.cooleshow.biz.dal.wrapper.UserInfoWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
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 springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 用户通讯录表(ImUserFriend)表控制层
 *
 * @author zx
 * @since 2022-03-22 10:45:59
 */
@Api(tags = "用户通讯录表")
@RestController
@Slf4j
@RequestMapping("${app-config.url.student:}/user")
public class UserController extends BaseController {

    @Autowired
    private LiveRoomService liveRoomService;

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 同步融云用户状态变更
     *
     * @param userState
     */
    @PostMapping(value = "/statusImUser")
    public void statusImUser(@RequestBody List<ImUserStateSync> userState) {
        log.info("opsRoom >>>>> : {}", JSONObject.toJSONString(userState));
        liveRoomService.opsRoom(userState);
    }



    @ApiOperation(value = "更新更新IM用户信息", notes = "更新用户信息")
    @PostMapping("/updateImUserInfo")
    public HttpResponseResult<JSONObject> updateImUserInfo(@Validated @RequestBody UserInfoWrapper.UpdateUser info) {

        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        info.setUserId(user.getId().intValue());
        info.setClient(ClientEnum.STUDENT);


        sysUserService.imDeviceId(info);
        return succeed();
    }

}

