package com.railway.business.catenary.service.impl;

import com.railway.business.baseinfo.service.IBaseStationService;
import com.railway.business.catenary.domain.BusJcebJyzqsjl;
import com.railway.business.catenary.domain.vo.JcebQueryVo;
import com.railway.business.catenary.mapper.BusJcebJyzqsjlMapper;
import com.railway.business.catenary.service.IBusJcebJyzqsjlService;
import com.railway.common.utils.StringUtils;
import com.railway.common.utils.poi.EasyExcelUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 检测表-绝缘子清扫记录
 *
 * @author author
 * @date 2021/10/21
 */
@Service
public class BusJcebJyzqsjlServiceImpl extends BusJcebServiceImpl<BusJcebJyzqsjl>
    implements IBusJcebJyzqsjlService {

  private final BusJcebJyzqsjlMapper busJcebJyzqsjlMapper;

  public BusJcebJyzqsjlServiceImpl(BusJcebJyzqsjlMapper busJcebJyzqsjlMapper,
      IBaseStationService stationService) {
    super(busJcebJyzqsjlMapper, stationService);
    this.busJcebJyzqsjlMapper = busJcebJyzqsjlMapper;
  }

  /**
   * 新增
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int create(BusJcebJyzqsjl busJcebJyzqsjl) {
    setCreate(busJcebJyzqsjl);
    return busJcebJyzqsjlMapper.insert(busJcebJyzqsjl);
  }

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

  /**
   * 更新
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int update(BusJcebJyzqsjl busJcebJyzqsjl) {
    setUpdate(busJcebJyzqsjl);
    return busJcebJyzqsjlMapper.update(busJcebJyzqsjl);
  }

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

  /**
   * 查询列表
   */
  @Override
  public List<BusJcebJyzqsjl> getList(BusJcebJyzqsjl busJcebJyzqsjl) {
    setList(busJcebJyzqsjl);
    return busJcebJyzqsjlMapper.getList(busJcebJyzqsjl);
  }

  /**
   * 提交
   *
   * @return 提交结果
   */
  @Override
  public int submit(BusJcebJyzqsjl busJcebJyzqsjl) {
    setSubmit(busJcebJyzqsjl);
    if (StringUtils.isEmpty(busJcebJyzqsjl.getId())) {
      return create(busJcebJyzqsjl);
    } else {
      return update(busJcebJyzqsjl);
    }
  }

  /**
   * 退回
   *
   * @return 退回结果
   */
  @Override
  public int reject(BusJcebJyzqsjl busJcebJyzqsjl) {
    setReject(busJcebJyzqsjl);
    return update(busJcebJyzqsjl);
  }

  /**
   * 确认
   *
   * @return 确认结果
   */
  @Override
  public int confirm(BusJcebJyzqsjl busJcebJyzqsjl) {
    setConfirm(busJcebJyzqsjl);
    return update(busJcebJyzqsjl);
  }

  /**
   * 导出Excel
   *
   * @param query 查询条件
   * @return 文件路径
   */
  @Override
  public String exportExcel(JcebQueryVo query) {
    List<BusJcebJyzqsjl> list = busJcebJyzqsjlMapper.getJcebList(query);
    for(int i=0;i<list.size();i++){
      list.get(i).setXh(i+1);
    }
    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("operator",list.get(0).getOperator());
    }
    String fileName = "绝缘子清扫记录.xls";
    String templateFileName = "excel/template/绝缘子清扫记录.xls";
    return new EasyExcelUtil().getExcel(params, list, templateFileName, fileName);
  }
}
