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.ProtocolSenderDataPointRepository;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

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


	private final
	ProtocolSenderDataPointRepository protocolSenderDataPointRepository;
	private ProtocolGatherDataPointService protocolGatherDataPointService;

	/**
	 * 构造方法
	 *
	 * @param protocolSenderDataPointRepository protocol sender data point repository
	 */
	public ProtocolSenderDataPointService(ProtocolSenderDataPointRepository protocolSenderDataPointRepository, ProtocolGatherDataPointService protocolGatherDataPointService) {
		this.protocolSenderDataPointRepository = protocolSenderDataPointRepository;
		this.protocolGatherDataPointService = protocolGatherDataPointService;
	}


	/**
	 * 查询某转发通道下负责的点位
	 * 通过tunnelId来查询点位
	 *
	 * @param tunnelId tunnel id
	 * @return ProtocolSenderDataPoint by tunnel id
	 */
	public List<ProtocolSenderDataPoint> getByTunnelId(String tunnelId) {
		ProtocolSenderDataPoint protocolDataPoint = new ProtocolSenderDataPoint();
		protocolDataPoint.setMagnification(null);
		protocolDataPoint.setTunnelId(tunnelId);
		return this.protocolSenderDataPointRepository.findAll(Example.of(protocolDataPoint));
	}


	/**
	 * 查询符合点位信息描述的转发点位
	 *
	 * @param protocolDataPoint protocol data point
	 * @return ProtocolDataPoint list
	 */
	public List<ProtocolSenderDataPoint> get(ProtocolSenderDataPoint protocolDataPoint) {
		return this.protocolSenderDataPointRepository.findAll(Example.of(protocolDataPoint));
	}


	/**
	 * 获取到通道最末尾的值以便批量添加转发点位时获取到最末点位
	 *
	 * @param tunnelId 通道id
	 * @return {@link Integer}
	 */
	public Integer getMaxProtocolNoInTunnel(String tunnelId) {
		return this.protocolSenderDataPointRepository.getMaxProtocolNoInTunnel(tunnelId);
	}


	/**
	 * 查询所有转发点位
	 *
	 * @return ProtocolSenderDataPoint
	 */
	public List<ProtocolSenderDataPoint> get() {
		return this.protocolSenderDataPointRepository.findAll();
	}
  /**
   * 查询所有转发点位
   *
   * @return ProtocolSenderDataPoint
   */
  public List<ProtocolSenderDataPoint> getAll() {
    return this.protocolSenderDataPointRepository.findAll();
  }
	/**
	 * 分页查询
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Page<ProtocolSenderDataPoint> getAll(Integer page, Integer size) {
		Pageable pageable = PageRequest.of(page - 1, size); //页码：前端从1开始，jpa从0开始，做个转换
		return protocolSenderDataPointRepository.findAll(pageable);
	}

	/**
	 * 通过通道id查询转发点表
	 *
	 * @param tunnelId 通道id
	 * @return ProtocolSenderDataPoint  protocol data  point
	 */
	public List<ProtocolSenderDataPoint> findByTunnelId(String tunnelId) {
		return this.protocolSenderDataPointRepository.findByTunnelId(tunnelId);
	}

	/**
	 * 通过采集点表查询转发点表
	 *
	 * @param gatherPoint 采集点表
	 * @return ProtocolSenderDataPoint  protocol data  point
	 */
	public List<ProtocolSenderDataPoint> findByGatherPoint(Integer gatherPoint) {
		return this.protocolSenderDataPointRepository.findByGatherPoint(gatherPoint);
	}

	/**
	 * 通过通道id，采集点表查询转发点表
	 *
	 * @param tunnelId    通道id
	 * @param gatherPoint 采集点表
	 * @return ProtocolSenderDataPoint  protocol data  point
	 */
	public List<ProtocolSenderDataPoint> findByTunnelIdAndGatherPoint(String tunnelId, Integer gatherPoint) {
		return protocolSenderDataPointRepository.findByTunnelIdAndAndGatherPoint(tunnelId, gatherPoint);
	}

	/**
	 * 保存转发点表
	 *
	 * @param protocolSenderDataPoint 点位实体
	 * @return ProtocolSenderDataPoint Protocol Sender Data Point
	 */

	public ProtocolSenderDataPoint save(ProtocolSenderDataPoint protocolSenderDataPoint) {
		protocolSenderDataPoint.setStationCode(SpringContextHolder.getBean(ElectricFieldService.class).get().getStationCode());
		return this.protocolSenderDataPointRepository.save(protocolSenderDataPoint);
	}

  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public ProtocolSenderDataPoint saveCloud(ProtocolSenderDataPoint protocolSenderDataPoint) {
    protocolSenderDataPoint.setStationCode(SpringContextHolder.getBean(ElectricFieldService.class).get().getStationCode());
    return this.protocolSenderDataPointRepository.save(protocolSenderDataPoint);
  }
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveCloud(List<ProtocolSenderDataPoint> protocolSenderDataPoints) {
		if (protocolSenderDataPoints != null && protocolSenderDataPoints.size() > 0) {
			protocolSenderDataPointRepository.deleteAll();
			for (ProtocolSenderDataPoint protocolSenderDataPoint : protocolSenderDataPoints) {
				protocolSenderDataPoint.setStationCode(SpringContextHolder.getBean(ElectricFieldService.class).get().getStationCode());
			}
			protocolSenderDataPointRepository.saveAll(protocolSenderDataPoints);
		}
	}


	/**
	 * 删除转发点表
	 *
	 * @param id 点表id
	 */
	public void deleteById(Integer id) {
		try {
			this.protocolSenderDataPointRepository.deleteById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void deleteAll(ProtocolSenderDataPoint point) {
		protocolSenderDataPointRepository.delete(point);
	}

	/**
	 * 通过通道id查询转发点表
	 *
	 * @param tunnelId 通道id
	 * @return ProtocolSenderDataPoint  protocol data  point
	 */
	public List<Map> getPointByTunnelId(String tunnelId) {
		List list = new ArrayList();
		List<ProtocolSenderDataPoint> senderDataPoints = protocolSenderDataPointRepository.findByTunnelId(tunnelId);
		List<ProtocolGatherDataPoint> gatherDataPoints = protocolGatherDataPointService.getAll();
		senderDataPoints.sort(Comparator.comparing(ProtocolSenderDataPoint::getProtocolPointNo));
		for (int i = 0; i < senderDataPoints.size(); i++) {
			Map<String, Object> map = new HashMap();
			if (senderDataPoints.get(i).getGatherPoint() != null) {
				for (int j = 0; j < gatherDataPoints.size(); j++) {
					if (senderDataPoints.get(i).getGatherPoint().equals(gatherDataPoints.get(j).getId())) {
						map.put("sender", senderDataPoints.get(i));
						map.put("equipmentId", gatherDataPoints.get(j).getEquipmentId());
						map.put("equipmentAttribute", gatherDataPoints.get(j).getEquipmentAttribute());
						list.add(map);
					}
				}
			} else {
				map.put("sender", senderDataPoints.get(i));
				map.put("equipmentId", null);
				map.put("equipmentAttribute", null);
				list.add(map);
			}
		}
		return list;
	}

	/**
	 * 通过通道id和采集id查询转发表
	 *
	 * @param tunnelId
	 * @param equipmentId
	 * @param equipmentType
	 * @return list
	 */
	public List getPointByTunnelIdAndPointId(String tunnelId, String equipmentId, Integer equipmentType) {
		List list = new ArrayList();
		if (equipmentId.equals("ALL")) {
			list = getPointByTunnelId(tunnelId);
			return list;
		}
		ProtocolGatherDataPoint point = new ProtocolGatherDataPoint();
		point.setEquipmentId(Integer.valueOf(equipmentId));
		EquipmentAttribute attribute1 = new EquipmentAttribute();
		attribute1.setEquipmentType(EquipmentTypeEnum.valueOf(equipmentType));
		point.setEquipmentAttribute(attribute1);
		List<ProtocolGatherDataPoint> points = this.protocolGatherDataPointService.get(point);
		for (int i = 0; i < points.size(); i++) {
			List<ProtocolSenderDataPoint> senderDataPoints = new ArrayList<>();
			if (tunnelId.equals("ALL")) {
				senderDataPoints = protocolSenderDataPointRepository.findByGatherPoint(points.get(i).getId());
			} else {
				senderDataPoints = protocolSenderDataPointRepository.findByTunnelIdAndAndGatherPoint(tunnelId, points.get(i).getId());
			}
			senderDataPoints.sort(Comparator.comparing(ProtocolSenderDataPoint::getProtocolPointNo));
			List<ProtocolGatherDataPoint> gatherDataPoints = protocolGatherDataPointService.getAll();
			for (int k = 0; k < senderDataPoints.size(); k++) {
				Map<String, Object> map = new HashMap();
				if (senderDataPoints.get(k).getGatherPoint() != null) {
					for (int r = 0; r < gatherDataPoints.size(); r++) {
						if (senderDataPoints.get(k).getGatherPoint().equals(gatherDataPoints.get(r).getId())) {
							map.put("sender", senderDataPoints.get(k));
							map.put("equipmentId", gatherDataPoints.get(r).getEquipmentId());
							map.put("equipmentAttribute", gatherDataPoints.get(r).getEquipmentAttribute());
							list.add(map);
						}
					}
				} else {
					map.put("sender", senderDataPoints.get(k));
					map.put("equipmentId", null);
					map.put("equipmentAttribute", null);
					list.add(map);
				}
			}
		}
		return list;
	}
}
