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.enums.JcebStatusEnum;
import com.railway.business.catenary.mapper.BusJcbJcxcMapper;
import com.railway.business.catenary.service.IBusJcbJcxcService;
import com.railway.common.utils.SecurityUtils;
import com.railway.common.utils.StringUtils;
import com.railway.common.utils.poi.EasyExcelUtil;
import java.util.List;
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) {
    super(stationService);
    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;
  }

  /**
   * 更新
   */
  @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 busJcbJcxc) {
    busJcbJcxc.setSubmitState(JcebStatusEnum.REPORTED.getCode());
    if (StringUtils.isEmpty(busJcbJcxc.getId())) {
      return create(busJcbJcxc);
    } else {
      return update(busJcbJcxc);
    }
  }

  /**
   * 退回
   */
  @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);
    String fileName = "交叉线岔.xls";
    String templateFileName = "excel/template/交叉线岔.xls";
    return new EasyExcelUtil().writeExcel(fileName, templateFileName, list);
  }

  /**
   * 查询明细列表
   * @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);
  }
}
