package com.yonge.cooleshow.tenant.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.microsvc.toolkit.common.response.template.R;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.TenantStaff;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.service.TenantStaffService;
import com.yonge.cooleshow.common.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@RequestMapping("${app-config.url.tenant:}/tenantStaff")
@Api(value = "机构员工表", tags = "机构员工信息")
public class TenantStaffController extends BaseController {

    @Resource
    SysUserFeignService sysUserFeignService;

    @Autowired
    private TenantStaffService tenantStaffService;

    @PostMapping("/logout")
    @ApiOperation(value = "退出登录", notes = "传入TenantInfo")
    public R<JSONObject> logout() {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return R.defaultR();
        }
        // 清理token和WXOpenId
        TenantStaff tenantStaff = tenantStaffService.getByUserId(sysUser.getId());
        if (tenantStaff != null) {
            sysUserFeignService.exitByPhoneAndOpenId(ClientEnum.ORGANIZATION.getCode().toLowerCase(),
                    sysUser.getPhone(), tenantStaff.getWxOpenid());
            // 清除WXOpenId
            tenantStaffService.update(null, Wrappers.<TenantStaff>lambdaUpdate()
                    .set(TenantStaff::getWxOpenid, null)
                    .eq(TenantStaff::getUserId, sysUser.getId()));
        }
        return R.defaultR();
    }
}
