package com.ruoyi.web.controller.ie;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.syzy.domain.BBusiHollandQuestions;
import com.ruoyi.syzy.domain.BBusiHollandRecords;
import com.ruoyi.syzy.domain.BBusiHollandSteps;
import com.ruoyi.syzy.service.IBBusiHollandQuestionsService;
import com.ruoyi.syzy.service.IBBusiHollandRecordsService;
import com.ruoyi.syzy.service.IBBusiHollandStepsService;
import com.ruoyi.web.service.CommService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "测评-职业兴趣测评(HOLLAND)")
@RequestMapping("front/syzy/holland")
public class FrontHollandController extends BaseController {

    @Autowired
    private IBBusiHollandStepsService stepsService;
    @Autowired
    private IBBusiHollandQuestionsService questionsService;
    @Autowired
    private IBBusiHollandRecordsService recordsService;

    @Autowired
    private CommService commService;
    @GetMapping("steps")
    @ApiOperation("01 测试步骤")
    @Anonymous
    public AjaxResult steps() {
        BBusiHollandSteps exam = new BBusiHollandSteps();
        exam.setStatus(1);
        List<BBusiHollandSteps> data = stepsService.selectBBusiHollandStepsList(exam);
        return AjaxResult.success(data);
    }

    @GetMapping("questions")
    @ApiOperation("02 步骤题目")
    @Anonymous
    public TableDataInfo question(@ApiParam("stepId") @RequestParam Long stepId) {
//        commService.requireVip();

        List<BBusiHollandQuestions> arr = questionsService.selectQuestionsByStepId(stepId);
        for (int i = 0; i < arr.size(); i++) {
            arr.get(i).setPercent((i + 1) / arr.size() * 100);
        }
        return getDataTable(arr);
    }

    @PostMapping("save")
    @ApiOperation("03 保存测试")
    public AjaxResult save(@RequestBody BBusiHollandRecords data) {
        long startTime = System.currentTimeMillis();
        data.setCustomerCode(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        recordsService.insertBBusiHollandRecords(data);
        logger.debug("used times:{}",System.currentTimeMillis()-startTime);
        return AjaxResult.success("生成评测报告成功!", data.getCode());
    }

    @GetMapping("record")
    @ApiOperation("04 测评记录")
    public TableDataInfo record(@ApiParam(value = "页数", example = "1") @RequestParam Integer pageNum,
        @ApiParam(value = "页大小", example = "15") @RequestParam Integer pageSize) {
        startPage();
        BBusiHollandRecords exam = new BBusiHollandRecords();
        exam.setCustomerCode(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        exam.setStatus(1);
        List<BBusiHollandRecords> arr = recordsService.selectBBusiHollandRecordsList(exam);
        return getDataTable(arr);
    }

    @GetMapping("record/detail")
    @ApiOperation("05 测评详情")
    public AjaxResult record(@ApiParam("测评批次") @RequestParam String code) {
        return AjaxResult.success(recordsService.info(code));
    }

}
