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

import com.github.pagehelper.Page;
import com.railway.business.baseinfo.service.IBaseStationService;
import com.railway.business.catenary.domain.BusJcbJcxc;
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.domain.vo.JcebStationVo;
import com.railway.business.catenary.enums.JcebTypeEnum;
import com.railway.business.catenary.mapper.BusJcbJcxcMapper;
import com.railway.business.catenary.service.IBusJcbJcxcService;
import com.railway.business.push.service.IBusPushMsgService;
import com.railway.common.utils.SecurityUtils;
import com.railway.common.utils.StringUtils;
import com.railway.common.utils.poi.EasyExcelUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 检查表-交叉线岔
 *
 * @author zcf
 * @date 2021/10/18
 */
@Service
public class BusJcbJcxcServiceImpl extends BusJcabServiceImpl implements IBusJcbJcxcService {

  private final BusJcbJcxcMapper busJcbJcxcMapper;

  public BusJcbJcxcServiceImpl(BusJcbJcxcMapper busJcbJcxcMapper,
      IBaseStationService stationService, IBusPushMsgService busPushMsgService) {
    super(stationService, busPushMsgService);
    this.busJcbJcxcMapper = busJcbJcxcMapper;
  }

  /**
   * 新增
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int create(BusJcbJcxc busJcbJcxc) {
    setCreate(busJcbJcxc);
    busJcbJcxc.setUserId(SecurityUtils.getUserId());
    return busJcbJcxcMapper.insert(busJcbJcxc);
  }

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

  /**
   * 获取检查表类型名
   * @return 类型名
   */
  @Override
  JcebTypeEnum getJcebTypeEnum() {
    return JcebTypeEnum.Jcxc;
  }

  /**
   * 更新
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int update(BusJcbJcxc busJcbJcxc) {
    setUpdate(busJcbJcxc);
    return busJcbJcxcMapper.update(busJcbJcxc);
  }

  /**
   * 获取单个
   */
  @Override
  public BusJcbJcxc getInfo(String id) {
    return busJcbJcxcMapper.getInfo(id);
  }

  /**
   * 查询列表
   */
  @Override
  public List<BusJcbJcxc> getList(BusJcbJcxc busJcbJcxc) {
    setList(busJcbJcxc);
    return busJcbJcxcMapper.getList(busJcbJcxc);
  }

  /**
   * 获取上次检查记录
   */
  @Override
  public BusJcbJcxc getLastRecord() {
    return busJcbJcxcMapper.getLastRecord(SecurityUtils.getUserId());
  }

  /**
   * 提交
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int submit(BusJcbJcxc bean) {
    setSubmit(bean);
    int ret;
    if (StringUtils.isEmpty(bean.getId())) {
      ret = create(bean);
    } else {
      ret = update(bean);
    }
    // 推送消息
    if(ret > 0) {
      pushSubmit(bean);
    }
    return ret;
  }

  /**
   * 退回
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int reject(BusJcbJcxc busJcbJcxc) {
    setReject(busJcbJcxc);
    return update(busJcbJcxc);
  }

  /**
   * 确认
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int confirm(BusJcbJcxc busJcbJcxc) {
    setConfirm(busJcbJcxc);
    return update(busJcbJcxc);
  }

  /**
   * 导出Excel
   *
   * @param query 查询条件
   * @return 文件路径
   */
  @Override
  public String exportExcel(JcebQueryVo query) {
      List<BusJcbJcxc> list = busJcbJcxcMapper.getJcebList(query);
      list = list.stream().map(x -> transData(x)).collect(Collectors.toList());
      Map<String, Object> params = new HashMap<>();
      String fileName = "交叉线岔检测（修）记录.xls";
      String templateFileName = "excel/template/交叉线岔检测（修）记录.xls";
      return new EasyExcelUtil().getExcel(params, list, templateFileName, fileName);
  }

  private BusJcbJcxc transData(BusJcbJcxc x) {
    x.setDcxh("18");
    x.setXiangbie("检测");
    if((Integer.parseInt(x.getXch()) & 1) == 1){
      // xch奇数
      if(x.getLgzzgc() != null) {
        x.setXxfx(x.getLgzzgc());
      } else {
        x.setXxfx(x.getGzyfzgc());
      }
    } else {
      // xch偶数
      if(x.getLgzzgc() != null) {
        x.setSxfx(x.getLgzzgc());
      } else {
        x.setSxfx(x.getGzyfzgc());
      }
    }
    x.setWxj(StringUtils.YES_VALUE.equals(x.getWxj()) ? "无" : "有");
    if(StringUtils.YES_VALUE.equals(x.getXcxzgazlg()) &&
            StringUtils.YES_VALUE.equals(x.getHdjx()) &&
            StringUtils.YES_VALUE.equals(x.getFhzzsq()) &&
            StringUtils.YES_VALUE.equals(x.getDqssxx()) &&
            StringUtils.YES_VALUE.equals(x.getLswst()) &&
            StringUtils.YES_VALUE.equals(x.getXzgldydx()) &&
            StringUtils.YES_VALUE.equals(x.getDxztlh()) &&
            StringUtils.YES_VALUE.equals(x.getWxj()) &&
            StringUtils.YES_VALUE.equals(x.getHddxxjzt()) &&
            StringUtils.YES_VALUE.equals(x.getJcdxzt()) &&
            StringUtils.YES_VALUE.equals(x.getDljxzt())
    ) {
      x.setGlbjzt("良好");
    } else {
      x.setGlbjzt("故障");
    }
    return x;

  }

  /**
   * 查询明细列表
   * @param query 查询参数
   * @return 数据记录明细
   */
  @Override
  public Page<JcebRecordVo> getListByType(JcebQueryVo query) {
    setJcebQuery(query);
    return busJcbJcxcMapper.getListByType(query);
  }

  /**
   * 查询汇总列表
   * @param query 查询参数
   * @return 汇总数据
   */
  @Override
  public JcebCountVo getCountByType(JcebQueryVo query) {
    setJcebQuery(query);
    return busJcbJcxcMapper.getCountByType(query);
  }

  /**
   * 查询筛选车间列表
   *
   * @param query 查询条件
   * @return 车间列表
   */
  @Override
  public List<JcebStationVo> listStation(JcebQueryVo query) {
    return busJcbJcxcMapper.listStation(query);
  }

}
