package com.cyksj.service.user.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.mapper.UserLoginRecordMapper;
import com.cyksj.model.entity.UserLoginRecord;
import com.cyksj.service.user.UserLoginRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 项目名: yhlxj
 * 文件名: UserLoginRecordServiceImpl
 * 创建者: JavaZou
 * 创建时间:2023/12/13 18:15
 */
@Service
@RequiredArgsConstructor
public class UserLoginRecordServiceImpl implements UserLoginRecordService {
	private final UserLoginRecordMapper userLoginRecordMapper;

	/**
	 * 用户每日活跃记录
	 */
	public void saveUserLoginDayRecord(Long userId) {
		//今日是否记录活跃登录
		Integer count = userLoginRecordMapper.selectCount(Wrappers.lambdaQuery(UserLoginRecord.class)
				.eq(UserLoginRecord::getUserId, userId)
				.ge(UserLoginRecord::getCreatedTime, DateUtil.beginOfDay(DateTime.now())));

		if (count == 0) {
			UserLoginRecord userLoginRecord = new UserLoginRecord();
			userLoginRecord.setUserId(userId);
			userLoginRecordMapper.insert(userLoginRecord);
		}
	}
}
