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

import com.railway.business.baseinfo.domain.BaseDeptStation;
import com.railway.business.baseinfo.domain.BusFjxgmd;
import com.railway.business.baseinfo.domain.vo.BusFjxgmdQueryVo;
import com.railway.business.baseinfo.service.IBaseStationService;
import com.railway.business.baseinfo.service.IBusFjxgmdService;
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.ArrayList;

import org.apache.commons.lang3.StringUtils;
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/fjxgmd", tags = "基础数据 - 附加悬挂锚段")
@RestController
@Validated
@RequestMapping(value = "business/catenary/bus/fjxgmd")
public class BusFjxgmdController extends BaseController {

  private final IBusFjxgmdService busFjxgmdService;
  private final IBaseStationService stationService;

  public BusFjxgmdController(IBusFjxgmdService busFjxgmdService,
      IBaseStationService stationService) {
    this.busFjxgmdService = busFjxgmdService;
    this.stationService = stationService;
  }

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

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

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

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

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

  @ApiOperation(value = "通过支柱信息获取对锚列表", notes = "通过支柱信息获取对锚列表,需要传查询条件", response = AjaxResult.class)
  @GetMapping("/getListByZz")
  public TableDataInfo getListByZz(BusFjxgmdQueryVo busFjxgmdQueryVo) {
    startPage();
    List<BusFjxgmd> list = new ArrayList<BusFjxgmd>();
    if (busFjxgmdQueryVo != null && StringUtils.isNoneBlank(busFjxgmdQueryVo.getXingbie())
        && busFjxgmdQueryVo.getLineId() != null) {
      list = busFjxgmdService.getListByZz(busFjxgmdQueryVo);
    } else {
      list = busFjxgmdService.getList(new BusFjxgmd());
    }
    return getDataTable(list);
  }

}
