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

import com.railway.business.baseinfo.service.IBaseStationService;
import com.railway.business.catenary.domain.BusJcebGjsfx;
import com.railway.business.catenary.domain.vo.JcebQueryVo;
import com.railway.business.catenary.mapper.BusJcebGjsfxMapper;
import com.railway.business.catenary.service.IBusJcebGjsfxService;
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 whh
 * @date 2021/10/19
 */
@Service
public class BusJcebGjsfxServiceImpl extends BusJcebServiceImpl<BusJcebGjsfx>
    implements IBusJcebGjsfxService {

  private final BusJcebGjsfxMapper busJcebGjsfxMapper;

  public BusJcebGjsfxServiceImpl(BusJcebGjsfxMapper busJcebGjsfxMapper,
      IBaseStationService stationService) {
    super(busJcebGjsfxMapper, stationService);
    this.busJcebGjsfxMapper = busJcebGjsfxMapper;
  }

  /**
   * 新增
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int create(BusJcebGjsfx busJcebGjsfx) {
    setCreate(busJcebGjsfx);
    return busJcebGjsfxMapper.insert(busJcebGjsfx);
  }

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

  /**
   * 更新
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int update(BusJcebGjsfx busJcebGjsfx) {
    setUpdate(busJcebGjsfx);
    return busJcebGjsfxMapper.update(busJcebGjsfx);
  }

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

  /**
   * 查询列表
   */
  @Override
  public List<BusJcebGjsfx> getList(BusJcebGjsfx busJcebGjsfx) {
    setList(busJcebGjsfx);
    return busJcebGjsfxMapper.getList(busJcebGjsfx);
  }

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

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

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

  /**
   * 导出Excel
   *
   * @param query 查询条件
   * @return 文件路径
   */
  @Override
  public String exportExcel(JcebQueryVo query) {
    List<BusJcebGjsfx> list = busJcebGjsfxMapper.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("operator",list.get(0).getOperator());
    }
    String fileName = "关节式分相检测记录.xls";
    String templateFileName = "excel/template/关节式分相检测记录.xls";
    return new EasyExcelUtil().getExcel(params, list, templateFileName, fileName);
  }

}
