package com.qxgmat.controller.admin;


import com.nuliji.tools.Response;
import com.nuliji.tools.ResponseHelp;
import com.nuliji.tools.Transform;
import com.qxgmat.data.dao.entity.ExaminationStruct;
import com.qxgmat.dto.admin.request.ExaminationStructDto;
import com.qxgmat.service.inline.ExaminationStructService;
import com.qxgmat.service.inline.ManagerLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController("AdminExaminationController")
@RequestMapping("/admin/examination")
@Api(tags = "模考接口", description = "模考相关", produces = MediaType.APPLICATION_JSON_VALUE)
public class ExaminationController {
    @Autowired
    private ManagerLogService managerLogService;
    @Autowired
    private ExaminationStructService examinationStructService;

    @RequestMapping(value = "/struct/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加模考层级", httpMethod = "POST")
    public Response<ExaminationStruct> add(@RequestBody @Validated ExaminationStructDto dto, HttpServletRequest request) {
        ExaminationStruct entity = Transform.dtoToEntity(dto);
        entity = examinationStructService.addPaper(entity);
        managerLogService.log(request);
        return ResponseHelp.success(entity);
    }
    @RequestMapping(value = "/struct/edit", method = RequestMethod.PUT)
    @ApiOperation(value = "编辑模考层级", httpMethod = "PUT")
    public Response<Boolean> edit(@RequestBody @Validated ExaminationStructDto dto, HttpServletRequest request) {
        ExaminationStruct entity = Transform.dtoToEntity(dto);
        entity = examinationStructService.editPaper(entity);
        managerLogService.log(request);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/struct/delete", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除模考层级", httpMethod = "DELETE")
    public Response<Boolean> delete(@RequestParam int id, HttpServletRequest request) {
        boolean result = examinationStructService.deletePaper(id);
        managerLogService.log(request);
        return ResponseHelp.success(result);
    }

    @RequestMapping(value = "/struct/all", method = RequestMethod.GET)
    @ApiOperation(value = "所有模考层级", httpMethod = "GET")
    public Response<List<ExaminationStruct>> all(HttpSession session) {
        List<ExaminationStruct> p = examinationStructService.all();
        return ResponseHelp.success(p);
    }


}
