package com.yonge.cooleshow.admin.controller;

import com.google.common.collect.Lists;
import com.yonge.cooleshow.api.feign.dto.ApiWrapper;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.service.SysUserService;
import com.yonge.cooleshow.biz.dal.service.im.ImGroupCoreService;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("${app-config.url.admin:}/sysUser")
@Api(value = "用户表", tags = "用户表")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private ImGroupCoreService imGroupCoreService;

    /**
     * 账号注销校验
     */
    @PostMapping("/logoffCheck")
    HttpResponseResult<List<ApiWrapper.LogOff>> accountLogoffCheck(@RequestParam("phone") String phone) {
        List<ApiWrapper.LogOff> logOffs = sysUserService.accountLogoffCheck(phone);
        if (!CollectionUtils.isEmpty(logOffs)) {
            return HttpResponseResult.succeed(logOffs);
        }
        return HttpResponseResult.succeed();
    }

    @PostMapping("/logoffQuitImGroup")
    HttpResponseResult<Boolean> logoffQuitImGroup(@RequestParam("phone") String phone){
        sysUserService.logoffQuitImGroup(phone);
        return HttpResponseResult.succeed(true);
    }

    @ApiOperation(value = "用户注销账号校验")
    @GetMapping(value = "/logoffUser/{id}")
    public HttpResponseResult<Void> logoffUser(@PathVariable("id") Long id) {
        SysUser sysUser = sysUserFeignService.queryUserById(id);
        if (sysUser == null) {
            return failed("用户不存在");
        }
        return sysUserFeignService.logoffUser(id);
    }
}
