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

import com.railway.business.baseinfo.domain.BaseDeptStation;
import com.railway.business.baseinfo.domain.BusJcxc;
import com.railway.business.baseinfo.service.IBaseStationService;
import com.railway.business.baseinfo.service.IBusJcxcService;
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/jcxc", tags = "基础数据 - 交叉线岔")
@RestController
@Validated
@RequestMapping(value = "business/catenary/bus/jcxc")
public class BusJcxcController extends BaseController {

  private final IBusJcxcService busJcxcService;
  private final IBaseStationService stationService;

  public BusJcxcController(IBusJcxcService busJcxcService,
      IBaseStationService stationService) {
    this.busJcxcService = busJcxcService;
    this.stationService = stationService;
  }

  private final static String FORK_TYPE = "jcxc";

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

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

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

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

  @ApiOperation(value = "列表")
  @GetMapping(value = "list")
  public TableDataInfo getList(BusJcxc busJcxc) {
    startPage();
    busJcxc.setXclx(FORK_TYPE);
    List<BusJcxc> list = busJcxcService.getList(busJcxc);
    return getDataTable(list);
  }

}
