package com.jeesharp.business.modules.member.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.member.entity.MemberSignInRecord;
import com.jeesharp.business.modules.member.mapper.MemberSignInRecordMapper;
import com.jeesharp.business.modules.member.service.MemberSignInRecordService;
import com.jeesharp.framework.pojo.Page;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员签到记录领域服务实现，基于 MyBatis-Plus 条件查询。
 *
 * @author jeesharp
 * @since 2026-05-08
 */
@Service
@AllArgsConstructor
public class MemberSignInRecordServiceImpl implements MemberSignInRecordService {

    private final MemberSignInRecordMapper memberSignInRecordMapper;

    @Override
    public boolean existsOnDate(String memberId, LocalDate signDate) {
        LambdaQueryWrapper<MemberSignInRecord> w = new LambdaQueryWrapper<>();
        w.eq(MemberSignInRecord::getMemberId, memberId);
        w.eq(MemberSignInRecord::getSignDate, signDate);
        w.eq(MemberSignInRecord::getDelFlag, "0");
        return memberSignInRecordMapper.selectCount(w) > 0;
    }

    @Override
    public long countByMemberId(String memberId) {
        LambdaQueryWrapper<MemberSignInRecord> w = new LambdaQueryWrapper<>();
        w.eq(MemberSignInRecord::getMemberId, memberId);
        w.eq(MemberSignInRecord::getDelFlag, "0");
        return memberSignInRecordMapper.selectCount(w);
    }

    @Override
    public List<Integer> listSignedDaysOfMonth(String memberId, YearMonth yearMonth) {
        LocalDate start = yearMonth.atDay(1);
        LocalDate end = yearMonth.atEndOfMonth();
        LambdaQueryWrapper<MemberSignInRecord> w = new LambdaQueryWrapper<>();
        w.eq(MemberSignInRecord::getMemberId, memberId);
        w.eq(MemberSignInRecord::getDelFlag, "0");
        w.between(MemberSignInRecord::getSignDate, start, end);
        w.orderByAsc(MemberSignInRecord::getSignDate);
        return memberSignInRecordMapper.selectList(w).stream()
                .map(r -> r.getSignDate().getDayOfMonth())
                .collect(Collectors.toList());
    }

    @Override
    public void saveRecord(MemberSignInRecord record) {
        if (record.getId() == null) {
            record.setId(IdUtil.getSnowflakeNextIdStr());
        }
        record.preInsert();
        if (record.getDelFlag() == null) {
            record.setDelFlag("0");
        }
        memberSignInRecordMapper.insert(record);
    }

    @Override
    public void updateRecord(MemberSignInRecord record) {
        record.preUpdate();
        memberSignInRecordMapper.updateById(record);
    }

    @Override
    public Page<MemberSignInRecord> findPageByMember(Page<MemberSignInRecord> page, String memberId) {
        LambdaQueryWrapper<MemberSignInRecord> w = new LambdaQueryWrapper<>();
        w.eq(MemberSignInRecord::getMemberId, memberId);
        w.eq(MemberSignInRecord::getDelFlag, "0");
        w.orderByDesc(MemberSignInRecord::getSignDate);
        return memberSignInRecordMapper.selectPage(page, w);
    }

    @Override
    public List<MemberSignInRecord> listRecentByMember(String memberId, int limit) {
        int cap = Math.min(Math.max(limit, 1), 500);
        LambdaQueryWrapper<MemberSignInRecord> w = new LambdaQueryWrapper<>();
        w.eq(MemberSignInRecord::getMemberId, memberId);
        w.eq(MemberSignInRecord::getDelFlag, "0");
        w.orderByDesc(MemberSignInRecord::getSignDate);
        w.last("LIMIT " + cap);
        return memberSignInRecordMapper.selectList(w);
    }
}
