package com.mcwl.memberCenter.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.common.constant.HttpStatus;
import com.mcwl.common.core.page.PageDomain;
import com.mcwl.common.core.page.TableDataInfo;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.memberCenter.domain.MemberConsume;
import com.mcwl.memberCenter.domain.vo.MemberConsumeVO;
import com.mcwl.memberCenter.mapper.MemberConsumeMapper;
import com.mcwl.memberCenter.service.MemberConsumeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class MemberConsumeServiceImpl extends ServiceImpl<MemberConsumeMapper, MemberConsume> implements MemberConsumeService {

    private final MemberConsumeMapper memberConsumeMapper;

    @Override
    public TableDataInfo getConsumePoints(PageDomain pageDomain) {

        Page<MemberConsume> page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());

        Long userId = SecurityUtils.getUserId();

        LambdaQueryWrapper<MemberConsume> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MemberConsume::getUserId, userId);

        memberConsumeMapper.selectPage(page, lqw);

        List<MemberConsume> memberConsumeList = page.getRecords();
        List<MemberConsumeVO> memberConsumeVOList = new ArrayList<>();
        for (MemberConsume memberConsume : memberConsumeList) {
            MemberConsumeVO memberConsumeVO = new MemberConsumeVO();
            BeanUtil.copyProperties(memberConsume, memberConsumeVO);
            memberConsumeVOList.add(memberConsumeVO);
        }

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(memberConsumeVOList);
        rspData.setTotal(page.getTotal());

        return rspData;
    }
}
