/*
 * Copyright 2022-2025 兮玥(190785909@qq.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.chestnut.member.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chestnut.common.utils.Assert;
import com.chestnut.common.utils.DateUtils;
import com.chestnut.common.utils.IdUtils;
import com.chestnut.member.domain.MemberSignInLog;
import com.chestnut.member.domain.dto.MemberComplementHistoryRequest;
import com.chestnut.member.exception.MemberErrorCode;
import com.chestnut.member.level.impl.SignInExpOperation;
import com.chestnut.member.mapper.MemberSignInLogMapper;
import com.chestnut.member.service.IMemberExpConfigService;
import com.chestnut.member.service.IMemberSignInLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class MemberSignInLogServiceImpl extends ServiceImpl<MemberSignInLogMapper, MemberSignInLog>
		implements IMemberSignInLogService {

	private final IMemberExpConfigService memberExpConfigService;

	@Override
	public void doSignIn(Long memberId) {
		LocalDateTime now = LocalDateTime.now();
		Integer signInKey = Integer.valueOf(now.format(DateUtils.FORMAT_YYYYMMDD));

		Long count = this.lambdaQuery().eq(MemberSignInLog::getMemberId, memberId)
				.eq(MemberSignInLog::getSignInKey, signInKey).count();
		Assert.isTrue(count == 0, MemberErrorCode.SIGN_IN_COMPLETED::exception);
		
		MemberSignInLog signInLog = new MemberSignInLog();
		signInLog.setLogId(IdUtils.getSnowflakeId());
		signInLog.setMemberId(memberId);
		signInLog.setSignInKey(signInKey);
		signInLog.setLogTime(now);
		this.save(signInLog);
		// 触发会员经验值操作
		this.memberExpConfigService.triggerExpOperation(SignInExpOperation.ID, signInLog.getMemberId());
	}

	@Override
	public void complementHistory(MemberComplementHistoryRequest req) {
		LocalDateTime signInDay = LocalDateTime.of(req.getYear(), req.getMonth(), req.getDay(), 0, 0, 0);
		Integer signInKey = Integer.valueOf(signInDay.format(DateUtils.FORMAT_YYYYMMDD));

		Long count = this.lambdaQuery().eq(MemberSignInLog::getMemberId, req.getOperator().getUserId())
				.eq(MemberSignInLog::getSignInKey, signInKey).count();
		Assert.isTrue(count == 0, MemberErrorCode.SIGN_IN_COMPLETED::exception);
		
		MemberSignInLog signInLog = new MemberSignInLog();
		signInLog.setLogId(IdUtils.getSnowflakeId());
		signInLog.setMemberId(req.getOperator().getUserId());
		signInLog.setSignInKey(signInKey);
		signInLog.setLogTime(LocalDateTime.now());
		this.save(signInLog);
		// 触发会员经验值操作
		this.memberExpConfigService.triggerExpOperation(SignInExpOperation.ID, signInLog.getMemberId());
	}
}