AppGameBettingTask.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. package com.game.business.task;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson2.JSONArray;
  4. import com.alibaba.fastjson2.JSONObject;
  5. import com.game.business.domain.*;
  6. import com.game.business.dto.AppItemCountDTO;
  7. import com.game.business.service.*;
  8. import com.game.business.vo.AppGameItemBettingVO;
  9. import com.game.common.constant.finance.FinTranType1;
  10. import com.game.common.constant.finance.FinTranType2;
  11. import com.game.common.constant.finance.FinTranType3;
  12. import com.game.common.core.domain.HttpRet;
  13. import com.game.common.core.redis.RedisCache;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.scheduling.annotation.Async;
  16. import org.springframework.stereotype.Component;
  17. import java.util.*;
  18. import java.util.stream.Collectors;
  19. @Component
  20. public class AppGameBettingTask {
  21. @Autowired
  22. private IAppGameBettingService appGameBettingService;
  23. @Autowired
  24. private IAppUserService appUserService;
  25. @Autowired
  26. private IFinTranRecordService finTranRecordService;
  27. @Autowired
  28. private IAppGameLotteryService appGameLotteryService;
  29. @Autowired
  30. private IAppGameService appGameService;
  31. @Autowired
  32. private IAppGameClassifyService appGameClassifyService;
  33. @Autowired
  34. private IAppGameItemService appGameItemService;
  35. @Autowired
  36. private RedisCache redisCache;
  37. @Autowired
  38. private IAppUserCountService appUserCountService;
  39. @Async("asyncExecutor")
  40. public void gameDataTask(String message, String gameCodeFinal){
  41. JSONArray dataArry = JSONArray.parseArray(message);
  42. Integer type = null;
  43. for (int i = 0; i < dataArry.size(); i++) {
  44. JSONObject jsonObject = dataArry.getJSONObject(i);
  45. if(jsonObject.containsKey("type")){
  46. type = jsonObject.getInteger("type");
  47. break;
  48. }
  49. }
  50. if(type == null){
  51. // System.out.println("游戏socket推送类型为空。");
  52. return;
  53. }
  54. // 更新倒计时
  55. if(type == 1){
  56. // JSONArray gameDataArry = null;
  57. String gameCode = null;
  58. String time = null;
  59. String classCode = null;
  60. for (int i = 0; i < dataArry.size(); i++) {
  61. JSONObject jsonObject = dataArry.getJSONObject(i);
  62. if(jsonObject.containsKey("data")){
  63. // gameDataArry = jsonObject.getJSONArray("data");
  64. gameCode = jsonObject.getString("id");
  65. }
  66. if(jsonObject.containsKey("time")){
  67. time = jsonObject.getString("time");
  68. }
  69. if(jsonObject.containsKey("id")){
  70. classCode = jsonObject.getString("id");
  71. }
  72. }
  73. if(classCode == null){
  74. // System.out.println("游戏socket推送倒计时,平台ID为空。");
  75. return;
  76. }
  77. if(gameCode == null){
  78. // System.out.println("游戏socket推送倒计时,data对象为空。");
  79. return;
  80. }
  81. if(time == null || time.trim().isEmpty()){
  82. // System.out.println("游戏socket推送倒计时,time为空。");
  83. return;
  84. }
  85. AppGameClassify appGameClassify = appGameClassifyService.getByCode(classCode);
  86. if(appGameClassify == null){
  87. // System.out.println("开奖游戏平台不存在:" + classCode);
  88. return;
  89. }
  90. List<AppGame> appGameList = appGameService.selectAppGameByClassId(appGameClassify.getId());
  91. if(appGameList == null || appGameList.isEmpty()){
  92. // System.out.println("开奖游戏平台没有游戏:" + classCode);
  93. return;
  94. }
  95. Map<String, List<AppGame>> codeGameMap = appGameList.stream().collect(Collectors.groupingBy(AppGame::getCode));
  96. if(!gameCodeFinal.equals(gameCode)){
  97. return;
  98. }
  99. if(!codeGameMap.containsKey(gameCode)){
  100. // System.out.println("开奖游戏不存在:" + gameCode);
  101. return;
  102. }
  103. AppGame appGame = codeGameMap.get(gameCode).get(0);
  104. appGame.setGameTime(time);
  105. appGameService.updateAppGame(appGame);
  106. return;
  107. }
  108. if(type != 2 && type != 3){
  109. return;
  110. }
  111. JSONArray gameArry = null;
  112. String classCode = null;
  113. for (int i = 0; i < dataArry.size(); i++) {
  114. JSONObject jsonObject = dataArry.getJSONObject(i);
  115. if(jsonObject.containsKey("gameTypeSortArrayVOS")){
  116. gameArry = jsonObject.getJSONArray("gameTypeSortArrayVOS");
  117. }
  118. if(jsonObject.containsKey("id")){
  119. classCode = jsonObject.getString("id");
  120. }
  121. }
  122. if(gameArry == null){
  123. // System.out.println("游戏socket推送期号或开奖,gameTypeSortArrayVOS对象为空。");
  124. return;
  125. }
  126. if(classCode == null){
  127. // System.out.println("游戏socket推送期号或开奖,平台ID为空。");
  128. return;
  129. }
  130. AppGameClassify appGameClassify = appGameClassifyService.getByCode(classCode);
  131. if(appGameClassify == null){
  132. // System.out.println("开奖游戏平台不存在:" + classCode);
  133. return;
  134. }
  135. List<AppGame> appGameList = appGameService.selectAppGameByClassId(appGameClassify.getId());
  136. if(appGameList == null || appGameList.isEmpty()){
  137. // System.out.println("开奖游戏平台没有游戏:" + classCode);
  138. return;
  139. }
  140. Map<String, List<AppGame>> codeGameMap = appGameList.stream().collect(Collectors.groupingBy(AppGame::getCode));
  141. for (int i = 0; i < gameArry.size(); i++) {
  142. JSONObject gameLotteryObject = gameArry.getJSONObject(i);
  143. String gameCode = gameLotteryObject.getString("id");
  144. if(gameCode == null){
  145. // System.out.println("游戏socket推送期号或开奖,游戏ID为空。");
  146. continue;
  147. }
  148. if(!gameCodeFinal.equals(gameCode)){
  149. continue;
  150. }
  151. if(!codeGameMap.containsKey(gameCode)){
  152. // System.out.println("开奖游戏不存在:" + gameCode);
  153. continue;
  154. }
  155. AppGame appGame = codeGameMap.get(gameCode).get(0);
  156. String gameDate = gameLotteryObject.getString("gameDate");
  157. if(gameDate == null){
  158. // System.out.println("游戏socket推送期号或开奖,期号为空。");
  159. continue;
  160. }
  161. if(type == 3){
  162. appGame.setGameDate(gameDate);
  163. appGameService.updateAppGame(appGame);
  164. continue;
  165. }
  166. Integer isLottery = gameLotteryObject.getInteger("isLottery");
  167. String gameLotterySucc = gameLotteryObject.getString("gameLotterySucc");
  168. if(isLottery != 1){
  169. continue;
  170. }
  171. if(gameLotterySucc == null || gameLotterySucc.trim().isEmpty()){
  172. // System.out.println("游戏socket推送倒计时,time为空。");
  173. continue;
  174. }
  175. long count = appGameLotteryService.selectCount(appGame.getClassifyId(), appGame.getId(), gameDate);
  176. if(count > 0){
  177. // System.out.println("游戏socket推送开奖,当前期已开过将,游戏编码:" + gameCode + ",期号:" + gameDate);
  178. continue;
  179. }
  180. // 保存开奖记录
  181. AppGameLottery appGameLottery = new AppGameLottery();
  182. appGameLottery.setGameId(appGame.getId());
  183. appGameLottery.setClassId(appGame.getClassifyId());
  184. appGameLottery.setGameDate(gameDate);
  185. appGameLottery.setIsLottery(isLottery);
  186. appGameLottery.setGameRecordDate(new Date());
  187. appGameLottery.setGameLotterySucc(gameLotterySucc);
  188. appGameLotteryService.save(appGameLottery);
  189. this.updateItme(appGame.getId(), gameLotterySucc);
  190. this.gameBettingTask(appGameClassify.getId(), appGame.getId(), gameDate, gameLotterySucc);
  191. appUserCountService.staticsUserCount(appGame.getId(),null);
  192. }
  193. }
  194. public void updateItme(Long gameId, String gameLotterySucc){
  195. // 获取上次开奖结果
  196. List<AppItemCountDTO> itemCountList = new ArrayList<>();
  197. String gameLotterySuccCache = redisCache.getCacheObject("G:GameCount:" + gameId);
  198. if(gameLotterySuccCache != null && !gameLotterySuccCache.trim().isEmpty()){
  199. itemCountList = JSONArray.parseArray(gameLotterySuccCache, AppItemCountDTO.class);
  200. }
  201. // 修改游戏选项
  202. String [] bettingItems = gameLotterySucc.split(",");
  203. List<AppItemCountDTO> nowItemCountList = new ArrayList<>();
  204. for(int j = 0; j < bettingItems.length; j++) {
  205. String bettingItemStr = bettingItems[j];
  206. AppItemCountDTO appItemCountDTO = new AppItemCountDTO();
  207. appItemCountDTO.setGameId(gameId);
  208. appItemCountDTO.setItemLocation(bettingItemStr);
  209. appItemCountDTO.setConsecutive(1);
  210. AppGameItem appGameItem = new AppGameItem();
  211. appGameItem.setGameId(gameId);
  212. appGameItem.setItemLocation(bettingItemStr);
  213. // 开奖次数
  214. appGameItemService.updateLotteryCount(appGameItem);
  215. if(!itemCountList.isEmpty()){
  216. for (AppItemCountDTO appItemCount : itemCountList){
  217. if(appItemCount.getItemLocation().equals(bettingItemStr)){
  218. appItemCountDTO.setConsecutive(appItemCount.getConsecutive() + 1);
  219. }
  220. }
  221. }
  222. // 更新连中
  223. appGameItemService.updateConsecutive(appItemCountDTO);
  224. nowItemCountList.add(appItemCountDTO);
  225. }
  226. redisCache.setCacheObject("G:GameCount:" + gameId, JSON.toJSONString(nowItemCountList));
  227. }
  228. public void gameBettingTask(Long classId, Long gameId, String gameDate, String bettingItem){
  229. List<AppGameBetting> list = appGameBettingService.getIsWinning(classId, gameId, gameDate, 0);
  230. if(list == null || list.isEmpty()){
  231. return;
  232. }
  233. for (int i = 0; i < list.size(); i++) {
  234. AppGameBetting appGameBetting = list.get(i);
  235. appGameBetting.setUpdateTime(new Date());
  236. String [] bettingItems = bettingItem.split(",");
  237. for(int j = 0; j < bettingItems.length; j++){
  238. String bettingItemStr = bettingItems[j];
  239. double bettingAmount = appGameBetting.getBettingAmount() * appGameBetting.getBettingMultiple();
  240. if(appGameBetting.getBettingGameType() == 0){
  241. if(!appGameBetting.getBettingItem().equals(bettingItemStr)){
  242. appGameBetting.setIsWinning(2);
  243. appGameBettingService.updateById(appGameBetting);
  244. continue;
  245. }
  246. }else if(appGameBetting.getBettingGameType() == 1 || appGameBetting.getBettingGameType() == 2){
  247. List<AppGameItemBettingVO> itemList = JSON.parseArray(appGameBetting.getBettingItem(), AppGameItemBettingVO.class);
  248. if(itemList == null || itemList.isEmpty()){
  249. appGameBetting.setIsWinning(2);
  250. appGameBettingService.updateById(appGameBetting);
  251. continue;
  252. }
  253. List<String> bettingItemList = itemList.stream().map(AppGameItemBettingVO::getBettingItem).collect(Collectors.toList());
  254. if(!bettingItemList.contains(bettingItemStr)){
  255. appGameBetting.setIsWinning(2);
  256. appGameBettingService.updateById(appGameBetting);
  257. continue;
  258. }
  259. if(appGameBetting.getBettingGameType() == 2){
  260. for (int k = 0; k < itemList.size(); k++) {
  261. AppGameItemBettingVO appGameItemBettingVO = itemList.get(k);
  262. if(appGameItemBettingVO.getBettingItem().equals(bettingItemStr)){
  263. bettingAmount = appGameBetting.getBettingAmount() * appGameItemBettingVO.getBettingMultiple();
  264. }
  265. }
  266. }
  267. }
  268. AppUser appUser = appUserService.selectAppUserByUserid(appGameBetting.getUserId());
  269. if(appUser == null){
  270. appGameBetting.setIsWinning(1);
  271. appGameBettingService.updateById(appGameBetting);
  272. continue;
  273. }
  274. // 更新用户余额
  275. if(appGameBetting.getBettingType() == 0){
  276. appUser.setDiamondCoin(appUser.getDiamondCoin() + bettingAmount );
  277. appUser.setDiamondCoinTotal(appUser.getDiamondCoinCashTotal() + bettingAmount);
  278. appUser.setDiamondCoinCash(appUser.getDiamondCoinCash() + bettingAmount);
  279. // 更新用户金币
  280. }else{
  281. appUser.setCoin(appUser.getCoin() + bettingAmount);
  282. // if(appUser.getRole() == 1){
  283. // appUser.setCoinCash(appUser.getCoinCash() + bettingAmount);
  284. // }
  285. }
  286. appUserService.updateAppUser(appUser);
  287. redisCache.deleteObject("U:UserInfo:" + appUser.getUserid());
  288. appGameBetting.setIsWinning(1);
  289. appGameBettingService.updateById(appGameBetting);
  290. FinTranRecord finTranRecord = new FinTranRecord();
  291. finTranRecord.setAfterCoin(appUser.getCoin());
  292. finTranRecord.setAfterDiamondCoin(appUser.getDiamondCoin());
  293. if(appGameBetting.getBettingType() == 0){
  294. finTranRecord.setCurrencyType(4);
  295. finTranRecord.setDiamondCoinChange(bettingAmount);
  296. finTranRecord.setCoinChange(0.00);
  297. }else{
  298. finTranRecord.setCurrencyType(2);
  299. finTranRecord.setCoinChange(bettingAmount);
  300. finTranRecord.setDiamondCoinChange(0.00);
  301. }
  302. finTranRecord.setAfterMoney(0.00);
  303. finTranRecord.setMoneyChange(0.00);
  304. finTranRecord.setAfterTicket(0.00);
  305. finTranRecord.setTicketChange(0.00);
  306. finTranRecord.setToUid(appUser.getUserid());
  307. finTranRecord.setUid(appUser.getUserid());
  308. finTranRecord.setSceneId1(0L);
  309. finTranRecord.setSceneId2(null);
  310. finTranRecord.setSceneType(0L);
  311. finTranRecord.setTranGroupId(appGameBetting.getId());
  312. if(appGameBetting.getBettingType() == 0){
  313. finTranRecord.setTranType1(FinTranType1.U_Income_Coin_Balance.getType());
  314. }else{
  315. finTranRecord.setTranType1(FinTranType1.U_Income_Coin.getType());
  316. }
  317. finTranRecord.setTranType2(FinTranType2.GAME_REWARD.getType());
  318. finTranRecord.setTranType3(FinTranType3.CHARGE_WIN_GAME.getType());
  319. finTranRecord.setRemarks("游戏中奖");
  320. finTranRecord.setConsumptionCoin(appGameBetting.getBettingAmount());
  321. finTranRecord.setConsumptionMoney(0.00);
  322. finTranRecord.setCommissionRelatedUid(0L);
  323. finTranRecord.setAgentId(appUser.getAgentId());
  324. finTranRecord.setCreateTime(new Date());
  325. finTranRecord.setFromUid(appGameBetting.getUserId());
  326. finTranRecord.setGoodsId(0L);
  327. finTranRecord.setGuildId(0L);
  328. finTranRecord.setManagerCoId(0L);
  329. finTranRecord.setManagerId(0L);
  330. finTranRecord.setPerc(0.00);
  331. finTranRecord.setProId(0L);
  332. finTranRecord.setProCount(0L);
  333. finTranRecord.setOrderId(appGameBetting.getId());
  334. finTranRecordService.insertFinTranRecord(finTranRecord);
  335. }
  336. }
  337. }
  338. }