package com.railway.business.catenary.service.impl;

import com.railway.business.baseinfo.domain.BaseDeptStation;
import com.railway.business.baseinfo.service.IBaseStationService;
import com.railway.business.catenary.domain.BusJcab;
import com.railway.business.catenary.domain.vo.JcebQueryVo;
import com.railway.business.catenary.enums.JcebOrderFieldEnum;
import com.railway.business.catenary.enums.JcebStatusEnum;
import com.railway.business.catenary.util.JcebQueryUtil;
import com.railway.common.enums.DelFlagEnum;
import com.railway.common.utils.SecurityUtils;
import com.railway.common.utils.StringUtils;
import com.railway.common.utils.sql.SqlUtil;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 检查表抽象服务类
 *
 * @author ZhaoMn
 * @date 2021/12/10
 */
@Slf4j
@Service
public abstract class BusJcabServiceImpl {

  private final IBaseStationService stationService;

  protected BusJcabServiceImpl(IBaseStationService stationService) {
    this.stationService = stationService;
  }

  protected void setJcebQuery(JcebQueryVo query) {
    // 设置查询权限
    JcebQueryUtil.setBusJcebQuery(query);
    query.setSubmitState(JcebStatusEnum.COMPLETED.getCode());
  }

  /**
   * 列表
   */
  protected void setList(BusJcab jcab) {
    // 设置查询权限
    JcebQueryUtil.setBusJcab(jcab);
    // 设置查询参数
    if (StringUtils.isNotEmpty(jcab.getOperator()) && StringUtils.isEmpty(jcab.getCheckUser())) {
      jcab.setCheckUser(jcab.getOperator());
    }
    if (StringUtils.isNotNull(jcab.getOperationDate()) && StringUtils.isNull(jcab.getCheckDate())) {
      jcab.setCheckDate(jcab.getOperationDate());
    }
    // 设置查询排序字段
    // 取BusJcab类的所有属性
    Field[] fields = BusJcab.class.getDeclaredFields();
    List<String> fieldList = Arrays.stream(fields).map(Field::getName).collect(Collectors.toList());
    // 如果排序字段不是BusJcab类的属性时，取默认排序字段
    log.info("前端指定的排序字段 - {}", jcab.getOrderBy());
    if (StringUtils.isNotEmpty(jcab.getOrderBy()) && fieldList.contains(jcab.getOrderBy())) {
      jcab.setOrderBy(SqlUtil.humpToLine(jcab.getOrderBy()));
      if (JcebOrderFieldEnum.OPERATION_DATE.getField().equals(jcab.getOrderBy())) {
        jcab.setOrderBy(JcebOrderFieldEnum.CHECK_DATE.getField());
      }
    } else {
      // 默认倒序排列
      jcab.setOrderBy(JcebOrderFieldEnum.CHECK_DATE.getField());
      jcab.setIsAsc("0");
    }
    log.info("最终使用的排序字段 - {}", jcab.getOrderBy());
  }

  /**
   * 创建
   */
  protected void setCreate(BusJcab jcab) {
    // 设置关联ID
    setDeptStationId(jcab);
    if (StringUtils.isBlank(jcab.getSubmitState())) {
      jcab.setSubmitState(JcebStatusEnum.UNREPORTED.getCode());
    }
    jcab.setDelFlag(DelFlagEnum.NOT_DELETE.getCode());
    jcab.setCreateTime(new Date());
    jcab.setCreateBy(SecurityUtils.getUsername());
  }

  /**
   * 更新
   */
  protected void setUpdate(BusJcab jcab) {
    if (StringUtils.isNotEmpty(jcab.getStationId())) {
      // 设置关联ID
      setDeptStationId(jcab);
    }
    jcab.setUpdateTime(new Date());
    jcab.setUpdateBy(SecurityUtils.getUsername());
  }

  /**
   * 设置区间ID
   */
  protected void setDeptStationId(BusJcab jcab) {
    BaseDeptStation deptStation = stationService.getInfo(jcab.getDeptId(),
        jcab.getLineId(), jcab.getStationId());
    jcab.setDeptStationId(deptStation.getId());
  }

  /**
   * 提交
   */
  protected void setSubmit(BusJcab jcab) {
    jcab.setSubmitState(JcebStatusEnum.REPORTED.getCode());
  }

  /**
   * 退回
   */
  protected void setReject(BusJcab jcab) {
    jcab.setConfirmUser(SecurityUtils.getUsername());
    jcab.setSubmitState(JcebStatusEnum.RETURNED.getCode());
  }

  /**
   * 确认
   */
  protected void setConfirm(BusJcab jcab) {
    jcab.setConfirmUser(SecurityUtils.getUsername());
    jcab.setSubmitState(JcebStatusEnum.COMPLETED.getCode());
  }

}
