package org.nl.acs.route.rest;

import org.nl.common.logging.annotation.Log;
import org.nl.acs.route.service.RouteLineService;
import org.nl.acs.route.service.dto.RouteLineDto;
import org.nl.acs.route.service.dto.RouteLineQueryParam;
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.sql.SQLException;
import java.util.Map;
import java.util.Set;

/**
* @author jiaolm
* @date 2023-05-09
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "路由路线管理")
@RequestMapping("/api/routeLine")
public class RouteLineController {

    private final RouteLineService routeLineService;


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

    @PostMapping
    @Log("新增路由路线")
    @ApiOperation("新增路由路线")
    //@PreAuthorize("@el.check('routeLine:add')")
    public ResponseEntity<Object> create(@RequestBody Map data) throws SQLException {
        routeLineService.create(data);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @GetMapping("/reload")
    @Log("数据同步")
    @ApiOperation("数据同步")
    //@PreAuthorize("@el.check('task:list')")
    public ResponseEntity<Object> reload() throws SQLException {
        routeLineService.reload();
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @PutMapping
    @Log("修改路由路线")
    @ApiOperation("修改路由路线")
    //@PreAuthorize("@el.check('routeLine:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody RouteLineDto dto) throws SQLException {
        routeLineService.update(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PutMapping("/enabled")
    @Log("启用禁用路由路线")
    @ApiOperation("启用禁用路由路线")
    //@PreAuthorize("@el.check('routeLine:edit')")
    public ResponseEntity<Object> enabled(@Validated @RequestBody RouteLineDto dto) throws SQLException {
        routeLineService.enabled(dto.getLine_uuid(), dto.getIs_active());
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


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

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


    @Log("查询最优路由路线")
    @ApiOperation("查询最优路由路线")
    @PostMapping(value = "/getShortPathLines")
    public ResponseEntity<Object> getShortPathLines(String device_code, String next_device_code, String route_plan_code) {
        return new ResponseEntity<>(routeLineService.getShortPathLines(device_code, next_device_code, route_plan_code), HttpStatus.OK);
    }
}
