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.BusJcbWjcxc;
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.BusJcbWjcxcMapper;
import com.railway.business.catenary.service.IBusJcbWjcxcService;
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/19
 */
@Service
public class BusJcbWjcxcServiceImpl extends BusJcabServiceImpl implements IBusJcbWjcxcService {

  private final BusJcbWjcxcMapper busJcbWjcxcMapper;

  public BusJcbWjcxcServiceImpl(BusJcbWjcxcMapper busJcbWjcxcMapper,
      IBaseStationService stationService) {
    super(stationService);
    this.busJcbWjcxcMapper = busJcbWjcxcMapper;
  }

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

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

  /**
   * 更新
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int update(BusJcbWjcxc busJcbWjcxc) {
    setUpdate(busJcbWjcxc);
    return busJcbWjcxcMapper.update(busJcbWjcxc);
  }

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

  /**
   * 查询列表
   */
  @Override
  public List<BusJcbWjcxc> getList(BusJcbWjcxc busJcbWjcxc) {
    setList(busJcbWjcxc);
    return busJcbWjcxcMapper.getList(busJcbWjcxc);
  }

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

  /**
   * 提交
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int submit(BusJcbWjcxc busJcbWjcxc) {
    busJcbWjcxc.setSubmitState(JcebStatusEnum.REPORTED.getCode());
    if (StringUtils.isEmpty(busJcbWjcxc.getId())) {
      return create(busJcbWjcxc);
    } else {
      return update(busJcbWjcxc);
    }
  }

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

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

  /**
   * 导出Excel
   *
   * @param query 查询条件
   * @return 文件路径
   */
  @Override
  public String exportExcel(JcebQueryVo query) {
    List<BusJcbWjcxc> list = busJcbWjcxcMapper.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 busJcbWjcxcMapper.getListByType(query);
  }

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