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.IgnoreSecurity;
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.RecordQuestionService;
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 javax.validation.Valid;

/**
 * @author yanghuan
 * @title: UnitTestController
 * @projectName ssj
 * @description: TODO
 * @date 2020/5/15 9:41
 */
@Api(value = "单元测评的接口",tags="单元测评的接口")
@RestController
@RequestMapping("/api/recordQuestionSys")
public class RecordQuestionSysController extends BaseController {

    @Reference(version = "1.0", timeout = 60000)
    private RecordQuestionService recordQuestionService;

    @Autowired
    private TokenManager tokenManager;

    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ApiOperation(value = "录题登录", httpMethod = "POST", notes = "录题登录")
    @IgnoreSecurity
    public ResponseEntity login(@RequestBody @Valid LoginReq req) {

        ResponseEntity responseEntity;
        try {
            responseEntity = recordQuestionService.login(req);
        } catch (Exception e) {
            logger.error("req={}", req);
            logger.error("录题登录异常", e);
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
        }
        return responseEntity;
    }

    @ModifySecurity
    @RequestMapping(value = "/addOrEditQuestion",method = RequestMethod.POST)
    @ApiOperation(value = "添加或修改题目",httpMethod = "POST",notes = "添加或修改题目")
    public ResponseEntity addOrEditQuestion(@RequestParam(name="accessToken") String accessToken, @RequestBody QuestionReuqest questionReuqest){
        ResponseEntity responseEntity;
        try {
            responseEntity = recordQuestionService.addOrEditQuestion(questionReuqest);
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
        }
        return responseEntity;
    }

    @ModifySecurity
    @RequestMapping(value = "deleteQuestion", method = RequestMethod.POST)
    @ApiOperation(value = "删除题目", httpMethod = "POST", notes = "删除题目")
    public ResponseEntity deleteQuestion(@RequestParam(name = "accessToken") String accessToken,
                                         @RequestBody @Valid DeleteReq req) {

        ResponseEntity responseEntity;
        try {
            responseEntity = recordQuestionService.deleteQuestion(req);
        } catch (Exception e) {
            logger.error("req={}", req);
            logger.error("删除题目异常", e);
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
        }
        return responseEntity;
    }


    @RequestMapping(value = "/findQuestionList",method = RequestMethod.POST)
    @ApiOperation(value = "查询题目",httpMethod = "POST",notes = "查询题目")
    public ResponseEntity findQuestionList(@RequestParam(name="accessToken") String accessToken, @RequestBody QuestionUnitRequest request){
        ResponseEntity responseEntity;
        try {
            responseEntity = recordQuestionService.findQuestionList( request);
        } 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 = recordQuestionService.getConditionInfo(true, 1);
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "系统异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/getKnowledge", method = RequestMethod.POST)
    @ApiOperation(value = "获取知识点", httpMethod = "POST", notes = "获取知识点")
    public ResponseEntity getKnowledge(@RequestParam(name = "accessToken") String accessToken, @RequestBody GetKnowledgeReq req) {
        ResponseEntity responseEntity;
        try {
            responseEntity = recordQuestionService.getKnowledge(req);
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "系统异常，请稍后重试");
        }
        return responseEntity;
    }

    @ModifySecurity
    @RequestMapping(value = "/publishQuestion", method = RequestMethod.POST)
    @ApiOperation(value = "发布题目", httpMethod = "POST", notes = "发布题目")
    public ResponseEntity publishQuestion(@RequestParam(name = "accessToken") String accessToken, @RequestBody PublishReq req) {
        ResponseEntity responseEntity;
        try {
            responseEntity = recordQuestionService.publishQuestion(req);
        } catch (Exception e) {
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "系统异常，请稍后重试");
        }
        return responseEntity;
    }


}
