package org.nl.acs.device.rest;

import org.nl.common.logging.annotation.Log;
import org.nl.acs.device.service.DeviceErpmappingService;
import org.nl.acs.device.service.dto.AcsDeviceErpmappingDto;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
* @author jiaolm
* @date 2023-05-09
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "设备-ERP关系映射管理")
@RequestMapping("/api/acsDeviceErpmapping")
public class DeviceErpmappingController {

    private final DeviceErpmappingService acsDeviceErpmappingService;


    @GetMapping
    @Log("查询设备-ERP映射")
    @ApiOperation("查询设备-ERP映射")
    //@PreAuthorize("@el.check('acsDeviceErpmapping:list')")
    public ResponseEntity<Object> query(@RequestParam Map whereJson, Pageable page) {
        return new ResponseEntity<>(acsDeviceErpmappingService.queryAll(whereJson, page), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增设备-ERP映射")
    @ApiOperation("新增设备-ERP映射")
    //@PreAuthorize("@el.check('acsDeviceErpmapping:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody AcsDeviceErpmappingDto dto) {
        acsDeviceErpmappingService.create(dto);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改设备-ERP映射")
    @ApiOperation("修改设备-ERP映射")
    //@PreAuthorize("@el.check('acsDeviceErpmapping:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody AcsDeviceErpmappingDto dto) {
        acsDeviceErpmappingService.update(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除设备-ERP映射")
    @ApiOperation("删除设备-ERP映射")
    //@PreAuthorize("@el.check('acsDeviceErpmapping:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        acsDeviceErpmappingService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("导出设备-ERP映射")
    @ApiOperation("导出设备-ERP映射")
    @GetMapping(value = "/download")
    //@PreAuthorize("@el.check('acsDeviceErpmapping:list')")
    public void download(HttpServletResponse response, @RequestParam Map whereJson) throws IOException {
        acsDeviceErpmappingService.download(acsDeviceErpmappingService.queryAll(whereJson), response);
    }
}
