/**
 * 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.toolkit.Wrappers;
import org.springblade.constant.BusinessConstant;
import org.springblade.constant.DictBusinessConstant;
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.OrderConfirmEnum;
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.baseinfo.region.entity.RegionInfoEntity;
import org.springblade.modules.baseinfo.region.service.IBaseInfoRegionService;
import org.springblade.modules.baseinfo.rtu.entity.RtuInfoEntity;
import org.springblade.modules.baseinfo.rtu.service.IRtuBaseInfoService;
import org.springblade.modules.business.check.order.dto.CheckOrderInfoDTO;
import org.springblade.modules.business.check.order.entity.CheckOrderInfoEntity;
import org.springblade.modules.business.check.order.service.IRtuCheckOrderService;
import org.springblade.modules.business.check.order.vo.CheckOrderHistroyVO;
import org.springblade.modules.business.check.order.vo.CheckOrderInfoVO;
import org.springblade.modules.business.check.process.service.IRtuOrderProcessService;
import org.springblade.modules.business.check.process.vo.OrderProcessInfoVO;
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;
	}

	private static IRtuBaseInfoService rtuBaseInfoService;

	private static IRtuBaseInfoService getRtuBaseInfoService() {
		if (rtuBaseInfoService == null) {
			rtuBaseInfoService = SpringUtil.getBean(IRtuBaseInfoService.class);
		}
		return rtuBaseInfoService;
	}


	private static IBaseInfoRegionService regionService;

	private static IBaseInfoRegionService getRegionService() {
		if (regionService == null) {
			regionService = SpringUtil.getBean(IBaseInfoRegionService.class);
		}
		return regionService;
	}

	private static IBaseInfoDictService baseInfoDictService;

	private static IBaseInfoDictService getBaseInfoDictService() {
		if (baseInfoDictService == null) {
			baseInfoDictService = SpringUtil.getBean(IBaseInfoDictService.class);
		}
		return baseInfoDictService;
	}

	private static IRtuOrderProcessService orderProcessService;

	private static IRtuOrderProcessService getOrderProcessService() {
		if (orderProcessService == null) {
			orderProcessService = SpringUtil.getBean(IRtuOrderProcessService.class);
		}
		return orderProcessService;
	}

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

	public CheckOrderHistroyVO entityVO(CheckOrderHistroyVO vo) {
		LambdaQueryWrapper<RtuInfoEntity> rtuLambdaQueryWrapper = Wrappers.<RtuInfoEntity>query().lambda();
		rtuLambdaQueryWrapper.eq(RtuInfoEntity::getRtuCode, vo.getRtuCode());
		RtuInfoEntity rtuInfoEntity = getRtuBaseInfoService().getOne(rtuLambdaQueryWrapper);
		LambdaQueryWrapper<RegionInfoEntity> regionQueryWrapper = Wrappers.<RegionInfoEntity>query().lambda();
		regionQueryWrapper.eq(RegionInfoEntity::getAdcd, rtuInfoEntity.getAdCode());
		RegionInfoEntity regionInfoEntity = getRegionService().getOne(regionQueryWrapper);
		String ancestors = regionInfoEntity.getAncestors() + "," + rtuInfoEntity.getAdCode();
		String[] ancestorsList = ancestors.split(",");
		if (ancestorsList.length >= 3) {
			String adCode = ancestorsList[2];
			LambdaQueryWrapper<DeptRegionEntity> wrapper = Wrappers.<DeptRegionEntity>query().lambda();
			wrapper.eq(DeptRegionEntity::getAdcd, adCode);
			List<DeptRegionEntity> deptRegionEntities = getDeptRegionService().list(wrapper);
			if (null != deptRegionEntities && deptRegionEntities.size() > 0) {
				for (DeptRegionEntity deptRegionEntity : deptRegionEntities) {
					Dept dept = getDeptService().getById(deptRegionEntity.getDeptId());
					if (null != dept) {
						if (dept.getDeptCategory() == 1) {
							vo.setManageCompany(dept.getDeptName());
						}
					}
				}
			}
		}
		return vo;
	}

	public CheckOrderInfoVO entityVO(CheckOrderInfoVO vo) {
		//处理人
		vo.setProcessorName(vo.getContactUserName());
		//行政区划名称
		String areaName = "";
		if (null != vo.getAdCity()) {
			areaName += vo.getAdCity();
		}
		if (null != vo.getAdDist()) {
			if (areaName.length() > 0) {
				areaName += "/";
			}
			areaName += vo.getAdDist();
		}
		vo.setAreaName(areaName);
		//工单来源名称
		if (vo.getOrderFromKey() != null) {
			DictBiz biz = getDictBizService().getById(vo.getOrderFromKey());
			vo.setOrderFromName(biz.getDictValue());
		}
		//工单状态名称
		if (vo.getOrderStatusKey() != null) {
			DictBiz biz = getDictBizService().getById(vo.getOrderStatusKey());
			vo.setOrderStatusName(biz.getDictValue());
		}

		//工单状态代码
		if (vo.getOrderStatusKey() != null) {
			DictBiz dictInfoEntity = getDictBizService().getById(vo.getOrderStatusKey());
			vo.setOrderStatus(Func.toInt(dictInfoEntity.getDictKey()));
		}
		//是否已经确认
		if (vo.getOrderConfirmUser() != null) {
			vo.setIsConfirm(1);
		} else {
			vo.setIsConfirm(0);
		}
		//维修填报
		if (vo.getProcessUser() != null) {
			vo.setOrderReportStatus(1);
		} else {
			vo.setOrderReportStatus(0);
		}
		//计算任务创建时长
		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);
		//计算维修时长
		if (vo.getOrderConfirm() == OrderConfirmEnum.ACTIVE_CONFIRM.getCode()) {
			if (vo.getOrderConfirmTime() != null) {
				duration = Func.between(vo.getOrderConfirmTime(), new Date());
				second = duration.getSeconds();
				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);
			}
		}
		//运维公司、运维管理员信息
		LambdaQueryWrapper<RtuInfoEntity> rtuLambdaQueryWrapper = Wrappers.<RtuInfoEntity>query().lambda();
		rtuLambdaQueryWrapper.eq(RtuInfoEntity::getRtuCode, vo.getRtuCode());
		RtuInfoEntity rtuInfoEntity = getRtuBaseInfoService().getOne(rtuLambdaQueryWrapper);
		LambdaQueryWrapper<RegionInfoEntity> regionQueryWrapper = Wrappers.<RegionInfoEntity>query().lambda();
		regionQueryWrapper.eq(RegionInfoEntity::getAdcd, rtuInfoEntity.getAdCode());
		RegionInfoEntity regionInfoEntity = getRegionService().getOne(regionQueryWrapper);
		String ancestors = regionInfoEntity.getAncestors() + "," + rtuInfoEntity.getAdCode();
		String[] ancestorsList = ancestors.split(",");
		if (ancestorsList.length >= 3) {
			String adCode = ancestorsList[2];
			LambdaQueryWrapper<DeptRegionEntity> wrapper = Wrappers.<DeptRegionEntity>query().lambda();
			wrapper.eq(DeptRegionEntity::getAdcd, adCode);
			List<DeptRegionEntity> deptRegionEntities = getDeptRegionService().list(wrapper);
			if (null != deptRegionEntities && deptRegionEntities.size() > 0) {
				for (DeptRegionEntity deptRegionEntity : deptRegionEntities) {
					Dept dept = getDeptService().getById(deptRegionEntity.getDeptId());
					if (null != dept) {
						if (dept.getDeptCategory() == 1) {
							vo.setManageCompany(dept.getDeptName());
							//查找运维公司负责人
							LambdaQueryWrapper<User> userLambdaQueryWrapper = Wrappers.<User>query().lambda();
							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.setManageCompanyAdminName(u.getRealName());
									vo.setManageCompanyAdminPhone(u.getPhone());
								}
							}
						}
					}
				}
			}
		}
		//预警信息
		RtuWarningInfoEntity rtuWarningInfoEntity = getRtuWarningService().getById(vo.getWarnId());
		if (null != rtuWarningInfoEntity) {
			String 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;
	}

}
