package com.czg.service.market.service.impl;

import com.czg.account.entity.ShopInfo;
import com.czg.account.service.ShopInfoService;
import com.czg.market.entity.MkPointsUserRecord;
import com.czg.market.service.MkPointsUserRecordService;
import com.czg.sa.StpKit;
import com.czg.service.market.mapper.MkPointsUserRecordMapper;
import com.czg.utils.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 会员积分变动记录 服务层实现。
 *
 * @author ww
 * @since 2025-12-08
 */
@Service
public class MkPointsUserRecordServiceImpl extends ServiceImpl<MkPointsUserRecordMapper, MkPointsUserRecord> implements MkPointsUserRecordService {

    @DubboReference
    private ShopInfoService shopInfoService;

    @Override
    public Page<MkPointsUserRecord> pageByPointsUserId(Integer page, Integer size, Long mkPointsUserId) {
        if (mkPointsUserId == null) {
            return PageUtil.emptyPage();
        }
        ShopInfo shopInfo = shopInfoService.getById(StpKit.USER.getShopId());
        if (shopInfo == null) {
            return PageUtil.emptyPage();
        }
        Page<MkPointsUserRecord> pages = page(Page.of(page, size),
                query().eq(MkPointsUserRecord::getMkPointsUserId, mkPointsUserId).orderBy(MkPointsUserRecord::getCreateTime, false));
        pages.getRecords().forEach(s -> s.setShopName(shopInfo.getShopName()));
        return pages;
    }

    @Override
    public Page<MkPointsUserRecord> getPointsUserRecord(Integer page, Integer size, Long mkPointsUserId) {
        if (mkPointsUserId == null) {
            return PageUtil.emptyPage();
        }
        PageHelper.startPage(page, size);
        List<MkPointsUserRecord> record = mapper.getPointsUserRecord(mkPointsUserId);
        return PageUtil.convert(new PageInfo<>(record));
    }

}
