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.BusJcbBlq;
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.BusJcbBlqMapper;
import com.railway.business.catenary.service.IBusJcbBlqService;
import com.railway.common.utils.StringUtils;
import com.railway.common.utils.poi.EasyExcelUtil;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 检查表-避雷器
 *
 * @author wuhonghao
 * @date 2021/10/17
 */
@Service
public class BusJcbBlqServiceImpl extends BusJcabServiceImpl implements IBusJcbBlqService {

  private final BusJcbBlqMapper busJcbBlqMapper;

  public BusJcbBlqServiceImpl(BusJcbBlqMapper busJcbBlqMapper,
      IBaseStationService stationService) {
    super(stationService);
    this.busJcbBlqMapper = busJcbBlqMapper;
  }

  /**
   * 新增
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int create(BusJcbBlq busJcbBlq) {
    setCreate(busJcbBlq);
    busJcbBlq.setUpdateTime(new Date());
    return busJcbBlqMapper.insert(busJcbBlq);
  }

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

  /**
   * 更新
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int update(BusJcbBlq busJcbBlq) {
    setUpdate(busJcbBlq);
    return busJcbBlqMapper.update(busJcbBlq);
  }

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

  /**
   * 查询列表
   */
  @Override
  public List<BusJcbBlq> getList(BusJcbBlq busJcbBlq) {
    setList(busJcbBlq);
    return busJcbBlqMapper.getList(busJcbBlq);
  }

  /**
   * 获取上次检测值
   */
  @Override
  public BusJcbBlq getLastOne(BusJcbBlq busJcbBlq) {
    return busJcbBlqMapper.getLastOne(busJcbBlq);
  }

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

  /**
   * 退回
   *
   * @return 退回结果
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int reject(BusJcbBlq busJcbBlq) {
    setReject(busJcbBlq);
    return busJcbBlqMapper.update(busJcbBlq);
  }

  /**
   * 确认
   *
   * @return 确认结果
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int confirm(BusJcbBlq busJcbBlq) {
    setConfirm(busJcbBlq);
    return busJcbBlqMapper.update(busJcbBlq);
  }

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

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