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.BusJcbDmcgyq;
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.BusJcbDmcgyqMapper;
import com.railway.business.catenary.service.IBusJcbDmcgyqService;
import com.railway.common.utils.SecurityUtils;
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 zcf
 * @date 2021/10/13
 */
@Service
public class BusJcbDmcgyqServiceImpl extends BusJcabServiceImpl implements IBusJcbDmcgyqService {

  private final BusJcbDmcgyqMapper busJcbDmcgyqMapper;

  public BusJcbDmcgyqServiceImpl(BusJcbDmcgyqMapper busJcbDmcgyqMapper,
      IBaseStationService stationService) {
    super(stationService);
    this.busJcbDmcgyqMapper = busJcbDmcgyqMapper;
  }

  /**
   * 新增
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int create(BusJcbDmcgyq busJcbDmcgyq) {
    setCreate(busJcbDmcgyq);
    busJcbDmcgyq.setUserId(SecurityUtils.getUserId());
    busJcbDmcgyq.setUpdateTime(new Date());
    return busJcbDmcgyqMapper.insert(busJcbDmcgyq);
  }

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

  /**
   * 更新
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int update(BusJcbDmcgyq busJcbDmcgyq) {
    setUpdate(busJcbDmcgyq);
    return busJcbDmcgyqMapper.update(busJcbDmcgyq);
  }

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

  /**
   * 查询列表
   */
  @Override
  public List<BusJcbDmcgyq> getList(BusJcbDmcgyq busJcbDmcgyq) {
    setList(busJcbDmcgyq);
    return busJcbDmcgyqMapper.getList(busJcbDmcgyq);
  }

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

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

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

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

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

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

}
