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

import com.railway.business.baseinfo.domain.BaseDeptStation;
import com.railway.business.baseinfo.domain.BusGlkg;
import com.railway.business.baseinfo.service.IBaseStationService;
import com.railway.business.baseinfo.service.IBusGlkgService;
import com.railway.common.core.controller.BaseController;
import com.railway.common.core.domain.AjaxResult;
import com.railway.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author fenghao
 */
@Api(value = "rest/catenary/bus/glkg", tags = "基础数据 - 隔离开关")
@RestController
@Validated
@RequestMapping(value = "business/catenary/bus/glkg")
public class BusGlkgController extends BaseController {

  private final IBusGlkgService busGlkgService;
  private final IBaseStationService stationService;

  public BusGlkgController(IBusGlkgService busGlkgService,
      IBaseStationService stationService) {
    this.busGlkgService = busGlkgService;
    this.stationService = stationService;
  }

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

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

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

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

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

}
