package org.nl.acs.address.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.nl.acs.address.service.AddressService;
import org.nl.acs.address.service.dto.AddressDto;
import org.nl.common.logging.annotation.Log;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
* @author jiaolm
* @date 2023-05-10
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "接口方法地址管理")
@RequestMapping("/api/Address")
public class AddressController {

    private final AddressService addressService;

    @GetMapping
    @Log("查询接口方法地址")
    @ApiOperation("查询接口方法地址")
    //@PreAuthorize("@el.check('Address:list')")
    public ResponseEntity<Object> query(@RequestParam Map whereJson, Pageable page) {
        return new ResponseEntity<>(addressService.queryAll(whereJson, page), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增接口方法地址")
    @ApiOperation("新增接口方法地址")
    //@PreAuthorize("@el.check('Address:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody AddressDto dto) {
        addressService.create(dto);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改接口方法地址")
    @ApiOperation("修改接口方法地址")
    //@PreAuthorize("@el.check('Address:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody AddressDto dto) {
        addressService.update(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除接口方法地址")
    @ApiOperation("删除接口方法地址")
    //@PreAuthorize("@el.check('Address:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        addressService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("导出接口方法地址")
    @ApiOperation("导出接口方法地址")
    @GetMapping(value = "/download")
    //@PreAuthorize("@el.check('Address:list')")
    public void download(HttpServletResponse response, @RequestParam Map whereJson) throws IOException {
        addressService.download(addressService.queryAll(whereJson), response);
    }
}
