IssueService.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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. //暂时注释
  256. // $recordImage = self::lotteryImage($info->issue_no);
  257. if ($recordImage) {
  258. // self::bettingGroupNotice('', [], url($recordImage));
  259. SendTelegramGroupMessageJob::dispatch('', [], url($recordImage), false);
  260. }
  261. BetService::betSettled($info->issue_no, $awards);
  262. DB::commit();
  263. return ['code' => self::YES, 'msg' => '开奖成功'];
  264. } catch (\Exception $e) {
  265. DB::rollBack();
  266. Log::error('开奖失败: ' . $e->getMessage() . $winning_numbers);
  267. return ['code' => self::NOT, 'msg' => '开奖失败'];
  268. }
  269. }
  270. // 虚拟开奖
  271. public static function fakeLotteryDraw($issue_no, $awards)
  272. {
  273. $fake_bet_list = Cache::get('fake_bet_' . $issue_no, []);
  274. $text = "";
  275. foreach ($fake_bet_list as $k => $v) {
  276. $lastStr = self::getLastChar($v['first_name'], 1);
  277. if (in_array($v['keywords'], $awards)) {
  278. $amount = (float)$v['amount'];
  279. $odds = (float)$v['odds'];
  280. $profit = $amount * $odds;
  281. if ($profit > 880000) {
  282. $profit = 880000; // 单注最高奖金880000
  283. }
  284. $item['profit'] = $profit;
  285. $yl = $profit - $amount;
  286. if ($k + 1 <= 30) {
  287. $text .= "私聊下注 【******" . $lastStr . "】 {$yl}\n";
  288. }
  289. } else {
  290. if ($k + 1 <= 30) {
  291. $text .= "私聊下注 【******" . $lastStr . "】 -{$v['amount']}\n";
  292. }
  293. }
  294. }
  295. return $text;
  296. }
  297. /**
  298. * @description: 获取中奖的奖项
  299. * @param {*} $winning_numbers
  300. * @return {*}
  301. */
  302. public static function award($winning_numbers)
  303. {
  304. $result = [];
  305. // 组合
  306. $sum = array_sum($winning_numbers);
  307. $section = self::getSection($sum); // 总和段位
  308. $result[] = $section;
  309. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  310. $result[] = $sumOddEven;
  311. $sumSize = self::calculateSumSize($sum); // 总和大小
  312. $result[] = $sumSize;
  313. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  314. if ($sumExtremeSize) {
  315. $result[] = $sumExtremeSize;
  316. }
  317. $sumCao = $sum . '操'; // 总和数字
  318. $result[] = $sumCao;
  319. $sumCombo = $sumSize . $sumOddEven; // 总和大小单双组合
  320. $result[] = $sumCombo;
  321. $sumBaoZi = self::isBaoZi($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 豹子
  322. if ($sumBaoZi) {
  323. $result[] = $sumBaoZi;
  324. }
  325. $sumPair = self::isPair($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 对子
  326. if ($sumPair) {
  327. $result[] = $sumPair;
  328. }
  329. $sumStraight = self::isStraight($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 顺子
  330. if ($sumStraight) {
  331. $result[] = $sumStraight;
  332. }
  333. $tail = self::getLastDigit($sum); // 总和尾数
  334. $result[] = $tail . '尾'; // 尾数
  335. $tailOddEven = self::calculateOddEven($tail); // 尾数单双
  336. $result[] = '尾' . $tailOddEven;
  337. $tailSize = self::calculateOneSize($tail); // 尾数大小
  338. $result[] = '尾' . $tailSize;
  339. $tailCombo = '尾' . $tailSize . $tailOddEven; // 尾数大小单双组合
  340. $result[] = $tailCombo;
  341. $numA = $winning_numbers[0]; // A球
  342. $result[] = $numA . 'A';
  343. $numAOddEven = self::calculateOddEven($numA); // A球单双
  344. $result[] = 'A' . $numAOddEven;
  345. $numASize = self::calculateOneSize($numA); // A球大小
  346. $result[] = 'A' . $numASize;
  347. $result[] = 'A' . $numASize . $numAOddEven; // A球大小单双组合
  348. $numB = $winning_numbers[1]; // B球
  349. $result[] = $numB . 'B';
  350. $numBOddEven = self::calculateOddEven($numB); // B球
  351. $result[] = 'B' . $numBOddEven;
  352. $numBSize = self::calculateOneSize($numB); // B球大小
  353. $result[] = 'B' . $numBSize;
  354. $result[] = 'B' . $numBSize . $numBOddEven; // B球大小单双组合
  355. $numC = $winning_numbers[2];
  356. $result[] = $numC . 'C';
  357. $numCOddEven = self::calculateOddEven($numC); // C球单双
  358. $result[] = 'C' . $numCOddEven;
  359. $numCSize = self::calculateOneSize($numC); // C球大小
  360. $result[] = 'C' . $numCSize;
  361. $result[] = 'C' . $numCSize . $numCOddEven; // C球大小单双组合
  362. return $result;
  363. }
  364. /**
  365. * @description: 算单双
  366. * @param {*} $number
  367. * @return {*}
  368. */
  369. public static function calculateOddEven($number)
  370. {
  371. if ($number & 1) {
  372. return GameplayRuleEnum::SINGLE;
  373. } else {
  374. return GameplayRuleEnum::DOUBLE;
  375. }
  376. }
  377. /**
  378. * @description: 总和大小
  379. * @param {*} $number
  380. * @return {*}
  381. */
  382. public static function calculateSumSize($number)
  383. {
  384. if ($number >= GameplayRuleEnum::SUM_BIG) {
  385. return GameplayRuleEnum::BIG;
  386. }
  387. if ($number <= GameplayRuleEnum::SUM_SMALL) {
  388. return GameplayRuleEnum::SMALL;
  389. }
  390. }
  391. /**
  392. * @description: 总和极值
  393. * @param {*} $number
  394. * @return {*}
  395. */
  396. public static function calculateSumExtremeSize($number)
  397. {
  398. $result = '';
  399. if ($number >= GameplayRuleEnum::SUM_EXTREME_BIG) {
  400. $result = GameplayRuleEnum::EXTREME_BIG;
  401. }
  402. if ($number <= GameplayRuleEnum::SUM_EXTREME_SMALL) {
  403. $result = GameplayRuleEnum::EXTREME_SMALL;
  404. }
  405. return $result;
  406. }
  407. /**
  408. * @description: 豹子
  409. * @param {int} $a
  410. * @param {int} $b
  411. * @param {int} $c
  412. * @return {*}
  413. */
  414. public static function isBaoZi(int $a, int $b, int $c)
  415. {
  416. $result = '';
  417. if ($a === $b && $b === $c) {
  418. $result = GameplayRuleEnum::BAO_ZI;
  419. }
  420. return $result;
  421. }
  422. /**
  423. * @description: 对子
  424. * @param {int} $a
  425. * @param {int} $b
  426. * @param {int} $c
  427. * @return {*}
  428. */
  429. public static function isPair($a, $b, $c)
  430. {
  431. $result = '';
  432. // 确保输入都是个位数
  433. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  434. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  435. return ''; // 或者抛出异常
  436. }
  437. if (($a == $b && $a != $c) ||
  438. ($a == $c && $a != $b) ||
  439. ($b == $c && $b != $a)) {
  440. $result = GameplayRuleEnum::PAIRS;
  441. }
  442. // 判断是否为对子情况
  443. return $result;
  444. }
  445. /**
  446. * @description: 顺子
  447. * @param {int} $a
  448. * @param {int} $b
  449. * @param {int} $c
  450. * @return {*}
  451. */
  452. public static function isStraight($a, $b, $c)
  453. {
  454. $result = '';
  455. // 确保输入都是个位数(0-9)
  456. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  457. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  458. return '';
  459. }
  460. // 去重(顺子必须三个不同数字)
  461. if ($a == $b || $a == $c || $b == $c) {
  462. return '';
  463. }
  464. // 检查是否是完全升序或完全降序的连续数字
  465. $numbers = [$a, $b, $c];
  466. sort($numbers); // 排序后检查是否是 x, x+1, x+2
  467. list($x, $y, $z) = $numbers;
  468. // 情况1:升序连续(1,2,3)
  469. $isAscending = ($x + 1 == $y) && ($y + 1 == $z);
  470. // 情况2:降序连续(3,2,1)
  471. $isDescending = ($z + 1 == $y) && ($y + 1 == $x);
  472. if ($isAscending || $isDescending) {
  473. $result = GameplayRuleEnum::STRAIGHT;
  474. }
  475. return $result;
  476. }
  477. /**
  478. * 获取数字的尾数
  479. * @param int $number 输入数字
  480. * @return int 尾数
  481. */
  482. public static function getLastDigit($number)
  483. {
  484. // 确保输入是整数
  485. $number = (int)$number;
  486. // 取绝对值,处理负数情况
  487. $number = abs($number);
  488. // 取模10得到尾数
  489. return $number % 10;
  490. }
  491. /**
  492. * @description: 尾大小
  493. * @param {*} $number
  494. * @return {*}
  495. */
  496. public static function calculateOneSize($number)
  497. {
  498. if ($number >= GameplayRuleEnum::ONE_BIG) {
  499. return GameplayRuleEnum::BIG;
  500. }
  501. if ($number <= GameplayRuleEnum::ONE_SMALL) {
  502. return GameplayRuleEnum::SMALL;
  503. }
  504. }
  505. /**
  506. * @description: 获取段位
  507. * @param {*} $number
  508. * @return {*}
  509. */
  510. public static function getSection($number)
  511. {
  512. $result = '';
  513. if ($number >= GameplayRuleEnum::SECTION_1[0] && $number <= GameplayRuleEnum::SECTION_1[1]) {
  514. $result = GameplayRuleEnum::ONE;
  515. } elseif ($number >= GameplayRuleEnum::SECTION_2[0] && $number <= GameplayRuleEnum::SECTION_2[1]) {
  516. $result = GameplayRuleEnum::TWO;
  517. } elseif ($number >= GameplayRuleEnum::SECTION_3[0] && $number <= GameplayRuleEnum::SECTION_3[1]) {
  518. $result = GameplayRuleEnum::THREE;
  519. } elseif ($number >= GameplayRuleEnum::SECTION_4[0] && $number <= GameplayRuleEnum::SECTION_4[1]) {
  520. $result = GameplayRuleEnum::FOUR;
  521. }
  522. return $result; // 不在任何段中
  523. }
  524. /**
  525. * @description: 近期开奖记录
  526. * @return {*}
  527. */
  528. public static function currentLotteryResults($memberId)
  529. {
  530. // $result = self::model()::where('status', self::model()::STATUS_DRAW)->orderBy('id','desc')->take(16)->get();
  531. // $text = "📅 近期开奖记录\n";
  532. // $text .= "====================\n";
  533. // if($result){
  534. // foreach($result as $k => $v){
  535. // $winArr = explode(',',$v->winning_numbers);
  536. // // 组合
  537. // $sum = array_sum($winArr);
  538. // $combo = [];
  539. // $sumOddEven = self::calculateOddEven($sum); // 总和单双
  540. // $sumSize = self::calculateSumSize($sum); // 总和大小
  541. // $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  542. // if(empty($sumExtremeSize)){
  543. // $sumExtremeSize = "-";
  544. // }
  545. // $tail = self::getLastDigit($sum); // 总和尾数
  546. // if($tail == 0){
  547. // $tail = '-'; // 尾数
  548. // }else{
  549. // $tail = '尾'.$tail; // 尾数
  550. // }
  551. // $text .= "回合:{$v->issue_no}期 \n";
  552. // $text .= "结果:".implode('+',explode(',',$v->winning_numbers))."=".array_sum(explode(',',$v->winning_numbers))." \n";
  553. // $text .= "组合:{$sumSize} {$sumOddEven} \n";
  554. // $text .= "极值:{$sumExtremeSize} \n";
  555. // $text .= "尾数:{$tail} \n";
  556. // $text .= "---------------------------\n";
  557. // }
  558. // self::telegram()->sendMessage([
  559. // 'chat_id' => $memberId,
  560. // 'text' => $text,
  561. // ]);
  562. // }else{
  563. // self::telegram()->sendMessage([
  564. // 'chat_id' => $memberId,
  565. // 'text' => "暂无开奖记录",
  566. // ]);
  567. // }
  568. $result = self::model()::where('status', self::model()::STATUS_DRAW)->orderBy('id', 'desc')->first();
  569. if ($result) {
  570. if ($result->image) {
  571. self::telegram()->sendPhoto([
  572. 'chat_id' => $memberId,
  573. 'photo' => InputFile::create(url($result->image)),
  574. ]);
  575. } else {
  576. // if($result->combo){
  577. // self::telegram()->sendMessage([
  578. // 'chat_id' => $memberId,
  579. // 'text' => "",
  580. // ]);
  581. // }else{
  582. self::telegram()->sendMessage([
  583. 'chat_id' => $memberId,
  584. 'text' => "暂无开奖记录",
  585. ]);
  586. // }
  587. }
  588. }
  589. }
  590. // 获取最新的开奖数据
  591. public static function getLatestIssue()
  592. {
  593. $url = "https://ydpc28.co/api/pc28/list";
  594. $result = file_get_contents($url);
  595. $result = json_decode($result, true);
  596. if ($result['errorCode'] != 0) {
  597. return ['code' => self::NOT, 'msg' => '获取最新期号失败'];
  598. }
  599. $nextDrawInfo = $result['data']['nextDrawInfo'];
  600. $startTime = $nextDrawInfo['currentBJTime'];
  601. // if($nextDrawInfo['nextDrawTime'] >= date('H:i:s')) {
  602. // $endTime = date('Y-m-d').' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  603. // }else{
  604. // $endTime = date('Y-m-d',strtotime('+1 day')).' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  605. // }
  606. $endTime = date('Y-m-d H:i:s', strtotime($startTime) + 210);
  607. $new = true;
  608. $list = $result['data']['list'];
  609. $listKey = [];
  610. foreach ($list as $k => $v) {
  611. $listKey[$v['lotNumber']] = $v;
  612. }
  613. $oldList = self::findAll(['status' => self::model()::STATUS_CLOSE]); // 获取所有封盘的期号
  614. foreach ($oldList as $k => $v) {
  615. if (isset($listKey[$v->issue_no])) {
  616. $issue = $listKey[$v->issue_no];
  617. $winning_numbers = implode(',', str_split((string)$issue['openCode']));
  618. $winArr = array_map('intval', explode(',', $winning_numbers));
  619. // 组合
  620. $sum = array_sum($winArr);
  621. $combo = [];
  622. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  623. $combo[] = $sumOddEven;
  624. $sumSize = self::calculateSumSize($sum); // 总和大小
  625. $combo[] = $sumSize;
  626. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  627. if ($sumExtremeSize) {
  628. $combo[] = $sumExtremeSize;
  629. }
  630. $sumBaoZi = self::isBaoZi($winArr[0], $winArr[1], $winArr[2]); // 豹子
  631. if ($sumBaoZi) {
  632. $combo[] = $sumBaoZi;
  633. }
  634. $sumPair = self::isPair($winArr[0], $winArr[1], $winArr[2]); // 对子
  635. if ($sumPair) {
  636. $combo[] = $sumPair;
  637. }
  638. $sumStraight = self::isStraight($winArr[0], $winArr[1], $winArr[2]); // 顺子
  639. if ($sumStraight) {
  640. $combo[] = $sumStraight;
  641. }
  642. $tail = self::getLastDigit($sum); // 总和尾数
  643. if ($tail == 0 || $tail == 9) {
  644. } else {
  645. $combo[] = '尾' . $tail; // 尾数
  646. }
  647. $key = 'lottery_numbers_' . $v->issue_no;
  648. $combo = implode(' ', $combo);
  649. if (Cache::add($key, $winning_numbers, 100)) {
  650. self::lotteryDraw($v->id, $winning_numbers, $combo, '');
  651. $new = false;
  652. }
  653. }
  654. }
  655. // sleep(5); // 等待开奖完成
  656. if ($new) {
  657. $latestIssue = $list[0]; // 最后开奖
  658. $new_issue_no = $latestIssue['lotNumber'] + 1; // 新期号
  659. $newInfo = self::findOne(['issue_no' => $new_issue_no]); // 找新的期号
  660. // 不存在
  661. if (!$newInfo) {
  662. $res = self::submit([
  663. 'issue_no' => $new_issue_no,
  664. 'status' => self::model()::STATUS_DRAFT,
  665. 'start_time' => $startTime,
  666. 'end_time' => $endTime,
  667. ]);
  668. Prediction::prediction($new_issue_no);
  669. $id = $res['key'] ?? 0;
  670. if ($id) {
  671. self::betting($id); // 开始下注
  672. }
  673. Cache::set('new_issue_no', $new_issue_no, 10); // 缓存
  674. }
  675. }
  676. return $result;
  677. }
  678. // 获取最新的开奖数据
  679. public static function getLatestIssue2()
  680. {
  681. $url = "https://ydpc28.co/api/pc28/list";
  682. $result = file_get_contents($url);
  683. $result = json_decode($result, true);
  684. if ($result['errorCode'] != 0) {
  685. return ['code' => self::NOT, 'msg' => '获取最新期号失败'];
  686. }
  687. $nextDrawInfo = $result['data']['nextDrawInfo'];
  688. $startTime = $nextDrawInfo['currentBJTime'];
  689. // if($nextDrawInfo['nextDrawTime'] >= date('H:i:s')) {
  690. // $endTime = date('Y-m-d').' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  691. // }else{
  692. // $endTime = date('Y-m-d',strtotime('+1 day')).' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  693. // }
  694. $endTime = date('Y-m-d H:i:s', strtotime($startTime) + 210);
  695. $new = true;
  696. $list = $result['data']['list'];
  697. $listKey = [];
  698. foreach ($list as $k => $v) {
  699. $listKey[$v['lotNumber']] = $v;
  700. }
  701. $oldList = self::findAll(['status' => self::model()::STATUS_CLOSE]); // 获取所有封盘的期号
  702. foreach ($oldList as $k => $v) {
  703. if (isset($listKey[$v->issue_no])) {
  704. $issue = $listKey[$v->issue_no];
  705. $winning_numbers = implode(',', str_split((string)$issue['openCode']));
  706. $winArr = array_map('intval', explode(',', $winning_numbers));
  707. // 组合
  708. $sum = array_sum($winArr);
  709. $combo = [];
  710. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  711. $combo[] = $sumOddEven;
  712. $sumSize = self::calculateSumSize($sum); // 总和大小
  713. $combo[] = $sumSize;
  714. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  715. if ($sumExtremeSize) {
  716. $combo[] = $sumExtremeSize;
  717. }
  718. $sumBaoZi = self::isBaoZi($winArr[0], $winArr[1], $winArr[2]); // 豹子
  719. if ($sumBaoZi) {
  720. $combo[] = $sumBaoZi;
  721. }
  722. $sumPair = self::isPair($winArr[0], $winArr[1], $winArr[2]); // 对子
  723. if ($sumPair) {
  724. $combo[] = $sumPair;
  725. }
  726. $sumStraight = self::isStraight($winArr[0], $winArr[1], $winArr[2]); // 顺子
  727. if ($sumStraight) {
  728. $combo[] = $sumStraight;
  729. }
  730. $tail = self::getLastDigit($sum); // 总和尾数
  731. if ($tail == 0 || $tail == 9) {
  732. } else {
  733. $combo[] = '尾' . $tail; // 尾数
  734. }
  735. $combo = implode(' ', $combo);
  736. self::lotteryDraw($v->id, $winning_numbers, $combo, '');
  737. }
  738. }
  739. return $result;
  740. }
  741. // 封盘倒数
  742. public static function syncCountdownIssue()
  743. {
  744. $now_date = date('Y-m-d H:i:s', time() + 50); // 提前50秒
  745. $info = self::model()::where('status', self::model()::STATUS_BETTING)->orderBy('end_time', 'asc')->first();
  746. if ($info) {
  747. if ($info['end_time'] < $now_date) {
  748. $replyInfo = KeyboardService::findOne(['button' => '封盘倒数']);
  749. if ($replyInfo) {
  750. $text = $replyInfo->reply;
  751. $buttons = json_decode($replyInfo->buttons, true);
  752. $image = $replyInfo->image;
  753. if ($image) {
  754. $image = url($image);
  755. }
  756. if (Cache::has('issue_countdown_' . $info->id)) {
  757. } else {
  758. // self::bettingGroupNotice($text, $buttons, $image);
  759. self::asyncBettingGroupNotice($text, $buttons, $image);
  760. Cache::put('issue_countdown_' . $info->id, true, 60); // 缓存50秒,防止多次发送
  761. }
  762. }
  763. }
  764. }
  765. }
  766. // 停止下注
  767. public static function syncCloseIssue()
  768. {
  769. $now_date = date('Y-m-d H:i:s', time() + 30); // 提前30秒
  770. $list = self::findAll(['status' => self::model()::STATUS_BETTING]);
  771. foreach ($list as $k => $v) {
  772. if ($v['end_time'] < $now_date) {
  773. self::closeBetting($v->id);
  774. }
  775. }
  776. }
  777. // 生成开奖图片
  778. public static function lotteryImage($issue_no)
  779. {
  780. $list = self::model()::where('issue_no', '<=', $issue_no)->where(self::getWhere(['status' => self::model()::STATUS_DRAW]))->orderBy('issue_no', 'desc')->take(20)->get();
  781. $records = $list->toArray();
  782. foreach ($records as $k => $v) {
  783. $winning_numbers = explode(',', $v['winning_numbers']);
  784. $v['winning_numbers'] = $winning_numbers;
  785. // 组合
  786. $sum = array_sum($winning_numbers);
  787. $v['sum'] = $sum;
  788. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  789. $sumSize = self::calculateSumSize($sum); // 总和大小
  790. $v['combo'] = $sumSize . ' ' . $sumOddEven;
  791. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  792. if (!$sumExtremeSize) {
  793. $sumExtremeSize = '-';
  794. }
  795. $v['extreme'] = $sumExtremeSize;
  796. $tail = self::getLastDigit($sum); // 总和尾数
  797. if ($tail === 0 || $tail === 9) {
  798. $tailStr = '-';
  799. } else {
  800. $tailStr = '尾' . $tail;
  801. }
  802. $v['tail'] = $tailStr;
  803. $records[$k] = $v;
  804. }
  805. $service = new LotteryImageService();
  806. $url = $service->generate($records);
  807. self::model()::where('issue_no', $issue_no)->update(['image' => $url]);
  808. return $url;
  809. }
  810. // 发送开奖图片
  811. public static function sendLotteryImage($chatId, $issueNo)
  812. {
  813. $recordImage = self::lotteryImage($issueNo);
  814. self::sendMessage($chatId, '', [], url($recordImage));
  815. // dispatch(new SendTelegramMessageJob('', [], url($recordImage)));
  816. }
  817. }