package com.jiayue.ipfcst.dataexchange.service;

import com.jiayue.ipfcst.common.core.util.SpringContextHolder;
import com.jiayue.ipfcst.common.data.constant.enums.EquipmentTypeEnum;
import com.jiayue.ipfcst.common.data.entity.EquipmentAttribute;
import com.jiayue.ipfcst.common.data.entity.ProtocolGatherDataPoint;
import com.jiayue.ipfcst.common.data.entity.ProtocolSenderDataPoint;
import com.jiayue.ipfcst.console.service.ElectricFieldService;
import com.jiayue.ipfcst.dataexchange.repository.ProtocolGatherDataPointRepository;
import com.jiayue.ipfcst.dataexchange.repository.ProtocolSenderDataPointRepository;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 点表表的操作service类
 *
 * @author 修唯xiuwei
 * @version 3.0
 */
@Service
public class ProtocolGatherDataPointService {

	private final
	ProtocolGatherDataPointRepository protocolGatherDataPointRepository;

	private final
	ProtocolSenderDataPointRepository protocolSenderDataPointRepository;

	private final
	EquipmentAttributeService equipmentAttributeService;


	/**
	 * 协议采集数据点服务
	 * 构造方法
	 *
	 * @param protocolGatherDataPointRepository 采集点位仓储
	 * @param protocolSenderDataPointRepository
	 * @param equipmentAttributeService         设备属性服务
	 */
	public ProtocolGatherDataPointService(ProtocolGatherDataPointRepository protocolGatherDataPointRepository, ProtocolSenderDataPointRepository protocolSenderDataPointRepository, EquipmentAttributeService equipmentAttributeService) {
		this.protocolGatherDataPointRepository = protocolGatherDataPointRepository;
		this.protocolSenderDataPointRepository = protocolSenderDataPointRepository;
		this.equipmentAttributeService = equipmentAttributeService;
	}


	/**
	 * 查询某采集通道下负责的点位
	 * 通过tunnelId来查询点位
	 *
	 * @param tunnelId tunnel id
	 * @return ProtocolDataPoint by tunnel id
	 */
	public List<ProtocolGatherDataPoint> getByTunnelId(String tunnelId) {
		ProtocolGatherDataPoint protocolDataPoint = new ProtocolGatherDataPoint();
		protocolDataPoint.setTunnelId(tunnelId);
		return this.protocolGatherDataPointRepository.findAll(Example.of(protocolDataPoint));
	}

	/**
	 * 查询某采集通道下负责的点位
	 * 通过tunnelId来查询点位
	 *
	 * @param eqId equipmentId id
	 * @return ProtocolDataPoint by tunnel id
	 */
	public List<ProtocolGatherDataPoint> getByEquipmentId(Integer eqId) {
		ProtocolGatherDataPoint protocolDataPoint = new ProtocolGatherDataPoint();
		protocolDataPoint.setEquipmentId(eqId);
		return this.protocolGatherDataPointRepository.findAll(Example.of(protocolDataPoint));
	}

	public List<ProtocolGatherDataPoint> getByTunnelIdAndeqId(String tunnelId, Integer eqId) {
		return protocolGatherDataPointRepository.findByTunnelIdAndEquipmentId(tunnelId, eqId);
	}

	/**
	 * 通过设备id
	 * 查询设备状态数据由哪些采集点位组成
	 *
	 * @param equipmentId   设备idid
	 * @param equipmentType 设备类型
	 * @return 采集点位集合
	 */
	public List<ProtocolGatherDataPoint> getByEquipmentId(Integer equipmentId, EquipmentTypeEnum equipmentType) {
		ProtocolGatherDataPoint protocolDataPoint = new ProtocolGatherDataPoint();
		EquipmentAttribute equipmentAttribute = new EquipmentAttribute();
		equipmentAttribute.setEquipmentType(equipmentType);
		protocolDataPoint.setEquipmentAttribute(equipmentAttribute);
		protocolDataPoint.setEquipmentId(equipmentId);
		return this.protocolGatherDataPointRepository.findAll(Example.of(protocolDataPoint));
	}


