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