package com.railway.web.controller.business.baseinfo;

import com.railway.business.baseinfo.domain.BaseDeptStation;
import com.railway.business.baseinfo.domain.BusBlq;
import com.railway.business.baseinfo.service.IBaseStationService;
import com.railway.business.baseinfo.service.IBusBlqService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.railway.common.core.controller.BaseController;
import com.railway.common.core.domain.AjaxResult;
import com.railway.common.core.page.TableDataInfo;
import javax.validation.Valid;
import java.util.List;

/**
 * @author wuhonghao
 */
@Api(value = "rest/catenary/bus/blq", tags = "基础管理 - 避雷器")
@RestController
@Validated
@RequestMapping(value = "business/catenary/bus/blq")
public class BusBlqController extends BaseController {

  private final IBusBlqService busBlqService;
  private final IBaseStationService stationService;

  public BusBlqController(IBusBlqService busBlqService, IBaseStationService stationService) {
    this.busBlqService = busBlqService;
    this.stationService = stationService;
  }

  @ApiOperation(value = "新增")
  @PostMapping("/add")
  public AjaxResult add(@Validated @RequestBody BusBlq busBlq) {
    BaseDeptStation deptStation = stationService.getInfo(busBlq.getDeptId(), busBlq.getLineId(),
        busBlq.getStationId());
    busBlq.setDeptStationId(deptStation.getId());
    return toAjax(busBlqService.create(busBlq));
  }

  @ApiOperation(value = "删除")
  @DeleteMapping("/{ids}")
  public AjaxResult delete(@RequestParam String[] ids) {
    return toAjax(busBlqService.delete(ids));
  }

  @ApiOperation(value = "更新")
  @PutMapping("/update")
  public AjaxResult update(@RequestBody @Valid BusBlq busBlq) {
    BaseDeptStation deptStation = stationService.getInfo(busBlq.getDeptId(), busBlq.getLineId(),
        busBlq.getStationId());
    busBlq.setDeptStationId(deptStation.getId());
    return toAjax(busBlqService.update(busBlq));
  }

  @ApiOperation(value = "单个")
  @GetMapping(value = {"/", "/{id}"})
  public AjaxResult getInfo(String id) {
    BusBlq info = busBlqService.getInfo(id);
    AjaxResult ajax = AjaxResult.success();
    ajax.put("info", info);
    return ajax;
  }

  @ApiOperation(value = "列表")
  @GetMapping(value = "list")
  public TableDataInfo getList(BusBlq busBlq) {
    startPage();
    List<BusBlq> list = busBlqService.getList(busBlq);
    return getDataTable(list);
  }

}
