/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2022/8/23
 */
package org.springblade.modules.business.equipment.inspection.base.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.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.enums.OrderConfirmEnum;
import org.springblade.enums.WarnKindEnum;
import org.springblade.modules.baseinfo.org.entity.DeptRegionEntity;
import org.springblade.modules.business.check.order.entity.CheckOrderInfoEntity;
import org.springblade.modules.business.check.order.service.IRtuCheckOrderService;
import org.springblade.modules.business.equipment.inspection.base.entity.EquipmentInspectionInfoEntity;
import org.springblade.modules.business.equipment.inspection.base.vo.EquipmentInspectionInfoVO;
import org.springblade.modules.business.warning.entity.RtuWarningInfoEntity;
import org.springblade.modules.business.warning.service.IRtuWarningService;

import java.util.List;
import java.util.Objects;

/***
 * Date:2022/8/23
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */

public class EquipmentInspectionWrapper extends BaseEntityWrapper<EquipmentInspectionInfoEntity, EquipmentInspectionInfoVO> {
	public static EquipmentInspectionWrapper build() {
		return new EquipmentInspectionWrapper();
	}

	private static IRtuCheckOrderService checkOrderService;

	private static IRtuCheckOrderService getCheckOrderService() {
		if (checkOrderService == null) {
			checkOrderService = SpringUtil.getBean(IRtuCheckOrderService.class);
		}
		return checkOrderService;
	}

	private static IRtuWarningService rtuWarningService;

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

	@Override
	public EquipmentInspectionInfoVO entityVO(EquipmentInspectionInfoEntity entity) {
		EquipmentInspectionInfoVO equipmentInspectionInfoVO = Objects.requireNonNull(BeanUtil.copy(entity, EquipmentInspectionInfoVO.class));
		return entityVO(equipmentInspectionInfoVO);
	}

	public EquipmentInspectionInfoVO entityVO(EquipmentInspectionInfoVO entity) {
		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.getRunStatusId()!=null && 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);
			}

			BladeUser user = AuthUtil.getUser();

			LambdaQueryWrapper<CheckOrderInfoEntity> wrapper = Wrappers.<CheckOrderInfoEntity>query().lambda();
			wrapper.eq(CheckOrderInfoEntity::getIsDeleted, 0);
			wrapper.eq(CheckOrderInfoEntity::getRtuCode, entity.getRtuCode());
			wrapper.eq(CheckOrderInfoEntity::getOrderClose, 0);

			entity.setOrderStatus(0);
			List<CheckOrderInfoEntity> list = getCheckOrderService().list(wrapper);
			if (null != list && list.size() > 0) {
				entity.setOrderStatus(1);
				entity.setOrderConfirm(list.get(0).getOrderConfirm());
				entity.setOrderClose(list.get(0).getOrderClose());
				if (entity.getOrderConfirm() == OrderConfirmEnum.ACTIVE_CONFIRM.getCode()){
					if (list.get(0).getOrderConfirmUser().equals(user.getUserId())){
						entity.setMyOrder(1);
					}else{
						entity.setMyOrder(0);
					}
				}
				entity.setOrderId(list.get(0).getId());
			}
		}

		return entity;
	}
}
