Ver código fonte

1、调整商户号记录到红包表
2、发送失败的红包进行退回调整商户配置

yys 1 semana atrás
pai
commit
570144a1b0

+ 1 - 1
fs-admin/src/main/java/com/fs/his/task/CompanyBalanceTask.java

@@ -140,7 +140,7 @@ public class CompanyBalanceTask {
      */
     public void checkMchTransferStatusByBatchID(String batchId,String companyIdStr,String appId) {
         Long companyId=Long.parseLong(companyIdStr);
-        R result=companyService.checkMchTransferStatusByBatchID(batchId,companyId,appId);
+        R result=companyService.checkMchTransferStatusByBatchID(batchId,companyId,appId,null);
         log.info("查询商户转账结果:{}",result);
     }
 

+ 1 - 1
fs-company/src/main/java/com/fs/company/controller/common/CommonController.java

@@ -199,7 +199,7 @@ public class CommonController
     {
         // 默认是前两天时间
         Long companyId=Long.parseLong(companyIdStr);
-        R result=companyService.checkMchTransferStatusByBatchID(outBatchNo,companyId,appId);
+        R result=companyService.checkMchTransferStatusByBatchID(outBatchNo,companyId,appId,null);
         log.info("查询商户转账结果:{}",result);
         return R.ok();
     }

+ 1 - 1
fs-service/src/main/java/com/fs/company/service/ICompanyService.java

@@ -200,7 +200,7 @@ public interface ICompanyService
 
     String checkMchTransferStatus(String outBatchNo,Long companyId);
 
-    R checkMchTransferStatusByBatchID(String batchId, Long companyId, String appId);
+    R checkMchTransferStatusByBatchID(String batchId, Long companyId, String appId, String mchId);
 
     void deleteRedPacketLogBatch();
 

+ 141 - 107
fs-service/src/main/java/com/fs/company/service/impl/CompanyServiceImpl.java

@@ -34,8 +34,10 @@ import com.fs.his.config.StoreConfig;
 import com.fs.his.domain.FsInquiryOrder;
 import com.fs.his.domain.FsStoreOrder;
 import com.fs.his.domain.FsStorePayment;
+import com.fs.his.domain.SysRedpacketConfigMore;
 import com.fs.his.dto.InquiryConfigDTO;
 import com.fs.his.mapper.FsStoreOrderMapper;
+import com.fs.his.mapper.SysRedpacketConfigMoreMapper;
 import com.fs.his.vo.OptionsVO;
 import com.fs.hisStore.domain.FsStoreOrderScrm;
 import com.fs.hisStore.domain.FsStorePaymentScrm;
@@ -89,6 +91,9 @@ public class CompanyServiceImpl implements ICompanyService
     private CompanyMapper companyMapper;
     @Autowired
     private CompanyConfigMapper companyConfigMapper;
+
+    @Autowired
+    private SysRedpacketConfigMoreMapper sysRedpacketConfigMoreMapper;
     @Autowired
     private ICompanyMiniappService companyMiniappService;
     @Autowired
@@ -284,63 +289,44 @@ public class CompanyServiceImpl implements ICompanyService
     }
 
     @Override
