/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2023/9/13
 */
package org.springblade.modules.map.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.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.baseinfo.rtu.wrapper.RtuInfoWrapper;
import org.springblade.modules.map.entity.DangerAreaEntity;
import org.springblade.modules.map.vo.DangerAreaVO;

import java.util.Objects;

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

	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 DangerAreaVO entityVO(DangerAreaEntity entity) {
		DangerAreaVO vo = Objects.requireNonNull(BeanUtil.copy(entity, DangerAreaVO.class));
		return this.entityVO(vo);
	}

	public DangerAreaVO entityVO(DangerAreaVO vo) {
		String stCode = vo.getStCode();
		if (null != stCode) {
			String rtuName = "";
			String[] stCodeArr = stCode.split(",");
			for (String st : stCodeArr) {
				LambdaQueryWrapper<RtuInfoEntity> wrapper = Wrappers.<RtuInfoEntity>query().lambda();
				wrapper.eq(RtuInfoEntity::getRtuCode, st);
				RtuInfoEntity rtuInfoEntity = getRtuBaseInfoService().getOne(wrapper);
				if (null != rtuInfoEntity) {
					if (rtuName.length() > 0) {
						rtuName += ",";
					}
					rtuName += rtuInfoEntity.getRtuName();
				}
			}
			vo.setRtuName(rtuName);
		}

		return vo;
	}
}
