package com.ym.mec.biz.service.impl;

import com.ym.mec.biz.dal.dao.SysConfigDao;
import com.ym.mec.biz.dal.dao.SysTenantConfigDao;
import com.ym.mec.biz.dal.entity.SysConfig;
import com.ym.mec.biz.dal.entity.SysTenantConfig;
import com.ym.mec.biz.event.TenantConfigChangeEvent;
import com.ym.mec.biz.service.SysTenantConfigService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.redis.service.RedisCache;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.util.collection.MapUtil;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Service
public class SysTenantConfigServiceImpl extends BaseServiceImpl<Integer, SysTenantConfig>  implements SysTenantConfigService {
	
	@Autowired
	private SysTenantConfigDao sysTenantConfigDao;
	@Autowired
	private SysConfigDao sysConfigDao;
	@Autowired
	private RedissonClient redissonClient;
	private static final String tenantKey = "sysTenantConfig";

	@Override
	public BaseDAO<Integer, SysTenantConfig> getDAO() {
		return sysTenantConfigDao;
	}

//	private ApplicationEventPublisher applicationEventPublisher;

//	@Override
//	public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
//		this.applicationEventPublisher = applicationEventPublisher;
//		applicationEventPublisher.publishEvent(new TenantConfigChangeEvent(this));
//	}

	@Override
	public void setTenantConfig(ConcurrentHashMap<Integer, ConcurrentHashMap<String, String>> tenantConfig) {
		RBucket<ConcurrentHashMap<Integer,ConcurrentHashMap<String,String>>> bucket = redissonClient.getBucket(tenantKey);
		bucket.set(tenantConfig);
	}

	@Override
	public String getTenantConfigValue(String paramName,Integer tenantId) {
//		RBucket<ConcurrentHashMap<Integer,ConcurrentHashMap<String,String>>> bucket = redissonClient.getBucket(tenantKey);
//		ConcurrentHashMap<Integer, ConcurrentHashMap<String, String>> tenantConfigMap = bucket.get();
//		ConcurrentHashMap<String, String> map = tenantConfigMap.get(tenantId);
//		if(map != null){
//			return map.get(paramName);
//		}
//		applicationEventPublisher.publishEvent(new TenantConfigChangeEvent(this,tenantId));
		return sysTenantConfigDao.getConfigValue(paramName,tenantId);
	}

	@Override
	public ConcurrentHashMap<Integer, ConcurrentHashMap<String, String>> getAllTenantConfig() {
		RBucket<ConcurrentHashMap<Integer,ConcurrentHashMap<String,String>>> bucket = redissonClient.getBucket(tenantKey);
		return bucket.get();
	}

	@Override
	public List<SysConfig> queryAll(Map<String, Object> params) {
		return sysTenantConfigDao.queryALl(params);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchUpSet(List<SysConfig> sysConfigs, Integer tenantId) {
		if(sysConfigs == null || sysConfigs.size() == 0){
			return;
		}
		//如果是平台账户只改默认值
		if(tenantId < 0){
			sysConfigDao.batchUpdate(sysConfigs);
			return;
		}
		List<Long> configIdList = sysConfigs.stream().map(e -> e.getId()).collect(Collectors.toList());
		if(tenantId > 0){
			sysTenantConfigDao.delByConfigId(configIdList,tenantId);
			sysTenantConfigDao.batchInsert(sysConfigs,tenantId);
		}
//		applicationEventPublisher.publishEvent(new TenantConfigChangeEvent(this,tenantId));
	}

	@Override
	public ConcurrentHashMap<String, String> queryAllMap(Integer tenantId) {
		List<Map<String, String>> maps = sysTenantConfigDao.queryAllMap(tenantId);
		ConcurrentHashMap<String, String> resultMap = null;
		if(maps != null){
			resultMap = new ConcurrentHashMap(MapUtil.convertMybatisMap(maps));
		}
		return resultMap;
	}

}
