|
|
@@ -13,9 +13,13 @@ import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.fs.common.constant.HttpStatus;
|
|
|
@@ -51,14 +55,11 @@ import com.fs.his.mapper.*;
|
|
|
import com.fs.his.param.FindUserByParam;
|
|
|
import com.fs.his.param.FsUserAddIntegralTemplateParam;
|
|
|
import com.fs.his.param.FsUserParam;
|
|
|
-import com.fs.his.service.IFsUserIntegralLogsService;
|
|
|
-import com.fs.his.service.IFsUserProjectTagService;
|
|
|
-import com.fs.his.service.IFsUserWxService;
|
|
|
+import com.fs.his.service.*;
|
|
|
import com.fs.his.utils.PhoneUtil;
|
|
|
-import com.fs.his.vo.FsUserVO;
|
|
|
-import com.fs.his.vo.FsUserExportListVO;
|
|
|
-import com.fs.his.vo.FsUserFollowDoctorVO;
|
|
|
-import com.fs.his.vo.UserVo;
|
|
|
+import com.fs.his.utils.ProfitShareUtils;
|
|
|
+import com.fs.his.utils.UniAdSignUtils;
|
|
|
+import com.fs.his.vo.*;
|
|
|
import com.fs.im.config.ImTypeConfig;
|
|
|
import com.fs.im.service.OpenIMService;
|
|
|
import com.fs.hisStore.domain.FsStoreOrderScrm;
|
|
|
@@ -87,6 +88,7 @@ import com.fs.watch.domain.vo.FsUserAndCompanyAndDoctorVo;
|
|
|
import com.fs.watch.service.WatchUserService;
|
|
|
import com.github.pagehelper.PageHelper;
|
|
|
import com.github.pagehelper.PageInfo;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.apache.http.client.ClientProtocolException;
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
@@ -101,7 +103,6 @@ import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import com.fs.his.service.IFsUserService;
|
|
|
import org.springframework.transaction.annotation.Propagation;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
@@ -115,6 +116,7 @@ import static com.fs.hisStore.enums.BillDetailEnum.CATEGORY_3;
|
|
|
* @author fs
|
|
|
* @date 2023-06-07
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
public class FsUserServiceImpl implements IFsUserService {
|
|
|
Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
@@ -186,6 +188,15 @@ public class FsUserServiceImpl implements IFsUserService {
|
|
|
|
|
|
@Autowired
|
|
|
private IFsUserWxService fsUserWxService;
|
|
|
+ @Autowired
|
|
|
+ private AdProfitDetailMapper adProfitDetailMapper;
|
|
|
+ @Autowired
|
|
|
+ private IFsIntegralExchangeService fsIntegralExchangeService;
|
|
|
+ @Autowired
|
|
|
+ private IFsUserIntegralLogsService fsUserIntegralLogsService;
|
|
|
+ @Autowired
|
|
|
+ private FsUserIntegralLogsMapper fsUserIntegralLogsMapper;
|
|
|
+
|
|
|
|
|
|
|
|
|
/**
|
|
|
@@ -1656,4 +1667,185 @@ public class FsUserServiceImpl implements IFsUserService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 接收 UniApp 激励广告回调
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public R uniCallBack(Map<String, Object> params) {
|
|
|
+ log.info("接收到uniapp激励广告回调参数:{}", params);
|
|
|
+
|
|
|
+ // 交易id
|
|
|
+ String transId = params.get("trans_id").toString();
|
|
|
+ String sign = params.get("sign").toString();
|
|
|
+
|
|
|
+ // 签名验证
|
|
|
+ if (!UniAdSignUtils.verifySign(transId, sign)) {
|
|
|
+ log.error("签名验证失败:trans_id={}, sign={}", transId, sign);
|
|
|
+ return R.ok().put("isValid", false);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 防止重复回调
|
|
|
+ int count = adProfitDetailMapper.selectCount(new LambdaQueryWrapper<AdProfitDetail>().eq(AdProfitDetail::getAdTransId, transId));
|
|
|
+ if (count > 0) {
|
|
|
+ return R.ok().put("isValid", false);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!params.containsKey("cpm")){
|
|
|
+ log.error("uniapp广告回调cpm为空,回调参数={}", params);
|
|
|
+ return R.ok().put("isValid", false);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 广告收益(元)
|
|
|
+ BigDecimal singleProfit = new BigDecimal(params.get("cpm").toString())
|
|
|
+ .divide(new BigDecimal("1000"), 6, RoundingMode.HALF_UP);
|
|
|
+
|
|
|
+ // 获取分润比例
|
|
|
+ String json = configService.selectConfigByKey("his.integral");
|
|
|
+ IntegralConfig config = JSONUtil.toBean(json, IntegralConfig.class);
|
|
|
+
|
|
|
+ List<BigDecimal> ratios = Arrays.asList(
|
|
|
+ config.getIntegralUserRatio(),
|
|
|
+ config.getIntegralCompanyRatio(),
|
|
|
+ config.getIntegralHuYiRatio(),
|
|
|
+ config.getIntegralYunLianRatio()
|
|
|
+ );
|
|
|
+
|
|
|
+ // 使用银行级分配算法
|
|
|
+ Map<Integer, BigDecimal> profitMap = ProfitShareUtils.allocate(singleProfit, ratios);
|
|
|
+
|
|
|
+ BigDecimal customerProfit = profitMap.get(0);
|
|
|
+ BigDecimal salesProfit = profitMap.get(1);
|
|
|
+ BigDecimal huyiProfit = profitMap.get(2);
|
|
|
+ BigDecimal yunlianProfit = profitMap.get(3);
|
|
|
+
|
|
|
+ // 计算用户本次广告积分
|
|
|
+ BigDecimal userIntegra = customerProfit.multiply(new BigDecimal("10")).setScale(0, RoundingMode.DOWN);
|
|
|
+ // 配置的保底收益
|
|
|
+ Integer minimumIntegral = config.getMinimumIntegral();
|
|
|
+ boolean resultCom = userIntegra.compareTo(BigDecimal.valueOf(minimumIntegral)) < 0;
|
|
|
+
|
|
|
+ // 查询用户的销售公司
|
|
|
+ String companyId = adProfitDetailMapper.getCompanyByUserId(params.get("user_id").toString());
|
|
|
+
|
|
|
+ // 入库
|
|
|
+ AdProfitDetail adProfitDetail = new AdProfitDetail();
|
|
|
+ adProfitDetail.setAdTransId(transId);
|
|
|
+ adProfitDetail.setAdTaskId(params.get("extra").toString());
|
|
|
+ adProfitDetail.setSumMoney(singleProfit);
|
|
|
+ adProfitDetail.setUserId(Long.valueOf(params.get("user_id").toString()));
|
|
|
+ // 如果用户收益小于保底收益,直接给用户保底 并且不进行分润
|
|
|
+ if (resultCom) {
|
|
|
+ adProfitDetail.setUserMoney(BigDecimal.valueOf(minimumIntegral).divide(new BigDecimal("10"),2,RoundingMode.DOWN));
|
|
|
+ } else {
|
|
|
+ adProfitDetail.setUserMoney(customerProfit);
|
|
|
+ adProfitDetail.setCompanyMoney(salesProfit);
|
|
|
+ adProfitDetail.setHuyiMoney(huyiProfit);
|
|
|
+ adProfitDetail.setYunlianMoney(yunlianProfit);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ adProfitDetail.setCompanyId(
|
|
|
+ StringUtils.isNotEmpty(companyId) ? Long.valueOf(companyId) : null
|
|
|
+ );
|
|
|
+
|
|
|
+ adProfitDetail.setCreateTime(LocalDateTime.now());
|
|
|
+ adProfitDetail.setOriginParam(JSON.toJSONString(params));
|
|
|
+
|
|
|
+ int result = adProfitDetailMapper.insert(adProfitDetail);
|
|
|
+
|
|
|
+ return R.ok().put("isValid", result > 0);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * uniapp广告 返回一个广告id
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public R createLogs(Long userId) {
|
|
|
+ String adTaskId = IdUtil.fastSimpleUUID() + userId;
|
|
|
+ return R.ok().put("extra", adTaskId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户钱包明细
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public R getWallet(Long userId) {
|
|
|
+ IntegralExchangeVo vo = fsUserMapper.getWallet(userId);
|
|
|
+ vo.setMayWithdraw(vo.getMayWithdraw().divide(BigDecimal.valueOf(100)));
|
|
|
+ vo.setTotalCommission(vo.getTotalCommission().divide(BigDecimal.valueOf(100)));
|
|
|
+ vo.setWithdrawFinish(vo.getWithdrawFinish().divide(BigDecimal.valueOf(100)));
|
|
|
+ vo.setWithdrawCash(new BigDecimal(vo.getWithdrawIntegral()).divide(BigDecimal.valueOf(1000),2,RoundingMode.DOWN));
|
|
|
+ return R.ok().put("data", vo);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户钱包 积分兑换佣金
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ //@Transactional(rollbackFor = Exception.class)
|
|
|
+ public R integralExchange(Long userId) {
|
|
|
+ // 查询用户钱包明细
|
|
|
+ FsUser fsUser = fsUserMapper.selectFsUserByUserId(userId);
|
|
|
+ if (BeanUtil.isEmpty(fsUser)) {
|
|
|
+ return R.error("用户不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ Long withdrawIntegral = fsUser.getWithdrawIntegral();
|
|
|
+ if (withdrawIntegral < 0){
|
|
|
+ return R.error("积分不足");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 积分转金额 1元=1000积分 库里存储的是单位是分:÷10
|
|
|
+ BigDecimal commission = new BigDecimal(withdrawIntegral ).divide(BigDecimal.valueOf(10),2,RoundingMode.DOWN);
|
|
|
+ // 更新用户积分 佣金
|
|
|
+ FsUser user = new FsUser();
|
|
|
+ user.setUserId(userId);
|
|
|
+ user.setIntegral(fsUser.getIntegral() - withdrawIntegral);
|
|
|
+ user.setWithdrawIntegral(0L);
|
|
|
+ user.setTotalCommission(fsUser.getTotalCommission().add(commission));
|
|
|
+ user.setUpdateTime(new Date());
|
|
|
+ user.setMayWithdraw(fsUser.getMayWithdraw().add(commission));
|
|
|
+ fsUserMapper.updateFsUser(user);
|
|
|
+ FsIntegralExchange fsIntegralExchange = new FsIntegralExchange();
|
|
|
+ fsIntegralExchange.setUserId(userId);
|
|
|
+ fsIntegralExchange.setNickName(fsUser.getNickName());
|
|
|
+ fsIntegralExchange.setPhone(fsUser.getPhone());
|
|
|
+ fsIntegralExchange.setCreateTime(new Date());
|
|
|
+ fsIntegralExchange.setIntegral(-withdrawIntegral);
|
|
|
+ fsIntegralExchangeService.insertFsIntegralExchange(fsIntegralExchange);
|
|
|
+ //添加积分记录
|
|
|
+ FsUserIntegralLogs logs = new FsUserIntegralLogs();
|
|
|
+ logs.setUserId(userId);
|
|
|
+ if(fsIntegralExchange.getId() != null){
|
|
|
+ logs.setBusinessId(fsIntegralExchange.getId().toString());
|
|
|
+ }
|
|
|
+ logs.setLogType(FsUserIntegralLogTypeEnum.TYPE_32.getValue());
|
|
|
+ logs.setIntegral(-withdrawIntegral);
|
|
|
+ logs.setBalance(fsUser.getIntegral() - withdrawIntegral);
|
|
|
+ logs.setCreateTime(new Date());
|
|
|
+ logs.setNickName(StringUtils.isNotEmpty(fsUser.getNickName()) ? fsUser.getNickName() : null);
|
|
|
+ logs.setPhone(fsUser.getPhone());
|
|
|
+ fsUserIntegralLogsService.insertFsUserIntegralLogs(logs);
|
|
|
+ return R.ok("兑换成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户钱包 获取兑换明细
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public R exchangDetail(Map<String, Object> params) {
|
|
|
+ PageHelper.startPage(Integer.parseInt(params.get("page").toString()), Integer.parseInt(params.get("limit").toString()));
|
|
|
+ ExchangeDetailVo detailVo = new ExchangeDetailVo();
|
|
|
+ detailVo.setUserId(Long.valueOf(params.get("userId").toString()));
|
|
|
+ detailVo.setLogType(FsUserIntegralLogTypeEnum.TYPE_32.getValue());
|
|
|
+ // 查询积分记录
|
|
|
+ List<ExchangeDetailVo> logsList =fsUserIntegralLogsMapper.getExchangDetailList(detailVo);
|
|
|
+ PageInfo<ExchangeDetailVo> logsPageInfo = new PageInfo<>(logsList);
|
|
|
+ Map<String,Object> rMap = new HashMap<>();
|
|
|
+ rMap.put("rows", logsPageInfo.getList());
|
|
|
+ rMap.put("total", logsPageInfo.getTotal());
|
|
|
+ return R.ok().put("data", rMap);
|
|
|
+ }
|
|
|
+
|
|
|
}
|