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

import com.github.pagehelper.Page;
import com.railway.business.baseinfo.domain.BusDmcgyq;
import com.railway.business.baseinfo.mapper.BusDmcgyqMapper;
import com.railway.business.baseinfo.service.IBaseStationService;
import com.railway.business.catenary.domain.BusJcbDmcgyq;
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.domain.vo.JcebStationVo;
import com.railway.business.catenary.enums.JcebTypeEnum;
import com.railway.business.catenary.mapper.BusJcbDmcgyqMapper;
import com.railway.business.catenary.service.IBusJcbDmcgyqService;
import com.railway.business.push.service.IBusPushMsgService;
import com.railway.common.core.page.PageDomain;
import com.railway.common.core.page.TableSupport;
import com.railway.common.utils.SecurityUtils;
import com.railway.common.utils.StringUtils;
import com.railway.common.utils.bean.BeanUtils;
import com.railway.common.utils.poi.EasyExcelUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 检查表-地面磁感应器
 *
 * @author zcf
 * @date 2021/10/13
 */
@Service
public class BusJcbDmcgyqServiceImpl extends BusJcabServiceImpl implements IBusJcbDmcgyqService {

  private final BusJcbDmcgyqMapper busJcbDmcgyqMapper;
  private final BusDmcgyqMapper busDmcgyqMapper;

  public BusJcbDmcgyqServiceImpl(BusJcbDmcgyqMapper busJcbDmcgyqMapper,
      IBaseStationService stationService, BusDmcgyqMapper busDmcgyqMapper,
      IBusPushMsgService busPushMsgService) {
    super(stationService, busPushMsgService);
    this.busJcbDmcgyqMapper = busJcbDmcgyqMapper;
    this.busDmcgyqMapper = busDmcgyqMapper;
  }

  /**
   * 新增
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int create(BusJcbDmcgyq busJcbDmcgyq) {
    setCreate(busJcbDmcgyq);
    busJcbDmcgyq.setUserId(SecurityUtils.getUserId());
    busJcbDmcgyq.setUpdateTime(new Date());
    return busJcbDmcgyqMapper.insert(busJcbDmcgyq);
  }

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

  /**
   * 获取检查表类型名
   * @return 类型名
   */
  @Override
  JcebTypeEnum getJcebTypeEnum() {
    return JcebTypeEnum.Dmcgyq;
  }

  /**
   * 更新
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int update(BusJcbDmcgyq busJcbDmcgyq) {
    setUpdate(busJcbDmcgyq);
    return busJcbDmcgyqMapper.update(busJcbDmcgyq);
  }

  /**
   * 获取单个
   */
  @Override
  public BusJcbDmcgyq getInfo(BusJcbDmcgyq busJcbDmcgyq) {
    return busJcbDmcgyqMapper.getInfo(busJcbDmcgyq);
  }

  /**
   * 查询列表
   */
  @Override
  public List<BusJcbDmcgyq> getList(BusJcbDmcgyq busJcbDmcgyq) {
    setList(busJcbDmcgyq);
    return busJcbDmcgyqMapper.getList(busJcbDmcgyq);
  }

  /**
   * 获取上次检查记录
   */
  @Override
  public BusJcbDmcgyq getLastRecord() {
    return busJcbDmcgyqMapper.getLastRecord(SecurityUtils.getUserId());
  }

  /**
   * 提交
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int submit(BusJcbDmcgyq bean) {
    setSubmit(bean);
    int ret;
    if(StringUtils.isEmpty(bean.getId())){
      ret = create(bean);
    }else {
      ret = update(bean);
    }
    // 推送消息
    if(ret > 0) {
      pushSubmit(bean);
    }
    return ret;
  }

  /**
   * 退回
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int reject(BusJcbDmcgyq busJcbDmcgyq) {
    setReject(busJcbDmcgyq);
    return update(busJcbDmcgyq);
  }

  /**
   * 确认
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int confirm(BusJcbDmcgyq busJcbDmcgyq) {
    setConfirm(busJcbDmcgyq);
    return update(busJcbDmcgyq);
  }

  /**
   * 导出Excel
   *
   * @param query 查询条件
   * @return 文件路径
   */
  @Override
  public String exportExcel(JcebQueryVo query) {
    List<BusJcbDmcgyq> list = busJcbDmcgyqMapper.getJcebList(query);
    List<BusJcbDmcgyq> resultList = new ArrayList<>();
    list.forEach(
            x -> {
              transDataForList(x, resultList);
            }
    );
    String fileName = "地磁感应器检测记录.xls";
    String templateFileName = "excel/template/地磁感应器检测记录.xls";
    return new EasyExcelUtil().writeExcel(fileName, templateFileName, resultList);
  }

