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. DB::commit();
  173. return ['code'=>self::YES, 'msg'=>'开奖成功'];
  174. } catch (\Exception $e) {
  175. DB::rollBack();
  176. Log::error('开奖失败: '.$e->getMessage());
  177. return ['code'=>self::NOT, 'msg'=>'开奖失败'];
  178. }
  179. }
  180. /**
  181. * @description: 获取中奖的奖项
  182. * @param {*} $winning_numbers
  183. * @return {*}
  184. */
  185. public static function award($winning_numbers)
  186. {
  187. $result = [];
  188. // 组合
  189. $sum = array_sum($winning_numbers);
  190. $section = self::getSection($sum); // 总和段位
  191. $result[] = $section;
  192. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  193. $result[] = $sumOddEven;
  194. $sumSize = self::calculateSumSize($sum); // 总和大小
  195. $result[] = $sumSize;
  196. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  197. if($sumExtremeSize){
  198. $result[] = $sumExtremeSize;
  199. }
  200. $sumCao = $sum . '操'; // 总和数字
  201. $result[] = $sumCao;
  202. $sumCombo = $sumSize. $sumOddEven; // 总和大小单双组合
  203. $result[] = $sumCombo;
  204. $sumBaoZi = self::isBaoZi($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 豹子
  205. if($sumBaoZi){
  206. $result[] = $sumBaoZi;
  207. }
  208. $sumPair = self::isPair($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 对子
  209. if($sumPair){
  210. $result[] = $sumPair;
  211. }
  212. $sumStraight = self::isStraight($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 顺子
  213. if($sumStraight){
  214. $result[] = $sumStraight;
  215. }
  216. $tail = self::getLastDigit($sum); // 总和尾数
  217. $result[] = $tail . '尾'; // 尾数
  218. $tailOddEven = self::calculateOddEven($tail); // 尾数单双
  219. $result[] = '尾'.$tailOddEven;
  220. $tailSize = self::calculateOneSize($tail); // 尾数大小
  221. $result[] = '尾'.$tailSize;
  222. $tailCombo = '尾'.$tailSize . $tailOddEven; // 尾数大小单双组合
  223. $result[] = $tailCombo;
  224. $numA = $winning_numbers[0]; // A球
  225. $result[] = $numA.'A';
  226. $numAOddEven = self::calculateOddEven($numA); // A球单双
  227. $result[] = 'A'.$numAOddEven;
  228. $numASize = self::calculateOneSize($numA); // A球大小
  229. $result[] = 'A'.$numASize;
  230. $numB = $winning_numbers[1]; // B球
  231. $result[] = $numB.'B';
  232. $numBOddEven = self::calculateOddEven($numB); // B球
  233. $result[] = 'B'.$numBOddEven;
  234. $numBSize = self::calculateOneSize($numB); // B球大小
  235. $result[] = 'B'.$numBSize;
  236. $numC = $winning_numbers[2];
  237. $result[] = $numC.'C';
  238. $numCOddEven = self::calculateOddEven($numC); // C球单双
  239. $result[] = 'C'.$numCOddEven;
  240. $numCSize = self::calculateOneSize($numC); // C球大小
  241. $result[] = 'C'.$numCSize;
  242. return $result;
  243. }
  244. /**
  245. * @description: 算单双
  246. * @param {*} $number
  247. * @return {*}
  248. */
  249. public static function calculateOddEven($number)
  250. {
  251. if ($number & 1) {
  252. return GameplayRuleEnum::SINGLE;
  253. } else {
  254. return GameplayRuleEnum::DOUBLE;
  255. }
  256. }
  257. /**
  258. * @description: 总和大小
  259. * @param {*} $number
  260. * @return {*}
  261. */
  262. public static function calculateSumSize($number)
  263. {
  264. if($number >= GameplayRuleEnum::SUM_BIG) {
  265. return GameplayRuleEnum::BIG;
  266. }
  267. if($number <= GameplayRuleEnum::SUM_SMALL){
  268. return GameplayRuleEnum::SMALL;
  269. }
  270. }
  271. /**
  272. * @description: 总和极值
  273. * @param {*} $number
  274. * @return {*}
  275. */
  276. public static function calculateSumExtremeSize($number)
  277. {
  278. $result = '';
  279. if($number >= GameplayRuleEnum::SUM_EXTREME_BIG) {
  280. $result = GameplayRuleEnum::EXTREME_BIG;
  281. }
  282. if($number <= GameplayRuleEnum::SUM_EXTREME_SMALL){
  283. $result = GameplayRuleEnum::EXTREME_SMALL;
  284. }
  285. return $result;
  286. }
  287. /**
  288. * @description: 豹子
  289. * @param {int} $a
  290. * @param {int} $b
  291. * @param {int} $c
  292. * @return {*}
  293. */
  294. public static function isBaoZi(int $a, int $b, int $c)
  295. {
  296. $result = '';
  297. if($a === $b && $b === $c){
  298. $result = GameplayRuleEnum::BAO_ZI;
  299. }
  300. return $result;
  301. }
  302. /**
  303. * @description: 对子
  304. * @param {int} $a
  305. * @param {int} $b
  306. * @param {int} $c
  307. * @return {*}
  308. */
  309. public static function isPair($a, $b, $c) {
  310. $result = '';
  311. // 确保输入都是个位数
  312. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  313. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  314. return ''; // 或者抛出异常
  315. }
  316. if(($a == $b && $a != $c) ||
  317. ($a == $c && $a != $b) ||
  318. ($b == $c && $b != $a)){
  319. $result = GameplayRuleEnum::PAIRS;
  320. }
  321. // 判断是否为对子情况
  322. return $result;
  323. }
  324. /**
  325. * @description: 顺子
  326. * @param {int} $a
  327. * @param {int} $b
  328. * @param {int} $c
  329. * @return {*}
  330. */
  331. public static function isStraight($a, $b, $c) {
  332. $result = '';
  333. // 确保输入都是个位数(0-9)
  334. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  335. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  336. return '';
  337. }
  338. // 去重(顺子必须三个不同数字)
  339. if ($a == $b || $a == $c || $b == $c) {
  340. return '';
  341. }
  342. // 检查是否是完全升序或完全降序的连续数字
  343. $numbers = [$a, $b, $c];
  344. sort($numbers); // 排序后检查是否是 x, x+1, x+2
  345. list($x, $y, $z) = $numbers;
  346. // 情况1:升序连续(1,2,3)
  347. $isAscending = ($x + 1 == $y) && ($y + 1 == $z);
  348. // 情况2:降序连续(3,2,1)
  349. $isDescending = ($z + 1 == $y) && ($y + 1 == $x);
  350. if($isAscending || $isDescending){
  351. $result = GameplayRuleEnum::STRAIGHT;
  352. }
  353. return $result;
  354. }
  355. /**
  356. * 获取数字的尾数
  357. * @param int $number 输入数字
  358. * @return int 尾数
  359. */
  360. public static function getLastDigit($number) {
  361. // 确保输入是整数
  362. $number = (int)$number;
  363. // 取绝对值,处理负数情况
  364. $number = abs($number);
  365. // 取模10得到尾数
  366. return $number % 10;
  367. }
  368. /**
  369. * @description: 尾大小
  370. * @param {*} $number
  371. * @return {*}
  372. */
  373. public static function calculateOneSize($number)
  374. {
  375. if($number >= GameplayRuleEnum::ONE_BIG) {
  376. return GameplayRuleEnum::BIG;
  377. }
  378. if($number <= GameplayRuleEnum::ONE_SMALL){
  379. return GameplayRuleEnum::SMALL;
  380. }
  381. }
  382. /**
  383. * @description: 获取段位
  384. * @param {*} $number
  385. * @return {*}
  386. */
  387. public static function getSection($number) {
  388. $result = '';
  389. if ($number >= GameplayRuleEnum::SECTION_1[0] && $number <= GameplayRuleEnum::SECTION_1[1]) {
  390. $result = GameplayRuleEnum::ONE;
  391. } elseif ($number >= GameplayRuleEnum::SECTION_2[0] && $number <= GameplayRuleEnum::SECTION_2[1]) {
  392. $result = GameplayRuleEnum::TWO;
  393. } elseif ($number >= GameplayRuleEnum::SECTION_3[0] && $number <= GameplayRuleEnum::SECTION_3[1]) {
  394. $result = GameplayRuleEnum::THREE;
  395. } elseif ($number >= GameplayRuleEnum::SECTION_4[0] && $number <= GameplayRuleEnum::SECTION_4[1]) {
  396. $result = GameplayRuleEnum::FOUR;
  397. }
  398. return $result; // 不在任何段中
  399. }
  400. }