/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2022/8/24
 */
package org.springblade.modules.business.warning.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.SpringUtil;
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.warning.entity.RtuWarningInfoEntity;
import org.springblade.modules.business.warning.vo.RtuWarningInfoVO;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;

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

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

	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 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;
	}

	@Override
	public RtuWarningInfoVO entityVO(RtuWarningInfoEntity entity) {
		RtuWarningInfoVO rtuWarningInfoVO = Objects.requireNonNull(BeanUtil.copy(entity, RtuWarningInfoVO.class));
		return this.entityVO(rtuWarningInfoVO);
	}

	public RtuWarningInfoVO entityVO(RtuWarningInfoVO 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(",");
		for (int i=2;i<ancestorsList.length;i++){
			String adCode=ancestorsList[i];
			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());
							break;
						}
					}
				}
			}
		}
		return vo;
	}
}
