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

import com.railway.business.baseinfo.domain.BaseDeptStation;
import com.railway.business.baseinfo.domain.BusSdtz;
import com.railway.business.baseinfo.domain.BusZzdzxx;
import com.railway.business.baseinfo.domain.vo.BusSmartQueryVo;
import com.railway.business.baseinfo.service.IBaseStationService;
import com.railway.business.baseinfo.service.IBusSdtzService;
import com.railway.business.baseinfo.service.IBusZzdzxxService;
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 wuhonghao
 */
@Api(value = "rest/catenary/bus/sdtz", tags = "智能查询 - 隧道台账")
@RestController
@Validated
@RequestMapping(value = "business/catenary/bus/sdtz")
public class BusSdtzController extends BaseController {

  private final IBusSdtzService busSdtzService;
  private final IBaseStationService stationService;
  private final IBusZzdzxxService busZzdzxxService;

  public BusSdtzController(IBusSdtzService busSdtzService,
      IBaseStationService stationService, IBusZzdzxxService busZzdzxxService) {
    this.busSdtzService = busSdtzService;
    this.stationService = stationService;
    this.busZzdzxxService = busZzdzxxService;
  }

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

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

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

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

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

  @ApiOperation(value = "智能查询")
  @GetMapping(value = "smartQuery")
  public TableDataInfo smartQuery(BusSmartQueryVo busSmartQueryVo) {
    //如果传入查询条件为支柱号则将支柱号转化为公里标
    if(busSmartQueryVo.getPillarCode()!=null){
      BusZzdzxx busZzdzxx = new BusZzdzxx();
      busZzdzxx.setPillarCode(busSmartQueryVo.getPillarCode());
      busZzdzxx.setXingbie(busSmartQueryVo.getXingbie());
      busZzdzxx.setStationId(busSmartQueryVo.getStationId());
      List<BusZzdzxx> list = busZzdzxxService.getList(busZzdzxx);
      if(list!=null&&list.size()==1){
        busSmartQueryVo.setMarker(list.get(0).getMarker());
      }else{
        return null;
      }
    }
    if(busSmartQueryVo.getPillarArea()==null||busSmartQueryVo.getPillarArea().equals("")){
      busSmartQueryVo.setPillarArea("500");
    }
    startPage();
    List<BusSdtz> list = busSdtzService.smartQuery(busSmartQueryVo);
    return getDataTable(list);
  }

}
