package com.yonge.cooleshow.tenant.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.microsvc.toolkit.common.response.paging.PageInfo;
import com.microsvc.toolkit.common.response.paging.QueryInfo;
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.service.TenantStaffService;
import com.yonge.cooleshow.biz.dal.service.TenantUnbindRecordService;
import com.yonge.cooleshow.biz.dal.wrapper.TenantUnbindRecordWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.common.enums.UserLockFlag;
import com.yonge.toolset.base.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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;


@Slf4j
@Validated
@RestController
@RequestMapping("${app-config.url.tenant:}/tenantUnbindRecord")
@Api(tags = "机构解绑申请记录")
public class TenantUnbindRecordController extends BaseController {

    @Autowired
    private TenantUnbindRecordService tenantUnbindRecordService;

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private TenantStaffService tenantStaffService;


    @ApiOperation(value = "查询分页", notes = "机构解绑申请记录- 传入 TenantUnbindRecordVo.TenantUnbindRecordQuery")
    @PostMapping("/page")
    public HttpResponseResult<PageInfo<TenantUnbindRecordWrapper.TenantUnbindRecord>>
    page(@RequestBody TenantUnbindRecordWrapper.TenantUnbindRecordQuery query) {
        // 查询数据
        IPage<TenantUnbindRecordWrapper.TenantUnbindRecord> pages =
                tenantUnbindRecordService.selectPage(QueryInfo.getPage(query), query);
        return succeed(QueryInfo.pageInfo(pages, pages.getRecords()));
    }

    @ApiOperation(value = "审核")
    @PostMapping("/audit")
    public HttpResponseResult<Boolean> audit(@RequestBody TenantUnbindRecordWrapper.Audio audio) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            throw new BizException("请登录");
        }
        Long id = sysUserFeignService.queryUserInfo().getId();
        TenantStaff employeeVo = tenantStaffService.lambdaQuery().eq(TenantStaff::getUserId, id)
                .last("limit 1").one();
        if (employeeVo == null || UserLockFlag.LOCKED.equals(employeeVo.getStatus())) {
            throw new BizException("权限不足");
        }
        tenantUnbindRecordService.tenantUserUnbindAudit(audio, sysUser.getId(), false);
        return succeed();
    }

    @PostMapping("/unbindTenant")
    @ApiOperation(value = "机构解绑")
    public HttpResponseResult<Boolean> unbindTenant(@Validated @RequestBody TenantUnbindRecordWrapper.Unbind unbind) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            throw new BizException("请登录");
        }
        tenantUnbindRecordService.unbindTenant(sysUser.getTenantId(), sysUser.getId(), unbind);
        return succeed();

    }


}