IssueService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\Issue;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\Log;
  9. use App\Services\GameplayRuleService;
  10. use App\Constants\GameplayRuleEnum;
  11. use App\Http\Controllers\admin\Game;
  12. /**
  13. * 投注
  14. */
  15. class IssueService extends BaseService
  16. {
  17. /**
  18. * @description: 模型
  19. * @return {string}
  20. */
  21. public static function model() :string
  22. {
  23. return Issue::class;
  24. }
  25. /**
  26. * @description: 枚举
  27. * @return {*}
  28. */
  29. public static function enum() :string
  30. {
  31. return '';
  32. }
  33. /**
  34. * @description: 获取查询条件
  35. * @param {array} $search 查询内容
  36. * @return {array}
  37. */
  38. public static function getWhere(array $search = []) :array
  39. {
  40. $where = [];
  41. if(isset($search['issue_no']) && !empty($search['issue_no'])){
  42. $where[] = ['issue_no', '=', $search['issue_no']];
  43. }
  44. if(isset($search['id']) && !empty($search['id'])){
  45. $where[] = ['id', '=', $search['id']];
  46. }
  47. if(isset($search['status']) && !empty($search['status'])){
  48. $where[] = ['status', '=', $search['status']];
  49. }
  50. return $where;
  51. }
  52. /**
  53. * @description: 查询单条数据
  54. * @param array $search
  55. * @return \App\Models\Coin|null
  56. */
  57. public static function findOne(array $search): ?Issue
  58. {
  59. return self::model()::where(self::getWhere($search))->first();
  60. }
  61. /**
  62. * @description: 查询所有数据
  63. * @param array $search
  64. * @return \Illuminate\Database\Eloquent\Collection
  65. */
  66. public static function findAll(array $search = [])
  67. {
  68. return self::model()::where(self::getWhere($search))->get();
  69. }
  70. /**
  71. * @description: 分页查询
  72. * @param array $search
  73. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  74. */
  75. public static function paginate(array $search = [])
  76. {
  77. $limit = isset($search['limit'])?$search['limit']:15;
  78. $paginator = self::model()::where(self::getWhere($search))->orderBy('issue_no','desc')->paginate($limit);
  79. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  80. }
  81. /**
  82. * @description:
  83. * @param {*} $params
  84. * @return {*}
  85. */
  86. public static function submit($params = [])
  87. {
  88. $result = false;
  89. $msg['code'] = self::NOT;
  90. $msg['msg'] = '';
  91. // 2. 判断是否是更新
  92. if (!empty($params['id'])) {
  93. // 更新
  94. $info = self::findOne(['id'=>$params['id']] );
  95. if (!$info) {
  96. $msg['msg'] = '期号不存在!';
  97. }else{
  98. $result = $info->update($params);
  99. $id = $params['id'];
  100. }
  101. } else {
  102. // 创建
  103. $result = $info = self::model()::create($params);
  104. $id = $result->id;
  105. }
  106. if($result){
  107. $msg['code'] = self::YES;
  108. $msg['msg'] = '设置成功';
  109. }else{
  110. $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
  111. }
  112. return $msg;
  113. }
  114. /**
  115. * @description: 开始下注
  116. * @param {*} $id
  117. * @return {*}
  118. */
  119. public static function betting($id)
  120. {
  121. $info = self::findOne(['id'=>$id]);
  122. if(!$info){
  123. return ['code'=>self::NOT, 'msg'=>'期号不存在'];
  124. }
  125. if(!in_array($info->status, [self::model()::STATUS_DRAFT,self::model()::STATUS_BETTING])){
  126. return ['code'=>self::NOT, 'msg'=>'期号状态不正确'];
  127. }
  128. $info->status = self::model()::STATUS_BETTING;
  129. $info->save();
  130. return ['code'=>self::YES, 'msg'=>'开始下注'];
  131. }
  132. /**
  133. * @description: 封盘
  134. * @param {*} $id
  135. * @return {*}
  136. */
  137. public static function closeBetting($id)
  138. {
  139. $info = self::findOne(['id'=>$id]);
  140. if(!$info){
  141. return ['code'=>self::NOT, 'msg'=>'期号不存在'];
  142. }
  143. if($info->status != self::model()::STATUS_BETTING){
  144. return ['code'=>self::NOT, 'msg'=>'期号状态不正确'];
  145. }
  146. $info->status = self::model()::STATUS_CLOSE;
  147. $info->save();
  148. return ['code'=>self::YES, 'msg'=>'封盘成功'];
  149. }
  150. /**
  151. * @description: 开奖
  152. * @param {*} $id
  153. * @param {*} $winning_numbers
  154. * @return {*}
  155. */
  156. public static function lotteryDraw($id, $winning_numbers)
  157. {
  158. $info = self::findOne(['id'=>$id]);
  159. if(!$info){
  160. return ['code'=>self::NOT, 'msg'=>'期号不存在'];
  161. }
  162. if($info->status != self::model()::STATUS_CLOSE){
  163. return ['code'=>self::NOT, 'msg'=>'期号状态不正确'];
  164. }
  165. // 计算中奖
  166. $awards = self::award(explode(',',$winning_numbers));
  167. DB::beginTransaction();
  168. try {
  169. $info->status = self::model()::STATUS_DRAW;
  170. $info->winning_numbers = $winning_numbers;
  171. $info->save();
  172. BetService::betSettled($info->issue_no, $awards);
  173. DB::commit();
  174. return ['code'=>self::YES, 'msg'=>'开奖成功'];
  175. } catch (\Exception $e) {
  176. DB::rollBack();
  177. Log::error('开奖失败: '.$e->getMessage());
  178. return ['code'=>self::NOT, 'msg'=>'开奖失败'];
  179. }
  180. }
  181. /**
  182. * @description: 获取中奖的奖项
  183. * @param {*} $winning_numbers
  184. * @return {*}
  185. */
  186. public static function award($winning_numbers)
  187. {
  188. $result = [];
  189. // 组合
  190. $sum = array_sum($winning_numbers);
  191. $section = self::getSection($sum); // 总和段位
  192. $result[] = $section;
  193. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  194. $result[] = $sumOddEven;
  195. $sumSize = self::calculateSumSize($sum); // 总和大小
  196. $result[] = $sumSize;
  197. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  198. if($sumExtremeSize){
  199. $result[] = $sumExtremeSize;
  200. }
  201. $sumCao = $sum . '操'; // 总和数字
  202. $result[] = $sumCao;
  203. $sumCombo = $sumSize. $sumOddEven; // 总和大小单双组合
  204. $result[] = $sumCombo;
  205. $sumBaoZi = self::isBaoZi($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 豹子
  206. if($sumBaoZi){
  207. $result[] = $sumBaoZi;
  208. }
  209. $sumPair = self::isPair($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 对子
  210. if($sumPair){
  211. $result[] = $sumPair;
  212. }
  213. $sumStraight = self::isStraight($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 顺子
  214. if($sumStraight){
  215. $result[] = $sumStraight;
  216. }
  217. $tail = self::getLastDigit($sum); // 总和尾数
  218. $result[] = $tail . '尾'; // 尾数
  219. $tailOddEven = self::calculateOddEven($tail); // 尾数单双
  220. $result[] = '尾'.$tailOddEven;
  221. $tailSize = self::calculateOneSize($tail); // 尾数大小
  222. $result[] = '尾'.$tailSize;
  223. $tailCombo = '尾'.$tailSize . $tailOddEven; // 尾数大小单双组合
  224. $result[] = $tailCombo;
  225. $numA = $winning_numbers[0]; // A球
  226. $result[] = $numA.'A';
  227. $numAOddEven = self::calculateOddEven($numA); // A球单双
  228. $result[] = 'A'.$numAOddEven;
  229. $numASize = self::calculateOneSize($numA); // A球大小
  230. $result[] = 'A'.$numASize;
  231. $numB = $winning_numbers[1]; // B球
  232. $result[] = $numB.'B';
  233. $numBOddEven = self::calculateOddEven($numB); // B球
  234. $result[] = 'B'.$numBOddEven;
  235. $numBSize = self::calculateOneSize($numB); // B球大小
  236. $result[] = 'B'.$numBSize;
  237. $numC = $winning_numbers[2];
  238. $result[] = $numC.'C';
  239. $numCOddEven = self::calculateOddEven($numC); // C球单双
  240. $result[] = 'C'.$numCOddEven;
  241. $numCSize = self::calculateOneSize($numC); // C球大小
  242. $result[] = 'C'.$numCSize;
  243. return $result;
  244. }
  245. /**
  246. * @description: 算单双
  247. * @param {*} $number
  248. * @return {*}
  249. */
  250. public static function calculateOddEven($number)
  251. {
  252. if ($number & 1) {
  253. return GameplayRuleEnum::SINGLE;
  254. } else {
  255. return GameplayRuleEnum::DOUBLE;
  256. }
  257. }
  258. /**
  259. * @description: 总和大小
  260. * @param {*} $number
  261. * @return {*}
  262. */
  263. public static function calculateSumSize($number)
  264. {
  265. if($number >= GameplayRuleEnum::SUM_BIG) {
  266. return GameplayRuleEnum::BIG;
  267. }
  268. if($number <= GameplayRuleEnum::SUM_SMALL){
  269. return GameplayRuleEnum::SMALL;
  270. }
  271. }
  272. /**
  273. * @description: 总和极值
  274. * @param {*} $number
  275. * @return {*}
  276. */
  277. public static function calculateSumExtremeSize($number)
  278. {
  279. $result = '';
  280. if($number >= GameplayRuleEnum::SUM_EXTREME_BIG) {
  281. $result = GameplayRuleEnum::EXTREME_BIG;
  282. }
  283. if($number <= GameplayRuleEnum::SUM_EXTREME_SMALL){
  284. $result = GameplayRuleEnum::EXTREME_SMALL;
  285. }
  286. return $result;
  287. }
  288. /**
  289. * @description: 豹子
  290. * @param {int} $a
  291. * @param {int} $b
  292. * @param {int} $c
  293. * @return {*}
  294. */
  295. public static function isBaoZi(int $a, int $b, int $c)
  296. {
  297. $result = '';
  298. if($a === $b && $b === $c){
  299. $result = GameplayRuleEnum::BAO_ZI;
  300. }
  301. return $result;
  302. }
  303. /**
  304. * @description: 对子
  305. * @param {int} $a
  306. * @param {int} $b
  307. * @param {int} $c
  308. * @return {*}
  309. */
  310. public static function isPair($a, $b, $c) {
  311. $result = '';
  312. // 确保输入都是个位数
  313. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  314. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  315. return ''; // 或者抛出异常
  316. }
  317. if(($a == $b && $a != $c) ||
  318. ($a == $c && $a != $b) ||
  319. ($b == $c && $b != $a)){
  320. $result = GameplayRuleEnum::PAIRS;
  321. }
  322. // 判断是否为对子情况
  323. return $result;
  324. }
  325. /**
  326. * @description: 顺子
  327. * @param {int} $a
  328. * @param {int} $b
  329. * @param {int} $c
  330. * @return {*}
  331. */
  332. public static function isStraight($a, $b, $c) {
  333. $result = '';
  334. // 确保输入都是个位数(0-9)
  335. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  336. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  337. return '';
  338. }
  339. // 去重(顺子必须三个不同数字)
  340. if ($a == $b || $a == $c || $b == $c) {
  341. return '';
  342. }
  343. // 检查是否是完全升序或完全降序的连续数字
  344. $numbers = [$a, $b, $c];
  345. sort($numbers); // 排序后检查是否是 x, x+1, x+2
  346. list($x, $y, $z) = $numbers;
  347. // 情况1:升序连续(1,2,3)
  348. $isAscending = ($x + 1 == $y) && ($y + 1 == $z);
  349. // 情况2:降序连续(3,2,1)
  350. $isDescending = ($z + 1 == $y) && ($y + 1 == $x);
  351. if($isAscending || $isDescending){
  352. $result = GameplayRuleEnum::STRAIGHT;
  353. }
  354. return $result;
  355. }
  356. /**
  357. * 获取数字的尾数
  358. * @param int $number 输入数字
  359. * @return int 尾数
  360. */
  361. public static function getLastDigit($number) {
  362. // 确保输入是整数
  363. $number = (int)$number;
  364. // 取绝对值,处理负数情况
  365. $number = abs($number);
  366. // 取模10得到尾数
  367. return $number % 10;
  368. }
  369. /**
  370. * @description: 尾大小
  371. * @param {*} $number
  372. * @return {*}
  373. */
  374. public static function calculateOneSize($number)
  375. {
  376. if($number >= GameplayRuleEnum::ONE_BIG) {
  377. return GameplayRuleEnum::BIG;
  378. }
  379. if($number <= GameplayRuleEnum::ONE_SMALL){
  380. return GameplayRuleEnum::SMALL;
  381. }
  382. }
  383. /**
  384. * @description: 获取段位
  385. * @param {*} $number
  386. * @return {*}
  387. */
  388. public static function getSection($number) {
  389. $result = '';
  390. if ($number >= GameplayRuleEnum::SECTION_1[0] && $number <= GameplayRuleEnum::SECTION_1[1]) {
  391. $result = GameplayRuleEnum::ONE;
  392. } elseif ($number >= GameplayRuleEnum::SECTION_2[0] && $number <= GameplayRuleEnum::SECTION_2[1]) {
  393. $result = GameplayRuleEnum::TWO;
  394. } elseif ($number >= GameplayRuleEnum::SECTION_3[0] && $number <= GameplayRuleEnum::SECTION_3[1]) {
  395. $result = GameplayRuleEnum::THREE;
  396. } elseif ($number >= GameplayRuleEnum::SECTION_4[0] && $number <= GameplayRuleEnum::SECTION_4[1]) {
  397. $result = GameplayRuleEnum::FOUR;
  398. }
  399. return $result; // 不在任何段中
  400. }
  401. }