package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.req.AuthOperaReq;
import com.yonge.cooleshow.biz.dal.dto.search.AuthEntryRecordSearch;
import com.yonge.cooleshow.biz.dal.service.TeacherAuthEntryRecordService;
import com.yonge.toolset.mybatis.support.PageUtil;
import com.yonge.cooleshow.biz.dal.vo.TeacherAuthEntryRecordVo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/teacherAuthEntryRecord")
@Api(value = "老师入驻审核表", tags = "老师入驻审核表")
public class TeacherAuthEntryRecordController extends BaseController {
    @Autowired
    private TeacherAuthEntryRecordService teacherAuthEntryRecordService;
    @Resource
    private SysUserFeignService sysUserFeignService;

    @GetMapping("/detail/{id}")
    @ApiOperation(value = "详情", notes = "传入id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", dataType = "long", required = true),
    })
    @PreAuthorize("@pcs.hasPermissions('teacherAuthEntryRecord/detail')")
    public HttpResponseResult<TeacherAuthEntryRecordVo> detail(@PathVariable("id") Long id) {
        TeacherAuthEntryRecordVo detail = teacherAuthEntryRecordService.detail(id);
        return succeed(detail);
    }

    @PostMapping("/page")
    @ApiOperation(value = "查询分页", notes = "传入AuthEntryRecordSearch")
    @PreAuthorize("@pcs.hasPermissions('teacherAuthEntryRecord/page')")
    public HttpResponseResult<PageInfo<TeacherAuthEntryRecordVo>> page(@RequestBody AuthEntryRecordSearch search) {
        IPage<TeacherAuthEntryRecordVo> pages = teacherAuthEntryRecordService.selectPage(PageUtil.getPage(search), search);
        return succeed(PageUtil.pageInfo(pages));
    }

    @PostMapping("/historyPage")
    @ApiOperation(value = "查询分页", notes = "传入AuthEntryRecordSearch")
    @PreAuthorize("@pcs.hasPermissions('teacherAuthEntryRecord/page')")
    public HttpResponseResult<PageInfo<TeacherAuthEntryRecordVo>> historyPage(@RequestBody AuthEntryRecordSearch search) {
        IPage<TeacherAuthEntryRecordVo> pages = teacherAuthEntryRecordService.historyPage(PageUtil.getPage(search), search);
        return succeed(PageUtil.pageInfo(pages));
    }

    @PostMapping("/doAuth")
    @ApiOperation(value = "审核", notes = "传入authOperaDto")
    @PreAuthorize("@pcs.hasPermissions('teacherAuthEntryRecord/doAuth')")
    public HttpResponseResult<Boolean> doAuth(@Valid @RequestBody AuthOperaReq authOperaReq) throws Exception {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return teacherAuthEntryRecordService.doAuth(authOperaReq, user);
    }
}