package com.ruoyi.web.controller.front;

import com.fasterxml.jackson.core.JsonProcessingException;
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.mxjb.domain.MxjbContants;
import com.ruoyi.sy.domain.SyTestExaminee;
import com.ruoyi.web.domain.CategoryDto;
import com.ruoyi.web.domain.PaperDto;
import com.ruoyi.web.service.CacheService;
import com.ruoyi.web.service.SyTestMajorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "测评-选专业")
@RestController
@RequestMapping("front/test/major/")
public class FrontSyMajorController extends BaseController {
    private final SyTestMajorService syTestMajorService;
    private final CacheService cacheService;

    public FrontSyMajorController(SyTestMajorService syTestMajorService, CacheService cacheService) {
        this.syTestMajorService = syTestMajorService;
        this.cacheService = cacheService;
    }

    @ApiOperation("01 查询测试历史")
    @GetMapping(value = "getTestList")
    public TableDataInfo getTestList(@ApiParam("1价值2职业3知识") @RequestParam Integer testType) {
        List<SyTestExaminee> examineeList = syTestMajorService.getTestList(testType);
        return getDataTable(examineeList);
    }

    @ApiOperation("02 查询测试内容")
    @GetMapping(value = "loadExamineePaper")
    public PaperDto loadExamineePaper(@ApiParam("测试用户id") @RequestParam Long examineeId) {
        PaperDto paperDto = syTestMajorService.loadPaperByExaminerId(examineeId);
        Map<String, String> stateMap = cacheService.selectDictDataMapByType(MxjbContants.StateTypeExaminee);
        paperDto.setStateStr(stateMap.get(String.valueOf(paperDto.getState())));
        paperDto.calculateAllow();
        return paperDto;
    }

    /**
     * 提取测试结果
     * @param testType
     * @return
     */
    @ApiOperation("03 提取测试结果")
    @GetMapping(value = "getTestMajorResult")
    public AjaxResult loadTestMajorResult(@ApiParam("1价值2职业3知识") @RequestParam(required = false) Integer testType,
                                          @ApiParam("用户代码") @RequestParam(required = false) Long examineeId) {
        try {
            if (null != examineeId) {
                return syTestMajorService.loadTestMajorResult(examineeId);
            }
            return syTestMajorService.loadTestMajorResult(testType, SecurityUtils.getLoginUser().getUser().getCode());
        } catch (JsonProcessingException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 提取测试结果涉及的专业树
     * @param testType
     * @param matchCode
     * @param level
     * @return
     */
    @ApiOperation("04 提取测试结果涉及的一二级专业")
    @GetMapping(value = "getMatchCategoryTree")
    public AjaxResult loadMatchCategoryTree(@ApiParam("1价值2职业3知识") @RequestParam Integer testType,
                                            @ApiParam("匹配代码") @RequestParam String matchCode,
                                            @ApiParam("级次") @RequestParam(required = false) Integer level) {
        return syTestMajorService.loadMatchCategoryTree(testType, matchCode, level);
    }

    /**
     * 根据用户的选科返回所有一二三级专业
     *
     * @return
     */
    @ApiOperation("05 提取测试结果涉及的一二三级专业树")
    @GetMapping(value = "getSelectMajorSubjectTree")
    public AjaxResult loadSelectMajorSubjectTree() {
        return syTestMajorService.loadSelectMajorSubjectTree();
    }

    @ApiOperation("05 提取所有的一二三级专业树")
    @GetMapping(value = "loadAllMajorSubjectTree")
    public AjaxResult loadAllMajorSubjectTree() {
        return syTestMajorService.loadAllMajorSubjectTree();
    }

    @ApiOperation("06 保存选择的专业类")
    @PostMapping(value = "saveUserSelectedCategory")
    public AjaxResult saveUserSelectedCategory(@RequestBody CategoryDto.UserCategory param) {
        return syTestMajorService.saveUserSelectedCategory(param);
    }

    @ApiOperation("07 保存选择的专业")
    @PostMapping(value = "saveUserSelectedMajor")
    public AjaxResult saveUserSelectedMajor(@RequestBody CategoryDto.UserMajor param) {
        return syTestMajorService.saveUserSelectedMajor(param);
    }

    @ApiOperation("07 查询选择的专业")
    @PostMapping(value = "getUserSelectedMajor")
    public AjaxResult getUserSelectedMajor() {
        return syTestMajorService.getUserSelectedMajor();
    }

    @ApiOperation("08 查询所有测试结果及选专业")
    @PostMapping(value = "getAllTestMajorResult")
    public AjaxResult getAllTestMajorResult() {
        try {
            return syTestMajorService.getAllTestMajorResult();
        } catch (JsonProcessingException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

}
