package com.yonge.cooleshow.admin.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.service.EmployeeService;
import com.yonge.cooleshow.biz.dal.service.TenantUnbindRecordService;
import com.yonge.cooleshow.biz.dal.vo.EmployeeVo;
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.security.access.prepost.PreAuthorize;
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 javax.annotation.Resource;


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

    @Autowired
    private TenantUnbindRecordService tenantUnbindRecordService;

    @Resource
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private EmployeeService employeeService;

    @ApiOperation(value = "查询分页", notes = "机构解绑申请记录- 传入 TenantUnbindRecordVo.TenantUnbindRecordQuery")
    @PreAuthorize("@pcs.hasPermissions('tenantUnbindRecord/page')")
    @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")
    @PreAuthorize("@pcs.hasPermissions('tenantUnbindRecord/audit')")
    public HttpResponseResult<Boolean> audit(@RequestBody TenantUnbindRecordWrapper.Audio audio) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            throw new BizException("请登录");
        }
        EmployeeVo employeeVo = employeeService.detail(sysUserFeignService.queryUserInfo().getId());
        if (employeeVo == null || UserLockFlag.LOCKED.equals(employeeVo.getLockFlag())) {
            throw new BizException("权限不足");
        }
        tenantUnbindRecordService.tenantUserUnbindAudit(audio, sysUser.getId(),true);
        return succeed();
    }
}