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

import com.railway.business.baseinfo.domain.BaseDeptStation;
import com.railway.business.baseinfo.domain.BusDmcgyq;
import com.railway.business.baseinfo.service.IBaseStationService;
import com.railway.business.baseinfo.service.IBusDmcgyqService;
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 zhangcaifeng
 */
@Api(value = "rest/catenary/bus/dmcgyq", tags = "基础数据 - 地面磁感应器")
@RestController
@Validated
@RequestMapping(value = "business/catenary/bus/dmcgyq")
public class BusDmcgyqController extends BaseController {

  private final IBusDmcgyqService busDmcgyqService;
  private final IBaseStationService stationService;

  public BusDmcgyqController(IBusDmcgyqService busDmcgyqService,
      IBaseStationService stationService) {
    this.busDmcgyqService = busDmcgyqService;
    this.stationService = stationService;
  }

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

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

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

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

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

}
