package com.jeesharp.business.modules.coupon.application.api;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.coupon.api.MemberCouponStatsApi;
import com.jeesharp.business.modules.coupon.dto.MemberCouponStoreStatsQueryDTO;
import com.jeesharp.business.modules.coupon.enums.CouponSceneCodeEnum;
import com.jeesharp.business.modules.coupon.mapper.MemberCouponMapper;
import com.jeesharp.business.modules.coupon.vo.MemberCouponChannelCountVO;
import com.jeesharp.business.modules.coupon.vo.MemberCouponDailyCountVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * {@link MemberCouponStatsApi} 实现，查询店铺线下券领券统计。
 *
 * @author jeesharp
 * @since 2026-06-25
 */
@Service
@AllArgsConstructor
public class MemberCouponStatsApiImpl implements MemberCouponStatsApi {

    private final MemberCouponMapper memberCouponMapper;

    @Override
    public Long countStoreOfflineReceive(MemberCouponStoreStatsQueryDTO query) {
        if (query == null || StrUtil.isBlank(query.getTenantId())) {
            return 0L;
        }
        Long count = memberCouponMapper.countStoreOfflineReceive(
                query.getTenantId(),
                CouponSceneCodeEnum.STORE_OFFLINE.getCode(),
                query.getStartDateTime(),
                query.getEndDateTime(),
                query.getChannelCode());
        return count != null ? count : 0L;
    }

    @Override
    public List<MemberCouponDailyCountVO> dailyStoreOfflineReceive(MemberCouponStoreStatsQueryDTO query) {
        if (query == null || StrUtil.isBlank(query.getTenantId())) {
            return Collections.emptyList();
        }
        List<MemberCouponDailyCountVO> list = memberCouponMapper.dailyStoreOfflineReceive(
                query.getTenantId(),
                CouponSceneCodeEnum.STORE_OFFLINE.getCode(),
                query.getStartDateTime(),
                query.getEndDateTime(),
                query.getChannelCode());
        return list != null ? list : Collections.emptyList();
    }

    @Override
    public List<MemberCouponChannelCountVO> channelStoreOfflineReceive(MemberCouponStoreStatsQueryDTO query) {
        if (query == null || StrUtil.isBlank(query.getTenantId())) {
            return Collections.emptyList();
        }
        List<MemberCouponChannelCountVO> list = memberCouponMapper.channelStoreOfflineReceive(
                query.getTenantId(),
                CouponSceneCodeEnum.STORE_OFFLINE.getCode(),
                query.getStartDateTime(),
                query.getEndDateTime());
        return list != null ? list : Collections.emptyList();
    }
}
