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

import com.railway.business.baseinfo.service.IBaseStationService;
import com.railway.business.catenary.domain.BusJcebJddz;
import com.railway.business.catenary.domain.vo.JcebQueryVo;
import com.railway.business.catenary.mapper.BusJcebJddzMapper;
import com.railway.business.catenary.service.IBusJcebJddzService;
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 zcf
 * @date 2021/10/24
 */
@Service
public class BusJcebJddzServiceImpl extends BusJcebServiceImpl<BusJcebJddz>
    implements IBusJcebJddzService {

  private final BusJcebJddzMapper busJcebJddzMapper;

  public BusJcebJddzServiceImpl(BusJcebJddzMapper busJcebJddzMapper,
      IBaseStationService stationService) {
    super(busJcebJddzMapper, stationService);
    this.busJcebJddzMapper = busJcebJddzMapper;
  }

  /**
   * 新增
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int create(BusJcebJddz busJcebJddz) {
    setCreate(busJcebJddz);
    return busJcebJddzMapper.insert(busJcebJddz);
  }

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

  /**
   * 更新
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int update(BusJcebJddz busJcebJddz) {
    setUpdate(busJcebJddz);
    return busJcebJddzMapper.update(busJcebJddz);
  }

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

  /**
   * 查询列表
   */
  @Override
  public List<BusJcebJddz> getList(BusJcebJddz busJcebJddz) {
    setList(busJcebJddz);
    return busJcebJddzMapper.getList(busJcebJddz);
  }

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

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

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

  /**
   * 导出Excel
   *
   * @param query 查询条件
   * @return 文件路径
   */
  @Override
  public String exportExcel(JcebQueryVo query) {
    List<BusJcebJddz> list = busJcebJddzMapper.getJcebList(query);
    Map<String, Object> params = new HashMap<>();
    if(!list.isEmpty()) {
      params.put("mdh", list.get(0).getMdh());
      params.put("stationName", list.get(0).getStationName());
    }
    String fileName = "接地电阻测量记录.xls";
    String templateFileName = "excel/template/接地电阻测量记录.xls";
    return new EasyExcelUtil().getExcel(params, list, templateFileName, fileName);
  }

}
