IssueService.php 26 KB

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