|
|
@@ -6,6 +6,8 @@ import cn.hutool.json.JSONUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.fs.app.facade.LiveFacadeService;
|
|
|
import com.fs.common.constant.LiveKeysConstant;
|
|
|
import com.fs.common.core.controller.BaseController;
|
|
|
@@ -13,13 +15,20 @@ import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.core.page.PageRequest;
|
|
|
import com.fs.common.core.page.TableDataInfo;
|
|
|
import com.fs.common.core.redis.RedisCache;
|
|
|
+import com.fs.common.exception.CustomException;
|
|
|
import com.fs.common.utils.StringUtils;
|
|
|
+import com.fs.company.domain.Company;
|
|
|
import com.fs.company.domain.CompanyUser;
|
|
|
+import com.fs.company.service.ICompanyService;
|
|
|
import com.fs.company.service.ICompanyUserService;
|
|
|
+import com.fs.course.config.CourseConfig;
|
|
|
+import com.fs.course.param.FsCourseSendRewardUParam;
|
|
|
import com.fs.his.domain.FsUser;
|
|
|
+import com.fs.his.domain.FsUserWx;
|
|
|
import com.fs.his.param.WxSendRedPacketParam;
|
|
|
import com.fs.his.service.IFsStorePaymentService;
|
|
|
import com.fs.his.service.IFsUserService;
|
|
|
+import com.fs.his.service.IFsUserWxService;
|
|
|
import com.fs.live.domain.*;
|
|
|
import com.fs.framework.aspectj.lock.DistributeLock;
|
|
|
import com.fs.live.param.CouponPO;
|
|
|
@@ -28,12 +37,14 @@ import com.fs.live.param.RedPO;
|
|
|
import com.fs.live.param.SignPO;
|
|
|
import com.fs.live.service.*;
|
|
|
import com.fs.live.vo.*;
|
|
|
+import com.fs.system.service.ISysConfigService;
|
|
|
import com.github.pagehelper.PageInfo;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.ZoneId;
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
@@ -91,6 +102,15 @@ public class LiveFacadeServiceImpl extends BaseController implements LiveFacadeS
|
|
|
@Autowired
|
|
|
private ICompanyUserService companyUserService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ICompanyService companyService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ISysConfigService configService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IFsUserWxService fsUserWxService;
|
|
|
+
|
|
|
@Autowired
|
|
|
private ILiveRewardRecordService liveRewardRecordService;
|
|
|
|
|
|
@@ -432,9 +452,14 @@ public class LiveFacadeServiceImpl extends BaseController implements LiveFacadeS
|
|
|
}
|
|
|
|
|
|
boolean anySuccess = claimResults.stream().anyMatch(m -> Boolean.TRUE.equals(m.get("success")));
|
|
|
- return R.ok(anySuccess ? "签到成功" : "签到成功,但奖励领取失败")
|
|
|
- .put("signNo", signNo)
|
|
|
- .put("rewards", claimResults);
|
|
|
+ // 有金额红包则返回其 one;否则只返回 msg
|
|
|
+ Map<String, Object> cashRed = claimResults.stream()
|
|
|
+ .filter(m -> Long.valueOf(6L).equals(m.get("rewardType")))
|
|
|
+ .findFirst().orElse(null);
|
|
|
+ if (cashRed != null) {
|
|
|
+ return R.ok(cashRed);
|
|
|
+ }
|
|
|
+ return R.ok(anySuccess ? "签到成功" : "签到成功,但奖励领取失败");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -444,7 +469,7 @@ public class LiveFacadeServiceImpl extends BaseController implements LiveFacadeS
|
|
|
*/
|
|
|
private R claimSignCashRed(Long liveId, Long userId, JSONObject item, SignPO sign) {
|
|
|
BigDecimal amount = item.getBigDecimal("amount");
|
|
|
- if (amount == null || amount.compareTo(java.math.BigDecimal.ZERO) <= 0) {
|
|
|
+ if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
return R.error("金额红包配置无效");
|
|
|
}
|
|
|
Live live = liveService.selectLiveByLiveId(liveId);
|
|
|
@@ -465,24 +490,28 @@ public class LiveFacadeServiceImpl extends BaseController implements LiveFacadeS
|
|
|
return R.error("绑定销售不存在或未归属公司,无法发放金额红包");
|
|
|
}
|
|
|
Long companyId = companyUser.getCompanyId();
|
|
|
- String openId = null;
|
|
|
- if (sign != null && sign.getSource() != null && sign.getSource() == 2
|
|
|
- && StringUtils.isNotEmpty(user.getCourseMaOpenId())) {
|
|
|
- openId = user.getCourseMaOpenId();
|
|
|
- } else if (StringUtils.isNotEmpty(user.getMpOpenId())) {
|
|
|
- openId = user.getMpOpenId();
|
|
|
- } else if (StringUtils.isNotEmpty(user.getCourseMaOpenId())) {
|
|
|
- openId = user.getCourseMaOpenId();
|
|
|
+ // 复用课程发奖 openId 解析逻辑:按 source 取 H5/小程序/App openId
|
|
|
+ FsCourseSendRewardUParam param = new FsCourseSendRewardUParam();
|
|
|
+ param.setUserId(userId);
|
|
|
+ param.setCompanyId(companyId);
|
|
|
+ param.setSource(3);
|
|
|
+ param.setAppId(sign != null ? sign.getAppId() : null);
|
|
|
+ String openId;
|
|
|
+ try {
|
|
|
+ openId = getOpenId(param, user);
|
|
|
+ } catch (CustomException e) {
|
|
|
+ return R.error(e.getMessage());
|
|
|
}
|
|
|
+
|
|
|
if (StringUtils.isEmpty(openId)) {
|
|
|
return R.error("请使用微信登录后再领取金额红包");
|
|
|
}
|
|
|
- WxSendRedPacketParam packetParam = new com.fs.his.param.WxSendRedPacketParam();
|
|
|
+ WxSendRedPacketParam packetParam = new WxSendRedPacketParam();
|
|
|
packetParam.setOpenId(openId);
|
|
|
- packetParam.setAmount(amount.setScale(1, java.math.RoundingMode.HALF_UP));
|
|
|
+ packetParam.setAmount(amount.setScale(1, RoundingMode.HALF_UP));
|
|
|
packetParam.setCompanyId(companyId);
|
|
|
- packetParam.setSource(sign != null && sign.getSource() != null ? sign.getSource() : 1);
|
|
|
- packetParam.setAppId(sign != null ? sign.getAppId() : null);
|
|
|
+ packetParam.setSource(param.getSource());
|
|
|
+ packetParam.setAppId(param.getAppId());
|
|
|
packetParam.setRedPacketMode(1);
|
|
|
packetParam.setUser(user);
|
|
|
try {
|
|
|
@@ -512,6 +541,81 @@ public class LiveFacadeServiceImpl extends BaseController implements LiveFacadeS
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 按来源获取用户 openId:1=H5 2=小程序 3=App
|
|
|
+ */
|
|
|
+ private String getOpenId(FsCourseSendRewardUParam param, FsUser user) {
|
|
|
+ Integer source = param.getSource() != null ? param.getSource() : 1;
|
|
|
+ Long userId = param.getUserId();
|
|
|
+ switch (source) {
|
|
|
+ case 1:
|
|
|
+ Company company = companyService.selectCompanyById(param.getCompanyId());
|
|
|
+ if (company == null) {
|
|
|
+ throw new CustomException("公司不存在,无法获取openId");
|
|
|
+ }
|
|
|
+ String appId = company.getCourseMaAppId();
|
|
|
+
|
|
|
+ // 公司配置为空时获取默认配置
|
|
|
+ if (StringUtils.isBlank(appId)) {
|
|
|
+ String json = configService.selectConfigByKey("course.config");
|
|
|
+ CourseConfig config = JSON.parseObject(json, CourseConfig.class);
|
|
|
+ if (config == null || StringUtils.isBlank(config.getMpAppId())) {
|
|
|
+ throw new CustomException("未配置公众号appId,无法获取openId");
|
|
|
+ }
|
|
|
+ appId = config.getMpAppId();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询openId
|
|
|
+ Wrapper<FsUserWx> queryWrapper = Wrappers.<FsUserWx>lambdaQuery()
|
|
|
+ .eq(FsUserWx::getFsUserId, userId)
|
|
|
+ .eq(FsUserWx::getAppId, appId);
|
|
|
+ FsUserWx fsUserWx = fsUserWxService.getOne(queryWrapper);
|
|
|
+ if (Objects.isNull(fsUserWx) || StringUtils.isBlank(fsUserWx.getOpenId())) {
|
|
|
+ throw new CustomException("获取openId失败");
|
|
|
+ }
|
|
|
+ return fsUserWx.getOpenId();
|
|
|
+ case 2:
|
|
|
+ if (StringUtils.isBlank(param.getAppId())) {
|
|
|
+ throw new CustomException("小程序appId不能为空");
|
|
|
+ }
|
|
|
+ FsUserWx userWx = fsUserWxService.selectByAppIdAndUserId(param.getAppId(), userId, 1);
|
|
|
+ if (Objects.nonNull(userWx) && StringUtils.isNotBlank(userWx.getOpenId())) {
|
|
|
+ return userWx.getOpenId();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isNotBlank(user.getCourseMaOpenId())) {
|
|
|
+ try {
|
|
|
+ handleFsUserWx(user, param.getAppId());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("【更新或插入用户与小程序的绑定关系失败】:{}", userId, e);
|
|
|
+ }
|
|
|
+ return user.getCourseMaOpenId();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ return user.getAppOpenId();
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新或插入用户与小程序的绑定关系
|
|
|
+ */
|
|
|
+ private void handleFsUserWx(FsUser user, String appId) {
|
|
|
+ FsUserWx fsUserWx = new FsUserWx();
|
|
|
+ fsUserWx.setType(1);
|
|
|
+ fsUserWx.setFsUserId(user.getUserId());
|
|
|
+ fsUserWx.setAppId(appId);
|
|
|
+ fsUserWx.setOpenId(user.getCourseMaOpenId());
|
|
|
+ fsUserWx.setUnionId(user.getUnionId());
|
|
|
+ fsUserWx.setCreateTime(new Date());
|
|
|
+ fsUserWx.setUpdateTime(new Date());
|
|
|
+ fsUserWxService.saveOrUpdateByUniqueKey(fsUserWx);
|
|
|
+ log.info("【更新或插入用户与小程序{}的绑定关系】:{}", appId, user.getUserId());
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 解析本次签到可领取奖励:优先读触发后 Redis 缓存(含 opLogId),否则回落已执行签到任务 content.rewards。
|
|
|
*/
|