|
|
@@ -1,21 +1,34 @@
|
|
|
package com.fs.qw.service.impl;
|
|
|
|
|
|
+import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.utils.DateUtils;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fs.common.utils.StringUtils;
|
|
|
+import com.fs.his.domain.FsUser;
|
|
|
+import com.fs.his.domain.FsUserWx;
|
|
|
+import com.fs.his.param.FsReceivePhoneRedPacketParam;
|
|
|
+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.qw.domain.BindPhoneRedPacketRecord;
|
|
|
-import com.fs.qw.service.IBindPhoneRedPacketRecordService;
|
|
|
+import com.fs.qw.mapper.BindPhoneRedPacketRecordMapper;
|
|
|
+import com.fs.qw.mapper.QwExternalContactMapper;
|
|
|
+import com.github.binarywang.wxpay.bean.transfer.TransferBillsResult;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import com.fs.qw.mapper.RedPacketMapper;
|
|
|
import com.fs.qw.domain.RedPacket;
|
|
|
import com.fs.qw.service.IRedPacketService;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
/**
|
|
|
* 授权手机号红包配置Service业务层处理
|
|
|
@@ -23,6 +36,7 @@ import com.fs.qw.service.IRedPacketService;
|
|
|
* @author fs
|
|
|
* @date 2026-03-17
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
public class RedPacketServiceImpl extends ServiceImpl<RedPacketMapper, RedPacket> implements IRedPacketService {
|
|
|
|
|
|
@@ -30,11 +44,20 @@ public class RedPacketServiceImpl extends ServiceImpl<RedPacketMapper, RedPacket
|
|
|
private RedPacketMapper redPacketMapper;
|
|
|
|
|
|
@Autowired
|
|
|
- private IBindPhoneRedPacketRecordService bindPhoneRedPacketRecordService;
|
|
|
+ private BindPhoneRedPacketRecordMapper bindPhoneRedPacketRecordMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private IFsUserService userService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private QwExternalContactMapper qwExternalContactMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IFsStorePaymentService paymentService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IFsUserWxService fsUserWxService;
|
|
|
+
|
|
|
/**
|
|
|
* 查询授权手机号红包配置
|
|
|
*
|
|
|
@@ -111,7 +134,7 @@ public class RedPacketServiceImpl extends ServiceImpl<RedPacketMapper, RedPacket
|
|
|
|
|
|
@Override
|
|
|
public R getPhoneRedPacketInfo(Long recordId) {
|
|
|
- BindPhoneRedPacketRecord record = bindPhoneRedPacketRecordService.selectBindPhoneRedPacketRecordById(recordId);
|
|
|
+ BindPhoneRedPacketRecord record = bindPhoneRedPacketRecordMapper.selectBindPhoneRedPacketRecordById(recordId);
|
|
|
if (ObjectUtil.isNull(record)) {
|
|
|
return R.error().put("msg","未找到授权红包信息");
|
|
|
}
|
|
|
@@ -121,4 +144,105 @@ public class RedPacketServiceImpl extends ServiceImpl<RedPacketMapper, RedPacket
|
|
|
map.put("amount",record.getAmount());
|
|
|
return R.ok().put("data",map);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public R receivePhoneRedPacket(FsReceivePhoneRedPacketParam param) {
|
|
|
+ FsUser user = userService.selectFsUserByUserId(param.getUserId());
|
|
|
+ if (ObjectUtil.isNull(user)) {
|
|
|
+ return R.error("没有用户信息,领取授权红包失败");
|
|
|
+ }
|
|
|
+ //当前用户是否添加企微
|
|
|
+ Long qwCount = qwExternalContactMapper.selectCountByUnionId(user.getUnionId());
|
|
|
+ if (qwCount < 1) {
|
|
|
+ return R.error("请添加企微后领取");
|
|
|
+ }
|
|
|
+ //领取授权红包记录
|
|
|
+ BindPhoneRedPacketRecord record = bindPhoneRedPacketRecordMapper.selectBindPhoneRedPacketRecordById(param.getRecordId());
|
|
|
+ if (ObjectUtil.isNull(record)) {
|
|
|
+ return R.error("授权红包信息错误");
|
|
|
+ }
|
|
|
+ //检测是否可领取授权红包
|
|
|
+ R r = checkIsReceiveRedPacket(param.getUserId(), param.getPhone());
|
|
|
+ if (!r.get("code").equals(200)) {
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ //构建领取红包参数
|
|
|
+ WxSendRedPacketParam redPacketParam = new WxSendRedPacketParam();
|
|
|
+ //处理多小程序问题
|
|
|
+ FsUserWx fsUserWx = fsUserWxService.selectByAppIdAndUserId(param.getAppId(),user.getUserId(),1);
|
|
|
+ if (fsUserWx ==null){
|
|
|
+ try {
|
|
|
+ handleFsUserWx(user,param.getAppId());
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("【更新或插入用户与小程序的绑定关系失败】:{}", user.getUserId());
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ redPacketParam.setOpenId(fsUserWx.getOpenId());
|
|
|
+ }
|
|
|
+ redPacketParam.setAmount(record.getAmount());
|
|
|
+ redPacketParam.setRedPacketMode(1);
|
|
|
+ redPacketParam.setAppId(param.getAppId());
|
|
|
+ redPacketParam.setMpAppId(param.getAppId());
|
|
|
+ redPacketParam.setUser(user);
|
|
|
+ redPacketParam.setPhoneTag(true);
|
|
|
+ //小程序
|
|
|
+ redPacketParam.setSource(2);
|
|
|
+ redPacketParam.setCompanyId(record.getCompanyId());
|
|
|
+ //发送红包
|
|
|
+ R result = paymentService.sendRedPacket(redPacketParam);
|
|
|
+ log.error("红包发送结果:{}", result);
|
|
|
+ if (result.get("code").equals(200)) {
|
|
|
+
|
|
|
+ BindPhoneRedPacketRecord map = new BindPhoneRedPacketRecord();
|
|
|
+ TransferBillsResult transferBillsResult;
|
|
|
+ if (result.get("isNew").equals(1)) {
|
|
|
+ transferBillsResult = (TransferBillsResult) result.get("data");
|
|
|
+ map.setResult(JSON.toJSONString(result));
|
|
|
+ map.setOutBatchNo(transferBillsResult.getOutBillNo());
|
|
|
+ } else {
|
|
|
+ map.setOutBatchNo(result.get("orderCode").toString());
|
|
|
+ }
|
|
|
+ map.setId(record.getId());
|
|
|
+ map.setPhone(param.getPhone());
|
|
|
+ map.setCollectType("1");
|
|
|
+ int updateBindPhoneRedPacketRecord = bindPhoneRedPacketRecordMapper.updateBindPhoneRedPacketRecord(map);
|
|
|
+ if (updateBindPhoneRedPacketRecord > 0) {
|
|
|
+ return R.ok().put("msg","领取授权红包成功");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return R.error("领取授权红包失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ private R checkIsReceiveRedPacket(Long userId,String phone) {
|
|
|
+ if (StringUtils.isEmpty(phone)) {
|
|
|
+ return R.error("手机号错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ Long userRecordCount = bindPhoneRedPacketRecordMapper.selectBindPhoneRedPacketRecordListCountByUserId(userId, "1");
|
|
|
+ if (userRecordCount > 0) {
|
|
|
+ return R.error("已领取一次授权红包");
|
|
|
+ }
|
|
|
+
|
|
|
+ Long phoneRecordCount = bindPhoneRedPacketRecordMapper.selectBindPhoneRedPacketRecordListCountByPhone(phone, "1");
|
|
|
+ if (phoneRecordCount > 0) {
|
|
|
+ return R.error("该手机号已领取过授权红包");
|
|
|
+ }
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ 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());
|
|
|
+ }
|
|
|
}
|