DownloadPdfService.java 3.58 KB
package com.tanpu.fund.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tanpu.common.model.fund.req.IncReportDownloadTimesReq;
import com.tanpu.fund.entity.generator.ReportDownloadSummary;
import com.tanpu.fund.mapper.generator.ReportDownloadSummaryMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DownloadPdfService {

    @Resource
    private ReportDownloadSummaryMapper reportDownloadSummaryMapper;

    private static final Integer onceAddTimes = 200;

    @Transactional(rollbackFor = Exception.class)
    public void emptyStandardVipCount(String userId) {
        LambdaQueryWrapper<ReportDownloadSummary> qw = new LambdaQueryWrapper<>();
        List<ReportDownloadSummary> reportDownloadSummaries = reportDownloadSummaryMapper.selectList(qw.eq(ReportDownloadSummary::getUserId, userId));
        if (CollectionUtils.isEmpty(reportDownloadSummaries)){
            ReportDownloadSummary entity = ReportDownloadSummary.builder().userId(userId).stdVipLimit(0).build();
            reportDownloadSummaryMapper.insert(entity);
        }else {
            ReportDownloadSummary entity = reportDownloadSummaries.get(0);
            entity.setStdVipLimit(0);
            entity.setDeepReportStd(0);
            entity.setHoldFundReportStd(0);
            entity.setCumulativeProfitReportStd(0);
            reportDownloadSummaryMapper.updateById(entity);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void addStandardVipCount(String userId) {
        LambdaQueryWrapper<ReportDownloadSummary> qw = new LambdaQueryWrapper<>();
        List<ReportDownloadSummary> reportDownloadSummaries = reportDownloadSummaryMapper.selectList(qw.eq(ReportDownloadSummary::getUserId, userId));
        if (CollectionUtils.isEmpty(reportDownloadSummaries)){
            ReportDownloadSummary entity = ReportDownloadSummary.builder().userId(userId).stdVipLimit(onceAddTimes).build();
            reportDownloadSummaryMapper.insert(entity);
        }else {
            ReportDownloadSummary entity = reportDownloadSummaries.get(0);
            entity.setStdVipLimit(entity.getStdVipLimit() + onceAddTimes);
            reportDownloadSummaryMapper.updateById(entity);
        }
    }

    /**
     * 增加报告下载次数
     *
     * @param req 请求参数
     * @return 总次数
     */
    @Transactional(rollbackFor = Exception.class)
    public int incReportDownloadTimes(IncReportDownloadTimesReq req) {
        ReportDownloadSummary record = reportDownloadSummaryMapper.selectOne(
                Wrappers.<ReportDownloadSummary>lambdaQuery().eq(ReportDownloadSummary::getUserId, req.getUserId()));
        if (record == null) {
            reportDownloadSummaryMapper.insert(
                    ReportDownloadSummary.builder()
                            .userId(req.getUserId())
                            .reportTimesBuy(req.getIncTimes())
                            .build()
            );
        } else {
            reportDownloadSummaryMapper.updateById(
                    ReportDownloadSummary.builder()
                            .id(record.getId())
                            .reportTimesBuy(record.getReportTimesBuy() + req.getIncTimes())
                            .build()
            );
        }
        return record == null ? req.getIncTimes() :  record.getReportTimesBuy() + req.getIncTimes();
    }
}