/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/22
 */
package org.springblade.modules.business.rtumanage.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.enums.WarnKindEnum;
import org.springblade.modules.business.data.entity.RtuDataGroundEntity;
import org.springblade.modules.business.data.entity.RtuDataRainEntity;
import org.springblade.modules.business.data.entity.RtuDataRiverEntity;
import org.springblade.modules.business.data.entity.RtuDataRsvrEntity;
import org.springblade.modules.business.data.service.IRtuDataGroundService;
import org.springblade.modules.business.data.service.IRtuDataRainService;
import org.springblade.modules.business.data.service.IRtuDataRiverService;
import org.springblade.modules.business.data.service.IRtuDataRsvrService;
import org.springblade.modules.business.rtumanage.entity.RtuStatusEntity;
import org.springblade.modules.business.rtumanage.vo.RtuStatusVO;
import org.springblade.modules.business.warning.entity.RtuWarningInfoEntity;
import org.springblade.modules.business.warning.service.IRtuWarningService;


import java.util.*;

/***
 * Date:2022/8/22
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
public class RtuManageWrapper extends BaseEntityWrapper<RtuStatusEntity, RtuStatusVO> {
	public static RtuManageWrapper build() {
		return new RtuManageWrapper();
	}

	private static IRtuDataRainService rtuDataRainService;

	private static IRtuDataRainService getRtuDataRainService() {
		if (rtuDataRainService == null) {
			rtuDataRainService = SpringUtil.getBean(IRtuDataRainService.class);
		}
		return rtuDataRainService;
	}

	private static IRtuDataRiverService rtuDataRiverService;

	private static IRtuDataRiverService getRtuDataRiverService() {
		if (rtuDataRiverService == null) {
			rtuDataRiverService = SpringUtil.getBean(IRtuDataRiverService.class);
		}
		return rtuDataRiverService;
	}

	private static IRtuDataRsvrService rtuDataRsvrService;

	private static IRtuDataRsvrService getRtuDataRsvrService() {
		if (rtuDataRsvrService == null) {
			rtuDataRsvrService = SpringUtil.getBean(IRtuDataRsvrService.class);
		}
		return rtuDataRsvrService;
	}

	private static IRtuDataGroundService rtuDataGroundService;

	private static IRtuDataGroundService getRtuDataGroundService() {
		if (rtuDataGroundService == null) {
			rtuDataGroundService = SpringUtil.getBean(IRtuDataGroundService.class);
		}
		return rtuDataGroundService;
	}

	private static IRtuWarningService rtuWarningService;

	private static IRtuWarningService getRtuWarningService() {
		if (rtuWarningService == null) {
			rtuWarningService = SpringUtil.getBean(IRtuWarningService.class);
		}
		return rtuWarningService;
	}

	@Override
	public RtuStatusVO entityVO(RtuStatusEntity entity) {
		RtuStatusVO rtuStatusVO = Objects.requireNonNull(BeanUtil.copy(entity, RtuStatusVO.class));
		return entityVO(rtuStatusVO);
	}

	public RtuStatusVO entityVO(RtuStatusVO entity) {
		String areaName = "";
		if (entity.getAdCity() != null) {
			areaName += entity.getAdCity();
		}
		if (entity.getAdDist() != null) {
			if (areaName.length() > 0) {
				areaName += "/";
			}
			areaName += entity.getAdDist();
		}
		entity.setAreaName(areaName);
		String rtuKindDesc = "";
		if (entity.getIsRain() != null && entity.getIsRain() == 1) {
			rtuKindDesc = "雨量站";
		}
		if (entity.getIsRes() != null && entity.getIsRes() == 1) {
			if (rtuKindDesc.length() > 0) {
				rtuKindDesc += "/";
			}
			rtuKindDesc += "水位站";
		} else if (entity.getIsRiver() != null && entity.getIsRiver() == 1) {
			if (rtuKindDesc.length() > 0) {
				rtuKindDesc += "/";
			}
			rtuKindDesc += "水位站";
		}
		if (entity.getIsGround() != null && entity.getIsGround() == 1) {
			if (rtuKindDesc.length() > 0) {
				rtuKindDesc += "/";
			}
			rtuKindDesc += "墒情站";
		}
		if (entity.getIsVideo() != null && entity.getIsVideo() == 1) {
			if (rtuKindDesc.length() > 0) {
				rtuKindDesc += "/";
			}
			rtuKindDesc += "视频站";
		}
		entity.setRtuKindDesc(rtuKindDesc);
		if (entity.getIsRain() != null &&  entity.getIsRain() == 1) {
			LambdaQueryWrapper<RtuDataRainEntity> wrapper = Wrappers.<RtuDataRainEntity>query().lambda();
			wrapper.eq(RtuDataRainEntity::getIsDeleted, 0L);
			wrapper.eq(RtuDataRainEntity::getRtuCode, entity.getRtuCode());
			RtuDataRainEntity rainEntity = getRtuDataRainService().getOne(wrapper);
			if (null != rainEntity) {
				if (rainEntity.getDrp() != null) {
					entity.setDrp(Func.toStr(rainEntity.getDrp()));
				} else {
					entity.setDrp("");
				}
			} else {
				entity.setDrp("");
			}
		}
		if (entity.getIsRiver() != null && entity.getIsRiver() == 1) {
			LambdaQueryWrapper<RtuDataRiverEntity> wrapper = Wrappers.<RtuDataRiverEntity>query().lambda();
			wrapper.eq(RtuDataRiverEntity::getIsDeleted, 0L);
			wrapper.eq(RtuDataRiverEntity::getRtuCode, entity.getRtuCode());
			RtuDataRiverEntity riverEntity = getRtuDataRiverService().getOne(wrapper);
			if (null != riverEntity) {
				if (riverEntity.getZ() != null) {
					entity.setZ(Func.toStr(riverEntity.getZ()));
				} else {
					entity.setZ("");
				}
			} else {
				entity.setZ("");
			}
		}
		if (entity.getIsRes() != null && entity.getIsRes() == 1) {
			LambdaQueryWrapper<RtuDataRsvrEntity> wrapper = Wrappers.<RtuDataRsvrEntity>query().lambda();
			wrapper.eq(RtuDataRsvrEntity::getIsDeleted, 0L);
			wrapper.eq(RtuDataRsvrEntity::getRtuCode, entity.getRtuCode());
			RtuDataRsvrEntity rsvrEntity = getRtuDataRsvrService().getOne(wrapper);
			if (null != rsvrEntity) {
				if (rsvrEntity.getRz() != null) {
					entity.setRz(Func.toStr(rsvrEntity.getRz()));
				} else {
					entity.setRz("");
				}
			} else {
				entity.setRz("");
			}
		}
		if (entity.getIsGround() != null && entity.getIsGround() == 1) {
			LambdaQueryWrapper<RtuDataGroundEntity> wrapper = Wrappers.<RtuDataGroundEntity>query().lambda();
			wrapper.eq(RtuDataGroundEntity::getIsDeleted, 0L);
			wrapper.eq(RtuDataGroundEntity::getRtuCode, entity.getRtuCode());
			RtuDataGroundEntity groundEntity = getRtuDataGroundService().getOne(wrapper);
			if (null != groundEntity) {
				String ground = "";
				if (groundEntity.getSlm10() != null) {
					ground += "slm10:" + Func.toStr(groundEntity.getSlm10());
				} else {
					ground += "slm10:";
				}
				if (groundEntity.getSlm20() != null) {
					ground += ",slm20:" + Func.toStr(groundEntity.getSlm20());
				} else {
					ground += ",slm20:";
				}
				if (groundEntity.getSlm30() != null) {
					ground += ",slm30:" + Func.toStr(groundEntity.getSlm30());
				} else {
					ground += ",slm30:";
				}
				if (groundEntity.getSlm40() != null) {
					ground += ",slm40:" + Func.toStr(groundEntity.getSlm40());
				} else {
					ground += ",slm40:";
				}
				if (groundEntity.getSlm50() != null) {
					ground += ",slm50:" + Func.toStr(groundEntity.getSlm50());
				} else {
					ground += ",slm50:";
				}
				if (groundEntity.getSlm60() != null) {
					ground += ",slm60:" + Func.toStr(groundEntity.getSlm60());
				} else {
					ground += ",slm60:";
				}
				if (groundEntity.getSlm80() != null) {
					ground += ",slm80:" + Func.toStr(groundEntity.getSlm80());
				} else {
					ground += ",slm80:";
				}
				if (groundEntity.getSlm100() != null) {
					ground += ",slm100:" + Func.toStr(groundEntity.getSlm100());
				} else {
					ground += ",slm100:";
				}
				entity.setGround(ground);
			} else {
				entity.setGround("");
			}
		}

		if (entity.getRunStatusId() ==1) {
			LambdaQueryWrapper<RtuWarningInfoEntity> queryWrapper = Wrappers.<RtuWarningInfoEntity>query().lambda();
			queryWrapper.eq(RtuWarningInfoEntity::getIsDeleted, 0);
			queryWrapper.eq(RtuWarningInfoEntity::getRtuCode, entity.getRtuCode());
			queryWrapper.eq(RtuWarningInfoEntity::getWarningStatus, 0);
			List<RtuWarningInfoEntity> rtuWarningInfoEntityList = getRtuWarningService().list(queryWrapper);
			if (null != rtuWarningInfoEntityList && rtuWarningInfoEntityList.size() > 0) {
				String warnKinds = "";
				for (RtuWarningInfoEntity rtuWarningInfoEntity : rtuWarningInfoEntityList) {
					if (warnKinds.length() > 0) {
						warnKinds += "/";
					}
					if (WarnKindEnum.WARN_CLOCK.getCode() == rtuWarningInfoEntity.getWarningKind()) {
						warnKinds += "测站时钟异常";
					} else if (WarnKindEnum.WARN_UP_MIN_DELAY.getCode() == rtuWarningInfoEntity.getWarningKind()) {
						warnKinds += "5分钟上报延时";
					} else if (WarnKindEnum.WARN_OFFLINE.getCode() == rtuWarningInfoEntity.getWarningKind()) {
						warnKinds += "测站离线";
					} else if (WarnKindEnum.WARN_RAIN_MISS_OUT.getCode() == rtuWarningInfoEntity.getWarningKind()) {
						warnKinds += "雨量站小时报漏报";
					} else if (WarnKindEnum.WARN_WL_MISS_OUT.getCode() == rtuWarningInfoEntity.getWarningKind()) {
						warnKinds += "水位站小时报漏报";
					} else if (WarnKindEnum.WARN_OUTLIER_VALUE.getCode() == rtuWarningInfoEntity.getWarningKind()) {
						warnKinds += "雨量疑似异常值";
					} else if (WarnKindEnum.WARN_RAIN_UP_HOUR_DELAY.getCode() == rtuWarningInfoEntity.getWarningKind()) {
						warnKinds += "雨量站小时上报延时";
					} else if (WarnKindEnum.WARN_WL_UP_HOUR_DELAY.getCode() == rtuWarningInfoEntity.getWarningKind()) {
						warnKinds += "水位站小时上报延时";
					}
				}
				entity.setWarnKindInfo(warnKinds);
			}
		}
		return entity;
	}
}
