IssueService.php 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Cao;
  4. use App\Models\CaoHistory;
  5. use App\Models\PcIssue;
  6. use App\Models\Prediction;
  7. use App\Services\BaseService;
  8. use App\Models\Issue;
  9. use App\Models\Config;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Collection;
  12. use Illuminate\Support\Facades\Cache;
  13. use Illuminate\Support\Facades\Log;
  14. use App\Services\GameplayRuleService;
  15. use App\Constants\GameplayRuleEnum;
  16. use App\Http\Controllers\admin\Lottery;
  17. use App\Services\KeyboardService;
  18. use App\Services\LotteryImageService;
  19. use Telegram\Bot\FileUpload\InputFile;
  20. use App\Jobs\SendTelegramMessageJob;
  21. use App\Jobs\SendTelegramGroupMessageJob;
  22. /**
  23. * 投注
  24. */
  25. class IssueService extends BaseService
  26. {
  27. /**
  28. * @description: 模型
  29. * @return {string}
  30. */
  31. public static function model(): string
  32. {
  33. return Issue::class;
  34. }
  35. /**
  36. * @description: 枚举
  37. * @return {*}
  38. */
  39. public static function enum(): string
  40. {
  41. return '';
  42. }
  43. /**
  44. * @description: 获取查询条件
  45. * @param {array} $search 查询内容
  46. * @return {array}
  47. */
  48. public static function getWhere(array $search = []): array
  49. {
  50. $where = [];
  51. if (isset($search['issue_no']) && !empty($search['issue_no'])) {
  52. $where[] = ['issue_no', '=', $search['issue_no']];
  53. }
  54. if (isset($search['id']) && !empty($search['id'])) {
  55. $where[] = ['id', '=', $search['id']];
  56. }
  57. if (isset($search['status']) && !empty($search['status'])) {
  58. $where[] = ['status', '=', $search['status']];
  59. }
  60. if (isset($search['abnormal']) && !empty($search['abnormal'])) {
  61. $where[] = ['end_time', '<', date('Y-m-d H:i:s', time() - 1800)];
  62. $where[] = ['status', '!=', self::model()::STATUS_DRAW];
  63. }
  64. return $where;
  65. }
  66. /**
  67. * @description: 查询单条数据
  68. * @param array $search
  69. * @return \App\Models\Coin|null
  70. */
  71. public static function findOne(array $search): ?Issue
  72. {
  73. return self::model()::where(self::getWhere($search))->first();
  74. }
  75. /**
  76. * @description: 查询所有数据
  77. * @param array $search
  78. * @return \Illuminate\Database\Eloquent\Collection
  79. */
  80. public static function findAll(array $search = [])
  81. {
  82. return self::model()::where(self::getWhere($search))->get();
  83. }
  84. /**
  85. * @description: 分页查询
  86. * @param array $search
  87. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  88. */
  89. public static function paginate(array $search = [])
  90. {
  91. $limit = isset($search['limit']) ? $search['limit'] : 15;
  92. $paginator = self::model()::where(self::getWhere($search))->orderBy('issue_no', 'desc')->paginate($limit);
  93. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  94. }
  95. /**
  96. * @description:
  97. * @param {*} $params
  98. * @return {*}
  99. */
  100. public static function submit($params = [])
  101. {
  102. $result = false;
  103. $msg['code'] = self::NOT;
  104. $msg['msg'] = '';
  105. // 2. 判断是否是更新
  106. if (!empty($params['id'])) {
  107. // 更新
  108. $info = self::findOne(['id' => $params['id']]);
  109. if (!$info) {
  110. $msg['msg'] = '期号不存在!';
  111. } else {
  112. $result = $info->update($params);
  113. $id = $params['id'];
  114. }
  115. } else {
  116. // 创建
  117. $result = $info = self::model()::create($params);
  118. $id = $result->id;
  119. }
  120. if ($result) {
  121. $msg['code'] = self::YES;
  122. $msg['msg'] = '设置成功';
  123. $msg['key'] = $id;
  124. } else {
  125. $msg['msg'] = empty($msg['msg']) ? '操作失败' : $msg['msg'];
  126. }
  127. return $msg;
  128. }
  129. /**
  130. * @description: 开始下注
  131. * @param {*} $id
  132. * @return {*}
  133. */
  134. public static function betting($id)
  135. {
  136. $info = self::findOne(['id' => $id]);
  137. if (!$info) {
  138. return ['code' => self::NOT, 'msg' => '期号不存在'];
  139. }
  140. if (!in_array($info->status, [self::model()::STATUS_DRAFT, self::model()::STATUS_BETTING])) {
  141. return ['code' => self::NOT, 'msg' => '期号状态不正确'];
  142. }
  143. $info->status = self::model()::STATUS_BETTING;
  144. $info->save();
  145. // $text = "";
  146. // $text .= "第".$info->issue_no."期\n";
  147. // $text .= "🔥🔥🔥🔥🔥开始下注🔥🔥🔥🔥🔥\n";
  148. $replyInfo = KeyboardService::findOne(['button' => '玩法规则']);
  149. if ($replyInfo) {
  150. $text = $replyInfo->reply;
  151. $buttons = json_decode($replyInfo->buttons, true);
  152. $image = $replyInfo->image;
  153. if ($image) {
  154. $image = url($image);
  155. }
  156. if (empty($buttons)) {
  157. $buttons = self::getOperateButton();
  158. }
  159. self::asyncBettingGroupNotice($text, $buttons, $image);
  160. }
  161. $replyInfo = KeyboardService::findOne(['button' => '开始下注']);
  162. if ($replyInfo) {
  163. $text = $replyInfo->reply;
  164. $buttons = json_decode($replyInfo->buttons, true);
  165. $image = $replyInfo->image;
  166. if ($image) {
  167. $image = url($image);
  168. }
  169. self::asyncBettingGroupNotice($text, $buttons, $image);
  170. }
  171. return ['code' => self::YES, 'msg' => '开始下注'];
  172. }
  173. /**
  174. * @description: 封盘
  175. * @param {*} $id
  176. * @return {*}
  177. */
  178. public static function closeBetting($id)
  179. {
  180. $info = self::findOne(['id' => $id]);
  181. if (!$info) {
  182. return ['code' => self::NOT, 'msg' => '期号不存在'];
  183. }
  184. if ($info->status != self::model()::STATUS_BETTING) {
  185. return ['code' => self::NOT, 'msg' => '期号状态不正确'];
  186. }
  187. $info->status = self::model()::STATUS_CLOSE;
  188. $info->save();
  189. $replyInfo = KeyboardService::findOne(['button' => '停止下注']);
  190. if ($replyInfo) {
  191. $text = $replyInfo->reply;
  192. $buttons = json_decode($replyInfo->buttons, true);
  193. $image = $replyInfo->image;
  194. if ($image) {
  195. $image = url($image);
  196. }
  197. // self::bettingGroupNotice($text, $buttons, $image);
  198. self::asyncBettingGroupNotice($text, $buttons, $image);
  199. }
  200. // 投注情况通知
  201. BetService::statNotice($info->issue_no);
  202. $replyInfo = KeyboardService::findOne(['button' => '封盘开奖']);
  203. if ($replyInfo) {
  204. $text = $replyInfo->reply;
  205. $buttons = json_decode($replyInfo->buttons, true);
  206. $image = $replyInfo->image;
  207. if ($image) {
  208. $image = url($image);
  209. }
  210. // self::bettingGroupNotice($text, $buttons, $image);
  211. self::asyncBettingGroupNotice($text, $buttons, $image);
  212. }
  213. return ['code' => self::YES, 'msg' => '封盘成功'];
  214. }
  215. /**
  216. * @description: 开奖失败
  217. * @param {*} $id
  218. * @return {*}
  219. */
  220. public static function lotteryDrawFail($id)
  221. {
  222. $result = false;
  223. $msg['code'] = self::NOT;
  224. $msg['msg'] = '';
  225. DB::beginTransaction();
  226. try {
  227. // 更新
  228. $info = self::findOne(['id' => $id]);
  229. if (!$info) {
  230. $msg['msg'] = '期号不存在!';
  231. } else {
  232. $params['status'] = self::model()::STATUS_FAIL;
  233. $result = $info->update($params);
  234. BetService::betFail($info->issue_no);
  235. }
  236. DB::commit();
  237. return ['code' => self::YES, 'msg' => '投注已退回'];
  238. } catch (\Exception $e) {
  239. DB::rollBack();
  240. return ['code' => self::NOT, 'msg' => '投注退回失败'];
  241. }
  242. if ($result) {
  243. $msg['code'] = self::YES;
  244. $msg['msg'] = '设置成功';
  245. } else {
  246. $msg['msg'] = empty($msg['msg']) ? '操作失败' : $msg['msg'];
  247. }
  248. return $msg;
  249. }
  250. /**
  251. * @description: 开奖
  252. * @param {*} $id
  253. * @param {*} $winning_numbers 开奖号码
  254. * @param {*} $combo 开奖组合
  255. * @param {*} $recordImage 开奖图片
  256. * @return {*}
  257. */
  258. public static function lotteryDraw($id, $winning_numbers, $combo, $recordImage)
  259. {
  260. $info = self::findOne(['id' => $id]);
  261. if (!$info) {
  262. return ['code' => self::NOT, 'msg' => '期号不存在'];
  263. }
  264. if ($info->status == self::model()::STATUS_DRAW) {
  265. return ['code' => self::NOT, 'msg' => '期号状态不正确'];
  266. }
  267. $winArr = array_map('intval', explode(',', $winning_numbers));
  268. // 计算中奖
  269. $awards = self::award(explode(',', $winning_numbers));
  270. DB::beginTransaction();
  271. try {
  272. $info->status = self::model()::STATUS_DRAW;
  273. $info->winning_numbers = $winning_numbers;
  274. $info->combo = $combo;
  275. $info->image = $recordImage;
  276. $info->save();
  277. $size = in_array("大", $awards);
  278. $size = $size ? "大" : "小";
  279. $oddOrEven = in_array("双", $awards);
  280. $oddOrEven = $oddOrEven ? "双" : "单";
  281. Prediction::result($info->issue_no, $size, $oddOrEven, $info->winning_numbers);
  282. Cao::updateData($awards);
  283. CaoHistory::updateData($awards);
  284. $replyInfo = KeyboardService::findOne(['button' => '本期开奖']);
  285. if ($replyInfo) {
  286. $text = $replyInfo->reply;
  287. $text .= "\n";
  288. $text .= $info->issue_no . "期 " . implode('+', explode(',', $winning_numbers)) . "=" . array_sum($winArr) . " " . $combo;
  289. $buttons = json_decode($replyInfo->buttons, true);
  290. $image = $replyInfo->image;
  291. if ($image) {
  292. $image = url($image);
  293. }
  294. if (empty($buttons)) {
  295. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  296. $buttons[] = [['text' => '✅唯一财务', 'callback_data' => "", 'url' => "https://t.me/{$serviceAccount}"]];
  297. }
  298. // self::bettingGroupNotice($text, $buttons, $image, true);
  299. SendTelegramGroupMessageJob::dispatch($text, $buttons, $image, true);
  300. }
  301. if (config('app.url') != 'https://botpc28.testx2.cc') {
  302. $recordImage = self::lotteryImage($info->issue_no);
  303. }
  304. if ($recordImage) {
  305. // self::bettingGroupNotice('', [], url($recordImage));
  306. SendTelegramGroupMessageJob::dispatch('', [], url($recordImage), false);
  307. }
  308. BetService::betSettled($info->issue_no, $awards);
  309. DB::commit();
  310. return ['code' => self::YES, 'msg' => '开奖成功'];
  311. } catch (\Exception $e) {
  312. DB::rollBack();
  313. Log::error('开奖失败: ' . $e->getMessage() . $winning_numbers);
  314. return ['code' => self::NOT, 'msg' => '开奖失败'];
  315. }
  316. }
  317. // 虚拟开奖
  318. public static function fakeLotteryDraw($issue_no, $awards)
  319. {
  320. $fake_bet_list = Cache::get('fake_bet_' . $issue_no, []);
  321. $text = "";
  322. foreach ($fake_bet_list as $k => $v) {
  323. $lastStr = self::getLastChar($v['first_name'], 1);
  324. if (in_array($v['keywords'], $awards)) {
  325. $amount = (float)$v['amount'];
  326. $odds = (float)$v['odds'];
  327. $profit = $amount * $odds;
  328. if ($profit > 880000) {
  329. $profit = 880000; // 单注最高奖金880000
  330. }
  331. $item['profit'] = $profit;
  332. $yl = $profit - $amount;
  333. if ($k + 1 <= 30) {
  334. $text .= "私聊下注 【******" . $lastStr . "】 {$yl}\n";
  335. }
  336. } else {
  337. if ($k + 1 <= 30) {
  338. $text .= "私聊下注 【******" . $lastStr . "】 -{$v['amount']}\n";
  339. }
  340. }
  341. }
  342. return $text;
  343. }
  344. /**
  345. * @description: 获取中奖的奖项
  346. * @param {*} $winning_numbers
  347. * @return {*}
  348. */
  349. public static function award($winning_numbers)
  350. {
  351. $result = [];
  352. // 组合
  353. $sum = array_sum($winning_numbers);
  354. $section = self::getSection($sum); // 总和段位
  355. $result[] = $section;
  356. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  357. $result[] = $sumOddEven;
  358. $sumSize = self::calculateSumSize($sum); // 总和大小
  359. $result[] = $sumSize;
  360. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  361. if ($sumExtremeSize) {
  362. $result[] = $sumExtremeSize;
  363. }
  364. $sumCao = $sum . '操'; // 总和数字
  365. $result[] = $sumCao;
  366. $sumCombo = $sumSize . $sumOddEven; // 总和大小单双组合
  367. $result[] = $sumCombo;
  368. $sumBaoZi = self::isBaoZi($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 豹子
  369. if ($sumBaoZi) {
  370. $result[] = $sumBaoZi;
  371. }
  372. $sumPair = self::isPair($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 对子
  373. if ($sumPair) {
  374. $result[] = $sumPair;
  375. }
  376. $sumStraight = self::isStraight($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 顺子
  377. if ($sumStraight) {
  378. $result[] = $sumStraight;
  379. }
  380. $tail = self::getLastDigit($sum); // 总和尾数
  381. $result[] = $tail . '尾'; // 尾数
  382. $tailOddEven = self::calculateOddEven($tail); // 尾数单双
  383. $result[] = '尾' . $tailOddEven;
  384. $tailSize = self::calculateOneSize($tail); // 尾数大小
  385. $result[] = '尾' . $tailSize;
  386. $tailCombo = '尾' . $tailSize . $tailOddEven; // 尾数大小单双组合
  387. $result[] = $tailCombo;
  388. $numA = $winning_numbers[0]; // A球
  389. $result[] = $numA . 'A';
  390. $numAOddEven = self::calculateOddEven($numA); // A球单双
  391. $result[] = 'A' . $numAOddEven;
  392. $numASize = self::calculateOneSize($numA); // A球大小
  393. $result[] = 'A' . $numASize;
  394. $result[] = 'A' . $numASize . $numAOddEven; // A球大小单双组合
  395. $numB = $winning_numbers[1]; // B球
  396. $result[] = $numB . 'B';
  397. $numBOddEven = self::calculateOddEven($numB); // B球
  398. $result[] = 'B' . $numBOddEven;
  399. $numBSize = self::calculateOneSize($numB); // B球大小
  400. $result[] = 'B' . $numBSize;
  401. $result[] = 'B' . $numBSize . $numBOddEven; // B球大小单双组合
  402. $numC = $winning_numbers[2];
  403. $result[] = $numC . 'C';
  404. $numCOddEven = self::calculateOddEven($numC); // C球单双
  405. $result[] = 'C' . $numCOddEven;
  406. $numCSize = self::calculateOneSize($numC); // C球大小
  407. $result[] = 'C' . $numCSize;
  408. $result[] = 'C' . $numCSize . $numCOddEven; // C球大小单双组合
  409. return $result;
  410. }
  411. /**
  412. * @description: 算单双
  413. * @param {*} $number
  414. * @return {*}
  415. */
  416. public static function calculateOddEven($number)
  417. {
  418. if ($number & 1) {
  419. return GameplayRuleEnum::SINGLE;
  420. } else {
  421. return GameplayRuleEnum::DOUBLE;
  422. }
  423. }
  424. /**
  425. * @description: 总和大小
  426. * @param {*} $number
  427. * @return {*}
  428. */
  429. public static function calculateSumSize($number)
  430. {
  431. if ($number >= GameplayRuleEnum::SUM_BIG) {
  432. return GameplayRuleEnum::BIG;
  433. }
  434. if ($number <= GameplayRuleEnum::SUM_SMALL) {
  435. return GameplayRuleEnum::SMALL;
  436. }
  437. }
  438. /**
  439. * @description: 总和极值
  440. * @param {*} $number
  441. * @return {*}
  442. */
  443. public static function calculateSumExtremeSize($number)
  444. {
  445. $result = '';
  446. if ($number >= GameplayRuleEnum::SUM_EXTREME_BIG) {
  447. $result = GameplayRuleEnum::EXTREME_BIG;
  448. }
  449. if ($number <= GameplayRuleEnum::SUM_EXTREME_SMALL) {
  450. $result = GameplayRuleEnum::EXTREME_SMALL;
  451. }
  452. return $result;
  453. }
  454. /**
  455. * @description: 豹子
  456. * @param {int} $a
  457. * @param {int} $b
  458. * @param {int} $c
  459. * @return {*}
  460. */
  461. public static function isBaoZi(int $a, int $b, int $c)
  462. {
  463. $result = '';
  464. if ($a === $b && $b === $c) {
  465. $result = GameplayRuleEnum::BAO_ZI;
  466. }
  467. return $result;
  468. }
  469. /**
  470. * @description: 对子
  471. * @param {int} $a
  472. * @param {int} $b
  473. * @param {int} $c
  474. * @return {*}
  475. */
  476. public static function isPair($a, $b, $c)
  477. {
  478. $result = '';
  479. // 确保输入都是个位数
  480. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  481. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  482. return ''; // 或者抛出异常
  483. }
  484. if (($a == $b && $a != $c) ||
  485. ($a == $c && $a != $b) ||
  486. ($b == $c && $b != $a)) {
  487. $result = GameplayRuleEnum::PAIRS;
  488. }
  489. // 判断是否为对子情况
  490. return $result;
  491. }
  492. /**
  493. * @description: 顺子
  494. * @param {int} $a
  495. * @param {int} $b
  496. * @param {int} $c
  497. * @return {*}
  498. */
  499. public static function isStraight($a, $b, $c)
  500. {
  501. $result = '';
  502. // 确保输入都是个位数(0-9)
  503. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  504. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  505. return '';
  506. }
  507. // 去重(顺子必须三个不同数字)
  508. if ($a == $b || $a == $c || $b == $c) {
  509. return '';
  510. }
  511. // 检查是否是完全升序或完全降序的连续数字
  512. $numbers = [$a, $b, $c];
  513. sort($numbers); // 排序后检查是否是 x, x+1, x+2
  514. list($x, $y, $z) = $numbers;
  515. // 情况1:升序连续(1,2,3)
  516. $isAscending = ($x + 1 == $y) && ($y + 1 == $z);
  517. // 情况2:降序连续(3,2,1)
  518. $isDescending = ($z + 1 == $y) && ($y + 1 == $x);
  519. if ($isAscending || $isDescending) {
  520. $result = GameplayRuleEnum::STRAIGHT;
  521. }
  522. return $result;
  523. }
  524. /**
  525. * 获取数字的尾数
  526. * @param int $number 输入数字
  527. * @return int 尾数
  528. */
  529. public static function getLastDigit($number)
  530. {
  531. // 确保输入是整数
  532. $number = (int)$number;
  533. // 取绝对值,处理负数情况
  534. $number = abs($number);
  535. // 取模10得到尾数
  536. return $number % 10;
  537. }
  538. /**
  539. * @description: 尾大小
  540. * @param {*} $number
  541. * @return {*}
  542. */
  543. public static function calculateOneSize($number)
  544. {
  545. if ($number >= GameplayRuleEnum::ONE_BIG) {
  546. return GameplayRuleEnum::BIG;
  547. }
  548. if ($number <= GameplayRuleEnum::ONE_SMALL) {
  549. return GameplayRuleEnum::SMALL;
  550. }
  551. }
  552. /**
  553. * @description: 获取段位
  554. * @param {*} $number
  555. * @return {*}
  556. */
  557. public static function getSection($number)
  558. {
  559. $result = '';
  560. if ($number >= GameplayRuleEnum::SECTION_1[0] && $number <= GameplayRuleEnum::SECTION_1[1]) {
  561. $result = GameplayRuleEnum::ONE;
  562. } elseif ($number >= GameplayRuleEnum::SECTION_2[0] && $number <= GameplayRuleEnum::SECTION_2[1]) {
  563. $result = GameplayRuleEnum::TWO;
  564. } elseif ($number >= GameplayRuleEnum::SECTION_3[0] && $number <= GameplayRuleEnum::SECTION_3[1]) {
  565. $result = GameplayRuleEnum::THREE;
  566. } elseif ($number >= GameplayRuleEnum::SECTION_4[0] && $number <= GameplayRuleEnum::SECTION_4[1]) {
  567. $result = GameplayRuleEnum::FOUR;
  568. }
  569. return $result; // 不在任何段中
  570. }
  571. /**
  572. * @description: 近期开奖记录
  573. * @return {*}
  574. */
  575. public static function currentLotteryResults($memberId)
  576. {
  577. // $result = self::model()::where('status', self::model()::STATUS_DRAW)->orderBy('id','desc')->take(16)->get();
  578. // $text = "📅 近期开奖记录\n";
  579. // $text .= "====================\n";
  580. // if($result){
  581. // foreach($result as $k => $v){
  582. // $winArr = explode(',',$v->winning_numbers);
  583. // // 组合
  584. // $sum = array_sum($winArr);
  585. // $combo = [];
  586. // $sumOddEven = self::calculateOddEven($sum); // 总和单双
  587. // $sumSize = self::calculateSumSize($sum); // 总和大小
  588. // $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  589. // if(empty($sumExtremeSize)){
  590. // $sumExtremeSize = "-";
  591. // }
  592. // $tail = self::getLastDigit($sum); // 总和尾数
  593. // if($tail == 0){
  594. // $tail = '-'; // 尾数
  595. // }else{
  596. // $tail = '尾'.$tail; // 尾数
  597. // }
  598. // $text .= "回合:{$v->issue_no}期 \n";
  599. // $text .= "结果:".implode('+',explode(',',$v->winning_numbers))."=".array_sum(explode(',',$v->winning_numbers))." \n";
  600. // $text .= "组合:{$sumSize} {$sumOddEven} \n";
  601. // $text .= "极值:{$sumExtremeSize} \n";
  602. // $text .= "尾数:{$tail} \n";
  603. // $text .= "---------------------------\n";
  604. // }
  605. // self::telegram()->sendMessage([
  606. // 'chat_id' => $memberId,
  607. // 'text' => $text,
  608. // ]);
  609. // }else{
  610. // self::telegram()->sendMessage([
  611. // 'chat_id' => $memberId,
  612. // 'text' => "暂无开奖记录",
  613. // ]);
  614. // }
  615. $result = self::model()::where('status', self::model()::STATUS_DRAW)->orderBy('id', 'desc')->first();
  616. if ($result) {
  617. if ($result->image) {
  618. // self::telegram()->sendPhoto([
  619. // 'chat_id' => $memberId,
  620. // 'photo' => InputFile::create(url($result->image)),
  621. // ]);
  622. return [
  623. 'chat_id' => $memberId,
  624. 'photo' => InputFile::create(url($result->image)),
  625. ];
  626. } else {
  627. // if($result->combo){
  628. // self::telegram()->sendMessage([
  629. // 'chat_id' => $memberId,
  630. // 'text' => "",
  631. // ]);
  632. // }else{
  633. // self::telegram()->sendMessage([
  634. // 'chat_id' => $memberId,
  635. // 'text' => lang("暂无开奖记录"),
  636. // ]);
  637. // }
  638. return
  639. [
  640. 'chat_id' => $memberId,
  641. 'text' => lang("暂无开奖记录"),
  642. ];
  643. }
  644. }
  645. }
  646. // 获取最新的开奖数据
  647. public static function getLatestIssue()
  648. {
  649. $url = "https://ydpc28.co/api/pc28/list";
  650. $result = file_get_contents($url);
  651. $result = json_decode($result, true);
  652. if ($result['errorCode'] != 0) {
  653. return ['code' => self::NOT, 'msg' => '获取最新期号失败'];
  654. }
  655. $nextDrawInfo = $result['data']['nextDrawInfo'];
  656. $startTime = $nextDrawInfo['currentBJTime'];
  657. // if($nextDrawInfo['nextDrawTime'] >= date('H:i:s')) {
  658. // $endTime = date('Y-m-d').' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  659. // }else{
  660. // $endTime = date('Y-m-d',strtotime('+1 day')).' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  661. // }
  662. $endTime = date('Y-m-d H:i:s', strtotime($startTime) + 210);
  663. $new = true;
  664. $list = $result['data']['list'];
  665. $listKey = [];
  666. foreach ($list as $k => $v) {
  667. $listKey[$v['lotNumber']] = $v;
  668. }
  669. $oldList = self::findAll(['status' => self::model()::STATUS_CLOSE]); // 获取所有封盘的期号
  670. foreach ($oldList as $k => $v) {
  671. if (isset($listKey[$v->issue_no])) {
  672. $issue = $listKey[$v->issue_no];
  673. $winning_numbers = implode(',', str_split((string)$issue['openCode']));
  674. $winArr = array_map('intval', explode(',', $winning_numbers));
  675. // 组合
  676. $sum = array_sum($winArr);
  677. $combo = [];
  678. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  679. $combo[] = $sumOddEven;
  680. $sumSize = self::calculateSumSize($sum); // 总和大小
  681. $combo[] = $sumSize;
  682. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  683. if ($sumExtremeSize) {
  684. $combo[] = $sumExtremeSize;
  685. }
  686. $sumBaoZi = self::isBaoZi($winArr[0], $winArr[1], $winArr[2]); // 豹子
  687. if ($sumBaoZi) {
  688. $combo[] = $sumBaoZi;
  689. }
  690. $sumPair = self::isPair($winArr[0], $winArr[1], $winArr[2]); // 对子
  691. if ($sumPair) {
  692. $combo[] = $sumPair;
  693. }
  694. $sumStraight = self::isStraight($winArr[0], $winArr[1], $winArr[2]); // 顺子
  695. if ($sumStraight) {
  696. $combo[] = $sumStraight;
  697. }
  698. $tail = self::getLastDigit($sum); // 总和尾数
  699. if ($tail == 0 || $tail == 9) {
  700. } else {
  701. $combo[] = '尾' . $tail; // 尾数
  702. }
  703. $key = 'lottery_numbers_' . $v->issue_no;
  704. $combo = implode(' ', $combo);
  705. if (Cache::add($key, $winning_numbers, 100)) {
  706. self::lotteryDraw($v->id, $winning_numbers, $combo, '');
  707. $new = false;
  708. }
  709. // Log::error('开奖缓存: ' .$key);
  710. // Log::error('开奖缓存结果: ' .($new ? '新开奖' : '已开奖') );
  711. }
  712. }
  713. // sleep(5); // 等待开奖完成
  714. if ($new) {
  715. $latestIssue = $list[0]; // 最后开奖
  716. $new_issue_no = $latestIssue['lotNumber'] + 1; // 新期号
  717. $newInfo = self::findOne(['issue_no' => $new_issue_no]); // 找新的期号
  718. // 不存在
  719. if (!$newInfo) {
  720. $res = self::submit([
  721. 'issue_no' => $new_issue_no,
  722. 'status' => self::model()::STATUS_DRAFT,
  723. 'start_time' => $startTime,
  724. 'end_time' => $endTime,
  725. ]);
  726. Prediction::prediction($new_issue_no);
  727. $id = $res['key'] ?? 0;
  728. if ($id) {
  729. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  730. if ($pc28Switch == 0) self::betting($id); // 开始下注
  731. }
  732. Cache::set('new_issue_no', $new_issue_no, 10); // 缓存
  733. }
  734. }
  735. return $result;
  736. }
  737. // 获取最新的开奖数据
  738. public static function getLatestIssue2()
  739. {
  740. $url = "https://ydpc28.co/api/pc28/list";
  741. $result = file_get_contents($url);
  742. $result = json_decode($result, true);
  743. if ($result['errorCode'] != 0) {
  744. return ['code' => self::NOT, 'msg' => '获取最新期号失败'];
  745. }
  746. $nextDrawInfo = $result['data']['nextDrawInfo'];
  747. $startTime = $nextDrawInfo['currentBJTime'];
  748. // if($nextDrawInfo['nextDrawTime'] >= date('H:i:s')) {
  749. // $endTime = date('Y-m-d').' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  750. // }else{
  751. // $endTime = date('Y-m-d',strtotime('+1 day')).' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  752. // }
  753. $endTime = date('Y-m-d H:i:s', strtotime($startTime) + 210);
  754. $new = true;
  755. $list = $result['data']['list'];
  756. $listKey = [];
  757. foreach ($list as $k => $v) {
  758. $listKey[$v['lotNumber']] = $v;
  759. }
  760. $oldList = self::findAll(['status' => self::model()::STATUS_CLOSE]); // 获取所有封盘的期号
  761. foreach ($oldList as $k => $v) {
  762. if (isset($listKey[$v->issue_no])) {
  763. $issue = $listKey[$v->issue_no];
  764. $winning_numbers = implode(',', str_split((string)$issue['openCode']));
  765. $winArr = array_map('intval', explode(',', $winning_numbers));
  766. // 组合
  767. $sum = array_sum($winArr);
  768. $combo = [];
  769. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  770. $combo[] = $sumOddEven;
  771. $sumSize = self::calculateSumSize($sum); // 总和大小
  772. $combo[] = $sumSize;
  773. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  774. if ($sumExtremeSize) {
  775. $combo[] = $sumExtremeSize;
  776. }
  777. $sumBaoZi = self::isBaoZi($winArr[0], $winArr[1], $winArr[2]); // 豹子
  778. if ($sumBaoZi) {
  779. $combo[] = $sumBaoZi;
  780. }
  781. $sumPair = self::isPair($winArr[0], $winArr[1], $winArr[2]); // 对子
  782. if ($sumPair) {
  783. $combo[] = $sumPair;
  784. }
  785. $sumStraight = self::isStraight($winArr[0], $winArr[1], $winArr[2]); // 顺子
  786. if ($sumStraight) {
  787. $combo[] = $sumStraight;
  788. }
  789. $tail = self::getLastDigit($sum); // 总和尾数
  790. if ($tail == 0 || $tail == 9) {
  791. } else {
  792. $combo[] = '尾' . $tail; // 尾数
  793. }
  794. $combo = implode(' ', $combo);
  795. self::lotteryDraw($v->id, $winning_numbers, $combo, '');
  796. }
  797. }
  798. return $result;
  799. }
  800. // 封盘倒数
  801. public static function syncCountdownIssue()
  802. {
  803. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  804. if ($pc28Switch == 1) {
  805. $info = PcIssue::where('status', PcIssue::STATUS_BETTING)->orderBy('end_time')->first();
  806. } else {
  807. $info = self::model()::where('status', self::model()::STATUS_BETTING)->orderBy('end_time', 'asc')->first();
  808. }
  809. if ($info) {
  810. $now_date = date('Y-m-d H:i:s', time() + 60); // 提前60秒
  811. if ($info['end_time'] < $now_date) {
  812. $replyInfo = KeyboardService::findOne(['button' => '封盘倒数']);
  813. if ($replyInfo) {
  814. $text = $replyInfo->reply;
  815. $buttons = json_decode($replyInfo->buttons, true);
  816. $image = $replyInfo->image;
  817. if ($image) {
  818. $image = url($image);
  819. }
  820. if (Cache::has('issue_countdown_' . $info->issue_no)) {
  821. } else {
  822. self::asyncBettingGroupNotice($text, $buttons, $image);
  823. Cache::put('issue_countdown_' . $info->issue_no, true, 60); // 缓存50秒,防止多次发送
  824. }
  825. }
  826. }
  827. }
  828. }
  829. // 停止下注
  830. public static function syncCloseIssue()
  831. {
  832. $now_date = date('Y-m-d H:i:s', time() + 30); // 提前30秒
  833. $list = self::findAll(['status' => self::model()::STATUS_BETTING]);
  834. foreach ($list as $k => $v) {
  835. if ($v['end_time'] < $now_date) {
  836. self::closeBetting($v->id);
  837. }
  838. }
  839. }
  840. // 生成开奖图片
  841. public static function lotteryImage($issue_no)
  842. {
  843. $list = self::model()::where('issue_no', '<=', $issue_no)->where(self::getWhere(['status' => self::model()::STATUS_DRAW]))->orderBy('issue_no', 'desc')->take(20)->get();
  844. $records = $list->toArray();
  845. foreach ($records as $k => $v) {
  846. $winning_numbers = explode(',', $v['winning_numbers']);
  847. $v['winning_numbers'] = $winning_numbers;
  848. // 组合
  849. $sum = array_sum($winning_numbers);
  850. $v['sum'] = $sum;
  851. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  852. $sumSize = self::calculateSumSize($sum); // 总和大小
  853. $v['combo'] = $sumSize . ' ' . $sumOddEven;
  854. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  855. if (!$sumExtremeSize) {
  856. $sumExtremeSize = '-';
  857. }
  858. $v['extreme'] = $sumExtremeSize;
  859. $tail = self::getLastDigit($sum); // 总和尾数
  860. if ($tail === 0 || $tail === 9) {
  861. $tailStr = '-';
  862. } else {
  863. $tailStr = '尾' . $tail;
  864. }
  865. $v['tail'] = $tailStr;
  866. $records[$k] = $v;
  867. }
  868. $service = new LotteryImageService();
  869. $url = $service->generate($records);
  870. self::model()::where('issue_no', $issue_no)->update(['image' => $url]);
  871. return $url;
  872. }
  873. // 发送开奖图片
  874. public static function sendLotteryImage($chatId, $issueNo)
  875. {
  876. $recordImage = self::lotteryImage($issueNo);
  877. self::sendMessage($chatId, '', [], url($recordImage));
  878. // dispatch(new SendTelegramMessageJob('', [], url($recordImage)));
  879. }
  880. }