package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.TenantInfo;
import com.yonge.cooleshow.biz.dal.service.TenantApplyRecordService;
import com.yonge.cooleshow.biz.dal.service.TenantEntryRecordService;
import com.yonge.cooleshow.biz.dal.service.TenantInfoService;
import com.yonge.cooleshow.biz.dal.wrapper.TenantApplyRecordWrapper;
import com.yonge.cooleshow.biz.dal.wrapper.TenantInfoWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
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;

/**
 * @Author：haonan
 * @Date：2023/8/1 19:02
 * @Filename：TenantEntryRecordController
 */
@RestController
@RequestMapping("/tenantApply")
@Api(value = "机构审核", tags = "机构审核")
public class TenantEntryRecordController extends BaseController {

    @Autowired
    TenantApplyRecordService tenantApplyRecordService;

    @Autowired
    SysUserFeignService sysUserFeignService;

    @Autowired
    TenantInfoService tenantInfoService;

    /**
     * 机构入驻审核分页查询
     */
    @PostMapping("/applyPage")
    @ApiOperation(value = "机构申请查询", notes = "机构申请查询")
    @PreAuthorize("@pcs.hasPermissions('tenantApply/applyPage')")
    public HttpResponseResult<PageInfo<TenantApplyRecordWrapper.TenantApplyRecord>> applyPage(@RequestBody TenantApplyRecordWrapper.TenantApplyRecordQuery query) {

        IPage<TenantApplyRecordWrapper.TenantApplyRecord> pages = tenantApplyRecordService.selectPage(QueryInfo.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }


    /**
     * 机构入驻审核历史记录查询
     */

    @PostMapping("/historyPage")
    @ApiOperation(value = "机构审核历史记录", notes = "机构审核历史记录")
    @PreAuthorize("@pcs.hasPermissions('tenantApply/historyPage')")
    public HttpResponseResult<PageInfo<TenantApplyRecordWrapper.TenantApply>> historyPage(@RequestBody TenantApplyRecordWrapper.TenantApplyRecordQuery query) {

       IPage<TenantApplyRecordWrapper.TenantApply> pages = tenantApplyRecordService.historyPage(QueryInfo.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }


    /**
     * 机构入驻审核本次提交查询
     */
    @PostMapping("/queryNow")
    @ApiOperation(value = "机构审核本次提交", notes = "机构审核本次提交")
    @PreAuthorize("@pcs.hasPermissions('tenantApply/queryNow')")
    public HttpResponseResult<TenantApplyRecordWrapper.TenantApplyRecord> queryNow(@RequestBody TenantApplyRecordWrapper.TenantApplyRecordQuery query) {

        TenantApplyRecordWrapper.TenantApplyRecord tenantApplyRecord = tenantApplyRecordService.queryNow(query);
        return succeed(tenantApplyRecord);
    }

    /**
     * 机构入驻审核功能
     */
    @PostMapping("/entry")
    @ApiOperation(value = "机构审核功能", notes = "机构审核功能")
    @PreAuthorize("@pcs.hasPermissions('tenantApply/entry')")
    public HttpResponseResult<Boolean> entry(@RequestBody TenantApplyRecordWrapper.TenantEntry entry) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        /*TenantInfo tenantInfo = tenantInfoService.lambdaQuery().eq(TenantInfo::getUserId, sysUser.getId())
                .last("limit 1").one();
        if (tenantInfo == null) {
            throw new BizException("非法请求");
        }*/
        Long verifyUserId = sysUser.getId();

        return succeed(tenantApplyRecordService.entry(entry,verifyUserId));
    }


}
