package com.ruoyi.web.controller.ie;

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.BBusiMbtiMajor;
import com.ruoyi.syzy.domain.BBusiMbtiQuestions;
import com.ruoyi.syzy.domain.BBusiMbtiRecords;
import com.ruoyi.syzy.domain.BBusiMbtiSteps;
import com.ruoyi.syzy.service.IBBusiMbtiQuestionsService;
import com.ruoyi.syzy.service.IBBusiMbtiRecordsService;
import com.ruoyi.syzy.service.IBBusiMbtiStepsService;
import com.ruoyi.util.PageUtil;
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 = "测评-职业性格测评(MBTI)")
@RequestMapping("front/syzy/mbti")
public class FrontMbtiController extends BaseController {

    @Autowired
    private IBBusiMbtiStepsService stepsService;
    @Autowired
    private IBBusiMbtiQuestionsService questionsService;
    @Autowired
    private IBBusiMbtiRecordsService recordsService;

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

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

        BBusiMbtiQuestions exam = new BBusiMbtiQuestions();
        exam.setStepId(stepId);
        exam.setStatus(1);
        List<BBusiMbtiQuestions> arr = questionsService.selectBBusiMbtiQuestionsList(exam);
        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 BBusiMbtiRecords data) {
        long startTime = System.currentTimeMillis();
        data.setCustomerCode(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        recordsService.insertBBusiMbtiRecords(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();
        BBusiMbtiRecords exam = new BBusiMbtiRecords();
        exam.setCustomerCode(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        exam.setStatus(1);
        List<BBusiMbtiRecords> arr = recordsService.selectBBusiMbtiRecordsList(exam);
        return getDataTable(arr);
    }

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

    @GetMapping("record/major")
    @ApiOperation("06 测评推荐专业")
    public TableDataInfo major(@ApiParam("测评批次") @RequestParam String code,
                               @ApiParam(value = "页数", example = "1") @RequestParam Integer pageNum,
                               @ApiParam(value = "页大小", example = "5") @RequestParam Integer pageSize) {
        List<BBusiMbtiMajor> majorList = recordsService.major(code);
        return getDataTable(PageUtil.buildListForPage(majorList, pageNum, pageSize));
    }

    @GetMapping("majors")
    @ApiOperation("06 兴趣码专业")
    public AjaxResult majors(@ApiParam("兴趣码") @RequestParam String code) {
        List<BBusiMbtiMajor> majorList = recordsService.majors(code);
        return AjaxResult.success(majorList);
    }

}
