IssueService.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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. return ['code'=>self::YES, 'msg'=>'封盘成功'];
  190. }
  191. /**
  192. * @description: 开奖
  193. * @param {*} $id
  194. * @param {*} $winning_numbers 开奖号码
  195. * @param {*} $combo 开奖组合
  196. * @param {*} $recordImage 开奖图片
  197. * @return {*}
  198. */
  199. public static function lotteryDraw($id, $winning_numbers ,$combo ,$recordImage)
  200. {
  201. $info = self::findOne(['id'=>$id]);
  202. if(!$info){
  203. return ['code'=>self::NOT, 'msg'=>'期号不存在'];
  204. }
  205. if($info->status != self::model()::STATUS_CLOSE){
  206. return ['code'=>self::NOT, 'msg'=>'期号状态不正确'];
  207. }
  208. // 计算中奖
  209. $awards = self::award(explode(',',$winning_numbers));
  210. DB::beginTransaction();
  211. try {
  212. self::lotteryImage($info->issue_no);
  213. $info->status = self::model()::STATUS_DRAW;
  214. $info->winning_numbers = $winning_numbers;
  215. $info->combo = $combo;
  216. $info->image = $recordImage;
  217. $info->save();
  218. $replyInfo = KeyboardService::findOne(['button' => '本期开奖']);
  219. if($replyInfo){
  220. $text = $replyInfo->reply;
  221. $text .= "\n";
  222. $text .= $info->issue_no."期 ".implode('+',explode(',',$winning_numbers))."=".array_sum(explode(',',$winning_numbers))." ".$combo;
  223. $buttons = json_decode($replyInfo->buttons,true);
  224. $image = $replyInfo->image;
  225. if($image){
  226. $image = url($image);
  227. }
  228. if(empty($buttons)){
  229. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  230. $buttons[] = [['text' => '✅ 唯一客服', 'callback_data' => "", 'url' => "https://t.me/{$serviceAccount}"]];
  231. }
  232. self::bettingGroupNotice($text, $buttons, $image);
  233. }
  234. if($recordImage){
  235. self::bettingGroupNotice('', [], url($recordImage));
  236. }
  237. BetService::betSettled($info->issue_no, $awards);
  238. DB::commit();
  239. return ['code'=>self::YES, 'msg'=>'开奖成功'];
  240. } catch (\Exception $e) {
  241. DB::rollBack();
  242. Log::error('开奖失败: '.$e->getMessage());
  243. return ['code'=>self::NOT, 'msg'=>'开奖失败'];
  244. }
  245. }
  246. /**
  247. * @description: 获取中奖的奖项
  248. * @param {*} $winning_numbers
  249. * @return {*}
  250. */
  251. public static function award($winning_numbers)
  252. {
  253. $result = [];
  254. // 组合
  255. $sum = array_sum($winning_numbers);
  256. $section = self::getSection($sum); // 总和段位
  257. $result[] = $section;
  258. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  259. $result[] = $sumOddEven;
  260. $sumSize = self::calculateSumSize($sum); // 总和大小
  261. $result[] = $sumSize;
  262. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  263. if($sumExtremeSize){
  264. $result[] = $sumExtremeSize;
  265. }
  266. $sumCao = $sum . '操'; // 总和数字
  267. $result[] = $sumCao;
  268. $sumCombo = $sumSize. $sumOddEven; // 总和大小单双组合
  269. $result[] = $sumCombo;
  270. $sumBaoZi = self::isBaoZi($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 豹子
  271. if($sumBaoZi){
  272. $result[] = $sumBaoZi;
  273. }
  274. $sumPair = self::isPair($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 对子
  275. if($sumPair){
  276. $result[] = $sumPair;
  277. }
  278. $sumStraight = self::isStraight($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 顺子
  279. if($sumStraight){
  280. $result[] = $sumStraight;
  281. }
  282. $tail = self::getLastDigit($sum); // 总和尾数
  283. $result[] = $tail . '尾'; // 尾数
  284. $tailOddEven = self::calculateOddEven($tail); // 尾数单双
  285. $result[] = '尾'.$tailOddEven;
  286. $tailSize = self::calculateOneSize($tail); // 尾数大小
  287. $result[] = '尾'.$tailSize;
  288. $tailCombo = '尾'.$tailSize . $tailOddEven; // 尾数大小单双组合
  289. $result[] = $tailCombo;
  290. $numA = $winning_numbers[0]; // A球
  291. $result[] = $numA.'A';
  292. $numAOddEven = self::calculateOddEven($numA); // A球单双
  293. $result[] = 'A'.$numAOddEven;
  294. $numASize = self::calculateOneSize($numA); // A球大小
  295. $result[] = 'A'.$numASize;
  296. $result[] = 'A'.$numASize.$numAOddEven; // A球大小单双组合
  297. $numB = $winning_numbers[1]; // B球
  298. $result[] = $numB.'B';
  299. $numBOddEven = self::calculateOddEven($numB); // B球
  300. $result[] = 'B'.$numBOddEven;
  301. $numBSize = self::calculateOneSize($numB); // B球大小
  302. $result[] = 'B'.$numBSize;
  303. $result[] = 'B'.$numBSize.$numBOddEven; // B球大小单双组合
  304. $numC = $winning_numbers[2];
  305. $result[] = $numC.'C';
  306. $numCOddEven = self::calculateOddEven($numC); // C球单双
  307. $result[] = 'C'.$numCOddEven;
  308. $numCSize = self::calculateOneSize($numC); // C球大小
  309. $result[] = 'C'.$numCSize;
  310. $result[] = 'C'.$numCSize.$numCOddEven; // C球大小单双组合
  311. return $result;
  312. }
  313. /**
  314. * @description: 算单双
  315. * @param {*} $number
  316. * @return {*}
  317. */
  318. public static function calculateOddEven($number)
  319. {
  320. if ($number & 1) {
  321. return GameplayRuleEnum::SINGLE;
  322. } else {
  323. return GameplayRuleEnum::DOUBLE;
  324. }
  325. }
  326. /**
  327. * @description: 总和大小
  328. * @param {*} $number
  329. * @return {*}
  330. */
  331. public static function calculateSumSize($number)
  332. {
  333. if($number >= GameplayRuleEnum::SUM_BIG) {
  334. return GameplayRuleEnum::BIG;
  335. }
  336. if($number <= GameplayRuleEnum::SUM_SMALL){
  337. return GameplayRuleEnum::SMALL;
  338. }
  339. }
  340. /**
  341. * @description: 总和极值
  342. * @param {*} $number
  343. * @return {*}
  344. */
  345. public static function calculateSumExtremeSize($number)
  346. {
  347. $result = '';
  348. if($number >= GameplayRuleEnum::SUM_EXTREME_BIG) {
  349. $result = GameplayRuleEnum::EXTREME_BIG;
  350. }
  351. if($number <= GameplayRuleEnum::SUM_EXTREME_SMALL){
  352. $result = GameplayRuleEnum::EXTREME_SMALL;
  353. }
  354. return $result;
  355. }
  356. /**
  357. * @description: 豹子
  358. * @param {int} $a
  359. * @param {int} $b
  360. * @param {int} $c
  361. * @return {*}
  362. */
  363. public static function isBaoZi(int $a, int $b, int $c)
  364. {
  365. $result = '';
  366. if($a === $b && $b === $c){
  367. $result = GameplayRuleEnum::BAO_ZI;
  368. }
  369. return $result;
  370. }
  371. /**
  372. * @description: 对子
  373. * @param {int} $a
  374. * @param {int} $b
  375. * @param {int} $c
  376. * @return {*}
  377. */
  378. public static function isPair($a, $b, $c) {
  379. $result = '';
  380. // 确保输入都是个位数
  381. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  382. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  383. return ''; // 或者抛出异常
  384. }
  385. if(($a == $b && $a != $c) ||
  386. ($a == $c && $a != $b) ||
  387. ($b == $c && $b != $a)){
  388. $result = GameplayRuleEnum::PAIRS;
  389. }
  390. // 判断是否为对子情况
  391. return $result;
  392. }
  393. /**
  394. * @description: 顺子
  395. * @param {int} $a
  396. * @param {int} $b
  397. * @param {int} $c
  398. * @return {*}
  399. */
  400. public static function isStraight($a, $b, $c) {
  401. $result = '';
  402. // 确保输入都是个位数(0-9)
  403. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  404. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  405. return '';
  406. }
  407. // 去重(顺子必须三个不同数字)
  408. if ($a == $b || $a == $c || $b == $c) {
  409. return '';
  410. }
  411. // 检查是否是完全升序或完全降序的连续数字
  412. $numbers = [$a, $b, $c];
  413. sort($numbers); // 排序后检查是否是 x, x+1, x+2
  414. list($x, $y, $z) = $numbers;
  415. // 情况1:升序连续(1,2,3)
  416. $isAscending = ($x + 1 == $y) && ($y + 1 == $z);
  417. // 情况2:降序连续(3,2,1)
  418. $isDescending = ($z + 1 == $y) && ($y + 1 == $x);
  419. if($isAscending || $isDescending){
  420. $result = GameplayRuleEnum::STRAIGHT;
  421. }
  422. return $result;
  423. }
  424. /**
  425. * 获取数字的尾数
  426. * @param int $number 输入数字
  427. * @return int 尾数
  428. */
  429. public static function getLastDigit($number) {
  430. // 确保输入是整数
  431. $number = (int)$number;
  432. // 取绝对值,处理负数情况
  433. $number = abs($number);
  434. // 取模10得到尾数
  435. return $number % 10;
  436. }
  437. /**
  438. * @description: 尾大小
  439. * @param {*} $number
  440. * @return {*}
  441. */
  442. public static function calculateOneSize($number)
  443. {
  444. if($number >= GameplayRuleEnum::ONE_BIG) {
  445. return GameplayRuleEnum::BIG;
  446. }
  447. if($number <= GameplayRuleEnum::ONE_SMALL){
  448. return GameplayRuleEnum::SMALL;
  449. }
  450. }
  451. /**
  452. * @description: 获取段位
  453. * @param {*} $number
  454. * @return {*}
  455. */
  456. public static function getSection($number) {
  457. $result = '';
  458. if ($number >= GameplayRuleEnum::SECTION_1[0] && $number <= GameplayRuleEnum::SECTION_1[1]) {
  459. $result = GameplayRuleEnum::ONE;
  460. } elseif ($number >= GameplayRuleEnum::SECTION_2[0] && $number <= GameplayRuleEnum::SECTION_2[1]) {
  461. $result = GameplayRuleEnum::TWO;
  462. } elseif ($number >= GameplayRuleEnum::SECTION_3[0] && $number <= GameplayRuleEnum::SECTION_3[1]) {
  463. $result = GameplayRuleEnum::THREE;
  464. } elseif ($number >= GameplayRuleEnum::SECTION_4[0] && $number <= GameplayRuleEnum::SECTION_4[1]) {
  465. $result = GameplayRuleEnum::FOUR;
  466. }
  467. return $result; // 不在任何段中
  468. }
  469. /**
  470. * @description: 近期开奖记录
  471. * @return {*}
  472. */
  473. public static function currentLotteryResults($memberId)
  474. {
  475. // $result = self::model()::where('status', self::model()::STATUS_DRAW)->orderBy('id','desc')->take(16)->get();
  476. // $text = "📅 近期开奖记录\n";
  477. // $text .= "====================\n";
  478. // if($result){
  479. // foreach($result as $k => $v){
  480. // $winArr = explode(',',$v->winning_numbers);
  481. // // 组合
  482. // $sum = array_sum($winArr);
  483. // $combo = [];
  484. // $sumOddEven = self::calculateOddEven($sum); // 总和单双
  485. // $sumSize = self::calculateSumSize($sum); // 总和大小
  486. // $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  487. // if(empty($sumExtremeSize)){
  488. // $sumExtremeSize = "-";
  489. // }
  490. // $tail = self::getLastDigit($sum); // 总和尾数
  491. // if($tail == 0){
  492. // $tail = '-'; // 尾数
  493. // }else{
  494. // $tail = '尾'.$tail; // 尾数
  495. // }
  496. // $text .= "回合:{$v->issue_no}期 \n";
  497. // $text .= "结果:".implode('+',explode(',',$v->winning_numbers))."=".array_sum(explode(',',$v->winning_numbers))." \n";
  498. // $text .= "组合:{$sumSize} {$sumOddEven} \n";
  499. // $text .= "极值:{$sumExtremeSize} \n";
  500. // $text .= "尾数:{$tail} \n";
  501. // $text .= "---------------------------\n";
  502. // }
  503. // self::telegram()->sendMessage([
  504. // 'chat_id' => $memberId,
  505. // 'text' => $text,
  506. // ]);
  507. // }else{
  508. // self::telegram()->sendMessage([
  509. // 'chat_id' => $memberId,
  510. // 'text' => "暂无开奖记录",
  511. // ]);
  512. // }
  513. $result = self::model()::where('status', self::model()::STATUS_DRAW)->orderBy('id','desc')->first();
  514. if($result){
  515. if($result->image){
  516. self::telegram()->sendPhoto([
  517. 'chat_id' => $memberId,
  518. 'photo' => InputFile::create(url($result->image)),
  519. ]);
  520. }else{
  521. // if($result->combo){
  522. // self::telegram()->sendMessage([
  523. // 'chat_id' => $memberId,
  524. // 'text' => "",
  525. // ]);
  526. // }else{
  527. self::telegram()->sendMessage([
  528. 'chat_id' => $memberId,
  529. 'text' => "暂无开奖记录",
  530. ]);
  531. // }
  532. }
  533. }
  534. }
  535. // 获取最新的开奖数据
  536. public static function getLatestIssue()
  537. {
  538. $url = "https://ydpc28.co/api/pc28/list";
  539. $result = file_get_contents($url);
  540. $result = json_decode($result,true);
  541. if($result['errorCode'] != 0){
  542. return ['code'=>self::NOT, 'msg'=>'获取最新期号失败'];
  543. }
  544. $nextDrawInfo = $result['data']['nextDrawInfo'];
  545. if($nextDrawInfo['nextDrawTime'] >= date('H:i:s')) {
  546. $endTime = date('Y-m-d').' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  547. }else{
  548. $endTime = date('Y-m-d',strtotime('+1 day')).' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  549. }
  550. $list = $result['data']['list'];
  551. $listKey = [];
  552. foreach($list as $k => $v){
  553. $listKey[$v['lotNumber']] = $v;
  554. }
  555. $oldList = self::findAll(['status' => self::model()::STATUS_CLOSE]); // 获取所有封盘的期号
  556. foreach($oldList as $k => $v){
  557. if(isset($listKey[$v->issue_no])){
  558. $issue = $listKey[$v->issue_no];
  559. $winning_numbers = implode(',', str_split((string)$issue['openCode']));
  560. $winArr = explode(',',$winning_numbers);
  561. // 组合
  562. $sum = array_sum($winArr);
  563. $combo = [];
  564. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  565. $combo[] = $sumOddEven;
  566. $sumSize = self::calculateSumSize($sum); // 总和大小
  567. $combo[] = $sumSize;
  568. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  569. if($sumExtremeSize){
  570. $combo[] = $sumExtremeSize;
  571. }
  572. $sumBaoZi = self::isBaoZi($winArr[0], $winArr[1], $winArr[2]); // 豹子
  573. if($sumBaoZi){
  574. $combo[] = $sumBaoZi;
  575. }
  576. $sumPair = self::isPair($winArr[0], $winArr[1], $winArr[2]); // 对子
  577. if($sumPair){
  578. $combo[] = $sumPair;
  579. }
  580. $sumStraight = self::isStraight($winArr[0], $winArr[1], $winArr[2]); // 顺子
  581. if($sumStraight){
  582. $combo[] = $sumStraight;
  583. }
  584. $tail = self::getLastDigit($sum); // 总和尾数
  585. if($tail == 0){
  586. }else{
  587. $combo[] = '尾'.$tail; // 尾数
  588. }
  589. $combo = implode(' ', $combo);
  590. self::lotteryDraw($v->id, $winning_numbers, $combo,'');
  591. }
  592. }
  593. $latestIssue = $list[0]; // 最后开奖
  594. $new_issue_no = $latestIssue['lotNumber'] + 1; // 新期号
  595. $newInfo = self::findOne(['issue_no' => $new_issue_no]); // 找新的期号
  596. // 不存在
  597. if(!$newInfo){
  598. $res = self::submit([
  599. 'issue_no' => $new_issue_no,
  600. 'status' => self::model()::STATUS_DRAFT,
  601. 'start_time' => date('Y-m-d H:i:s'),
  602. 'end_time' => $endTime,
  603. ]);
  604. $id = $res['key']??0;
  605. if($id){
  606. self::betting($id); // 开始下注
  607. }
  608. }
  609. return $result;
  610. }
  611. // 停止下注
  612. public static function syncCloseIssue()
  613. {
  614. $now_date = date('Y-m-d H:i:s',time() + 20); // 提前20秒
  615. $list = self::findAll(['status' => self::model()::STATUS_BETTING]);
  616. foreach($list as $k => $v){
  617. if($v['end_time'] < $now_date){
  618. self::closeBetting($v->id);
  619. }
  620. }
  621. }
  622. // 生成开奖图片
  623. public static function lotteryImage($issue_no)
  624. {
  625. $list = self::model()::where('issue_no','<',$issue_no)->where(self::getWhere(['status' => self::model()::STATUS_DRAW]))->orderBy('issue_no','desc')->take(20)->get();
  626. $records = $list->toArray();
  627. foreach($records as $k => $v){
  628. $winning_numbers = explode(',',$v['winning_numbers']);
  629. $v['winning_numbers'] = $winning_numbers;
  630. // 组合
  631. $sum = array_sum($winning_numbers);
  632. $v['sum'] = $sum;
  633. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  634. $sumSize = self::calculateSumSize($sum); // 总和大小
  635. $v['combo'] = $sumSize . ' ' . $sumOddEven;
  636. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  637. if(!$sumExtremeSize){
  638. $sumExtremeSize = '-';
  639. }
  640. $v['extreme'] = $sumExtremeSize;
  641. $tail = self::getLastDigit($sum); // 总和尾数
  642. if($tail === 0){
  643. $tailStr = '-';
  644. }else{
  645. $tailStr = '尾' . $tail;
  646. }
  647. $v['tail'] = $tailStr;
  648. $records[$k] = $v;
  649. }
  650. $service = new LotteryImageService();
  651. $url = $service->generate($records);
  652. self::model()::where('issue_no', $issue_no)->update(['image' => $url]);
  653. return $url;
  654. }
  655. }