package com.palmnest.service.impl;

import com.palmnest.application.Constants;
import com.palmnest.application.core.util.DateUtils;
import com.palmnest.dao.UserDateActiveDao;
import com.palmnest.model.UserDateActiveCount;
import com.palmnest.service.UserDateActiveManager;
import com.palmnest.service.redis.BaseRedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
@Service("userDateActiveManager")
public class UserDateActiveManagerImpl extends GenericManagerImpl<UserDateActiveCount, Long> implements UserDateActiveManager {


	private UserDateActiveDao userDateActiveDao;

	@Autowired
	public void setUserDateActiveDao(UserDateActiveDao userDateActiveDao) {
		this.userDateActiveDao = userDateActiveDao;
		this.dao = userDateActiveDao;
	}

	@Autowired
	private BaseRedisService redisService;


	@Async
	public void saveUserToLocal() {
		// 获取今天的日期字符串 yyyy-MM-dd
		String todayStr = DateUtils.getDate("");
		log.info("saveUserToLocal/todayStr:" + todayStr);
		// 获取今天总的活跃量 格式: userActive:yyyy-MM-dd
		String todayKey = Constants.USER_ACTIVE.concat(":").concat(todayStr);
		log.info("saveUserToLocal/todayKey:" + todayKey);
		// 获取今天所有的活跃量
		Set<String> allKeySet = redisService.getKeyValues(todayKey);
		try {
			if (allKeySet != null && allKeySet.size() > 0) {
				//拿到相应的数据userActive:yyyy-MM-dd:orgId:userId
				for (String keySet : allKeySet) {
					String[] split = keySet.split(":");
					// userId
					String userId = split[3];
					// 项目id
					String orgId = split[2];
					// 日期
					String date = split[1];
					log.info("saveUserToLocal/date:" + date + "/orgId:" + orgId + "/userId:" + userId);
					UserDateActiveCount userActive = userDateActiveDao.getUserDateActive(date, orgId, userId);
					if (userActive != null) {
						log.info("saveUserToLocal1/date:" + date + "/orgId:" + orgId + "/userId:" + userId);
						userActive.setModifyTime(new Date());
						userDateActiveDao.updateObj(userActive);
					} else {
						log.info("saveUserToLocal2/date:" + date + "/orgId:" + orgId + "/userId:" + userId);
						userActive = new UserDateActiveCount();
						userActive.setCount(1);
						userActive.setDate(DateUtils.getDateByString(date, DateUtils.YYYY_MM_DD));
						userActive.setModifyTime(new Date());
						userActive.setOrgId(Integer.parseInt(orgId));
						userActive.setUserId(Integer.parseInt(userId));
						userActive.setCreateTime(new Date());
						userDateActiveDao.save(userActive);
					}
				}
			}
		} catch (Exception e) {
			log.error("UserDateActiveManagerImpl---saveUserToLocal---:" + e.getMessage());
			e.printStackTrace();
		}
		redisService.delKeyLike(todayKey);
	}


	@Override
	public List<Map<String, Object>> getUserDateActive(String orgId,
													   int dateType, String date, String endDate) {
		return userDateActiveDao.getUserDateActive(orgId, dateType, date, endDate);
	}

}
