IssueService.php 33 KB

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