package org.nl.acs.storage_cell.rest;

import org.nl.common.logging.annotation.Log;
import org.nl.acs.storage_cell.domain.StorageCell;
import org.nl.acs.storage_cell.service.StorageCellService;
import org.nl.acs.storage_cell.service.dto.StorageCellDto;
import org.nl.acs.storage_cell.service.dto.StorageCellQueryParam;
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;
import java.util.Set;

/**
* @author jiaolm
* @date 2023-05-09
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "货位信息管理")
@RequestMapping("/api/storageCell")
public class StorageCellController {

    private final StorageCellService storageCellService;

    @GetMapping
    @Log("查询库位管理")
    @ApiOperation("查询库位管理")
    //@PreAuthorize("@el.check('storageCell:list')")
    public ResponseEntity<Object> query(@RequestParam Map whereJson, Pageable page) {
        return new ResponseEntity<>(storageCellService.queryAll(whereJson, page), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增库位管理")
    @ApiOperation("新增库位管理")
    //@PreAuthorize("@el.check('storageCell:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody StorageCellDto dto) {
        storageCellService.create(dto);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改库位管理")
    @ApiOperation("修改库位管理")
    //@PreAuthorize("@el.check('storageCell:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody StorageCellDto dto) {
        storageCellService.update(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除库位管理")
    @ApiOperation("删除库位管理")
    //@PreAuthorize("@el.check('storageCell:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        storageCellService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("导出库位管理")
    @ApiOperation("导出库位管理")
    @GetMapping(value = "/download")
    //@PreAuthorize("@el.check('storageCell:list')")
    public void download(HttpServletResponse response, @RequestParam Map whereJson) throws IOException {
        storageCellService.download(storageCellService.queryAll(whereJson), response);
    }
}
