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.ExportStructure;
import cn.com.qmth.examcloud.core.questions.service.ExportStructureService;
import cn.com.qmth.examcloud.web.support.ControllerSupport;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chenken
 * @date 2017年7月12日 下午4:28:55
 * @company QMTH
 * @description ExportStructureController.java
 */
@Controller
@RequestMapping("${api_cqb}/")
public class ExportStructureController extends ControllerSupport {

    @Autowired
    private ExportStructureService exportStructureService;

    @ApiOperation(value = "保存导出结构", notes = "保存导出结构")
    @PostMapping(value = "/saveExportStructure")
    public ResponseEntity<Object> saveExportStructure(@RequestBody ExportStructure exportStructure) {
        try {
            User user = getAccessUser();
            exportStructureService.saveExportStructure(exportStructure, user);
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @ApiOperation(value = "分页查询导出结构", notes = "分页查询导出结构")
    @PostMapping(value = "/findPageByExportStructure/{curPage}/{pageSize}")
    public ResponseEntity<Object> findPageByExportStructure(@PathVariable int curPage, @PathVariable int pageSize,
                                                            @RequestBody ExportStructure exportStructure) {
        try {
            User user = getAccessUser();
            exportStructure.setOrgId(user.getRootOrgId() + "");
            Page<ExportStructure> pageExportStructure = exportStructureService.findPageByExportStructure(exportStructure, curPage, pageSize);
            return new ResponseEntity<>(pageExportStructure, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @ApiOperation(value = "根据考试ID查询导出结构", notes = "根据考试ID查询导出结构")
    @GetMapping(value = "/findExportStructure/{examId}")
    public ResponseEntity<Object> findExportStructure(@PathVariable String examId) {
        User user = getAccessUser();
        ExportStructure exportStructure = exportStructureService.findByExportStructure(user.getRootOrgId() + "", examId);
        return new ResponseEntity<>(exportStructure, HttpStatus.OK);
    }

    @ApiOperation(value = "根据考试类型查询考试id", notes = "根据考试类型查询考试id")
    @GetMapping(value = "/findExportStructureByExamType/{examType}")
    public ResponseEntity<Object> findExportStructureByExamType(@PathVariable String examType) {
        User user = getAccessUser();
        List<String> list = exportStructureService.findExportStructureByExamType(examType, user);
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

    @ApiOperation(value = "根据导出结构id删除", notes = "根据导出结构删除")
    @DeleteMapping(value = "/deleteExportStructure/{id}")
    public ResponseEntity<Object> deleteExportStructure(@PathVariable String id) {
        exportStructureService.deleteExportStructure(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}

