package cn.pluss.platform.bill.impl;

import cn.pluss.platform.bill.MemberTransStatisticsService;
import cn.pluss.platform.entity.MemberTransStatistics;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.MemberTransStatisticsMapper;
import cn.pluss.platform.userApp.UserAppService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员消费日记录 服务实现类
 * </p>
 *
 * @author Djh
 * @since 2021-03-17
 */
@Service
public class MemberTransStatisticsServiceImpl extends ServiceImpl<MemberTransStatisticsMapper, MemberTransStatistics> implements MemberTransStatisticsService {

    @Setter(onMethod_ = {@Autowired})
    private UserAppService userAppService;

    @Resource
    private HttpServletRequest request;

    @Override
    public <E extends IPage<MemberTransStatistics>> E page(E page, Wrapper<MemberTransStatistics> queryWrapper) {
        MemberTransStatistics entity = queryWrapper.getEntity();
        if (entity.getUserId() == null) {
            throw new MsgException("缺少userId");
        }

        UserApp uaBoss = userAppService.getBossUserApp(entity.getUserId(), request.getHeader("merchantCode"));
        if (uaBoss != null) {
            entity.setUserId(uaBoss.getUserId().intValue());
        }

        return super.page(page, queryWrapper);
    }
}