  /**
   * 检测表list
   * @param query
   * @return
   */
  @Override
  public List<BusJcbDmcgyq> getJcebList(JcebQueryVo query) {
      List<BusJcbDmcgyq> list = busJcbDmcgyqMapper.getJcebList(query);
      List<BusJcbDmcgyq> resultList = new ArrayList<>();
      list.forEach(
              x -> {
               transDataForList(x, resultList);
              }
      );
      PageDomain pageDomain = TableSupport.buildPageRequest();
      Integer pageNum = pageDomain.getPageNum();
      Integer pageSize = pageDomain.getPageSize();
      return resultList.stream().limit(pageSize * pageNum).collect(Collectors.toList());
  }

  /**
   * 获取单个jceb
   */
  @Override
  public BusJcbDmcgyq getJcebInfo(BusJcbDmcgyq busJcbDmcgyq) {
    BusJcbDmcgyq result = busJcbDmcgyqMapper.getJcebInfo(busJcbDmcgyq);
    return transData(result, busJcbDmcgyq.getGyqNumber());
  }

  private void transDataForList(BusJcbDmcgyq x, List<BusJcbDmcgyq> resultList) {
      if(StringUtils.YES_VALUE.equals(x.getBspwgAzlg()) &&
              StringUtils.YES_VALUE.equals(x.getBspwgBsqx()) &&
              StringUtils.YES_VALUE.equals(x.getDgqwgBmqj()) &&
              StringUtils.YES_VALUE.equals(x.getDgqwgWzws()) &&
              StringUtils.YES_VALUE.equals(x.getDgqwgAzlg())) {
        x.setWsjbjsd("无");
      } else {
        x.setWsjbjsd("有");
      }
      BusDmcgyq busDmcgyq = busDmcgyqMapper.getInfo(x.getGyqId().toString());
      BusDmcgyq query = new BusDmcgyq();
      query.setXingbie(busDmcgyq.getXingbie());
      query.setFxmc(busDmcgyq.getFxmc());
      query.setDeptStationId(busDmcgyq.getDeptStationId());
      List<BusDmcgyq> busDmcgyqList = busDmcgyqMapper.getList(query);

      if(x.getDgqclG1() != null) {
        BusJcbDmcgyq g1detail = new BusJcbDmcgyq();
        x.setGyqNumber("G1");
        x.setScz(x.getDgqclG1());
        x.setCzlc(getCzlc(busDmcgyqList,"G1"));
        BeanUtils.copyBeanProp(g1detail, x);
        resultList.add(g1detail);
      }
      if(x.getDgqclG2() != null) {
        BusJcbDmcgyq g2detail = new BusJcbDmcgyq();
        x.setGyqNumber("G2");
        x.setScz(x.getDgqclG2());
        x.setCzlc(getCzlc(busDmcgyqList,"G2"));
        BeanUtils.copyBeanProp(g2detail, x);
        resultList.add(g2detail);
      }
      if(x.getDgqclG3() != null) {
        BusJcbDmcgyq g3detail = new BusJcbDmcgyq();
        x.setGyqNumber("G3");
        x.setScz(x.getDgqclG3());
        x.setCzlc(getCzlc(busDmcgyqList,"G3"));
        BeanUtils.copyBeanProp(g3detail, x);
        resultList.add(g3detail);
      }
      if(x.getDgqclG4() != null) {
        BusJcbDmcgyq g4detail = new BusJcbDmcgyq();
        x.setGyqNumber("G4");
        x.setScz(x.getDgqclG4());
        x.setCzlc(getCzlc(busDmcgyqList,"G4"));
        BeanUtils.copyBeanProp(g4detail, x);
        resultList.add(g4detail);
      }
  }

  private String getCzlc(List<BusDmcgyq> busDmcgyqList, String type) {
      return busDmcgyqList.stream()
              .filter(bus -> bus.getSbbh().equals(type))
              .map(bus -> bus.getCzlc()==null?"":bus.getCzlc().toString())
              .findFirst().orElse("");
  }

  private BusJcbDmcgyq transData(BusJcbDmcgyq x, String gyqNumber) {
    if(StringUtils.YES_VALUE.equals(x.getBspwgAzlg()) &&
            StringUtils.YES_VALUE.equals(x.getBspwgBsqx()) &&
            StringUtils.YES_VALUE.equals(x.getDgqwgBmqj()) &&
            StringUtils.YES_VALUE.equals(x.getDgqwgWzws()) &&
            StringUtils.YES_VALUE.equals(x.getDgqwgAzlg())) {
      x.setWsjbjsd("无");
    } else {
      x.setWsjbjsd("有");
    }

    if("G1".equals(gyqNumber)) {
      x.setScz(x.getDgqclG1());
    }
    if("G2".equals(gyqNumber)) {
      x.setScz(x.getDgqclG2());
    }
    if("G3".equals(gyqNumber)) {
      x.setScz(x.getDgqclG3());
    }
    if("G4".equals(gyqNumber)) {
      x.setScz(x.getDgqclG4());
    }
    return x;
  }

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

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

  /**
   * 查询筛选车间列表
   *
   * @param query 查询条件
   * @return 车间列表
   */
  @Override
  public List<JcebStationVo> listStation(JcebQueryVo query) {
    return busJcbDmcgyqMapper.listStation(query);
  }

}
