/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2022/8/22
 */
package org.springblade.modules.baseinfo.rtu.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.vo.RtuInfoVO;
import org.springblade.modules.system.service.IDictBizService;

import java.util.*;

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

	private static IDictBizService dictBizService;

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

	private static IBaseInfoRegionService  regionService;

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


	@Override
	public RtuInfoVO entityVO(RtuInfoEntity entity) {
		RtuInfoVO rtuInfoVO = Objects.requireNonNull(BeanUtil.copy(entity, RtuInfoVO.class));
		return this.entityVO(rtuInfoVO);
	}

	public RtuInfoVO entityVO(RtuInfoVO entity) {
		List<String> siteKinds = new ArrayList<>();
		String rtuKindDesc = "";
		if (entity.getIsRain() != null && entity.getIsRain() == 1) {
			rtuKindDesc = "雨量站";
			siteKinds.add("1");
		}

		if (entity.getIsRiver() != null && entity.getIsRiver() == 1) {
			if (rtuKindDesc.length() > 0) {
				rtuKindDesc += "/";
			}
			rtuKindDesc += "水位站";
			siteKinds.add("2");
		}

		if (entity.getIsRes() != null && entity.getIsRes() == 1) {
			if (rtuKindDesc.length() > 0) {
				rtuKindDesc += "/";
			}
			rtuKindDesc += "水位站";
			siteKinds.add("3");
		}

		if (entity.getIsGround() != null && entity.getIsGround() == 1) {
			if (rtuKindDesc.length() > 0) {
				rtuKindDesc += "/";
			}
			rtuKindDesc += "墒情站";
			siteKinds.add("4");
		}
		if (entity.getIsVideo() != null && entity.getIsVideo() == 1) {
			if (rtuKindDesc.length() > 0) {
				rtuKindDesc += "/";
			}
			rtuKindDesc += "视频站";
			siteKinds.add("5");
		}
		entity.setRtuKindDesc(rtuKindDesc);
		String[] kinds = siteKinds.toArray(new String[siteKinds.size()]);
		entity.setSiteKind(kinds);

		LambdaQueryWrapper<RegionInfoEntity> regionInfoEntityLambdaQueryWrapper = Wrappers.<RegionInfoEntity>query().lambda();
		regionInfoEntityLambdaQueryWrapper.eq(RegionInfoEntity::getIsDeleted, 0);
		regionInfoEntityLambdaQueryWrapper.eq(RegionInfoEntity::getAdcd, entity.getAdCode());
		RegionInfoEntity regionInfoEntity = getRegionService().getOne(regionInfoEntityLambdaQueryWrapper);
		if(null != regionInfoEntity) {
			regionInfoEntityLambdaQueryWrapper = Wrappers.<RegionInfoEntity>query().lambda();
			regionInfoEntityLambdaQueryWrapper.eq(RegionInfoEntity::getIsDeleted, 0);
			regionInfoEntityLambdaQueryWrapper.eq(RegionInfoEntity::getAdcd, regionInfoEntity.getParentCode());
			RegionInfoEntity region = getRegionService().getOne(regionInfoEntityLambdaQueryWrapper);
			entity.setAdCityCode(region.getAdcd());
		}

		return entity;
	}
}
