IssueService.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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. $result = '';
  421. // 确保输入都是个位数
  422. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  423. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  424. return ''; // 或者抛出异常
  425. }
  426. if(($a == $b && $a != $c) ||
  427. ($a == $c && $a != $b) ||
  428. ($b == $c && $b != $a)){
  429. $result = GameplayRuleEnum::PAIRS;
  430. }
  431. // 判断是否为对子情况
  432. return $result;
  433. }
  434. /**
  435. * @description: 顺子
  436. * @param {int} $a
  437. * @param {int} $b
  438. * @param {int} $c
  439. * @return {*}
  440. */
  441. public static function isStraight($a, $b, $c) {
  442. $result = '';
  443. // 确保输入都是个位数(0-9)
  444. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  445. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  446. return '';
  447. }
  448. // 去重(顺子必须三个不同数字)
  449. if ($a == $b || $a == $c || $b == $c) {
  450. return '';
  451. }
  452. // 检查是否是完全升序或完全降序的连续数字
  453. $numbers = [$a, $b, $c];
  454. sort($numbers); // 排序后检查是否是 x, x+1, x+2
  455. list($x, $y, $z) = $numbers;
  456. // 情况1:升序连续(1,2,3)
  457. $isAscending = ($x + 1 == $y) && ($y + 1 == $z);
  458. // 情况2:降序连续(3,2,1)
  459. $isDescending = ($z + 1 == $y) && ($y + 1 == $x);
  460. if($isAscending || $isDescending){
  461. $result = GameplayRuleEnum::STRAIGHT;
  462. }
  463. return $result;
  464. }
  465. /**
  466. * 获取数字的尾数
  467. * @param int $number 输入数字
  468. * @return int 尾数
  469. */
  470. public static function getLastDigit($number) {
  471. // 确保输入是整数
  472. $number = (int)$number;
  473. // 取绝对值,处理负数情况
  474. $number = abs($number);
  475. // 取模10得到尾数
  476. return $number % 10;
  477. }
  478. /**
  479. * @description: 尾大小
  480. * @param {*} $number
  481. * @return {*}
  482. */
  483. public static function calculateOneSize($number)
  484. {
  485. if($number >= GameplayRuleEnum::ONE_BIG) {
  486. return GameplayRuleEnum::BIG;
  487. }
  488. if($number <= GameplayRuleEnum::ONE_SMALL){
  489. return GameplayRuleEnum::SMALL;
  490. }
  491. }
  492. /**
  493. * @description: 获取段位
  494. * @param {*} $number
  495. * @return {*}
  496. */
  497. public static function getSection($number) {
  498. $result = '';
  499. if ($number >= GameplayRuleEnum::SECTION_1[0] && $number <= GameplayRuleEnum::SECTION_1[1]) {
  500. $result = GameplayRuleEnum::ONE;
  501. } elseif ($number >= GameplayRuleEnum::SECTION_2[0] && $number <= GameplayRuleEnum::SECTION_2[1]) {
  502. $result = GameplayRuleEnum::TWO;
  503. } elseif ($number >= GameplayRuleEnum::SECTION_3[0] && $number <= GameplayRuleEnum::SECTION_3[1]) {
  504. $result = GameplayRuleEnum::THREE;
  505. } elseif ($number >= GameplayRuleEnum::SECTION_4[0] && $number <= GameplayRuleEnum::SECTION_4[1]) {
  506. $result = GameplayRuleEnum::FOUR;
  507. }
  508. return $result; // 不在任何段中
  509. }
  510. /**
  511. * @description: 近期开奖记录
  512. * @return {*}
  513. */
  514. public static function currentLotteryResults($memberId)
  515. {
  516. // $result = self::model()::where('status', self::model()::STATUS_DRAW)->orderBy('id','desc')->take(16)->get();
  517. // $text = "📅 近期开奖记录\n";
  518. // $text .= "====================\n";
  519. // if($result){
  520. // foreach($result as $k => $v){
  521. // $winArr = explode(',',$v->winning_numbers);
  522. // // 组合
  523. // $sum = array_sum($winArr);
  524. // $combo = [];
  525. // $sumOddEven = self::calculateOddEven($sum); // 总和单双
  526. // $sumSize = self::calculateSumSize($sum); // 总和大小
  527. // $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  528. // if(empty($sumExtremeSize)){
  529. // $sumExtremeSize = "-";
  530. // }
  531. // $tail = self::getLastDigit($sum); // 总和尾数
  532. // if($tail == 0){
  533. // $tail = '-'; // 尾数
  534. // }else{
  535. // $tail = '尾'.$tail; // 尾数
  536. // }
  537. // $text .= "回合:{$v->issue_no}期 \n";
  538. // $text .= "结果:".implode('+',explode(',',$v->winning_numbers))."=".array_sum(explode(',',$v->winning_numbers))." \n";
  539. // $text .= "组合:{$sumSize} {$sumOddEven} \n";
  540. // $text .= "极值:{$sumExtremeSize} \n";
  541. // $text .= "尾数:{$tail} \n";
  542. // $text .= "---------------------------\n";
  543. // }
  544. // self::telegram()->sendMessage([
  545. // 'chat_id' => $memberId,
  546. // 'text' => $text,
  547. // ]);
  548. // }else{
  549. // self::telegram()->sendMessage([
  550. // 'chat_id' => $memberId,
  551. // 'text' => "暂无开奖记录",
  552. // ]);
  553. // }
  554. $result = self::model()::where('status', self::model()::STATUS_DRAW)->orderBy('id','desc')->first();
  555. if($result){
  556. if($result->image){
  557. self::telegram()->sendPhoto([
  558. 'chat_id' => $memberId,
  559. 'photo' => InputFile::create(url($result->image)),
  560. ]);
  561. }else{
  562. // if($result->combo){
  563. // self::telegram()->sendMessage([
  564. // 'chat_id' => $memberId,
  565. // 'text' => "",
  566. // ]);
  567. // }else{
  568. self::telegram()->sendMessage([
  569. 'chat_id' => $memberId,
  570. 'text' => "暂无开奖记录",
  571. ]);
  572. // }
  573. }
  574. }
  575. }
  576. // 获取最新的开奖数据
  577. public static function getLatestIssue()
  578. {
  579. $url = "https://ydpc28.co/api/pc28/list";
  580. $result = file_get_contents($url);
  581. $result = json_decode($result,true);
  582. if($result['errorCode'] != 0){
  583. return ['code'=>self::NOT, 'msg'=>'获取最新期号失败'];
  584. }
  585. $nextDrawInfo = $result['data']['nextDrawInfo'];
  586. $startTime = $nextDrawInfo['currentBJTime'];
  587. // if($nextDrawInfo['nextDrawTime'] >= date('H:i:s')) {
  588. // $endTime = date('Y-m-d').' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  589. // }else{
  590. // $endTime = date('Y-m-d',strtotime('+1 day')).' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  591. // }
  592. $endTime = date('Y-m-d H:i:s',strtotime($startTime) + 210);
  593. $list = $result['data']['list'];
  594. $listKey = [];
  595. foreach($list as $k => $v){
  596. $listKey[$v['lotNumber']] = $v;
  597. }
  598. $oldList = self::findAll(['status' => self::model()::STATUS_CLOSE]); // 获取所有封盘的期号
  599. foreach($oldList as $k => $v){
  600. if(isset($listKey[$v->issue_no])){
  601. $issue = $listKey[$v->issue_no];
  602. $winning_numbers = implode(',', str_split((string)$issue['openCode']));
  603. $winArr = array_map('intval',explode(',',$winning_numbers));
  604. // 组合
  605. $sum = array_sum($winArr);
  606. $combo = [];
  607. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  608. $combo[] = $sumOddEven;
  609. $sumSize = self::calculateSumSize($sum); // 总和大小
  610. $combo[] = $sumSize;
  611. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  612. if($sumExtremeSize){
  613. $combo[] = $sumExtremeSize;
  614. }
  615. $sumBaoZi = self::isBaoZi($winArr[0], $winArr[1], $winArr[2]); // 豹子
  616. if($sumBaoZi){
  617. $combo[] = $sumBaoZi;
  618. }
  619. $sumPair = self::isPair($winArr[0], $winArr[1], $winArr[2]); // 对子
  620. if($sumPair){
  621. $combo[] = $sumPair;
  622. }
  623. $sumStraight = self::isStraight($winArr[0], $winArr[1], $winArr[2]); // 顺子
  624. if($sumStraight){
  625. $combo[] = $sumStraight;
  626. }
  627. $tail = self::getLastDigit($sum); // 总和尾数
  628. if($tail == 0 || $tail == 9){
  629. }else{
  630. $combo[] = '尾'.$tail; // 尾数
  631. }
  632. $combo = implode(' ', $combo);
  633. self::lotteryDraw($v->id, $winning_numbers, $combo,'');
  634. }
  635. }
  636. sleep(3); // 等待开奖完成
  637. $latestIssue = $list[0]; // 最后开奖
  638. $new_issue_no = $latestIssue['lotNumber'] + 1; // 新期号
  639. $newInfo = self::findOne(['issue_no' => $new_issue_no]); // 找新的期号
  640. // 不存在
  641. if(!$newInfo){
  642. $res = self::submit([
  643. 'issue_no' => $new_issue_no,
  644. 'status' => self::model()::STATUS_DRAFT,
  645. 'start_time' => $startTime,
  646. 'end_time' => $endTime,
  647. ]);
  648. $id = $res['key']??0;
  649. if($id){
  650. self::betting($id); // 开始下注
  651. }
  652. Cache::set('new_issue_no',$new_issue_no, 10); // 缓存
  653. }
  654. return $result;
  655. }
  656. // 封盘倒数
  657. public static function syncCountdownIssue()
  658. {
  659. $now_date = date('Y-m-d H:i:s',time() + 50); // 提前50秒
  660. $info = self::model()::where('status', self::model()::STATUS_BETTING)->orderBy('end_time','asc')->first();
  661. if($info){
  662. if($info['end_time'] < $now_date){
  663. $replyInfo = KeyboardService::findOne(['button' => '封盘倒数']);
  664. if($replyInfo){
  665. $text = $replyInfo->reply;
  666. $buttons = json_decode($replyInfo->buttons,true);
  667. $image = $replyInfo->image;
  668. if($image){
  669. $image = url($image);
  670. }
  671. if(Cache::has('issue_countdown_'.$info->id)){
  672. }else{
  673. self::bettingGroupNotice($text, $buttons, $image);
  674. Cache::put('issue_countdown_'.$info->id, true, 60); // 缓存50秒,防止多次发送
  675. }
  676. }
  677. }
  678. }
  679. }
  680. // 停止下注
  681. public static function syncCloseIssue()
  682. {
  683. $now_date = date('Y-m-d H:i:s',time() + 30); // 提前30秒
  684. $list = self::findAll(['status' => self::model()::STATUS_BETTING]);
  685. foreach($list as $k => $v){
  686. if($v['end_time'] < $now_date){
  687. self::closeBetting($v->id);
  688. }
  689. }
  690. }
  691. // 生成开奖图片
  692. public static function lotteryImage($issue_no)
  693. {
  694. $list = self::model()::where('issue_no','<=',$issue_no)->where(self::getWhere(['status' => self::model()::STATUS_DRAW]))->orderBy('issue_no','desc')->take(20)->get();
  695. $records = $list->toArray();
  696. foreach($records as $k => $v){
  697. $winning_numbers = explode(',',$v['winning_numbers']);
  698. $v['winning_numbers'] = $winning_numbers;
  699. // 组合
  700. $sum = array_sum($winning_numbers);
  701. $v['sum'] = $sum;
  702. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  703. $sumSize = self::calculateSumSize($sum); // 总和大小
  704. $v['combo'] = $sumSize . ' ' . $sumOddEven;
  705. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  706. if(!$sumExtremeSize){
  707. $sumExtremeSize = '-';
  708. }
  709. $v['extreme'] = $sumExtremeSize;
  710. $tail = self::getLastDigit($sum); // 总和尾数
  711. if($tail === 0 || $tail === 9){
  712. $tailStr = '-';
  713. }else{
  714. $tailStr = '尾' . $tail;
  715. }
  716. $v['tail'] = $tailStr;
  717. $records[$k] = $v;
  718. }
  719. $service = new LotteryImageService();
  720. $url = $service->generate($records);
  721. self::model()::where('issue_no', $issue_no)->update(['image' => $url]);
  722. return $url;
  723. }
  724. }