package com.zhongzheng.controller.exam;

import com.zhongzheng.common.annotation.Log;
import com.zhongzheng.common.core.controller.BaseController;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.common.core.page.TableDataInfo;
import com.zhongzheng.common.enums.BusinessType;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.framework.web.service.WxTokenService;
import com.zhongzheng.modules.exam.bo.*;
import com.zhongzheng.modules.exam.service.IExamApplyGoodsService;
import com.zhongzheng.modules.exam.service.IExamApplyService;
import com.zhongzheng.modules.exam.service.IExamApplySiteService;
import com.zhongzheng.modules.exam.vo.*;
import com.zhongzheng.modules.user.bo.UserAppSubscribeEditBo;
import com.zhongzheng.modules.user.bo.UserSubscribeAddBo;
import com.zhongzheng.modules.user.bo.UserSubscribeEditBo;
import com.zhongzheng.modules.user.bo.UserSubscribeQueryBo;
import com.zhongzheng.modules.user.entity.ClientLoginUser;
import com.zhongzheng.modules.user.service.IUserSubscribeService;
import com.zhongzheng.modules.user.vo.UserSubscribeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 考试安排Controller
 * 
 * @author ruoyi
 * @date 2021-12-07
 */
@Api(value = "考试预约", tags = {"考试预约"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/apply")
public class ExamApplyController extends BaseController {

    private final IExamApplyService iExamApplyService;

    private final IExamApplySiteService iExamApplySiteService;

    private final IExamApplyGoodsService iExamApplyGoodsService;

    private final WxTokenService wxTokenService;

    private final IUserSubscribeService iUserSubscribeService;

    /**
     * 获取考试安排详细信息
     */
    @ApiOperation("点击预约报考按钮")
    @GetMapping("/subscribe")
    public AjaxResult<ExamUserApplyVo> subscribe(ExamApplyQueryBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        ExamUserApplyVo examUserApplyVo = iExamApplyService.subscribe(bo);
        return AjaxResult.success(examUserApplyVo);
    }

    /**
     * 获取考试安排详细信息
     */
    @ApiOperation("预约报考下一步按钮 1 进入有考陪有考试地点得预约考试  2进入无考陪有考试地点预约考试 3无考试次数购买商品")
    @GetMapping("/subscribeNext")
    public AjaxResult<Long> subscribeNext(ExamApplyQueryBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        Long count = iExamApplyService.subscribeNext(bo);
        return AjaxResult.success(count);
    }


    /**
     * 获取考试安排详细信息
     */
    @ApiOperation("获得考试的考试地点")
    @GetMapping("/subscribeApplySite")
    public AjaxResult<List<ExamUserApplySiteVo>> subscribeApplySite(ExamApplyQueryBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        List<ExamUserApplySiteVo> count = iExamApplyService.subscribeApplySite(bo);
        return AjaxResult.success(count);
    }

    /**
     * 获取考试安排详细信息
     */
    @ApiOperation("获得考试的考培地点")
    @GetMapping("/subscribeApplySiteTrain")
    public AjaxResult<List<ExamUserApplySiteVo>> subscribeApplySiteTrain(ExamApplyQueryBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        List<ExamUserApplySiteVo> count = iExamApplyService.subscribeApplySiteTrain(bo);
        return AjaxResult.success(count);
    }

    /**
     * 新增用户预约考试
     */
    @ApiOperation("新增用户预约考试")
    @Log(title = "用户预约考试", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Long> add(@RequestBody UserSubscribeAddBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return AjaxResult.success(iUserSubscribeService.insertByAddBo(bo));
    }

    /**
     * 查询用户预约考试列表
     */
    @ApiOperation("查询报考数据列表")
    @GetMapping("/list")
    public TableDataInfo<UserSubscribeVo> listSubscribe(UserSubscribeQueryBo bo) {
        startPage();
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        List<UserSubscribeVo> list = iUserSubscribeService.listSubscribe(bo);
        return getDataTable(list);
    }

    /**
     * 修改用户预约考试
     */
    @ApiOperation("修改用户预约考试")
    @Log(title = "用户预约考试", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult<Void> edit(@RequestBody UserAppSubscribeEditBo bo) {
        return toAjax(iUserSubscribeService.updateByEditSingleBo(bo) ? 1 : 0);
    }
}