	/**
	 * 得到
	 * 根据设备id，设备类型，对应字段 来确定点位
	 *
	 * @param equipmentId   设备id id
	 * @param equipmentType 设备类型
	 * @param fieldName     字段名
	 * @return 采集点位
	 */
	public ProtocolGatherDataPoint get(Integer equipmentId, EquipmentTypeEnum equipmentType, String fieldName) {
		EquipmentAttribute equipmentAttribute = new EquipmentAttribute();
		equipmentAttribute.setFieldName(fieldName);
		equipmentAttribute.setEquipmentType(equipmentType);
		List<EquipmentAttribute> list = equipmentAttributeService.get(equipmentAttribute);
		if (list.size() == 1) {
			equipmentAttribute = list.get(0);
		} else {
			return null;
		}
		ProtocolGatherDataPoint protocolDataPoint = new ProtocolGatherDataPoint();
		protocolDataPoint.setEquipmentId(equipmentId);
		protocolDataPoint.setEquipmentAttribute(equipmentAttribute);
		List<ProtocolGatherDataPoint> list2 = this.protocolGatherDataPointRepository.findAll(Example.of(protocolDataPoint));
		if (list2.size() == 1) {
			return list2.get(0);
		} else {
			return null;
		}
	}


	/**
	 * 查询符合点位信息描述的采集点位
	 *
	 * @param protocolDataPoint protocol data point
	 * @return ProtocolGatherDataPoint list
	 */
	public List<ProtocolGatherDataPoint> get(ProtocolGatherDataPoint protocolDataPoint) {
		if (protocolDataPoint == null) {
			return getAll();
		}
		return this.protocolGatherDataPointRepository.findAll(Example.of(protocolDataPoint));
	}


	/**
	 * 查询所有点位信息描述的采集点位
	 *
	 * @return ProtocolGatherDataPoint list
	 */
	public List<ProtocolGatherDataPoint> getAll() {
		return this.protocolGatherDataPointRepository.findAll();
	}

	/**
	 * 根据id删除点位
	 *
	 * @param id 点位ID
	 */
	public void deleteById(Integer id) {
		List<ProtocolSenderDataPoint> protocolSenderDataPointList = protocolSenderDataPointRepository.findAll();
		List<ProtocolSenderDataPoint> protocolSenderDataPoints = protocolSenderDataPointList.stream().filter(w -> w.getGatherPoint() != null && w.getGatherPoint().equals(id)).collect(Collectors.toList());
		this.protocolGatherDataPointRepository.deleteById(id);
		if (protocolSenderDataPoints.size() > 0) {
			for (ProtocolSenderDataPoint protocolSenderDataPoint : protocolSenderDataPoints) {
				protocolSenderDataPointRepository.deleteById(protocolSenderDataPoint.getId());
			}
		}
	}

	/**
	 * 储存采集点位
	 *
	 * @param protocolDataPoint protocol data point
	 * @return ProtocolGatherDataPoint protocol gather data point
	 */
	public ProtocolGatherDataPoint save(ProtocolGatherDataPoint protocolDataPoint) {
		protocolDataPoint.setStationCode(SpringContextHolder.getBean(ElectricFieldService.class).get().getStationCode());
		if (protocolDataPoint.getShowOnHomePage() == null) {
			protocolDataPoint.setShowOnHomePage(0);
		}
		return this.protocolGatherDataPointRepository.save(protocolDataPoint);
	}

  /**
   * upjson文件解析更新保存
   * @param protocolDataPoint
   */
  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public void saveCloud(ProtocolGatherDataPoint protocolDataPoint) {
    protocolDataPoint.setStationCode(SpringContextHolder.getBean(ElectricFieldService.class).get().getStationCode());
    if (protocolDataPoint.getShowOnHomePage() == null) {
      protocolDataPoint.setShowOnHomePage(0);
    }
    this.protocolGatherDataPointRepository.save(protocolDataPoint);
  }

