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