GameModel.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model;
  4. use think\Model;
  5. /**
  6. * 商户游戏模型
  7. */
  8. class GameModel extends Model
  9. {
  10. // 设置表名
  11. protected $name = 'game_list';
  12. // 设置主键
  13. protected $pk = 'id';
  14. // 设置自动时间戳
  15. protected $autoWriteTimestamp = 'int';
  16. // 设置字段类型
  17. protected $type = [
  18. 'id' => 'int',
  19. 'game_platform' => 'int',
  20. 'game_id' => 'int',
  21. 'title' => 'string',
  22. 'title_en' => 'string',
  23. 'image' => 'string',
  24. 'image_en' => 'string',
  25. 'rtp' => 'float',
  26. 'rtp_type' => 'int',
  27. 'free_game_status' => 'int',
  28. 'bet_line_count' => 'int',
  29. 'bet_max_level' => 'int',
  30. 'max_multiple_count' => 'int',
  31. 'deposit_list' => 'string',
  32. 'default_deposit' => 'float',
  33. 'default_deposit_level' => 'int',
  34. 'min_deposit' => 'float',
  35. 'terminal_spin' => 'int',
  36. 'status' => 'int',
  37. 'create_time' => 'int',
  38. 'update_time' => 'int',
  39. ];
  40. // 游戏平台常量
  41. const PLATFORM_PG = 1; // PG平台
  42. const PLATFORM_PP = 2; // PP平台
  43. const PLATFORM_CQ9 = 3; // CQ9平台
  44. const PLATFORM_JDB = 4; // JDB平台
  45. const PLATFORM_JILI = 5; // JILI平台
  46. // 状态常量
  47. const STATUS_NORMAL = 0; // 正常
  48. const STATUS_MAINTAIN = 1; // 维护
  49. const STATUS_DISABLED = 2; // 停用
  50. // RTP类型常量
  51. const RTP_TYPE_FIXED = 1; // 固定RTP
  52. const RTP_TYPE_DYNAMIC = 2; // 动态RTP
  53. const RTP_TYPE_CUSTOM = 3; // 自定义RTP
  54. // 免费游戏状态
  55. const FREE_GAME_DISABLED = 0; // 不支持购买免费游戏
  56. const FREE_GAME_ENABLED = 1; // 支持购买免费游戏
  57. // 止损止赢功能
  58. const TERMINAL_SPIN_DISABLED = 0; // 关闭止损止赢
  59. const TERMINAL_SPIN_ENABLED = 1; // 开启止损止赢
  60. /**
  61. * 获取游戏平台文本
  62. */
  63. public static function getPlatformText($platform): string
  64. {
  65. $platformMap = [
  66. self::PLATFORM_PG => 'PG',
  67. self::PLATFORM_PP => 'PP',
  68. self::PLATFORM_CQ9 => 'CQ9',
  69. self::PLATFORM_JDB => 'JDB',
  70. self::PLATFORM_JILI => 'JILI',
  71. ];
  72. return $platformMap[$platform] ?? '未知';
  73. }
  74. /**
  75. * 获取状态文本
  76. */
  77. public static function getStatusText($status): string
  78. {
  79. $statusMap = [
  80. self::STATUS_NORMAL => '正常',
  81. self::STATUS_MAINTAIN => '维护',
  82. self::STATUS_DISABLED => '停用',
  83. ];
  84. return $statusMap[$status] ?? '未知';
  85. }
  86. /**
  87. * 获取RTP类型文本
  88. */
  89. public static function getRtpTypeText($rtpType): string
  90. {
  91. $rtpTypeMap = [
  92. self::RTP_TYPE_FIXED => '固定',
  93. self::RTP_TYPE_DYNAMIC => '动态',
  94. self::RTP_TYPE_CUSTOM => '自定义',
  95. ];
  96. return $rtpTypeMap[$rtpType] ?? '未知';
  97. }
  98. /**
  99. * 获取游戏列表
  100. *
  101. * 注意:merchant_game_list表没有merchant_id字段,
  102. * 如果需要按商户过滤,需要关联其他表或调整表结构
  103. */
  104. public static function getGameList($page = 1, $limit = 10, $filters = [])
  105. {
  106. $query = self::where('id', '>', 0); // 基础查询
  107. // 应用过滤条件
  108. if (!empty($filters['title'])) {
  109. $query->where(function($q) use ($filters) {
  110. $q->where('title', 'like', '%' . $filters['title'] . '%')
  111. ->whereOr('title_en', 'like', '%' . $filters['title'] . '%');
  112. });
  113. }
  114. if (!empty($filters['game_id'])) {
  115. $query->where('game_id', $filters['game_id']);
  116. }
  117. if (isset($filters['game_platform']) && $filters['game_platform'] !== '') {
  118. $query->where('game_platform', $filters['game_platform']);
  119. }
  120. if (isset($filters['status']) && $filters['status'] !== '') {
  121. $query->where('status', $filters['status']);
  122. }
  123. if (isset($filters['rtp_type']) && $filters['rtp_type'] !== '') {
  124. $query->where('rtp_type', $filters['rtp_type']);
  125. }
  126. if (isset($filters['free_game_status']) && $filters['free_game_status'] !== '') {
  127. $query->where('free_game_status', $filters['free_game_status']);
  128. }
  129. if (isset($filters['terminal_spin']) && $filters['terminal_spin'] !== '') {
  130. $query->where('terminal_spin', $filters['terminal_spin']);
  131. }
  132. // RTP范围
  133. if (!empty($filters['rtp_min'])) {
  134. $query->where('rtp', '>=', $filters['rtp_min']);
  135. }
  136. if (!empty($filters['rtp_max'])) {
  137. $query->where('rtp', '<=', $filters['rtp_max']);
  138. }
  139. // 最高倍数范围
  140. if (!empty($filters['max_multiple_min'])) {
  141. $query->where('max_multiple_count', '>=', $filters['max_multiple_min']);
  142. }
  143. if (!empty($filters['max_multiple_max'])) {
  144. $query->where('max_multiple_count', '<=', $filters['max_multiple_max']);
  145. }
  146. // 创建时间范围
  147. if (!empty($filters['create_time_start'])) {
  148. $query->where('create_time', '>=', strtotime($filters['create_time_start']));
  149. }
  150. if (!empty($filters['create_time_end'])) {
  151. $query->where('create_time', '<=', strtotime($filters['create_time_end']));
  152. }
  153. // 排序
  154. $order = $filters['order'] ?? 'id';
  155. $sort = $filters['sort'] ?? 'desc';
  156. $query->order($order, $sort);
  157. // 获取总数
  158. $total = $query->count();
  159. // 获取列表
  160. $list = $query->page($page, $limit)->select();
  161. return [
  162. 'list' => $list,
  163. 'total' => $total,
  164. 'page' => $page,
  165. 'limit' => $limit
  166. ];
  167. }
  168. /**
  169. * 获取游戏详情
  170. */
  171. public static function getGameDetail($id)
  172. {
  173. return self::where('id', $id)->find();
  174. }
  175. /**
  176. * 创建游戏
  177. */
  178. public static function createGame($data)
  179. {
  180. // 处理押注配置
  181. if (isset($data['deposit_list']) && is_array($data['deposit_list'])) {
  182. $data['deposit_list'] = json_encode($data['deposit_list']);
  183. }
  184. return self::create($data);
  185. }
  186. /**
  187. * 更新游戏
  188. */
  189. public static function updateGame($id, $data)
  190. {
  191. // 处理押注配置
  192. if (isset($data['deposit_list']) && is_array($data['deposit_list'])) {
  193. $data['deposit_list'] = json_encode($data['deposit_list']);
  194. }
  195. return self::where('id', $id)->update($data);
  196. }
  197. /**
  198. * 更新游戏状态
  199. */
  200. public static function updateGameStatus($id, $status)
  201. {
  202. return self::where('id', $id)->update(['status' => $status]);
  203. }
  204. /**
  205. * 批量更新游戏状态
  206. */
  207. public static function batchUpdateStatus($ids, $status)
  208. {
  209. return self::whereIn('id', $ids)->update(['status' => $status]);
  210. }
  211. /**
  212. * 获取游戏统计信息
  213. */
  214. public static function getGameStatistics()
  215. {
  216. $totalGames = self::count();
  217. $normalGames = self::where('status', self::STATUS_NORMAL)->count();
  218. $maintainGames = self::where('status', self::STATUS_MAINTAIN)->count();
  219. $disabledGames = self::where('status', self::STATUS_DISABLED)->count();
  220. // 按平台统计
  221. $platformStats = [];
  222. $platforms = [
  223. self::PLATFORM_PG => 'PG',
  224. self::PLATFORM_PP => 'PP',
  225. self::PLATFORM_CQ9 => 'CQ9',
  226. self::PLATFORM_JDB => 'JDB',
  227. self::PLATFORM_JILI => 'JILI',
  228. ];
  229. foreach ($platforms as $platform => $name) {
  230. $platformStats[$name] = self::where('game_platform', $platform)->count();
  231. }
  232. return [
  233. 'total_games' => $totalGames,
  234. 'normal_games' => $normalGames,
  235. 'maintain_games' => $maintainGames,
  236. 'disabled_games' => $disabledGames,
  237. 'platform_stats' => $platformStats,
  238. 'avg_rtp' => self::avg('rtp'),
  239. 'free_game_enabled' => self::where('free_game_status', self::FREE_GAME_ENABLED)->count(),
  240. 'terminal_spin_enabled' => self::where('terminal_spin', self::TERMINAL_SPIN_ENABLED)->count(),
  241. ];
  242. }
  243. /**
  244. * 获取所有游戏平台
  245. */
  246. public static function getAllPlatforms(): array
  247. {
  248. return [
  249. ['value' => self::PLATFORM_PG, 'text' => 'PG'],
  250. ['value' => self::PLATFORM_PP, 'text' => 'PP'],
  251. ['value' => self::PLATFORM_CQ9, 'text' => 'CQ9'],
  252. ['value' => self::PLATFORM_JDB, 'text' => 'JDB'],
  253. ['value' => self::PLATFORM_JILI, 'text' => 'JILI'],
  254. ];
  255. }
  256. /**
  257. * 解析押注配置
  258. */
  259. public function getDepositListAttr($value)
  260. {
  261. if (empty($value)) {
  262. return [];
  263. }
  264. // 如果是JSON字符串,解析它
  265. $decoded = json_decode($value, true);
  266. if (json_last_error() === JSON_ERROR_NONE) {
  267. return $decoded;
  268. }
  269. // 如果是逗号分隔的字符串,转换为数组
  270. if (strpos($value, ',') !== false) {
  271. return array_map('floatval', explode(',', $value));
  272. }
  273. return [];
  274. }
  275. /**
  276. * 检查游戏ID是否已存在
  277. */
  278. public static function checkGameIdExists($gameId, $excludeId = null): bool
  279. {
  280. $query = self::where('game_id', $gameId);
  281. if ($excludeId) {
  282. $query->where('id', '<>', $excludeId);
  283. }
  284. return $query->count() > 0;
  285. }
  286. }