  /**
   * 清空原数据再保存新点表数据
   * @param beans
   */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveCloud(List<ProtocolGatherDataPoint> beans) {
    protocolGatherDataPointRepository.deleteAll();
		if (beans != null && beans.size() > 0) {
			for (ProtocolGatherDataPoint bean : beans) {
				bean.setStationCode(SpringContextHolder.getBean(ElectricFieldService.class).get().getStationCode());
				if (bean.getShowOnHomePage() == null) {
					bean.setShowOnHomePage(0);
				}
				this.protocolGatherDataPointRepository.save(bean);
			}
		}
	}


	/**
	 * 根据设备id和设备属性查询采集点表
	 *
	 * @param equipmentId          设备id
	 * @param equipmentAttributeId 设备属性id
	 * @return ProtocolGatherDataPoint  list
	 */
	public List<ProtocolGatherDataPoint> getProtocolGatherDataPoint(Integer equipmentId, Integer equipmentAttributeId) {
		EquipmentAttribute equipmentAttribute = new EquipmentAttribute();
		equipmentAttribute.setId(equipmentAttributeId);
		return this.protocolGatherDataPointRepository.findByEquipmentIdAndEquipmentAttribute(equipmentId, equipmentAttribute);
	}

	/**
	 * 根据通道和设备查询
	 *
	 * @param tunnelId
	 * @param equipmentId
	 * @param equipmentType
	 * @return ProtocolGatherDataPoint  list
	 */
	public List<ProtocolGatherDataPoint> getProtocolGatherDataPoint(String tunnelId, String equipmentId, Integer equipmentType) {
		if (equipmentId.equals("ALL")) {
			List<ProtocolGatherDataPoint> list = getByTunnelId(tunnelId);
			return list;
		}
		ProtocolGatherDataPoint point = new ProtocolGatherDataPoint();
		point.setEquipmentId(Integer.valueOf(equipmentId));
		EquipmentAttribute attribute1 = new EquipmentAttribute();
		attribute1.setEquipmentType(EquipmentTypeEnum.valueOf(equipmentType));
		point.setEquipmentAttribute(attribute1);
		if (tunnelId.equals("ALL")) {
			List<ProtocolGatherDataPoint> points = get(point);
			return points;
		} else {
			point.setTunnelId(tunnelId);
			List<ProtocolGatherDataPoint> points = get(point);
			return points;
		}
	}

	/**
	 * 根据点表id来查询采集点表
	 *
	 * @param id 点表id
	 * @return ProtocolGatherDataPoint  list
	 */
	public ProtocolGatherDataPoint getDataPointById(Integer id) {

		return this.protocolGatherDataPointRepository.findById(id).get();

	}

	/**
	 * 批量储存采集点位
	 *
	 * @param protocolDataPoints protocol data points
	 * @return ProtocolGatherDataPoint list
	 */
	public List<ProtocolGatherDataPoint> saveAll(List<ProtocolGatherDataPoint> protocolDataPoints) {
		return this.protocolGatherDataPointRepository.saveAll(protocolDataPoints);
	}

	public void deleteAll(ProtocolGatherDataPoint protocolDataPoint) {
		List<ProtocolSenderDataPoint> protocolSenderDataPointList = protocolSenderDataPointRepository.findAll();
		List<ProtocolSenderDataPoint> protocolSenderDataPoints = protocolSenderDataPointList.stream().filter(w -> w.getGatherPoint() != null && w.getGatherPoint().equals(protocolDataPoint.getId())).collect(Collectors.toList());
		protocolGatherDataPointRepository.delete(protocolDataPoint);
		if (protocolSenderDataPoints.size() > 0) {
			for (ProtocolSenderDataPoint protocolSenderDataPoint : protocolSenderDataPoints) {
				protocolSenderDataPointRepository.deleteById(protocolSenderDataPoint.getId());
			}
		}
	}
}
