IssueService.php 12 KB

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