package com.qmth.paper.library.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.paper.library.business.bean.result.EditResult;
import com.qmth.paper.library.business.service.BasicFileTypeService;
import com.qmth.paper.library.common.bean.params.BasicFileTypeParams;
import com.qmth.paper.library.common.contant.ApiPrefixConstant;
import com.qmth.paper.library.common.contant.SystemConstant;
import com.qmth.paper.library.common.util.Result;
import com.qmth.paper.library.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

/**
 * @Description 扫描-其他文件类型-前端控制器
 */
@Api(tags = "其他文件类型Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + ApiPrefixConstant.PREFIX_BASIC + "/filetype")
public class BasicFileTypeController {

    @Resource
    private BasicFileTypeService basicFileTypeService;

    @ApiOperation(value = "分页查询")
    @PostMapping("/page")
    @ApiResponses({ @ApiResponse(code = 200, message = "查询成功", response = Result.class) })
    public Result page(@ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
            @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        return ResultUtil.ok(basicFileTypeService.pageData(pageNumber, pageSize));
    }

    @ApiOperation(value = "列表查询")
    @PostMapping("/list")
    @ApiResponses({ @ApiResponse(code = 200, message = "查询成功", response = Result.class) })
    public Result list() {
        return ResultUtil.ok(basicFileTypeService.listBasicFileType());
    }

    @ApiOperation(value = "新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "保存成功", response = EditResult.class)})
    public Result saveBasicFileType(@Valid @RequestBody BasicFileTypeParams params, BindingResult bindingResult) throws IllegalAccessException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(basicFileTypeService.saveBasicFileType(params));
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "删除成功", response = EditResult.class)})
    public Result deleteBasicFileType(@ApiParam(value = "文件类型ID", required = true) @RequestParam String id) {
        return ResultUtil.ok(basicFileTypeService.deleteBasicFileTypeById(SystemConstant.convertIdToLong(id)));
    }

}
