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

import com.github.pagehelper.Page;
import com.railway.business.baseinfo.domain.BaseDeptStation;
import com.railway.business.baseinfo.service.IBaseStationService;
import com.railway.business.catenary.domain.BusJceb;
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.JcebOrderFieldEnum;
import com.railway.business.catenary.enums.JcebStatusEnum;
import com.railway.business.catenary.mapper.BusJcebMapper;
import com.railway.business.catenary.service.IBusJcebService;
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;
import org.springframework.transaction.annotation.Transactional;

/**
 * 检测表抽象服务类
 *
 * @author zhaomn
 */
@Slf4j
@Service
public abstract class BusJcebServiceImpl<T extends BusJceb> implements IBusJcebService<T> {

  private final BusJcebMapper<T> busJcebMapper;
  private final IBaseStationService stationService;

  public BusJcebServiceImpl(BusJcebMapper<T> busJcebMapper, IBaseStationService stationService) {
    this.busJcebMapper = busJcebMapper;
    this.stationService = stationService;
  }

  /**
   * 删除
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int delete(String[] ids) {
    int r = 0;
    for (String id : ids) {
      int j = busJcebMapper.delete(id);
      r = r + j;
    }
    return r;
  }

  /**
   * 查询列表
   */
  @Override
  public Page<JcebRecordVo> getListByType(JcebQueryVo query) {
    // 设置查询权限
    JcebQueryUtil.setBusJcebQuery(query);
    return busJcebMapper.getListByType(query);
  }

  /**
   * 查询汇总列表
   */
  @Override
  public JcebCountVo getCountByType(JcebQueryVo query) {
    // 设置查询权限
    JcebQueryUtil.setBusJcebQuery(query);
    return busJcebMapper.getCountByType(query);
  }

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

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

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

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

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

  /**
   * 退回
   */
  protected void setReject(BusJceb jceb) {
    jceb.setSubmitState(JcebStatusEnum.RETURNED.getCode());
  }

  /**
   * 确认
   */
  protected void setConfirm(BusJceb jceb) {
    jceb.setSubmitState(JcebStatusEnum.COMPLETED.getCode());
  }

}
