package com.palmnest.service.user.impl;

import com.palmnest.application.Constants;
import com.palmnest.application.core.util.DateHelper;
import com.palmnest.service.UserActiveManager;
import com.palmnest.service.UserDateActiveManager;
import com.palmnest.service.redis.BaseRedisService;
import com.palmnest.service.user.ModuleUserCountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @Author HXT
 * @Description TODO
 * @Date 2019/1/10 16:16
 **/
@Slf4j
@Service
public class ModuleUserCountServiceImpl implements ModuleUserCountService {


	@Autowired
	private BaseRedisService      redisService;
	@Autowired
	private UserActiveManager     userActiveManager;
	@Autowired
	private UserDateActiveManager userDateActiveManager;

	@Override
	@Async
	public void saveModuleUserToRedis(String serviceCode, Integer userId, Integer orgId) {
		try {
			String currentDateStr = DateHelper.getCurrentDateStr(DateHelper.CURRENT_DATE);// 当前日期字符串 yyyy-MM-dd
			// 构建redis bitMap存储的key值;
			// 格式为: 总称:日期:项目:模块   moduleActive:2019-1-10:605:FW-001
			String key = Constants.MODULE_ACTIVE + ":" + currentDateStr + ":" + orgId + ":" + serviceCode;
			log.info("key：" + key);
			log.info("redisService.getBit(key,userId)：" + redisService.getBit(key, userId));
			if (!redisService.getBit(key, userId)) {
				redisService.setBit(key, userId, true);// 设置用户活跃状态为true
			}

			String userKey = Constants.USER_ACTIVE + ":" + currentDateStr + ":" + orgId + ":" + userId;
			log.info("userKey：" + userKey);
			redisService.set(userKey, "1");
		} catch (Exception e) {
			log.error("ModuleUserCountServiceImpl saveModuleUser 更新模块用户状态异常=====>" + e.getMessage());
		}
	}

	@Override
	@Async
	public void saveModuleUserToLocal() {
		userActiveManager.saveModuleUserToLocal();
		userDateActiveManager.saveUserToLocal();
	}


}
