全部
常见问题
产品动态
精选推荐

多商户—每月帐单定时任务优化

管理 管理 编辑 删除

近期,我发现测试环境的帐单数据有些异常,2月份之后的数据都没有,在检查演示环境后,发现演示环境正常。最后确定是测试环境在暴力测试或手动改动数据后,导致的问题—数据库没有当月平台月帐单记录,从而导致定时任务一直执行不下去,月账单记录一直缺失。

优化方案:

在每月1号凌晨2点,因各种情况导致的平台上月账单记录不存在的情况下,定时任务依旧能正常执行,统计上个月的数据,初始化当月数据

(以后的版本sql中不会再有当月月账单记录的插入)

优化后的代码:

改动Java项目中StatisticsTaskServiceImpl类下monthStatement()方法


/**
     * 每月帐单定时任务
     * 每月第一天2点执行
     * 获取上个月所有的帐单记录,然后通过累加获取月帐单记录
     * 生成当月帐单记录,数值为0
     */
    @Override
    public void monthStatement() {
        String lastMonth = DateUtil.lastMonth().toString(DateConstants.DATE_FORMAT_MONTH);
        PlatformMonthStatement platformMonthStatement = platformMonthStatementService.getByMonth(lastMonth);
        List<MerchantMonthStatement> merchantMonthStatementList = CollUtil.newArrayList();
        List<Integer> merIdList = merchantService.getAllId();
        if (ObjectUtil.isNull(platformMonthStatement)) {
            LOGGER.error("每月帐单定时任务,未查询到上一个月的数据,现在开始初始化上个月数据");
            // 初始化上一个月的数据,值为0
            platformMonthStatement = new PlatformMonthStatement();
            platformMonthStatement.setDataDate(lastMonth);
            // 初始化上一个月的商户帐单数据,值为0
            merchantMonthStatementList = merchantMonthStatementService.findByMonth(lastMonth);
            List<MerchantMonthStatement> tempMerchantMonthStatementList = CollUtil.newArrayList();
            if (CollUtil.isNotEmpty(merchantMonthStatementList)) {
                if (merIdList.size() != merchantMonthStatementList.size()) {
                    // 处理上个约没有月账单的商户数据
                    List<Integer> staMerIdList = merchantMonthStatementList.stream().map(MerchantMonthStatement::getMerId).collect(Collectors.toList());
                    for (Integer merId : merIdList) {
                        if (!staMerIdList.contains(merId)) {
                            MerchantMonthStatement merchantMonthStatement = new MerchantMonthStatement();
                            merchantMonthStatement.setMerId(merId);
                            merchantMonthStatement.setDataDate(lastMonth);
                            tempMerchantMonthStatementList.add(merchantMonthStatement);
                        }
                    }
                }
            } else {
                merIdList.forEach(merId -> {
                    MerchantMonthStatement merchantMonthStatement = new MerchantMonthStatement();
                    merchantMonthStatement.setMerId(merId);
                    merchantMonthStatement.setDataDate(lastMonth);
                    tempMerchantMonthStatementList.add(merchantMonthStatement);
                });
            }

            PlatformMonthStatement finalPlatformMonthStatement = platformMonthStatement;
            Boolean execute = transactionTemplate.execute(e -> {
                boolean save = platformMonthStatementService.save(finalPlatformMonthStatement);
                if (!save) {
                    LOGGER.error("每月帐单定时任务,初始化上个月平台月帐单失败,触发回滚");
                    e.setRollbackOnly();
                    return Boolean.FALSE;
                }
                save = merchantMonthStatementService.saveBatch(tempMerchantMonthStatementList, 100);
                if (!save) {
                    LOGGER.error("每月帐单定时任务,初始化上个月商户月帐单失败,触发回滚");
                    e.setRollbackOnly();
                    return Boolean.FALSE;
                }
                return Boolean.TRUE;
            });
            if (!execute) {
                LOGGER.error("每月帐单定时任务,因没有上个月数据,初始化上个月数据时失败");
                return;
            }
            platformMonthStatement.setId(finalPlatformMonthStatement.getId());
            if (CollUtil.isNotEmpty(tempMerchantMonthStatementList)) {
                merchantMonthStatementList.addAll(tempMerchantMonthStatementList);
            }
        }
        writePlatformMonthStatement(platformMonthStatement);
        writeMerchantMonthStatement(merchantMonthStatementList, lastMonth);
        PlatformMonthStatement finalTempPlatformMonthStatement = platformMonthStatement;
        List<MerchantMonthStatement> finalMerchantMonthStatementList = merchantMonthStatementList;
        transactionTemplate.execute(e -> {
            boolean save = platformMonthStatementService.updateById(finalTempPlatformMonthStatement);
            if (!save) {
                LOGGER.error("每月帐单定时任务,更新平台月帐单失败,触发回滚");
                e.setRollbackOnly();
                return Boolean.FALSE;
            }
            save = merchantMonthStatementService.updateBatchById(finalMerchantMonthStatementList, 100);
            if (!save) {
                LOGGER.error("每月帐单定时任务,更新商户月帐单失败,触发回滚");
                e.setRollbackOnly();
                return Boolean.FALSE;
            }
            save = initMonthStatement(merIdList);
            if (!save) {
                LOGGER.error("每月帐单定时任务,初始化平台当月帐单失败,触发回滚");
                e.setRollbackOnly();
                return Boolean.FALSE;
            }
            return Boolean.TRUE;
        });
    }

最后

在代码改动发布后,如果你跟我一样,当前2023-05月,在数据库没有的记录的情况下,想看到统计的2023-04月的统计数据

在平台端的管理后台,维护 → 定时任务管理 → 定时任务,找到每月帐单定时任务,点击触发即可

就可以在eb_platform_month_statement和eb_merchant_month_statement表中看到对应数据

4c86a202305061505459499.png

请登录后查看

❄指缝de阳光 最后编辑于2023-05-06 15:09:58

快捷回复
回复({{post_count}}) {{!is_user ? '我的回复' :'全部回复'}}
回复从新到旧

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}}

作者 管理员 企业

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest==1? '取消推荐': '推荐'}}
{{item.floor}}#
{{item.user_info.title}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
{{item.like_count}}
{{item.showReply ? '取消回复' : '回复'}}
删除
回复
回复

{{itemc.user_info.nickname}}

{{itemc.user_name}}

作者 管理员 企业

回复 {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}   {{itemc.ip_address}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回复' : '回复'}}
删除
回复
回复
查看更多
回复
回复
1568
{{like_count}}
{{collect_count}}
添加回复 ({{post_count}})

相关推荐

❄指缝de阳光 管理员
暂无简介

回答

27

发布

3

经验

355

快速安全登录

使用微信扫码登录
{{item.label}} {{item.label}} {{item.label}} 板块推荐 常见问题 产品动态 精选推荐 首页头条 首页动态 首页推荐
加精
取 消 确 定
回复
回复
问题:
问题自动获取的帖子内容,不准确时需要手动修改. [获取答案]
答案:
提交
bug 需求 取 消 确 定

微信登录/注册

切换手机号登录

{{ bind_phone ? '绑定手机' : '手机登录'}}

{{codeText}}
切换微信登录/注册
暂不绑定
CRMEB客服

CRMEB咨询热线 咨询热线

400-8888-794

微信扫码咨询

CRMEB开源商城下载 开源下载 CRMEB官方论坛 帮助文档
返回顶部 返回顶部
CRMEB客服