package cn.pluss.platform.bill.impl;

import cn.pluss.platform.entity.MerchantBillStatistics;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.MerchantBillStatisticsMapper;
import cn.pluss.platform.bill.MerchantBillStatisticsService;
import cn.pluss.platform.userApp.UserAppService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.context.annotation.Lazy;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 按日统计商户的账单 服务实现类
 * </p>
 *
 * @author Djh
 * @since 2021-03-11
 */
@Service
public class MerchantBillStatisticsServiceImpl extends ServiceImpl<MerchantBillStatisticsMapper, MerchantBillStatistics> implements MerchantBillStatisticsService {

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

    @Resource
    private HttpServletRequest request;

    @Override
    public <E extends IPage<MerchantBillStatistics>> E page(E page, Wrapper<MerchantBillStatistics> queryWrapper) {
        MerchantBillStatistics 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);
    }
}
