/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.dm.printingRequest.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.dm.printingRequest.domain.DmPrintingRequest;
import me.zhengjie.modules.dm.printingRequest.service.DmPrintingRequestService;
import me.zhengjie.modules.dm.printingRequest.service.dto.DmPrintingRequestQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author xwx
* @date 2021-03-15
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "dmPrintingRequest管理")
@RequestMapping("/api/dmPrintingRequest")
public class DmPrintingRequestController {

    private final DmPrintingRequestService dmPrintingRequestService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('dmPrintingRequest:list')")
    public void download(HttpServletResponse response, DmPrintingRequestQueryCriteria criteria) throws IOException {
        dmPrintingRequestService.download(dmPrintingRequestService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询dmPrintingRequest")
    @ApiOperation("查询dmPrintingRequest")
    @PreAuthorize("@el.check('dmPrintingRequest:list')")
    public ResponseEntity<Object> query(DmPrintingRequestQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(dmPrintingRequestService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增dmPrintingRequest")
    @ApiOperation("新增dmPrintingRequest")
    @PreAuthorize("@el.check('dmPrintingRequest:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody DmPrintingRequest resources) throws IOException {
        return new ResponseEntity<>(dmPrintingRequestService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改dmPrintingRequest")
    @ApiOperation("修改dmPrintingRequest")
    @PreAuthorize("@el.check('dmPrintingRequest:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody DmPrintingRequest resources){
        dmPrintingRequestService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除dmPrintingRequest")
    @ApiOperation("删除dmPrintingRequest")
    @PreAuthorize("@el.check('dmPrintingRequest:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        Map<String,Object> map=dmPrintingRequestService.deleteAll(ids);
        return new ResponseEntity<>(map,HttpStatus.OK);
    }

    @PostMapping(value = "addAapprovalUser")
    @Log("新增文印审批人")
    @ApiOperation("新增文印审批人")
    @PreAuthorize("@el.check('dmPrintingRequest:edit')")
    public ResponseEntity<Object> addAapprovalUser(@Validated @RequestBody DmPrintingRequest resources){
        dmPrintingRequestService.addApprovalUser(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PutMapping(value = "recallApprovalUser")
    @Log("撤回文印审批人")
    @ApiOperation("撤回文印审批人")
    @PreAuthorize("@el.check('dmPrintingRequest:edit')")
    public ResponseEntity<Object> recallApprovalUser(@Validated @RequestBody DmPrintingRequest resources){
        dmPrintingRequestService.recallApprovalUser(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping(value = "info")
    @Log("查询ddmPrintingRequest")
    @ApiOperation("查询dmPrintingRequest")
    public ResponseEntity<Object> info(Long id){
        if(id == null ){
            return null;
        }
        return new ResponseEntity<>(dmPrintingRequestService.findInfo(id),HttpStatus.OK);
    }
}
