AppUserCountTask.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. package com.game.business.task;
  2. import cn.hutool.core.date.DateUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.dynamic.datasource.annotation.DSTransactional;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.game.business.domain.*;
  7. import com.game.business.service.*;
  8. import com.game.common.constant.finance.FinTranType1;
  9. import com.game.common.constant.finance.FinTranType3;
  10. import com.game.common.core.redis.RedisCache;
  11. import com.game.common.entity.KeyValue;
  12. import com.game.common.utils.DateUtils;
  13. import com.game.common.utils.StringUtils;
  14. import lombok.extern.log4j.Log4j2;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Component;
  17. import java.math.BigDecimal;
  18. import java.util.*;
  19. import java.util.function.Function;
  20. import java.util.stream.Collectors;
  21. @Component("appUserCountTask")
  22. @Log4j2
  23. public class AppUserCountTask {
  24. @Autowired
  25. private IAppUsersChargeService appUsersChargeService;
  26. @Autowired
  27. private IAppUserService appUserService;
  28. @Autowired
  29. private IAppUsersCashrecordService appUsersCashrecordService;
  30. @Autowired
  31. private IFinTranRecordService finTranRecordService;
  32. @Autowired
  33. private IAppUserLiveDividedRecordService appUserLiveDividedRecordService;
  34. @Autowired
  35. private IAppUserGameRecordCountService appUserGameRecordCountService;
  36. @Autowired
  37. private IAppGameBettingService appGameBettingService;
  38. @Autowired
  39. private IAppUserCountService appUserCountService;
  40. @Autowired
  41. private IAppUserGameCountService appUserGameCountService;
  42. @Autowired
  43. private IAppUserAgentService appUserAgentService;
  44. @Autowired
  45. private IAppUserCountDividendService appUserCountDividendService;
  46. @Autowired
  47. private RedisCache redisCache;
  48. /**
  49. * 统计用户前一天
  50. * */
  51. public void statistics(String dateTime){
  52. log.info("开始统计用户每日直播、游戏、佣金");
  53. try {
  54. Date curDate = DateUtil.offsetDay(DateUtils.getNowDate(), -1);
  55. if (StringUtils.isNotBlank(dateTime)) {
  56. curDate = DateUtil.parse(dateTime, "yyyy-MM-dd");
  57. } else {
  58. dateTime = DateUtils.parseDateToStr("yyyy-MM-dd", curDate);
  59. }
  60. AppUser queryUser = new AppUser();
  61. queryUser.setAgentFlag(1);
  62. List<AppUser> list = appUserService.selectAppUserList(queryUser);//查询所有代理用户
  63. List<AppUsersCharge> appUsersChargeList = appUsersChargeService.getChargeList(null, dateTime.concat(" 00:00:00"), dateTime.concat(" 23:59:59"));
  64. List<AppUsersCashrecord> appUsersCashrecords = appUsersCashrecordService.getUserCashList(null, dateTime.concat(" 00:00:00"), dateTime.concat(" 23:59:59"));
  65. List<FinTranRecord> finTranRecordList = finTranRecordService.getUserRecordList(null, dateTime.concat(" 00:00:00"), dateTime.concat(" 23:59:59"));
  66. List<AppUserLiveDividedRecord> liveDividedRecordList = appUserLiveDividedRecordService.selectByDate(null, dateTime.concat(" 00:00:00"), dateTime.concat(" 23:59:59"));
  67. List<AppUserGameRecordCount> gameRecordCountList = appUserGameRecordCountService.selectByDate(null, dateTime.concat(" 00:00:00"), dateTime.concat(" 23:59:59"),null,null);
  68. List<AppGameBetting> gameBettingList = appGameBettingService.selectListByDate(null, dateTime.concat(" 00:00:00"), dateTime.concat(" 23:59:59"),null,null);
  69. if (null != list && list.size() > 0) {
  70. List<AppUserCount> appUserCountList = new ArrayList<>();
  71. String finalDateTime = dateTime;
  72. list.forEach(appUser -> {
  73. AppUserCount appUserCount = new AppUserCount();
  74. appUserCount.setUserId(appUser.getUserid());
  75. appUserCount.setStrDate(finalDateTime);
  76. appUserCount.setAgentUserId(appUser.getPid());
  77. appUserCount.setUpdate(true);//全量更新
  78. //充值总额
  79. appUserCount.setRechargeAmount(appUsersChargeList.stream().filter(e -> e.getUid().equals(appUser.getUserid())).mapToDouble(e -> e.getCoin().doubleValue()).sum());
  80. //提现金额
  81. appUserCount.setWithdrawalAmount(appUsersCashrecords.stream().filter(e -> e.getUid().equals(appUser.getUserid())).mapToDouble(e -> e.getMoney().doubleValue()).sum());
  82. //充值服务费/手续费
  83. appUserCount.setRechargeCommission(appUsersChargeList.stream().filter(e -> e.getUid().equals(appUser.getUserid())).mapToDouble(e -> e.getPlatformService()).sum());
  84. //充值赠送余额
  85. /*appUserCount.setRechargeGive(finTranRecordList.stream().filter(e -> {
  86. return e.getUid().equals(appUser.getUserid()) &&
  87. e.getTranType1().intValue() == FinTranType1.U_Income_Coin_Balance.getType() &&
  88. e.getTranType3().intValue() == FinTranType3.CHARGE_IN_REWARD.getType();
  89. }).mapToDouble(e->e.getCoinChange()).sum()); */
  90. appUserCount.setRechargeGive(appUsersChargeList.stream().filter(e -> {
  91. return e.getUid().equals(appUser.getUserid());
  92. }).mapToDouble(e->null==e.getCoinGive()?0:e.getCoinGive().doubleValue()).sum());
  93. //提现服务费/手续费
  94. appUserCount.setWithdrawalCommission(appUsersCashrecords.stream().filter(e -> e.getUid().equals(appUser.getUserid())).mapToDouble(e -> e.getService().doubleValue()).sum());
  95. //直播消费金额
  96. Map<String, KeyValue> type1Keys = Arrays.stream(FinTranType1.getKeyValues(1)).collect(Collectors.toMap(e -> e.a, Function.identity(), (k1, k2) -> k2)); //支出交易类型
  97. Map<String, KeyValue> type3Keys = Arrays.stream(FinTranType3.getKeyValues(1)).collect(Collectors.toMap(e -> e.a, Function.identity(), (k1, k2) -> k2)); //支出交易类型
  98. appUserCount.setLiveUseAmount(finTranRecordList.stream().filter(e -> {
  99. return e.getUid().equals(appUser.getUserid()) &&
  100. null != type1Keys.get(String.valueOf(e.getTranType1().intValue())) &&
  101. null != type3Keys.get(String.valueOf(e.getTranType3().intValue()));
  102. }).mapToDouble(e -> dealMoney(e.getCoinChange())).sum());
  103. //直播佣金
  104. appUserCount.setLiveCommission(liveDividedRecordList.stream().filter(e -> e.getUserId().equals(appUser.getUserid())).mapToDouble(e -> e.getMoney().doubleValue()).sum());
  105. //游戏下注金额
  106. appUserCount.setGameBetting(gameBettingList.stream().filter(e->e.getUserId().equals(appUser.getUserid())).mapToDouble(AppGameBetting::getBettingAmount).sum());
  107. //游戏佣金
  108. appUserCount.setGameCommission(gameRecordCountList.stream().filter(e -> e.getUserId().equals(appUser.getUserid())).mapToDouble(e -> e.getCommission()).sum());
  109. //游戏输
  110. appUserCount.setGameLoseAmount(gameBettingList.stream().filter(
  111. e -> e.getUserId().equals(appUser.getUserid()) && e.getIsWinning().intValue() == 2
  112. ).mapToDouble(e ->
  113. e.getBettingAmount()
  114. ).sum());
  115. //游戏赢
  116. appUserCount.setGameWinAmount(gameBettingList.stream().filter(
  117. e -> e.getUserId().equals(appUser.getUserid()) && e.getIsWinning().intValue() == 1
  118. ).mapToDouble(e ->
  119. e.getBettingAmount() * e.getBettingMultiple()
  120. ).sum());
  121. appUserCountService.createAppUserCount(appUserCount);
  122. });
  123. }
  124. //统计当日用户游戏输赢
  125. if (null != gameBettingList && gameBettingList.size() > 0) {
  126. Map<String, List<AppGameBetting>> map = gameBettingList.stream().
  127. collect(Collectors.groupingBy(
  128. e -> String.valueOf(e.getUserId()).concat("_").
  129. concat(String.valueOf(e.getGameId())).
  130. concat("_").concat(String.valueOf(e.getIsWinning()))
  131. ));
  132. for (String key : map.keySet()) {
  133. List<AppGameBetting> appGameBettingList = map.get(key);
  134. Long userId = Long.valueOf(key.split("_")[0]);
  135. Long gameId = Long.valueOf(key.split("_")[1]);
  136. AppUserGameCount appUserGameCount = new AppUserGameCount();
  137. appUserGameCount.setUserId(userId);
  138. appUserGameCount.setGameId(gameId);
  139. appUserGameCount.setStrDate(dateTime);
  140. appUserGameCount.setGameLoseAmount(appGameBettingList.stream().filter(e -> 2 == e.getIsWinning()).mapToDouble(
  141. e -> e.getBettingAmount()
  142. ).sum());
  143. appUserGameCount.setGameWinAmount(appGameBettingList.stream().filter(e -> 2 == e.getIsWinning()).mapToDouble(
  144. e -> e.getBettingAmount() * e.getBettingMultiple()
  145. ).sum());
  146. appUserGameCount.setGameCommission(gameRecordCountList.stream().filter(
  147. e -> e.getUserId().equals(userId) && e.getGameId().equals(gameId)
  148. ).mapToDouble(e -> e.getCommission()).sum());//自己得到的佣金
  149. appUserGameCountService.save(appUserGameCount);
  150. }
  151. }
  152. }catch (Exception e){
  153. log.info("用户每日直播、游戏、佣金统计异常:{}",e.getMessage());
  154. }
  155. log.info("用户每日直播、游戏、佣金统计完成");
  156. }
  157. /**
  158. * 统计用户直播/充值 (增量更新 1分钟)
  159. * */
  160. public void statisticsLive(){
  161. log.info("开始增量更新用户每日直播、佣金");
  162. try {
  163. Long userId = null;
  164. Date curDate = new Date();
  165. String dateTime = DateUtil.format(curDate, "yyyy-MM-dd");
  166. Date lastDate = appUserCountService.selectLastDate();
  167. String lastDateStr = DateUtil.format(lastDate,"yyyy-MM-dd HH:mm:ss");
  168. if(!dateTime.equals(DateUtil.format(lastDate,"yyyy-MM-dd"))){
  169. lastDateStr = DateUtil.format(curDate, "yyyy-MM-dd").concat(" 00:00:00");
  170. }
  171. String endTimeStr = DateUtil.format(curDate,"yyyy-MM-dd HH:mm:ss");
  172. AppUser queryUser = new AppUser();
  173. queryUser.setAgentFlag(1);
  174. List<AppUser> list = appUserService.selectAppUserList(queryUser);//查询所有代理用户
  175. List<AppUsersCharge> appUsersChargeList = appUsersChargeService.getChargeList(userId, lastDateStr, endTimeStr);
  176. List<AppUsersCashrecord> appUsersCashrecords = appUsersCashrecordService.getUserCashList(userId, lastDateStr, endTimeStr);
  177. List<FinTranRecord> finTranRecordList = finTranRecordService.getUserRecordList(null!=userId?String.valueOf(userId):null, lastDateStr, endTimeStr);
  178. List<AppUserLiveDividedRecord> liveDividedRecordList = appUserLiveDividedRecordService.selectByDate(userId, lastDateStr, endTimeStr);
  179. if (null != list && list.size() > 0) {
  180. List<AppUserCount> appUserCountList = new ArrayList<>();
  181. String finalDateTime = dateTime;
  182. list.forEach(appUser -> {
  183. AppUserCount appUserCount = new AppUserCount();
  184. appUserCount.setUserId(appUser.getUserid());
  185. appUserCount.setStrDate(finalDateTime);
  186. appUserCount.setAgentUserId(appUser.getPid());
  187. appUserCount.setCountTime(curDate);
  188. //充值总额
  189. appUserCount.setRechargeAmount(appUsersChargeList.stream().filter(e -> e.getUid().equals(appUser.getUserid())).mapToDouble(e -> e.getCoin().doubleValue()).sum());
  190. //提现金额
  191. appUserCount.setWithdrawalAmount(appUsersCashrecords.stream().filter(e -> e.getUid().equals(appUser.getUserid())).mapToDouble(e -> e.getMoney().doubleValue()).sum());
  192. //充值服务费/手续费
  193. appUserCount.setRechargeCommission(appUsersChargeList.stream().filter(e -> e.getUid().equals(appUser.getUserid())).mapToDouble(e -> e.getPlatformService()).sum());
  194. //充值赠送余额
  195. /*appUserCount.setRechargeGive(finTranRecordList.stream().filter(e -> {
  196. return e.getUid().equals(appUser.getUserid()) &&
  197. e.getTranType1().intValue() == FinTranType1.U_Income_Coin_Balance.getType() &&
  198. e.getTranType3().intValue() == FinTranType3.CHARGE_IN_REWARD.getType();
  199. }).mapToDouble(e->e.getCoinChange()).sum());*/
  200. appUserCount.setRechargeGive(appUsersChargeList.stream().filter(e -> {
  201. return e.getUid().equals(appUser.getUserid());
  202. }).mapToDouble(e->null==e.getCoinGive()?0:e.getCoinGive().doubleValue()).sum());
  203. //提现服务费/手续费
  204. appUserCount.setWithdrawalCommission(appUsersCashrecords.stream().filter(e -> e.getUid().equals(appUser.getUserid())).mapToDouble(e -> e.getService().doubleValue()).sum());
  205. //直播消费金额
  206. Map<String, KeyValue> type1Keys = Arrays.stream(FinTranType1.getKeyValues(1)).collect(Collectors.toMap(e -> e.a, Function.identity(), (k1, k2) -> k2)); //支出交易类型
  207. Map<String, KeyValue> type3Keys = Arrays.stream(FinTranType3.getKeyValues(1)).collect(Collectors.toMap(e -> e.a, Function.identity(), (k1, k2) -> k2)); //支出交易类型
  208. appUserCount.setLiveUseAmount(finTranRecordList.stream().filter(e -> {
  209. return e.getUid().equals(appUser.getUserid()) &&
  210. null != type1Keys.get(String.valueOf(e.getTranType1().intValue())) &&
  211. null != type3Keys.get(String.valueOf(e.getTranType3().intValue()));
  212. }).mapToDouble(e -> dealMoney(e.getCoinChange())).sum());
  213. //直播佣金
  214. appUserCount.setLiveCommission(liveDividedRecordList.stream().filter(e -> e.getUserId().equals(appUser.getUserid())).mapToDouble(e -> e.getMoney().doubleValue()).sum());
  215. appUserCountService.createAppUserCount(appUserCount);
  216. });
  217. }
  218. }catch (Exception e){
  219. log.info("增量更新用户每日直播、佣金统计异常:{}",e.getMessage());
  220. }
  221. log.info("增量更新用户每日直播、佣金统计完成");
  222. }
  223. /**
  224. * 计算分红
  225. * @param dateTime 格式yyyy-MM-dd
  226. * */
  227. @DSTransactional
  228. public void calculateDividends(String dateTime){
  229. try {
  230. log.info("开始每周代理分红");
  231. dealDividendsNew(dateTime);
  232. log.info("每周代理分红完成");
  233. }catch (Exception e){
  234. log.info("每周分红失败:{}",e.getMessage());
  235. }
  236. }
  237. /**
  238. * 分红(已废弃)
  239. * */
  240. private void dealDividends(String dateTime){
  241. Date curDate = DateUtil.offsetDay(DateUtils.getNowDate(), -1);
  242. if (StringUtils.isNotBlank(dateTime)) {
  243. curDate = DateUtil.parse(dateTime, "yyyy-MM-dd");
  244. } else {
  245. dateTime = DateUtils.parseDateToStr("yyyy-MM-dd", curDate);
  246. }
  247. Date beginTime = DateUtil.beginOfWeek(curDate);
  248. Date endTime = DateUtil.endOfWeek(curDate);
  249. //查询所有总代理用户
  250. AppUser queryUser = new AppUser();
  251. queryUser.setAgentFlag(1);
  252. List<AppUser> list = appUserService.selectAppUserList(queryUser);
  253. // List<AppUser> list = appUserService.selectTeamUserList();
  254. if (null != list && list.size() > 0) {
  255. list.forEach(appUser -> {
  256. String beginTimeStr = DateUtil.format(beginTime,"yyyy-MM-dd");
  257. String endTimeStr = DateUtil.format(endTime,"yyyy-MM-dd");
  258. //查询用户所有下级本周盈亏
  259. /*LambdaQueryWrapper<AppUserCount> queryWrapper = new LambdaQueryWrapper<>();
  260. queryWrapper.ge(AppUserCount::getStrDate, beginTimeStr);
  261. queryWrapper.le(AppUserCount::getStrDate, endTimeStr);
  262. queryWrapper.eq(AppUserCount::getAgentUserId, appUser.getUserid());
  263. List<AppUserCount> appUserCountList = appUserCountService.list(queryWrapper);*/
  264. List<AppUserCount> appUserCountList = appUserCountService.getAppUserCountNew(appUser.getUserid(),beginTimeStr,endTimeStr);
  265. if (null != appUserCountList && appUserCountList.size() > 0) {
  266. //游戏输钱
  267. double loseAmount = appUserCountList.stream().mapToDouble(e -> e.getGameLoseAmount()).sum();
  268. //游戏赢钱
  269. double winAmount = appUserCountList.stream().mapToDouble(e -> e.getGameWinAmount()).sum();
  270. //提现手续费
  271. double withdrawalCommission = appUserCountList.stream().mapToDouble(e -> e.getWithdrawalCommission()).sum();
  272. //游戏佣金
  273. double gameCommission = appUserCountList.stream().mapToDouble(e -> e.getGameCommission()).sum();
  274. //直播佣金
  275. double liveCommission = appUserCountList.stream().mapToDouble(e -> e.getLiveCommission()).sum();
  276. //充值赠送
  277. double rechargeGive = appUserCountList.stream().mapToDouble(e -> e.getRechargeGive()).sum();
  278. //计算分红金额 (输-赢)
  279. // double money = (loseAmount - winAmount) - withdrawalCommission - gameCommission - liveCommission;
  280. double money = (loseAmount - winAmount) - gameCommission - rechargeGive;
  281. if (money > 0) {
  282. //金额大于0 满足分红
  283. //查询当前用户所有下级
  284. AppUserAgent queryAppUserAgent = new AppUserAgent();
  285. queryAppUserAgent.setPid(appUser.getUserid());
  286. List<AppUserAgent> appUserAgentList = appUserAgentService.selectAppUserAgentList(queryAppUserAgent);
  287. if (null != appUserAgentList && appUserAgentList.size() > 0) {
  288. appUserAgentList.forEach(userAgent -> {
  289. if (userAgent.getDividendGuaranteeRate() <= 0.00) {
  290. log.info("用户{},团队亏损{},或未设置分红比例,停止分红", userAgent.getUserId(), money);
  291. return;
  292. }
  293. double rate = userAgent.getDividendGuaranteeRate();
  294. double dividends = BigDecimal.valueOf(money * rate/100).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
  295. AppUserCountDividend appUserCountDividend = new AppUserCountDividend();
  296. appUserCountDividend.setUserId(userAgent.getUserId());
  297. appUserCountDividend.setDateNo(DateUtil.format(endTime, "yyyy-MM-dd"));
  298. appUserCountDividend.setBeginTime(beginTime);
  299. appUserCountDividend.setEndTime(endTime);
  300. appUserCountDividend.setProfitLoss(money);
  301. appUserCountDividend.setRate(rate);
  302. appUserCountDividend.setCoin(0.00);
  303. appUserCountDividend.setDiamondCoin(dividends);
  304. appUserCountDividend.setStatus(0);
  305. appUserCountDividendService.insertAppUserCountDividend(appUserCountDividend);
  306. });
  307. } else {
  308. log.info("用户{},所有下级亏损{},查询下级为空,停止分红", appUser.getUserid(), money);
  309. }
  310. } else {
  311. log.info("用户{},所有下级亏损{},不满足分红条件", appUser.getUserid(), money);
  312. }
  313. }
  314. });
  315. }
  316. }
  317. /**
  318. * 分红
  319. * */
  320. private void dealDividendsNew(String dateTime){
  321. Date curDate = DateUtil.offsetDay(DateUtils.getNowDate(), -1);
  322. if (StringUtils.isNotBlank(dateTime)) {
  323. curDate = DateUtil.parse(dateTime, "yyyy-MM-dd");
  324. } else {
  325. dateTime = DateUtils.parseDateToStr("yyyy-MM-dd", curDate);
  326. }
  327. Date beginTime = DateUtil.beginOfWeek(curDate);
  328. Date endTime = DateUtil.endOfWeek(curDate);
  329. //查询所有总代理用户
  330. AppUser queryUser = new AppUser();
  331. queryUser.setAgentFlag(1);
  332. List<AppUser> list = appUserService.selectAppUserList(queryUser);
  333. // List<AppUser> list = appUserService.selectTeamUserList();
  334. if (null != list && list.size() > 0) {
  335. list.forEach(appUser -> {
  336. AppUserAgent userAgent = appUserAgentService.selectInfo(appUser.getUserid());
  337. if (userAgent.getDividendGuaranteeRate() <= 0.00) {
  338. log.info("用户{},未设置分红比例,停止分红", userAgent.getUserId());
  339. return;
  340. }
  341. String beginTimeStr = DateUtil.format(beginTime,"yyyy-MM-dd");
  342. String endTimeStr = DateUtil.format(endTime,"yyyy-MM-dd");
  343. //查询用户所有下级本周盈亏
  344. /*LambdaQueryWrapper<AppUserCount> queryWrapper = new LambdaQueryWrapper<>();
  345. queryWrapper.ge(AppUserCount::getStrDate, beginTimeStr);
  346. queryWrapper.le(AppUserCount::getStrDate, endTimeStr);
  347. queryWrapper.eq(AppUserCount::getAgentUserId, appUser.getUserid());
  348. List<AppUserCount> appUserCountList = appUserCountService.list(queryWrapper);*/
  349. List<AppUserCount> appUserCountList = appUserCountService.getAppUserCountNew(appUser.getUserid(),beginTimeStr,endTimeStr);
  350. if (null != appUserCountList && appUserCountList.size() > 0) {
  351. //游戏输钱
  352. double loseAmount = appUserCountList.stream().mapToDouble(e -> e.getGameLoseAmount()).sum();
  353. //游戏赢钱
  354. double winAmount = appUserCountList.stream().mapToDouble(e -> {
  355. return e.getGameWinAmount() - (e.getGameBetting() - e.getGameLoseAmount()); //实际赢钱 = 赢 - 中奖本金
  356. }).sum();
  357. //提现手续费
  358. double withdrawalCommission = appUserCountList.stream().mapToDouble(e -> e.getWithdrawalCommission()).sum();
  359. //游戏佣金
  360. double gameCommission = appUserCountList.stream().mapToDouble(e -> e.getGameCommission()).sum();
  361. //直播佣金
  362. double liveCommission = appUserCountList.stream().mapToDouble(e -> e.getLiveCommission()).sum();
  363. //充值赠送
  364. double rechargeGive = appUserCountList.stream().mapToDouble(e -> e.getRechargeGive()).sum();
  365. //计算分红金额 (输-赢)
  366. // double money = (loseAmount - winAmount) - withdrawalCommission - gameCommission - liveCommission;
  367. double money = (loseAmount - winAmount) - gameCommission - rechargeGive;
  368. if (money > 0) {
  369. //金额大于0 满足分红
  370. double rate = userAgent.getDividendGuaranteeRate();
  371. double dividends = BigDecimal.valueOf(money * rate/100).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
  372. AppUserCountDividend appUserCountDividend = new AppUserCountDividend();
  373. appUserCountDividend.setUserId(userAgent.getUserId());
  374. appUserCountDividend.setDateNo(DateUtil.format(endTime, "yyyy-MM-dd"));
  375. appUserCountDividend.setBeginTime(beginTime);
  376. appUserCountDividend.setEndTime(endTime);
  377. appUserCountDividend.setProfitLoss(money);
  378. appUserCountDividend.setRate(rate);
  379. appUserCountDividend.setCoin(0.00);
  380. appUserCountDividend.setDiamondCoin(dividends);
  381. appUserCountDividend.setStatus(0);
  382. appUserCountDividendService.insertAppUserCountDividend(appUserCountDividend);
  383. } else {
  384. log.info("用户{},所有下级亏损{},没有亏损停止分红", appUser.getUserid(), money);
  385. }
  386. } else {
  387. log.info("用户{},团队没有流水停止分红", appUser.getUserid());
  388. }
  389. });
  390. }
  391. }
  392. /**
  393. * 更新用户下级列表
  394. * */
  395. public void statisticsTeam(){
  396. AppUserAgent query = new AppUserAgent();
  397. List<AppUserAgent> userAgents = appUserAgentService.selectAppUserAgentList(query);
  398. userAgents.forEach(appUserAgent -> {
  399. AppUserAgent updateAgent = new AppUserAgent();
  400. updateAgent.setId(appUserAgent.getId());
  401. List<String> userIds = userAgents.stream().filter(e->e.getPid().equals(appUserAgent.getUserId())).map(e->{return String.valueOf(e.getUserId());}).collect(Collectors.toList());
  402. updateAgent.setSubset(String.join(",",userIds));
  403. appUserAgentService.updateAppUserAgent(updateAgent);
  404. });
  405. }
  406. public void reloadDividendCache(String dateTime,String topUserId){
  407. appUserCountDividendService.reloadCache(dateTime,StringUtils.isBlank(topUserId)?null:Arrays.asList(Long.valueOf(topUserId)));
  408. }
  409. /**
  410. * 金额为负数则转为正数
  411. * */
  412. private double dealMoney(double money){
  413. return money<0?money*-1:money;
  414. }
  415. }