-    public R checkMchTransferStatusByBatchID(String batchId, Long companyId, String appId) {
-        // 获取配置信息
-        CourseConfig courseConfig = JSONUtil.toBean(configService.selectConfigByKey("course.config"), CourseConfig.class);
-
-        String json;
-        RedPacketConfig config = new RedPacketConfig();
-        // 根据红包模式获取配置
-        switch (courseConfig.getRedPacketMode()){
-            case 1:
-                json = configService.selectConfigByKey("redPacket.config");
-                config = JSONUtil.toBean(json, RedPacketConfig.class);
-                break;
-            case 2:
-                json = companyConfigService.selectRedPacketConfigByKey(companyId);
-                //如果分公司配置为空就走总后台的配置
-                if (StringUtils.isEmpty(json)){
-                    json = configService.selectConfigByKey("redPacket.config");
-                }
-                config = JSONUtil.toBean(json, RedPacketConfig.class);
-                break;
-            default:
-                throw new UnsupportedOperationException("当前红包模式不支持!");
+    public R checkMchTransferStatusByBatchID(String batchId, Long companyId, String appId, String mchId) {
+        // 参数校验
+        if (StringUtils.isEmpty(batchId)) {
+            return R.error("批次号不能为空");
         }
-
-        //创建微信订单
-        WxPayConfig payConfig = new WxPayConfig();
-        // 如果是应用app
-        String appJson = configService.selectConfigByKey("app.config");
-        AppConfig app = JSONUtil.toBean(appJson, AppConfig.class);
-        if(StringUtils.equals(app.getAppId(),appId)){
-            json = configService.selectConfigByKey("his.AppRedPacket");
-            AppRedPacketConfig appRedPacketConfig = JSONUtil.toBean(json, AppRedPacketConfig.class);
-            BeanUtils.copyProperties(appRedPacketConfig,payConfig);
-        }else {
-            BeanUtils.copyProperties(config,payConfig);
+        if (companyId == null) {
+            return R.error("公司ID不能为空");
         }
+        if (StringUtils.isEmpty(appId)) {
+            return R.error("appId不能为空");
+        }
+
+        // 获取红包配置
+        RedPacketConfig config = getRedPacketConfig(companyId, mchId);
 
+        // 构建微信支付配置
+        WxPayConfig payConfig = buildWxPayConfig(appId, config);
 
         WxPayService wxPayService = new WxPayServiceImpl();
         wxPayService.setConfig(payConfig);
-        TransferService transferService=wxPayService.getTransferService();
+        TransferService transferService = wxPayService.getTransferService();
+
+        Map<String, Object> map = new HashMap<>(2);
+        map.put("status", "待确认");
 
-        Map<String,Object> map = new HashMap<>();
-        map.put("status","待确认"); //
         if (config.getIsNew() != null && config.getIsNew() == 1) {
             try {
-                TransferBillsGetResult queryRedPacketResult = transferService.getBillsByTransferBillNo(batchId);
-                logger.info("FsCourseRedPacketLog-batchId:{},【红包处理】查询批次结果:{}",batchId,queryRedPacketResult.toString());
-                if(("SUCCESS").equals(queryRedPacketResult.getState())){
-                    map.put("status","success");
-                }else if(("FAIL").equals(queryRedPacketResult.getState())){
-                    map.put("status","fail");
+                TransferBillsGetResult result = transferService.getBillsByTransferBillNo(batchId);
+                logger.info("FsCourseRedPacketLog-batchId:{},【红包处理】查询批次结果:{}", batchId, result.toString());
+                if ("SUCCESS".equals(result.getState())) {
+                    map.put("status", "success");
+                } else if ("FAIL".equals(result.getState())) {
+                    map.put("status", "fail");
                 }
                 return R.ok(map);
             } catch (WxPayException e) {
-                logger.error(e.getMessage());
-                return R.error(e.getMessage());
+                logger.error("查询转账批次状态异常, batchId:{}, error:{}", batchId, e.getMessage(), e);
+                return R.error("查询转账状态失败:" + e.getMessage());
             }
         } else {
             QueryTransferBatchesRequest request = new QueryTransferBatchesRequest();
@@ -348,20 +334,72 @@ public class CompanyServiceImpl implements ICompanyService
             request.setNeedQueryDetail(false);
 
             try {
-                QueryTransferBatchesResult queryTransferBatchesResult = transferService.transferBatchesBatchId(request);
-                logger.info("FsCourseRedPacketLog-batchId,【红包处理】查询批次结果:{}",batchId,queryTransferBatchesResult.toString());
-                if(("FINISHED").equals(queryTransferBatchesResult.getTransferBatch().getBatchStatus())){
-                    map.put("status","success");
-                }else if(("CLOSED").equals(queryTransferBatchesResult.getTransferBatch().getBatchStatus())){
-                    map.put("status","fail");
+                QueryTransferBatchesResult result = transferService.transferBatchesBatchId(request);
+                logger.info("FsCourseRedPacketLog-batchId:{},【红包处理】查询批次结果:{}", batchId, result.toString());
+                if ("FINISHED".equals(result.getTransferBatch().getBatchStatus())) {
+                    map.put("status", "success");
+                } else if ("CLOSED".equals(result.getTransferBatch().getBatchStatus())) {
+                    map.put("status", "fail");
                 }
                 return R.ok(map);
             } catch (WxPayException e) {
-                logger.error(e.getMessage());
-                return R.error(e.getMessage());
+                logger.error("查询转账批次状态异常, batchId:{}, error:{}", batchId, e.getMessage(), e);
+                return R.error("查询转账状态失败:" + e.getMessage());
+            }
+        }
+    }
+
+    /**
+     * 获取红包配置
+     */
+    private RedPacketConfig getRedPacketConfig(Long companyId, String mchId) {
+        RedPacketConfig config = new RedPacketConfig();
+        if (StringUtils.isNotEmpty(mchId)) {
+            SysRedpacketConfigMore sysRedpacketConfigMore = sysRedpacketConfigMoreMapper.selectSysRedpacketConfigMoreByMchId(mchId);
+            BeanUtils.copyProperties(sysRedpacketConfigMore, config);
+            // 设置回调地址,在notifyUrl末尾拼接companyId
+            String notifyUrl = sysRedpacketConfigMore.getNotifyUrl();
+            if (StringUtils.isNotEmpty(notifyUrl)) {
+                config.setNotifyUrl(notifyUrl + "/" + companyId);
             }
+            return config;
         }
 
+        // 根据红包模式获取配置
+        CourseConfig courseConfig = JSONUtil.toBean(configService.selectConfigByKey("course.config"), CourseConfig.class);
+        String json;
+        switch (courseConfig.getRedPacketMode()) {
+            case 1:
+                json = configService.selectConfigByKey("redPacket.config");
+                break;
+            case 2:
+                json = companyConfigService.selectRedPacketConfigByKey(companyId);
+                // 如果分公司配置为空就走总后台的配置
+                if (StringUtils.isEmpty(json)) {
+                    json = configService.selectConfigByKey("redPacket.config");
+                }
+                break;
+            default:
+                throw new UnsupportedOperationException("当前红包模式不支持!");
+        }
+        return JSONUtil.toBean(json, RedPacketConfig.class);
+    }
+
+    /**
+     * 构建微信支付配置
+     */
+    private WxPayConfig buildWxPayConfig(String appId, RedPacketConfig config) {
+        WxPayConfig payConfig = new WxPayConfig();
+        String appJson = configService.selectConfigByKey("app.config");
+        AppConfig app = JSONUtil.toBean(appJson, AppConfig.class);
+        if (StringUtils.equals(app.getAppId(), appId)) {
+            String json = configService.selectConfigByKey("his.AppRedPacket");
+            AppRedPacketConfig appRedPacketConfig = JSONUtil.toBean(json, AppRedPacketConfig.class);
+            BeanUtils.copyProperties(appRedPacketConfig, payConfig);
+        } else {
+            BeanUtils.copyProperties(config, payConfig);
+        }
+        return payConfig;
     }
 
     /**
@@ -408,63 +446,59 @@ public class CompanyServiceImpl implements ICompanyService
 
     @Override
     public R changeMerchant(CompanyVO companyVO) {
-        // 长春张磊的不允许换 自用交易写死
-        if("20".equals(companyVO.getCompanyId().toString())){
-            return R.error("该公司不允许换商户!");
+
+        // 通过商户号查询SysRedpacketConfigMore表获取配置
+        String redPacketMerchant = companyVO.getRedPacketMerchant();
+        if(StringUtils.isEmpty(redPacketMerchant)){
+            return R.error("商户号不能为空!");
+        }
+        SysRedpacketConfigMore sysRedpacketConfigMore = sysRedpacketConfigMoreMapper.selectSysRedpacketConfigMoreByMchId(redPacketMerchant);
+        if(sysRedpacketConfigMore == null){
+            return R.error("该商户不存在");
         }
 
-        CompanyConfig companyConfig =companyConfigMapper.selectCompanyConfigByKey(companyVO.getCompanyId(),"redPacket:config");
-
-        String updateKey=REDIS_KEY_PREFIX + companyVO.getCompanyId();
-        // 小艾的商户
-        if("1103448555".equals(companyVO.getRedPacketMerchant())){
-            String redisKey = REDIS_KEY_PREFIX + 20;
-            String json = redisTemplate.opsForValue().get(redisKey);
-            if(json!=null){
-                RedPacketConfig config = JSONUtil.toBean(json, RedPacketConfig.class);
-                String url = config.getNotifyUrl();
-                url=url.substring(0,url.lastIndexOf("/")+1)+companyVO.getCompanyId();
-                config.setNotifyUrl(url);
-                redisTemplate.opsForValue().set(updateKey,JSONUtil.toJsonStr(config));
-
-                if(companyConfig!=null){
-                    // 更新数据库
-                    CompanyConfig updateConfig= new CompanyConfig();
-                    updateConfig.setCompanyId(companyVO.getCompanyId());
-                    updateConfig.setConfigId(companyConfig.getConfigId());
-                    updateConfig.setConfigValue(JSONUtil.toJsonStr(config));
-                    companyConfigMapper.updateCompanyConfig(updateConfig);
-                }else {
-                    CompanyConfig save=new CompanyConfig();
-                    save.setCompanyId(companyVO.getCompanyId());
-                    save.setConfigKey("redPacket:config");
-                    save.setConfigValue(JSONUtil.toJsonStr(config));
-                    save.setConfigType("N");
-                    companyConfigMapper.insertCompanyConfig( save);
-                }
-            }
-        }else if("1700010711".equals(companyVO.getRedPacketMerchant())){
-            if(redisCache.getCacheObject(updateKey) != null){
-                redisCache.deleteObject(updateKey);
-                if(companyConfig!=null){
-                    // 更新数据库
-                    CompanyConfig updateConfig= new CompanyConfig();
-                    updateConfig.setCompanyId(companyVO.getCompanyId());
-                    updateConfig.setConfigId(companyConfig.getConfigId());
-                    updateConfig.setConfigValue("");
-                    companyConfigMapper.updateCompanyConfig(updateConfig);
-                }else {
-                    CompanyConfig save=new CompanyConfig();
-                    save.setCompanyId(companyVO.getCompanyId());
-                    save.setConfigKey("redPacket:config");
-                    save.setConfigValue("");
-                    save.setConfigType("N");
-                    companyConfigMapper.insertCompanyConfig( save);
-                }
-            }
+        String updateKey = REDIS_KEY_PREFIX + companyVO.getCompanyId();
+        CompanyConfig companyConfig = companyConfigMapper.selectCompanyConfigByKey(companyVO.getCompanyId(),"redPacket:config");
 
+        // 构建RedPacketConfig
+        RedPacketConfig config = new RedPacketConfig();
+        config.setAppId(sysRedpacketConfigMore.getAppId());
+        config.setMiniappId(sysRedpacketConfigMore.getMiniappId());
+        config.setMchId(sysRedpacketConfigMore.getMchId());
+        config.setMchKey(sysRedpacketConfigMore.getMchKey());
+        config.setKeyPath(sysRedpacketConfigMore.getKeyPath());
+        config.setPrivateKeyPath(sysRedpacketConfigMore.getPrivateKeyPath());
+        config.setPrivateCertPath(sysRedpacketConfigMore.getPrivateCertPath());
+        config.setApiV3Key(sysRedpacketConfigMore.getApiV3Key());
+        config.setPublicKeyId(sysRedpacketConfigMore.getPublicKeyId());
+        config.setPublicKeyPath(sysRedpacketConfigMore.getPublicKeyPath());
+        config.setIsNew(sysRedpacketConfigMore.getIsNew());
+
+        // 设置回调地址,在notifyUrl末尾拼接companyId
+        String notifyUrl = sysRedpacketConfigMore.getNotifyUrl();
+        if(StringUtils.isNotEmpty(notifyUrl)){
+            notifyUrl = notifyUrl + "/" + companyVO.getCompanyId();
+        }
+        config.setNotifyUrl(notifyUrl);
+        config.setNotifyUrlScrm(sysRedpacketConfigMore.getNotifyUrlScrm());
+
+        // 更新Redis缓存
+        redisTemplate.opsForValue().set(updateKey, JSONUtil.toJsonStr(config));
+
+        // 更新/新增数据库配置
+        if(companyConfig != null){
+            CompanyConfig updateConfig = new CompanyConfig();
+            updateConfig.setCompanyId(companyVO.getCompanyId());
+            updateConfig.setConfigId(companyConfig.getConfigId());
+            updateConfig.setConfigValue(JSONUtil.toJsonStr(config));
+            companyConfigMapper.updateCompanyConfig(updateConfig);
         }else {
-            return R.error("该商户不存在");
+            CompanyConfig save = new CompanyConfig();
+            save.setCompanyId(companyVO.getCompanyId());
+            save.setConfigKey("redPacket:config");
+            save.setConfigValue(JSONUtil.toJsonStr(config));
+            save.setConfigType("N");
+            companyConfigMapper.insertCompanyConfig(save);
         }
 
         return R.ok();
@@ -1981,7 +2015,7 @@ public class CompanyServiceImpl implements ICompanyService
             }
 
             if(!StringUtils.isEmpty(redLogs.getBatchId())){
-                R result=checkMchTransferStatusByBatchID(redLogs.getBatchId(),redLogs.getCompanyId(),redLogs.getAppId());
+                R result=checkMchTransferStatusByBatchID(redLogs.getBatchId(),redLogs.getCompanyId(),redLogs.getAppId(),redLogs.getMchId());
                 if("200".equals(String.valueOf(result.get("code")))){
                     FsCourseRedPacketLog update = new FsCourseRedPacketLog();
                     update.setUpdateTime(new Date());

+ 26 - 2
fs-service/src/main/java/com/fs/course/service/impl/FsUserCourseVideoServiceImpl.java

@@ -29,8 +29,10 @@ import com.fs.common.utils.date.DateUtil;
 import com.fs.company.constant.CompanyTrafficConstants;
 import com.fs.company.domain.Company;
 import com.fs.company.domain.CompanyCompanyFsuser;
+import com.fs.company.domain.CompanyConfig;
 import com.fs.company.domain.CompanyUser;
 import com.fs.company.mapper.CompanyCompanyFsuserMapper;
+import com.fs.company.mapper.CompanyConfigMapper;
 import com.fs.company.mapper.CompanyMapper;
 import com.fs.company.mapper.CompanyUserMapper;
 import com.fs.company.service.ICompanyService;
@@ -38,6 +40,7 @@ import com.fs.config.cloud.CloudHostProper;
 import com.fs.course.config.CourseConfig;
 import com.fs.course.config.RandomRedPacketConfig;
 import com.fs.course.config.RandomRedPacketRule;
+import com.fs.course.config.RedPacketConfig;
 import com.fs.course.domain.*;
 import com.fs.course.dto.CoursePackageDTO;
 import com.fs.course.dto.FsUserCourseVideoDTO;
@@ -131,6 +134,11 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
     @Value("${isNewWxMerchant}")
     private Boolean isNewWxMerchant;
     private static final String registeredRealLink = "/pages_course/register.html?link=";
+
+    private static final String REDIS_KEY_PREFIX = "red_packet_config:";
+
+    @Autowired
+    private CompanyConfigMapper companyConfigMapper;
     @Autowired
     private OpenIMService openIMService;
     @Autowired
@@ -2147,7 +2155,15 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
                     redPacketLog.setWatchLogId(log.getLogId() != null ? log.getLogId() : null);
                     redPacketLog.setPeriodId(param.getPeriodId());
                     redPacketLog.setAppId(param.getAppId());
-
+                    CompanyConfig companyConfig = companyConfigMapper.selectCompanyConfigByKey(log.getCompanyId(),"redPacket:config");
+                    if(ObjectUtil.isNotEmpty(companyConfig)&&StringUtils.isNotEmpty(companyConfig.getConfigValue())){
+                        RedPacketConfig redPacketConfig = JSONUtil.toBean(companyConfig.getConfigValue(), RedPacketConfig.class);
+                        redPacketLog.setMchId(redPacketConfig.getMchId());
+                    }else {
+                        String json = configService.selectConfigByKey("redPacket.config");
+                        RedPacketConfig redPacketConfig = JSONUtil.toBean(json, RedPacketConfig.class);
+                        redPacketLog.setMchId(redPacketConfig.getMchId());
+                    }
                     redPacketLogMapper.insertFsCourseRedPacketLog(redPacketLog);
 
                     // 更新观看记录的奖励类型
@@ -5425,7 +5441,15 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
                     redPacketLog.setPeriodId(log.getPeriodId());
                     redPacketLog.setAppId(packetParam.getAppId());
                     redPacketLog.setWatchType(1);
-
+                    CompanyConfig companyConfig = companyConfigMapper.selectCompanyConfigByKey(log.getCompanyId(),"redPacket:config");
+                    if(ObjectUtil.isNotEmpty(companyConfig)&&StringUtils.isNotEmpty(companyConfig.getConfigValue())){
+                        RedPacketConfig redPacketConfig = JSONUtil.toBean(companyConfig.getConfigValue(), RedPacketConfig.class);
+                        redPacketLog.setMchId(redPacketConfig.getMchId());
+                    }else {
+                        String json = configService.selectConfigByKey("redPacket.config");
+                        RedPacketConfig redPacketConfig = JSONUtil.toBean(json, RedPacketConfig.class);
+                        redPacketLog.setMchId(redPacketConfig.getMchId());
+                    }
                     redPacketLogMapper.insertFsCourseRedPacketLog(redPacketLog);
 
                     // 更新观看记录的奖励类型

+ 8 - 0
fs-service/src/main/java/com/fs/his/mapper/SysRedpacketConfigMoreMapper.java

@@ -60,4 +60,12 @@ public interface SysRedpacketConfigMoreMapper extends BaseMapper<SysRedpacketCon
      * @return 结果
      */
     int deleteSysRedpacketConfigMoreByIds(Long[] ids);
+
+    /**
+     * 通过商户号查询多商户配置
+     *
+     * @param mchId 商户号
+     * @return 多商户配置
+     */
+    SysRedpacketConfigMore selectSysRedpacketConfigMoreByMchId(String mchId);
 }

+ 5 - 0
fs-service/src/main/resources/mapper/his/SysRedpacketConfigMoreMapper.xml

@@ -115,4 +115,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             #{id}
         </foreach>
     </delete>
+
+    <select id="selectSysRedpacketConfigMoreByMchId" parameterType="String" resultMap="SysRedpacketConfigMoreResult">
+        <include refid="selectSysRedpacketConfigMoreVo"/>
+        where mch_id = #{mchId}
+    </select>
 </mapper>