IssueService.php 31 KB

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