IssueService.php 31 KB

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