IssueService.php 33 KB

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