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

import com.railway.business.catenary.domain.vo.JcebCountVo;
import com.railway.business.catenary.domain.vo.JcebQueryVo;
import com.railway.business.catenary.domain.vo.JcebRecordVo;
import com.railway.business.catenary.enums.JcebTypeEnum;
import com.railway.business.catenary.enums.SortTypeEnum;
import com.railway.business.catenary.service.IBusJcbBlqService;
import com.railway.business.catenary.service.IBusJcbDmcgyqService;
import com.railway.business.catenary.service.IBusJcbFdfxjyqService;
import com.railway.business.catenary.service.IBusJcbGlkgService;
import com.railway.business.catenary.service.IBusJcbJcxcService;
import com.railway.business.catenary.service.IBusJcbWjcxcService;
import com.railway.business.catenary.service.IBusJcebGjsfxService;
import com.railway.business.catenary.service.IBusJcebJddzService;
import com.railway.business.catenary.service.IBusJcebJyzqsjlService;
import com.railway.business.catenary.service.IBusJcebLbjghjlService;
import com.railway.business.catenary.service.IBusJcebQmjcjlService;
import com.railway.business.catenary.service.IBusJcebXsjcjlService;
import com.railway.common.core.controller.BaseController;
import com.railway.common.core.domain.AjaxResult;
import com.railway.common.core.page.TableDataInfo;
import com.railway.common.utils.StringUtils;
import com.railway.system.service.ISysFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ZhaoMn
 */
@Slf4j
@Api(value = "rest/catenary/bus/jceb/view", tags = "检测表 - 汇总接口")
@RestController
@Validated
@RequestMapping(value = "business/catenary/bus/jceb/view")
public class BusJcebController extends BaseController {

  private final IBusJcebXsjcjlService xsjcjlService;
  private final IBusJcebQmjcjlService qmjcjlService;
  private final IBusJcebLbjghjlService lbjghjlService;
  private final IBusJcebJyzqsjlService jyzqsjlService;
  private final IBusJcebJddzService jddzService;
  private final IBusJcebGjsfxService gjsfxService;

  private final IBusJcbBlqService blqService;
  private final IBusJcbDmcgyqService dmcgyqService;
  private final IBusJcbFdfxjyqService fdfxjyqService;
  private final IBusJcbGlkgService glkgService;
  private final IBusJcbJcxcService jcxcService;
  private final IBusJcbWjcxcService wjcxcService;

  private final ISysFileService fileService;

  public BusJcebController(
      IBusJcebXsjcjlService xsjcjlService,
      IBusJcebQmjcjlService qmjcjlService,
      IBusJcebLbjghjlService lbjghjlService,
      IBusJcebJyzqsjlService jyzqsjlService,
      IBusJcebJddzService jddzService,
      IBusJcebGjsfxService gjsfxService,
      IBusJcbBlqService blqService,
      IBusJcbDmcgyqService dmcgyqService,
      IBusJcbFdfxjyqService fdfxjyqService,
      IBusJcbGlkgService glkgService,
      IBusJcbJcxcService jcxcService,
      IBusJcbWjcxcService wjcxcService, ISysFileService fileService) {
    this.xsjcjlService = xsjcjlService;
    this.qmjcjlService = qmjcjlService;
    this.lbjghjlService = lbjghjlService;
    this.jyzqsjlService = jyzqsjlService;
    this.jddzService = jddzService;
    this.gjsfxService = gjsfxService;
    this.blqService = blqService;
    this.dmcgyqService = dmcgyqService;
    this.fdfxjyqService = fdfxjyqService;
    this.glkgService = glkgService;
    this.jcxcService = jcxcService;
    this.wjcxcService = wjcxcService;
    this.fileService = fileService;
  }


  @ApiOperation(value = "列表")
  @GetMapping(value = "listByType")
  public TableDataInfo listByType(JcebQueryVo query) {
    startPage();
    JcebTypeEnum jcebTypeEnum = JcebTypeEnum.ofValue(query.getJcebType());
    List<JcebRecordVo> list = null;
    switch (jcebTypeEnum) {
      case Jddz:
        list = jddzService.getListByType(query);
        break;
      case Gjsfx:
        list = gjsfxService.getListByType(query);
        break;
      case Qmjcjl:
        list = qmjcjlService.getListByType(query);
        break;
      case Xsjcjl:
        list = xsjcjlService.getListByType(query);
        break;
      case Jyzqsjl:
        list = jyzqsjlService.getListByType(query);
        break;
      case Lbjghjl:
        list = lbjghjlService.getListByType(query);
        break;
      case Blq:
        list = blqService.getListByType(query);
        break;
      case Dmcgyq:
        list = dmcgyqService.getListByType(query);
        break;
      case Fdfxjyq:
        list = fdfxjyqService.getListByType(query);
        break;
      case Glkg:
        list = glkgService.getListByType(query);
        break;
      case Jcxc:
        list = jcxcService.getListByType(query);
        break;
      case Wjcxc:
        list = wjcxcService.getListByType(query);
        break;
      default:
        break;
    }
    return getDataTable(list);
  }

