package com.ssj.aijia.opentest;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.framework.core.security.annotation.ModifySecurity;
import com.ssj.framework.core.security.manager.TokenManager;
import com.ssj.framework.core.util.ResponseConstant;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.service.conch.opentest.OpenTestService;
import com.ssj.service.conch.opentest.dto.ConditionDto;
import com.ssj.service.conch.opentest.request.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 小塾测评的接口
 *
 * @author Mount Liu
 * @date 2020/2/11
 */
@Api(value = "小塾测评的接口", tags = "小塾测评的接口")
@RestController
@RequestMapping("/api/xiaoshuTest")
public class OpenTestController extends BaseController {
    @Reference(version = "1.0", timeout = 60000)
    private OpenTestService openTestService;

    @Autowired
    private TokenManager tokenManager;

    @ModifySecurity
    @RequestMapping(value = "/saveInfo", method = RequestMethod.POST)
    @ApiOperation(value = "完善资料页", httpMethod = "POST", notes = "完善资料页")
    public ResponseEntity saveInfo(@RequestParam(name = "accessToken") String accessToken, @RequestBody SaveInfoReq req) {
        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = openTestService.saveInfo(req, userId);
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/getMyInfo", method = RequestMethod.POST)
    @ApiOperation(value = "获取我的信息", httpMethod = "POST", notes = "获取我的信息")
    public ResponseEntity getMyInfo(@RequestParam(name = "accessToken") String accessToken) {
        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = openTestService.getMyInfo(userId);
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/getQuestion", method = RequestMethod.POST)
    @ApiOperation(value = "筛选试卷", httpMethod = "POST", notes = "筛选试卷")
    public ResponseEntity getQuestion(@RequestParam(name = "accessToken") String accessToken, @RequestBody GetQuestionReq req) {
        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = openTestService.getQuestion(req, userId);
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/getQuestionV1", method = RequestMethod.POST)
    @ApiOperation(value = "筛选试卷", httpMethod = "POST", notes = "筛选试卷")
    public ResponseEntity getQuestionV1(@RequestParam(name = "accessToken") String accessToken, @RequestBody GetQuestionV1Req req) {
        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = openTestService.getQuestionV1(req, userId);
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/getTestList", method = RequestMethod.POST)
    @ApiOperation(value = "筛选试卷", httpMethod = "POST", notes = "筛选试卷")
    public ResponseEntity getTestList(@RequestParam(name = "accessToken") String accessToken) {
        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = openTestService.getTestList(userId);
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/getTestListV1", method = RequestMethod.POST)
    @ApiOperation(value = "筛选试卷", httpMethod = "POST", notes = "筛选试卷")
    public ResponseEntity getTestListV1(@RequestParam(name = "accessToken") String accessToken, @RequestBody GetTestListReq req) {
        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = openTestService.getTestListV1(userId, req.getType());
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/getResult", method = RequestMethod.POST)
    @ApiOperation(value = "查看答题结果", httpMethod = "POST", notes = "查看答题结果")
    public ResponseEntity getResult(@RequestParam(name = "accessToken") String accessToken, @RequestBody GetResultReq req) {
        ResponseEntity responseEntity;
        try {
            responseEntity = openTestService.getResult(req);
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/getResultV1", method = RequestMethod.POST)
    @ApiOperation(value = "查看答题结果", httpMethod = "POST", notes = "查看答题结果")
    public ResponseEntity getResultV1(@RequestParam(name = "accessToken") String accessToken, @RequestBody GetResultReq req) {
        ResponseEntity responseEntity;
        try {
            responseEntity = openTestService.getResultV1(req);
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @ModifySecurity
    @RequestMapping(value = "/saveResult", method = RequestMethod.POST)
    @ApiOperation(value = "提交答题结果", httpMethod = "POST", notes = "提交答题结果")
    public ResponseEntity saveResult(@RequestParam(name = "accessToken") String accessToken, @RequestBody SaveResultReq req) {
        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = openTestService.saveResult(req, userId);
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @ModifySecurity
    @RequestMapping(value = "/saveResultV1", method = RequestMethod.POST)
    @ApiOperation(value = "提交答题结果", httpMethod = "POST", notes = "提交答题结果")
    public ResponseEntity saveResultV1(@RequestParam(name = "accessToken") String accessToken, @RequestBody SaveResultV1Req req) {
        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = openTestService.saveResultV1(req, userId);
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/getReport", method = RequestMethod.POST)
    @ApiOperation(value = "查看诊断报告", httpMethod = "POST", notes = "查看诊断报告")
    public ResponseEntity getReport(@RequestParam(name = "accessToken") String accessToken, @RequestBody GetResultReq req) {
        ResponseEntity responseEntity;
        try {
            responseEntity = openTestService.getReport(req);
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/getRankingList", method = RequestMethod.POST)
    @ApiOperation(value = "查看排行榜", httpMethod = "POST", notes = "查看排行榜")
    public ResponseEntity getRankingList(@RequestParam(name = "accessToken") String accessToken, @RequestBody GetResultReq req) {
        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = openTestService.getRankingList(req, userId);
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/getConditionInfo", method = RequestMethod.POST)
    @ApiOperation(value = "获取年级科目版本信息", httpMethod = "POST", notes = "获取年级科目版本信息")
    public ResponseEntity getConditionInfo(@RequestParam(name = "accessToken") String accessToken) {
        ResponseEntity responseEntity;
        try {
            responseEntity = openTestService.getConditionInfo();
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/getConditionInfoV1", method = RequestMethod.POST)
    @ApiOperation(value = "获取年级科目版本信息", httpMethod = "POST", notes = "获取年级科目版本信息")
    public ResponseEntity getConditionInfoV1(@RequestParam(name = "accessToken") String accessToken) {
        ResponseEntity responseEntity;
        try {
            responseEntity = openTestService.getConditionInfoV1();
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @ModifySecurity
    @RequestMapping(value = "/deleteResult", method = RequestMethod.POST)
    @ApiOperation(value = "重新测评", httpMethod = "POST", notes = "重新测评")
    public ResponseEntity deleteResult(@RequestParam(name = "accessToken") String accessToken, @RequestBody GetResultReq req) {
        ResponseEntity responseEntity;
        try {
            responseEntity = openTestService.deleteResult(req.getResultId());
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @ModifySecurity
    @RequestMapping(value = "/setConditionInfo", method = RequestMethod.POST)
    @ApiOperation(value = "设置年级科目版本信息", httpMethod = "POST", notes = "设置年级科目版本信息")
    public ResponseEntity getConditionInfo(@RequestParam(name = "accessToken") String accessToken, @RequestParam(name = "value") String value) {
        ResponseEntity responseEntity;
        try {
            responseEntity = openTestService.setConditionInfo(value);
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }
}
