IssueService.php 27 KB

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