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.RoutePlanService;
import org.nl.acs.route.service.dto.RoutePlanDto;
import org.nl.acs.route.service.dto.RoutePlanQueryParam;
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/routePlan")
public class RoutePlanController {

    private final RoutePlanService routePlanService;

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

    @PostMapping
    @Log("新增路由方案")
    @ApiOperation("新增路由方案")
    //@PreAuthorize("@el.check('routePlan:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody RoutePlanDto dto) {
        routePlanService.create(dto);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改路由方案")
    @ApiOperation("修改路由方案")
    //@PreAuthorize("@el.check('routePlan:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody RoutePlanDto dto) {
        routePlanService.update(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

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

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

    @GetMapping("/selectList")
    @Log("下拉选路由方案")
    @ApiOperation("下拉选路由方案")
    //@PreAuthorize("@el.check('routePlan:list')")
    public ResponseEntity<Object> selectList() {
        return new ResponseEntity<>(routePlanService.selectList(), HttpStatus.OK);
    }
}
