package org.nl.acs.route.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.nl.acs.route.service.RouteLocService;
import org.nl.acs.route.service.dto.RouteLocDto;
import org.nl.acs.route.service.dto.RouteLocQueryParam;
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;
import java.util.Set;

/**
* @author jiaolm
* @date 2023-05-09
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "路由坐标管理")
@RequestMapping("/api/routeLoc")
public class RouteLocController {

    private final RouteLocService routeLocService;

    @GetMapping
    @Log("查询路由坐标")
    @ApiOperation("查询路由坐标")
    //@PreAuthorize("@el.check('routeLoc:list')")
    public ResponseEntity<Object> query(@RequestParam Map whereJson, Pageable page) {
        return new ResponseEntity<>(routeLocService.queryAll(whereJson, page), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增路由坐标")
    @ApiOperation("新增路由坐标")
    //@PreAuthorize("@el.check('routeLoc:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody RouteLocDto dto) {
        routeLocService.create(dto);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改路由坐标")
    @ApiOperation("修改路由坐标")
    //@PreAuthorize("@el.check('routeLoc:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody RouteLocDto dto) {
        routeLocService.update(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除路由坐标")
    @ApiOperation("删除路由坐标")
    //@PreAuthorize("@el.check('routeLoc:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        routeLocService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("导出路由坐标")
    @ApiOperation("导出路由坐标")
    @GetMapping(value = "/download")
    //@PreAuthorize("@el.check('routeLoc:list')")
    public void download(HttpServletResponse response, Map whereJson) throws IOException {
        routeLocService.download(routeLocService.queryAll(whereJson), response);
    }
}
