IssueService.php 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Cao;
  4. use App\Models\CaoHistory;
  5. use App\Models\PcIssue;
  6. use App\Models\Prediction;
  7. use App\Services\BaseService;
  8. use App\Models\Issue;
  9. use App\Models\Config;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Collection;
  12. use Illuminate\Support\Facades\Cache;
  13. use Illuminate\Support\Facades\Log;
  14. use App\Services\GameplayRuleService;
  15. use App\Constants\GameplayRuleEnum;
  16. use App\Http\Controllers\admin\Lottery;
  17. use App\Services\KeyboardService;
  18. use App\Services\LotteryImageService;
  19. use Telegram\Bot\FileUpload\InputFile;
  20. use App\Jobs\SendTelegramMessageJob;
  21. use App\Jobs\SendTelegramGroupMessageJob;
  22. /**
  23. * 投注
  24. */
  25. class IssueService extends BaseService
  26. {
  27. /**
  28. * @description: 模型
  29. * @return {string}
  30. */
  31. public static function model(): string
  32. {
  33. return Issue::class;
  34. }
  35. /**
  36. * @description: 枚举
  37. * @return {*}
  38. */
  39. public static function enum(): string
  40. {
  41. return '';
  42. }
  43. /**
  44. * @description: 获取查询条件
  45. * @param {array} $search 查询内容
  46. * @return {array}
  47. */
  48. public static function getWhere(array $search = []): array
  49. {
  50. $where = [];
  51. if (isset($search['issue_no']) && !empty($search['issue_no'])) {
  52. $where[] = ['issue_no', '=', $search['issue_no']];
  53. }
  54. if (isset($search['id']) && !empty($search['id'])) {
  55. $where[] = ['id', '=', $search['id']];
  56. }
  57. if (isset($search['status']) && !empty($search['status'])) {
  58. $where[] = ['status', '=', $search['status']];
  59. }
  60. if (isset($search['abnormal']) && !empty($search['abnormal'])) {
  61. $where[] = ['end_time', '<', date('Y-m-d H:i:s', time() - 1800)];
  62. $where[] = ['status', '!=', self::model()::STATUS_DRAW];
  63. }
  64. return $where;
  65. }
  66. /**
  67. * @description: 查询单条数据
  68. * @param array $search
  69. * @return \App\Models\Coin|null
  70. */
  71. public static function findOne(array $search): ?Issue
  72. {
  73. return self::model()::where(self::getWhere($search))->first();
  74. }
  75. /**
  76. * @description: 查询所有数据
  77. * @param array $search
  78. * @return \Illuminate\Database\Eloquent\Collection
  79. */
  80. public static function findAll(array $search = [])
  81. {
  82. return self::model()::where(self::getWhere($search))->get();
  83. }
  84. /**
  85. * @description: 分页查询
  86. * @param array $search
  87. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  88. */
  89. public static function paginate(array $search = [])
  90. {
  91. $limit = isset($search['limit']) ? $search['limit'] : 15;
  92. $paginator = self::model()::where(self::getWhere($search))->orderBy('issue_no', 'desc')->paginate($limit);
  93. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  94. }
  95. /**
  96. * @description:
  97. * @param {*} $params
  98. * @return {*}
  99. */
  100. public static function submit($params = [])
  101. {
  102. $result = false;
  103. $msg['code'] = self::NOT;
  104. $msg['msg'] = '';
  105. // 2. 判断是否是更新
  106. if (!empty($params['id'])) {
  107. // 更新
  108. $info = self::findOne(['id' => $params['id']]);
  109. if (!$info) {
  110. $msg['msg'] = '期号不存在!';
  111. } else {
  112. $result = $info->update($params);
  113. $id = $params['id'];
  114. }
  115. } else {
  116. // 创建
  117. $result = $info = self::model()::create($params);
  118. $id = $result->id;
  119. }
  120. if ($result) {
  121. $msg['code'] = self::YES;
  122. $msg['msg'] = '设置成功';
  123. $msg['key'] = $id;
  124. } else {
  125. $msg['msg'] = empty($msg['msg']) ? '操作失败' : $msg['msg'];
  126. }
  127. return $msg;
  128. }
  129. /**
  130. * @description: 开始下注
  131. * @param {*} $id
  132. * @return {*}
  133. */
  134. public static function betting($id)
  135. {
  136. $info = self::findOne(['id' => $id]);
  137. if (!$info) {
  138. return ['code' => self::NOT, 'msg' => '期号不存在'];
  139. }
  140. if (!in_array($info->status, [self::model()::STATUS_DRAFT, self::model()::STATUS_BETTING])) {
  141. return ['code' => self::NOT, 'msg' => '期号状态不正确'];
  142. }
  143. $info->status = self::model()::STATUS_BETTING;
  144. $info->save();
  145. // $text = "";
  146. // $text .= "第".$info->issue_no."期\n";
  147. // $text .= "🔥🔥🔥🔥🔥开始下注🔥🔥🔥🔥🔥\n";
  148. $replyInfo = KeyboardService::findOne(['button' => '玩法规则']);
  149. if ($replyInfo) {
  150. $text = $replyInfo->reply;
  151. $buttons = json_decode($replyInfo->buttons, true);
  152. $image = $replyInfo->image;
  153. if ($image) {
  154. $image = url($image);
  155. }
  156. if (empty($buttons)) {
  157. $buttons = self::getOperateButton();
  158. }
  159. self::asyncBettingGroupNotice($text, $buttons, $image);
  160. }
  161. $replyInfo = KeyboardService::findOne(['button' => '开始下注']);
  162. if ($replyInfo) {
  163. $text = $replyInfo->reply;
  164. $buttons = json_decode($replyInfo->buttons, true);
  165. $image = $replyInfo->image;
  166. if ($image) {
  167. $image = url($image);
  168. }
  169. self::asyncBettingGroupNotice($text, $buttons, $image);
  170. }
  171. return ['code' => self::YES, 'msg' => '开始下注'];
  172. }
  173. /**
  174. * @description: 封盘
  175. * @param {*} $id
  176. * @return {*}
  177. */
  178. public static function closeBetting($id)
  179. {
  180. $info = self::findOne(['id' => $id]);
  181. if (!$info) {
  182. return ['code' => self::NOT, 'msg' => '期号不存在'];
  183. }
  184. if ($info->status != self::model()::STATUS_BETTING) {
  185. return ['code' => self::NOT, 'msg' => '期号状态不正确'];
  186. }
  187. $info->status = self::model()::STATUS_CLOSE;
  188. $info->save();
  189. $replyInfo = KeyboardService::findOne(['button' => '停止下注']);
  190. if ($replyInfo) {
  191. $text = $replyInfo->reply;
  192. $buttons = json_decode($replyInfo->buttons, true);
  193. $image = $replyInfo->image;
  194. if ($image) {
  195. $image = url($image);
  196. }
  197. // self::bettingGroupNotice($text, $buttons, $image);
  198. self::asyncBettingGroupNotice($text, $buttons, $image);
  199. }
  200. // 投注情况通知
  201. BetService::statNotice($info->issue_no);
  202. $replyInfo = KeyboardService::findOne(['button' => '封盘开奖']);
  203. if ($replyInfo) {
  204. $text = $replyInfo->reply;
  205. $buttons = json_decode($replyInfo->buttons, true);
  206. $image = $replyInfo->image;
  207. if ($image) {
  208. $image = url($image);
  209. }
  210. // self::bettingGroupNotice($text, $buttons, $image);
  211. self::asyncBettingGroupNotice($text, $buttons, $image);
  212. }
  213. return ['code' => self::YES, 'msg' => '封盘成功'];
  214. }
  215. /**
  216. * @description: 开奖失败
  217. * @param {*} $id
  218. * @return {*}
  219. */
  220. public static function lotteryDrawFail($id)
  221. {
  222. $result = false;
  223. $msg['code'] = self::NOT;
  224. $msg['msg'] = '';
  225. DB::beginTransaction();
  226. try {
  227. // 更新
  228. $info = self::findOne(['id' => $id]);
  229. if (!$info) {
  230. $msg['msg'] = '期号不存在!';
  231. } else {
  232. $params['status'] = self::model()::STATUS_FAIL;
  233. $result = $info->update($params);
  234. BetService::betFail($info->issue_no);
  235. }
  236. DB::commit();
  237. return ['code' => self::YES, 'msg' => '投注已退回'];
  238. } catch (\Exception $e) {
  239. DB::rollBack();
  240. return ['code' => self::NOT, 'msg' => '投注退回失败'];
  241. }
  242. if ($result) {
  243. $msg['code'] = self::YES;
  244. $msg['msg'] = '设置成功';
  245. } else {
  246. $msg['msg'] = empty($msg['msg']) ? '操作失败' : $msg['msg'];
  247. }
  248. return $msg;
  249. }
  250. /**
  251. * @description: 开奖
  252. * @param {*} $id
  253. * @param {*} $winning_numbers 开奖号码
  254. * @param {*} $combo 开奖组合
  255. * @param {*} $recordImage 开奖图片
  256. * @return {*}
  257. */
  258. public static function lotteryDraw($id, $winning_numbers, $combo, $recordImage)
  259. {
  260. $info = self::findOne(['id' => $id]);
  261. if (!$info) {
  262. return ['code' => self::NOT, 'msg' => '期号不存在'];
  263. }
  264. if ($info->status == self::model()::STATUS_DRAW) {
  265. return ['code' => self::NOT, 'msg' => '期号状态不正确'];
  266. }
  267. $winArr = array_map('intval', explode(',', $winning_numbers));
  268. // 计算中奖
  269. $awards = self::award(explode(',', $winning_numbers));
  270. DB::beginTransaction();
  271. try {
  272. $info->status = self::model()::STATUS_DRAW;
  273. $info->winning_numbers = $winning_numbers;
  274. $info->combo = $combo;
  275. $info->image = $recordImage;
  276. $info->save();
  277. $size = in_array("大", $awards);
  278. $size = $size ? "大" : "小";
  279. $oddOrEven = in_array("双", $awards);
  280. $oddOrEven = $oddOrEven ? "双" : "单";
  281. Prediction::result($info->issue_no, $size, $oddOrEven, $info->winning_numbers);
  282. Cao::updateData($awards);
  283. CaoHistory::updateData($awards);
  284. $replyInfo = KeyboardService::findOne(['button' => '本期开奖']);
  285. if ($replyInfo) {
  286. $text = $replyInfo->reply;
  287. $text .= "\n";
  288. $text .= $info->issue_no . ": " . implode('+', explode(',', $winning_numbers)) . "=" . array_sum($winArr) . " " . $combo;
  289. $buttons = json_decode($replyInfo->buttons, true);
  290. $image = $replyInfo->image;
  291. if ($image) {
  292. $image = url($image);
  293. }
  294. if (empty($buttons)) {
  295. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  296. $buttons[] = [['text' => lang('✅唯一财务'), 'callback_data' => "", 'url' => "https://t.me/{$serviceAccount}"]];
  297. }
  298. // self::bettingGroupNotice($text, $buttons, $image, true);
  299. SendTelegramGroupMessageJob::dispatch($text, $buttons, $image, true);
  300. }
  301. $recordImage = self::lotteryImage($info->issue_no);
  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. return [
  621. 'chat_id' => $memberId,
  622. 'photo' => InputFile::create(url($result->image)),
  623. ];
  624. } else {
  625. // if($result->combo){
  626. // self::telegram()->sendMessage([
  627. // 'chat_id' => $memberId,
  628. // 'text' => "",
  629. // ]);
  630. // }else{
  631. // self::telegram()->sendMessage([
  632. // 'chat_id' => $memberId,
  633. // 'text' => lang("暂无开奖记录"),
  634. // ]);
  635. // }
  636. return
  637. [
  638. 'chat_id' => $memberId,
  639. 'text' => lang("暂无开奖记录"),
  640. ];
  641. }
  642. }
  643. }
  644. // 获取最新的开奖数据
  645. public static function getLatestIssue()
  646. {
  647. $url = "https://ydpc28.co/api/pc28/list";
  648. $result = file_get_contents($url);
  649. $result = json_decode($result, true);
  650. if ($result['errorCode'] != 0) {
  651. return ['code' => self::NOT, 'msg' => '获取最新期号失败'];
  652. }
  653. $nextDrawInfo = $result['data']['nextDrawInfo'];
  654. $startTime = $nextDrawInfo['currentBJTime'];
  655. // if($nextDrawInfo['nextDrawTime'] >= date('H:i:s')) {
  656. // $endTime = date('Y-m-d').' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  657. // }else{
  658. // $endTime = date('Y-m-d',strtotime('+1 day')).' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  659. // }
  660. $endTime = date('Y-m-d H:i:s', strtotime($startTime) + 210);
  661. $new = true;
  662. $list = $result['data']['list'];
  663. $listKey = [];
  664. foreach ($list as $k => $v) {
  665. $listKey[$v['lotNumber']] = $v;
  666. }
  667. $oldList = self::findAll(['status' => self::model()::STATUS_CLOSE]); // 获取所有封盘的期号
  668. foreach ($oldList as $k => $v) {
  669. if (isset($listKey[$v->issue_no])) {
  670. $issue = $listKey[$v->issue_no];
  671. $winning_numbers = implode(',', str_split((string)$issue['openCode']));
  672. $winArr = array_map('intval', explode(',', $winning_numbers));
  673. // 组合
  674. $sum = array_sum($winArr);
  675. $combo = [];
  676. $sumSize = self::calculateSumSize($sum); // 总和大小
  677. $combo[] = $sumSize;
  678. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  679. $combo[] = $sumOddEven;
  680. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  681. if ($sumExtremeSize) {
  682. $combo[] = $sumExtremeSize;
  683. }
  684. $sumBaoZi = self::isBaoZi($winArr[0], $winArr[1], $winArr[2]); // 豹子
  685. if ($sumBaoZi) {
  686. $combo[] = $sumBaoZi;
  687. }
  688. $sumPair = self::isPair($winArr[0], $winArr[1], $winArr[2]); // 对子
  689. if ($sumPair) {
  690. $combo[] = $sumPair;
  691. }
  692. $sumStraight = self::isStraight($winArr[0], $winArr[1], $winArr[2]); // 顺子
  693. if ($sumStraight) {
  694. $combo[] = $sumStraight;
  695. }
  696. $tail = self::getLastDigit($sum); // 总和尾数
  697. if ($tail == 0 || $tail == 9) {
  698. } else {
  699. $combo[] = '尾' . $tail; // 尾数
  700. }
  701. $key = 'lottery_numbers_' . $v->issue_no;
  702. $combo = implode(' ', $combo);
  703. if (Cache::add($key, $winning_numbers, 100)) {
  704. $res = self::lotteryDraw($v->id, $winning_numbers, $combo, '');
  705. if ($res['code'] == self::NOT) {
  706. Log::error("开奖失败:{$v->issue_no} {$res['meg']} ");
  707. }
  708. $new = false;
  709. }else{
  710. Log::error("开奖失败:{$v->issue_no} 1111111111111 ");
  711. }
  712. // Log::error('开奖缓存: ' .$key);
  713. // Log::error('开奖缓存结果: ' .($new ? '新开奖' : '已开奖') );
  714. }
  715. }
  716. // sleep(5); // 等待开奖完成
  717. if ($new) {
  718. $latestIssue = $list[0]; // 最后开奖
  719. $new_issue_no = $latestIssue['lotNumber'] + 1; // 新期号
  720. $newInfo = self::findOne(['issue_no' => $new_issue_no]); // 找新的期号
  721. // 不存在
  722. if (!$newInfo) {
  723. $res = self::submit([
  724. 'issue_no' => $new_issue_no,
  725. 'status' => self::model()::STATUS_DRAFT,
  726. 'start_time' => $startTime,
  727. 'end_time' => $endTime,
  728. ]);
  729. Prediction::prediction($new_issue_no);
  730. $id = $res['key'] ?? 0;
  731. if ($id) {
  732. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  733. if ($pc28Switch == 0) self::betting($id); // 开始下注
  734. }
  735. Cache::set('new_issue_no', $new_issue_no, 10); // 缓存
  736. }
  737. }
  738. return $result;
  739. }
  740. // 获取最新的开奖数据
  741. public static function getLatestIssue2()
  742. {
  743. $url = "https://ydpc28.co/api/pc28/list";
  744. $result = file_get_contents($url);
  745. $result = json_decode($result, true);
  746. if ($result['errorCode'] != 0) {
  747. return ['code' => self::NOT, 'msg' => '获取最新期号失败'];
  748. }
  749. $nextDrawInfo = $result['data']['nextDrawInfo'];
  750. $startTime = $nextDrawInfo['currentBJTime'];
  751. // if($nextDrawInfo['nextDrawTime'] >= date('H:i:s')) {
  752. // $endTime = date('Y-m-d').' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  753. // }else{
  754. // $endTime = date('Y-m-d',strtotime('+1 day')).' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  755. // }
  756. $endTime = date('Y-m-d H:i:s', strtotime($startTime) + 210);
  757. $new = true;
  758. $list = $result['data']['list'];
  759. $listKey = [];
  760. foreach ($list as $k => $v) {
  761. $listKey[$v['lotNumber']] = $v;
  762. }
  763. $oldList = self::findAll(['status' => self::model()::STATUS_CLOSE]); // 获取所有封盘的期号
  764. foreach ($oldList as $k => $v) {
  765. if (isset($listKey[$v->issue_no])) {
  766. $issue = $listKey[$v->issue_no];
  767. $winning_numbers = implode(',', str_split((string)$issue['openCode']));
  768. $winArr = array_map('intval', explode(',', $winning_numbers));
  769. // 组合
  770. $sum = array_sum($winArr);
  771. $combo = [];
  772. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  773. $combo[] = $sumOddEven;
  774. $sumSize = self::calculateSumSize($sum); // 总和大小
  775. $combo[] = $sumSize;
  776. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  777. if ($sumExtremeSize) {
  778. $combo[] = $sumExtremeSize;
  779. }
  780. $sumBaoZi = self::isBaoZi($winArr[0], $winArr[1], $winArr[2]); // 豹子
  781. if ($sumBaoZi) {
  782. $combo[] = $sumBaoZi;
  783. }
  784. $sumPair = self::isPair($winArr[0], $winArr[1], $winArr[2]); // 对子
  785. if ($sumPair) {
  786. $combo[] = $sumPair;
  787. }
  788. $sumStraight = self::isStraight($winArr[0], $winArr[1], $winArr[2]); // 顺子
  789. if ($sumStraight) {
  790. $combo[] = $sumStraight;
  791. }
  792. $tail = self::getLastDigit($sum); // 总和尾数
  793. if ($tail == 0 || $tail == 9) {
  794. } else {
  795. $combo[] = '尾' . $tail; // 尾数
  796. }
  797. $combo = implode(' ', $combo);
  798. self::lotteryDraw($v->id, $winning_numbers, $combo, '');
  799. }
  800. }
  801. return $result;
  802. }
  803. // 封盘倒数
  804. public static function syncCountdownIssue()
  805. {
  806. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  807. if ($pc28Switch == 1) {
  808. $info = PcIssue::where('status', PcIssue::STATUS_BETTING)->orderBy('end_time')->first();
  809. } else {
  810. $info = self::model()::where('status', self::model()::STATUS_BETTING)->orderBy('end_time', 'asc')->first();
  811. }
  812. if ($info) {
  813. $now_date = date('Y-m-d H:i:s', time() + 60); // 提前60秒
  814. if ($info['end_time'] < $now_date) {
  815. $replyInfo = KeyboardService::findOne(['button' => '封盘倒数']);
  816. if ($replyInfo) {
  817. $text = $replyInfo->reply;
  818. $buttons = json_decode($replyInfo->buttons, true);
  819. $image = $replyInfo->image;
  820. if ($image) {
  821. $image = url($image);
  822. }
  823. if (Cache::has('issue_countdown_' . $info->issue_no)) {
  824. } else {
  825. self::asyncBettingGroupNotice($text, $buttons, $image);
  826. Cache::put('issue_countdown_' . $info->issue_no, true, 60); // 缓存50秒,防止多次发送
  827. }
  828. }
  829. }
  830. }
  831. }
  832. // 停止下注
  833. public static function syncCloseIssue()
  834. {
  835. $now_date = date('Y-m-d H:i:s', time() + 30); // 提前30秒
  836. $list = self::findAll(['status' => self::model()::STATUS_BETTING]);
  837. foreach ($list as $k => $v) {
  838. if ($v['end_time'] < $now_date) {
  839. self::closeBetting($v->id);
  840. }
  841. }
  842. }
  843. // 生成开奖图片
  844. public static function lotteryImage($issue_no)
  845. {
  846. $list = self::model()::where('issue_no', '<=', $issue_no)->where(self::getWhere(['status' => self::model()::STATUS_DRAW]))->orderBy('issue_no', 'desc')->take(20)->get();
  847. $records = $list->toArray();
  848. foreach ($records as $k => $v) {
  849. $winning_numbers = explode(',', $v['winning_numbers']);
  850. $v['winning_numbers'] = $winning_numbers;
  851. // 组合
  852. $sum = array_sum($winning_numbers);
  853. $v['sum'] = $sum;
  854. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  855. $sumSize = self::calculateSumSize($sum); // 总和大小
  856. $v['combo'] = $sumSize . ' ' . $sumOddEven;
  857. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  858. if (!$sumExtremeSize) {
  859. $sumExtremeSize = '-';
  860. }
  861. $v['extreme'] = $sumExtremeSize;
  862. $tail = self::getLastDigit($sum); // 总和尾数
  863. if ($tail === 0 || $tail === 9) {
  864. $tailStr = '-';
  865. } else {
  866. $tailStr = '尾' . $tail;
  867. }
  868. $v['tail'] = $tailStr;
  869. $records[$k] = $v;
  870. }
  871. $service = new LotteryImageService();
  872. $url = $service->generate($records);
  873. self::model()::where('issue_no', $issue_no)->update(['image' => $url]);
  874. return $url;
  875. }
  876. // 发送开奖图片
  877. public static function sendLotteryImage($chatId, $issueNo)
  878. {
  879. $recordImage = self::lotteryImage($issueNo);
  880. self::sendMessage($chatId, '', [], url($recordImage));
  881. // dispatch(new SendTelegramMessageJob('', [], url($recordImage)));
  882. }
  883. }