  @ApiOperation(value = "导出Excel")
  @GetMapping(value = "exportExcel")
  public AjaxResult exportExcel(JcebQueryVo query) {
    JcebTypeEnum jcebTypeEnum = JcebTypeEnum.ofValue(query.getJcebType());
    String localFilePath = null;

    switch (jcebTypeEnum) {
      case Jddz:
        localFilePath = jddzService.exportExcel(query);
        break;
      case Gjsfx:
        localFilePath = gjsfxService.exportExcel(query);
        break;
      case Qmjcjl:
        localFilePath = qmjcjlService.exportExcel(query);
        break;
      case Xsjcjl:
        localFilePath = xsjcjlService.exportExcel(query);
        break;
      case Jyzqsjl:
        localFilePath = jyzqsjlService.exportExcel(query);
        break;
      case Lbjghjl:
        localFilePath = lbjghjlService.exportExcel(query);
        break;
      case Blq:
        localFilePath = blqService.exportExcel(query);
        break;
      case Dmcgyq:
        localFilePath = dmcgyqService.exportExcel(query);
        break;
      case Fdfxjyq:
        localFilePath = fdfxjyqService.exportExcel(query);
        break;
      case Glkg:
        localFilePath = glkgService.exportExcel(query);
        break;
      case Jcxc:
        localFilePath = jcxcService.exportExcel(query);
        break;
      case Wjcxc:
        localFilePath = wjcxcService.exportExcel(query);
        break;
      default:
        break;
    }
    if (StringUtils.isEmpty(localFilePath)) {
      return AjaxResult.error("下载失败");
    }
    return fileService.uploadFile(localFilePath);
  }

  @ApiOperation(value = "汇总列表")
  @GetMapping(value = "listAll")
  public TableDataInfo listAll(JcebQueryVo query) {
    startPage();
    List<JcebCountVo> list = new ArrayList<>();
    for (JcebTypeEnum jcebTypeEnum : JcebTypeEnum.values()) {
      JcebCountVo jcebRecordVo = null;
      switch (jcebTypeEnum) {
        case Jddz:
          jcebRecordVo = jddzService.getCountByType(query);
          break;
        case Gjsfx:
          jcebRecordVo = gjsfxService.getCountByType(query);
          break;
        case Qmjcjl:
          jcebRecordVo = qmjcjlService.getCountByType(query);
          break;
        case Xsjcjl:
          jcebRecordVo = xsjcjlService.getCountByType(query);
          break;
        case Jyzqsjl:
          jcebRecordVo = jyzqsjlService.getCountByType(query);
          break;
        case Lbjghjl:
          jcebRecordVo = lbjghjlService.getCountByType(query);
          break;
        case Blq:
          jcebRecordVo = blqService.getCountByType(query);
          break;
        case Dmcgyq:
          jcebRecordVo = dmcgyqService.getCountByType(query);
          break;
        case Fdfxjyq:
          jcebRecordVo = fdfxjyqService.getCountByType(query);
          break;
        case Glkg:
          jcebRecordVo = glkgService.getCountByType(query);
          break;
        case Jcxc:
          jcebRecordVo = jcxcService.getCountByType(query);
          break;
        case Wjcxc:
          jcebRecordVo = wjcxcService.getCountByType(query);
          break;
        default:
          break;
      }
      if (null != jcebRecordVo) {
        jcebRecordVo.setJcebType(jcebTypeEnum.getCode());
        list.add(jcebRecordVo);
      }
    }
    // 汇总结果按日期排序
    SortTypeEnum sortType = SortTypeEnum.ofValue(query.getIsAsc());
    if (null == sortType || sortType == SortTypeEnum.DESC) {
      list.sort(Comparator.comparing(JcebCountVo::getOperationDate).reversed()
          .thenComparing(JcebCountVo::getCount).reversed()
          .thenComparing(JcebCountVo::getJcebType));
    } else {
      log.info("排序类型 - {}", sortType.getNotes());
      list.sort(Comparator.comparing(JcebCountVo::getOperationDate)
          .thenComparing(JcebCountVo::getCount).thenComparing(JcebCountVo::getJcebType));
    }
    return getDataTable(list);
  }

}
