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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.cache.DictBizCache;
import org.springblade.constant.BusinessConstant;
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.OrderStatusEnum;
import org.springblade.enums.WarnKindEnum;
import org.springblade.modules.baseinfo.dict.entity.DictInfoEntity;
import org.springblade.modules.baseinfo.dict.service.IBaseInfoDictService;
import org.springblade.modules.baseinfo.org.entity.DeptRegionEntity;
import org.springblade.modules.baseinfo.org.service.IDeptRegionService;
import org.springblade.modules.business.check.order.entity.CheckOrderInfoEntity;
import org.springblade.modules.business.check.order.vo.CheckOrderInfoVO;
import org.springblade.modules.business.data.service.IRtuDataRainService;
import org.springblade.modules.business.warning.entity.RtuWarningInfoEntity;
import org.springblade.modules.business.warning.service.IRtuWarningService;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.entity.DictBiz;
import org.springblade.modules.system.entity.Role;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IDictBizService;
import org.springblade.modules.system.service.IRoleService;
import org.springblade.modules.system.service.IUserService;

import java.time.Duration;
import java.util.*;

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

	private static IDictBizService dictBizService;

	private static IDictBizService getDictBizService() {
		if (dictBizService == null) {
			dictBizService = SpringUtil.getBean(IDictBizService.class);
		}
		return dictBizService;
	}

	private static IDeptRegionService deptRegionService;

	private static IDeptRegionService getDeptRegionService() {
		if (deptRegionService == null) {
			deptRegionService = SpringUtil.getBean(IDeptRegionService.class);
		}
		return deptRegionService;
	}

	private static IDeptService deptService;

	private static IDeptService getDeptService() {
		if (deptService == null) {
			deptService = SpringUtil.getBean(IDeptService.class);
		}
		return deptService;
	}

	private static IUserService userService;

	private static IUserService getUserService() {
		if (userService == null) {
			userService = SpringUtil.getBean(IUserService.class);
		}
		return userService;
	}

	private static IRoleService roleService;

	private static IRoleService getRoleService() {
		if (roleService == null) {
			roleService = SpringUtil.getBean(IRoleService.class);
		}
		return roleService;
	}

	private static IRtuWarningService rtuWarningService;

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

	@Override
	public CheckOrderInfoVO entityVO(CheckOrderInfoEntity entity) {
		CheckOrderInfoVO checkOrderInfoVO = Objects.requireNonNull(BeanUtil.copy(entity, CheckOrderInfoVO.class));
		return this.entityVO(checkOrderInfoVO);
	}

	public CheckOrderInfoVO entityVO(CheckOrderInfoVO vo) {
		String areaName = "";
		if (null != vo.getAdCity()) {
			areaName += vo.getAdCity();
		}
		if (null != vo.getAdDist()) {
			if (areaName.length() > 0) {
				areaName += "/";
			}
			areaName += vo.getAdDist();
		}
		vo.setAreaName(areaName);
		DictBiz biz = DictBizCache.getById(vo.getOrderFromKey());
		vo.setOrderFromName(biz.getDictValue());
		biz = DictBizCache.getById(vo.getOrderStatusKey());
		vo.setOrderStatusName(biz.getDictValue());
		if (vo.getOrderConfirmUser() != null) {
			vo.setIsConfirm(1);
		} else {
			vo.setIsConfirm(0);
		}
		if (vo.getOrderConfirmTime() != null) {
			Duration duration = Func.between(vo.getOrderConfirmTime(), new Date());
			long second = duration.getSeconds();
			String taskTime = "";
			if (second >= 60 * 60) {
				if (second >= 24 * 60 * 60) {
					taskTime = "" + second / (24 * 60 * 60) + "天";
					second = second % (24 * 60 * 60);
				}
				if (second >= 60 * 60) {
					taskTime += "" + second / (60 * 60) + "小时";
					second = second % (60 * 60);
				}
				if (second >= 60) {
					taskTime += "" + second / (60) + "分";
				}
			} else {
				taskTime = "1小时内";
			}
			vo.setTaskProcessDuration(taskTime);
		}
		if (vo.getOrderStatusKey() != null) {
			DictBiz dictInfoEntity = getDictBizService().getById(vo.getOrderStatusKey());
			vo.setOrderStatus(Func.toInt(dictInfoEntity.getDictKey()));
		}
		String ancestors = vo.getAncestors();
		if (null != ancestors && ancestors.length() > 0) {
			String[] ancestorsList = ancestors.split(",");
			String adCode;
			if (ancestorsList.length >= 3) {
				adCode = ancestorsList[2];
			} else {
				adCode = vo.getAdCode();
			}
			if (null != adCode) {
				LambdaQueryWrapper<DeptRegionEntity> wrapper = Wrappers.<DeptRegionEntity>query().lambda();
				wrapper.eq(DeptRegionEntity::getIsDeleted, 0);
				wrapper.eq(DeptRegionEntity::getAdcd, adCode);
				List<DeptRegionEntity> deptRegionEntities = getDeptRegionService().list(wrapper);
				if (null != deptRegionEntities && deptRegionEntities.size() > 0) {
					String manageCompany = "";
					for (DeptRegionEntity deptRegionEntity : deptRegionEntities) {
						Dept dept = getDeptService().getById(deptRegionEntity.getDeptId());
						if (null != dept) {
							if (manageCompany.length() > 0) {
								manageCompany += " / ";
							}
							manageCompany += dept.getDeptName();
							//查找运维公司负责人
							if (vo.getOrderConfirm() == 0) {
								LambdaQueryWrapper<User> userLambdaQueryWrapper = Wrappers.<User>query().lambda();
								userLambdaQueryWrapper.eq(User::getIsDeleted, 0);
								userLambdaQueryWrapper.eq(User::getDeptId, dept.getId());
								List<User> userList = getUserService().list(userLambdaQueryWrapper);
								for (User u : userList) {
									Role role = getRoleService().getById(u.getRoleId());
									if (BusinessConstant.ROLE_COMPANY_ADMIN.equals(role.getRoleAlias())) {
										vo.setContactUserName(u.getRealName());
										vo.setContactPhone(u.getPhone());
										break;
									}
								}
							}
						}
					}
					vo.setManageCompany(manageCompany);
				}
			}
		}
		Duration duration = Func.between(vo.getCreateTime(), new Date());
		long second = duration.getSeconds();
		String taskTime = "";
		if (second >= 60 * 60) {
			if (second >= 24 * 60 * 60) {
				taskTime = "" + second / (24 * 60 * 60) + "天";
				second = second % (24 * 60 * 60);
			}
			if (second >= 60 * 60) {
				taskTime += "" + second / (60 * 60) + "小时";
				second = second % (60 * 60);
			}
			if (second >= 60) {
				taskTime += "" + second / (60) + "分";
			}
		} else {
			taskTime = "1小时内";
		}
		vo.setTaskCreateDuration(taskTime);
		LambdaQueryWrapper<RtuWarningInfoEntity> queryWrapper = Wrappers.<RtuWarningInfoEntity>query().lambda();
		queryWrapper.eq(RtuWarningInfoEntity::getIsDeleted, 0);
		queryWrapper.eq(RtuWarningInfoEntity::getRtuCode, vo.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 += "水位站小时上报延时";
				}
			}
			vo.setWarnKindInfo(warnKinds);
		}
		return vo;
	}

}
