/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2022/8/22
 */
package org.springblade.modules.baseinfo.region.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.constant.BusinessConstant;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.baseinfo.region.dto.RegionInfoDTO;
import org.springblade.modules.baseinfo.region.entity.RegionInfoEntity;
import org.springblade.modules.baseinfo.region.entity.RegionTreeNodeEntity;
import org.springblade.modules.baseinfo.region.mapper.RegionInfoMapper;
import org.springblade.modules.baseinfo.region.service.IBaseInfoRegionService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/***
 * Date:2022/8/22
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Service
public class BaseInfoRegionServiceImpl extends BaseServiceImpl<RegionInfoMapper, RegionInfoEntity> implements IBaseInfoRegionService {

	@Resource
	private RedisTemplate redisTemplate;

	@Override
	public boolean excludeRegion(RegionInfoDTO dto) {
		LambdaUpdateWrapper<RegionInfoEntity> updateWrapper = Wrappers.<RegionInfoEntity>update().lambda();
		updateWrapper.set(RegionInfoEntity::getExcludeAd, 1);
		updateWrapper.and(wrapper -> wrapper.or().eq(RegionInfoEntity::getAdcd, dto.getAdcd()).or().like(RegionInfoEntity::getAncestors, dto.getAdcd()));
		return this.update(updateWrapper);
	}

	@Override
	public boolean resetRegion(RegionInfoDTO dto) {
		LambdaUpdateWrapper<RegionInfoEntity> updateWrapper = Wrappers.<RegionInfoEntity>update().lambda();
		updateWrapper.set(RegionInfoEntity::getExcludeAd, 0);
		updateWrapper.and(wrapper -> wrapper.or().eq(RegionInfoEntity::getAdcd, dto.getAdcd()).or().like(RegionInfoEntity::getAncestors, dto.getAdcd()));
		return this.update(updateWrapper);
	}

	@Override
	public boolean updateAdminRegionCache() {
		ValueOperations<String, String> opsHash = redisTemplate.opsForValue();
		opsHash.set("region.ver.admin", "");
		return true;
	}

	@Override
	public boolean updateDeptRegionCache(Long deptId) {
		ValueOperations<String, String> opsHash = redisTemplate.opsForValue();
		opsHash.set("region.ver."+ Func.toStr(deptId), "");
		return true;
	}

	private RegionTreeNodeEntity getNode(List<RegionTreeNodeEntity> list, String adcd) {
		if (null != list && list.size() > 0) {
			for (RegionTreeNodeEntity entity : list) {
				if (entity.getValue().equals(adcd)) {
					return entity;
				} else {
					RegionTreeNodeEntity node = getNode(entity.getChildren(), adcd);
					if (null != node) {
						return node;
					}
				}
			}
		}
		return null;
	}

	@Override
	public RegionTreeNodeEntity getRegionNode(String roleName, String deptId, String adcd) {
		ValueOperations<String, String> opsHash = redisTemplate.opsForValue();
		if (BusinessConstant.ROLE_SYS_ADMIN.equals(roleName)) {
			String regionJson = opsHash.get("region.admin");
			if (null != regionJson) {
				List<RegionTreeNodeEntity> nodeEntities = JSON.parseObject(regionJson, new TypeReference<List<RegionTreeNodeEntity>>() {
				});
				return this.getNode(nodeEntities, adcd);
			}
		} else {
			String regionJson = opsHash.get("region." + deptId);
			if (null != regionJson) {
				List<RegionTreeNodeEntity> nodeEntities = JSON.parseObject(regionJson, new TypeReference<List<RegionTreeNodeEntity>>() {
				});
				return this.getNode(nodeEntities, adcd);
			}
		}
		return null;
	}

}
