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

import com.railway.business.baseinfo.service.IBaseStationService;
import com.railway.business.catenary.domain.BusJcebXsjcjl;
import com.railway.business.catenary.domain.vo.JcebQueryVo;
import com.railway.business.catenary.mapper.BusJcebXsjcjlMapper;
import com.railway.business.catenary.service.IBusJcebXsjcjlService;
import com.railway.business.catenary.util.JcebQueryUtil;
import com.railway.common.utils.SecurityUtils;
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 BusJcebXsjcjlServiceImpl extends BusJcebServiceImpl<BusJcebXsjcjl>
    implements IBusJcebXsjcjlService {

  private final BusJcebXsjcjlMapper busJcebXsjcjlMapper;

  public BusJcebXsjcjlServiceImpl(BusJcebXsjcjlMapper busJcebXsjcjlMapper,
      IBaseStationService stationService) {
    super(busJcebXsjcjlMapper, stationService);
    this.busJcebXsjcjlMapper = busJcebXsjcjlMapper;
  }

  /**
   * 新增
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int create(BusJcebXsjcjl busJcebXsjcjl) {
    busJcebXsjcjl.setUserId(SecurityUtils.getUserId());
    setCreate(busJcebXsjcjl);
    return busJcebXsjcjlMapper.insert(busJcebXsjcjl);
  }

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

  /**
   * 更新
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int update(BusJcebXsjcjl busJcebXsjcjl) {
    setUpdate(busJcebXsjcjl);
    return busJcebXsjcjlMapper.update(busJcebXsjcjl);
  }

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

  /**
   * 查询列表
   */
  @Override
  public List<BusJcebXsjcjl> getList(BusJcebXsjcjl busJcebXsjcjl) {
    setList(busJcebXsjcjl);
    return busJcebXsjcjlMapper.getList(busJcebXsjcjl);
  }

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

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

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

  @Override
  public String exportExcel(JcebQueryVo query) {
    JcebQueryUtil.setBusJcebQuery(query);
    List<BusJcebXsjcjl> list = busJcebXsjcjlMapper.getJcebList(query);
    Map<String, Object> params = new HashMap<>();
    if(!list.isEmpty()) {
      params.put("stationName", list.get(0).getStationName());
    }
    String fileName = "全面检查记录.xls";
    String templateFileName = "excel/template/全面检查记录.xls";
    return new EasyExcelUtil().getExcel(params, list, templateFileName, fileName);
  }

}
