package cn.com.qmth.examcloud.core.questions.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.com.qmth.examcloud.core.questions.dao.entity.QuestionAudio;
import cn.com.qmth.examcloud.core.questions.service.QuestionAudioService;
import cn.com.qmth.examcloud.support.filestorage.FileStorageUtil;
import cn.com.qmth.examcloud.web.support.Naked;
import io.swagger.annotations.ApiOperation;

/**
 * @author chenken
 * @date 2017年8月2日 下午1:49:04
 * @company QMTH
 * @description QuestionAudioController.java
 */
@RestController
@RequestMapping("${api_cqb}/")
public class QuestionAudioController {
//    @Autowired
//    private SysProperty sysProperty;

    @Autowired
    private QuestionAudioService questionAudioService;

    @ApiOperation(value = "通过questionId和fileName获取试题音频文件", notes = "通过questionId和fileName获取试题音频文件")
    @GetMapping(value = "/questionAudio/{fileName}/{questionId}")
    public ResponseEntity<Object> findQuestionAudio(@PathVariable String fileName, @PathVariable String questionId) {
        QuestionAudio questionAudio = questionAudioService.findByQuestionIdAndFileName(questionId, fileName);
        return new ResponseEntity<>(questionAudio, HttpStatus.OK);
    }

    @Naked
    @ApiOperation(value = "通过ID获取试题音频文件", notes = "通过ID获取试题音频文件")
    @GetMapping(value = "/questionAudio/{questionAudioId}")
    public ResponseEntity<Object> findQuestionAudioById(@PathVariable String questionAudioId) {
        QuestionAudio questionAudio = questionAudioService.findAudioById(questionAudioId);

        if (questionAudio != null) {
            //补全路径
//            questionAudio.setFileUrl(sysProperty.getDomain() + questionAudio.getFileUrl());
        	//通用存储
        	questionAudio.setFileUrl(FileStorageUtil.realPath(questionAudio.getFileUrl()));
        }

        return new ResponseEntity<>(questionAudio, HttpStatus.OK);
    }

}