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.BusJcbFdfxjyq;
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.BusJcbFdfxjyqMapper;
import com.railway.business.catenary.service.IBusJcbFdfxjyqService;
import com.railway.common.utils.StringUtils;
import com.railway.common.utils.poi.EasyExcelUtil;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * dxf分选绝缘器检查表
 *
 * @author wuhonghao
 * @date 2021/10/14
 */
@Slf4j
@Service
public class BusJcbFdfxjyqServiceImpl extends BusJcabServiceImpl implements IBusJcbFdfxjyqService {

  private final BusJcbFdfxjyqMapper busJcbFdfxjyqMapper;

  public BusJcbFdfxjyqServiceImpl(BusJcbFdfxjyqMapper busJcbFdfxjyqMapper,
      IBaseStationService stationService) {
    super(stationService);
    this.busJcbFdfxjyqMapper = busJcbFdfxjyqMapper;
  }

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

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

  /**
   * 更新
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int update(BusJcbFdfxjyq busJcbFdfxjyq) {
    setUpdate(busJcbFdfxjyq);
    return busJcbFdfxjyqMapper.update(busJcbFdfxjyq);
  }

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

  /**
   * 查询列表
   */
  @Override
  public List<BusJcbFdfxjyq> getList(BusJcbFdfxjyq busJcbFdfxjyq) {
    setList(busJcbFdfxjyq);
    return busJcbFdfxjyqMapper.getList(busJcbFdfxjyq);
  }

  /**
   * 获取上次检测值
   */
  @Override
  public BusJcbFdfxjyq getLastOne(BusJcbFdfxjyq busJcbFdfxjyq) {
    return busJcbFdfxjyqMapper.getLastOne(busJcbFdfxjyq);
  }

  /**
   * 提交
   *
   * @return 提交结果
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int submit(BusJcbFdfxjyq busJcbFdfxjyq) {
    busJcbFdfxjyq.setSubmitState(JcebStatusEnum.REPORTED.getCode());
    log.info("id = {}, isEmpty = {}", busJcbFdfxjyq.getId(), StringUtils.isEmpty(busJcbFdfxjyq.getId()));
    if(StringUtils.isEmpty(busJcbFdfxjyq.getId())){
      return create(busJcbFdfxjyq);
    }else {
      return update(busJcbFdfxjyq);
    }
  }

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

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

  /**
   * 导出Excel
   *
   * @param query 查询条件
   * @return 文件路径
   */
  @Override
  public String exportExcel(JcebQueryVo query) {
    List<BusJcbFdfxjyq> list = busJcbFdfxjyqMapper.getJcebList(query);
    Map<String, Object> params = new HashMap<>();
    if(!list.isEmpty()) {
      params.put("stationName", list.get(0).getStationName());
      params.put("createBy",list.get(0).getCreateBy());
      params.put("confirmUser",list.get(0).getConfirmUser());
    }

    String fileName = "分段绝缘器检测记录.xls";
    String templateFileName = "excel/template/分段绝缘器检测记录.xls";
    return new EasyExcelUtil().getExcel(params, list, templateFileName, fileName);
  }


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

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