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

import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetailUnit;
import cn.com.qmth.examcloud.core.questions.dao.entity.Question;
import cn.com.qmth.examcloud.core.questions.service.PaperDetailUnitService;
import cn.com.qmth.examcloud.core.questions.service.bean.dto.PaperDetailUnitExp;
import cn.com.qmth.examcloud.web.support.ControllerSupport;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * Created by songyue on 16/12/28.
 * updated by weiwenhai on 2018.9.28
 */
@RestController
@RequestMapping("${api_cqb}/")
public class PaperDetailUnitController extends ControllerSupport {

    @Autowired
    PaperDetailUnitService unitService;

    /**
     * 获取单个小题对应的试题
     *
     * @param unit_id
     * @return
     */
    @ApiOperation(value = "获取小题对应的试题", notes = "获取小题对应的试题")
    @GetMapping(value = "/paperDetailUnit/question/{unit_id}")
    public Question getQuestionByUnitId(String unit_id) {
        return unitService.getQuestionByPaperDetailUnitId(unit_id);
    }

    /**
     * 获取小题
     *
     * @param unit_id
     * @return
     */
    @ApiOperation(value = "获取小题", notes = "获取小题")
    @GetMapping(value = "/paperDetailUnit/{unit_id}")
    public PaperDetailUnit getPaperDetailUnitId(@PathVariable String unit_id) {
        return unitService.findById(unit_id);
    }

    /**
     * 更新小题信息
     *
     * @param pdu
     * @return
     */
    @ApiOperation(value = "更新小题", notes = "更新小题")
    @PutMapping(value = "/paperDetailUnit")
    public ResponseEntity<Object> updatePaperDetailUnit(@RequestBody PaperDetailUnitExp pdu) {
        User user = getAccessUser();
        unitService.savePaperDetailUnit(pdu, user);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 新增小题
     *
     * @param pdu
     * @return
     */
    @ApiOperation(value = "新增小题", notes = "新增小题")
    @PostMapping(value = "/paperDetailUnit")
    public ResponseEntity<Object> addPaperDetailUnit(@RequestBody PaperDetailUnitExp pdu) {
        User user = getAccessUser();
        PaperDetailUnit pdUnit = unitService.savePaperDetailUnit(pdu, user);
        return new ResponseEntity<>(pdUnit, HttpStatus.OK);
    }

    /**
     * 删除小题
     *
     * @return
     */
    @ApiOperation(value = "删除小题", notes = "删除小题")
    @DeleteMapping(value = "/paperDetailUnit/{unitId}")
    public ResponseEntity<Object> removePaperDetailUnit(@PathVariable String unitId) {
        User user = getAccessUser();
        unitService.deletePaperDetailUnit(unitId, user);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
