IssueService.php 31 KB

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