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 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 java.util.List;
import javax.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhangcaifeng
 */
@Api(value = "rest/catenary/bus/wjcxc", tags = "基础数据 - 无交叉线岔")
@RestController
@Validated
@RequestMapping(value = "business/catenary/bus/wjcxc")
public class BusWjcxcController extends BaseController {

  private final IBusJcxcService busJcxcService;
  private final IBaseStationService stationService;

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

  private final static String FORK_TYPE = "wjcxc";

  @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);
  }

}
