GameModel.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. /**
  145. * 获取游戏详情
  146. */
  147. public static function getGameDetail($id, $merchantId)
  148. {
  149. return self::where('id', $id)
  150. ->where('merchant_id', $merchantId)
  151. ->find();
  152. }
  153. /**
  154. * 创建游戏
  155. */
  156. public static function createGame($data, $merchantId)
  157. {
  158. // 处理押注配置
  159. if (isset($data['deposit_list']) && is_array($data['deposit_list'])) {
  160. $data['deposit_list'] = json_encode($data['deposit_list']);
  161. }
  162. // 添加商户ID
  163. $data['merchant_id'] = $merchantId;
  164. return self::create($data);
  165. }
  166. /**
  167. * 更新游戏
  168. */
  169. public static function updateGame($id, $data, $merchantId)
  170. {
  171. // 处理押注配置
  172. if (isset($data['deposit_list']) && is_array($data['deposit_list'])) {
  173. $data['deposit_list'] = json_encode($data['deposit_list']);
  174. }
  175. return self::where('id', $id)
  176. ->where('merchant_id', $merchantId)
  177. ->update($data);
  178. }
  179. /**
  180. * 更新游戏状态
  181. */
  182. public static function updateGameStatus($ids, $merchantId, $status)
  183. {
  184. return self::whereIn('id', $ids)
  185. ->where('merchant_id', $merchantId)
  186. ->update(['status' => $status]);
  187. }
  188. /**
  189. * 批量更新游戏状态
  190. */
  191. public static function batchUpdateStatus($ids, $status, $merchantId)
  192. {
  193. return self::whereIn('id', $ids)
  194. ->where('merchant_id', $merchantId)
  195. ->update(['status' => $status]);
  196. }
  197. /**
  198. * 获取游戏统计信息
  199. */
  200. public static function getGameStatistics($merchantId)
  201. {
  202. $query = self::where('merchant_id', $merchantId);
  203. $totalGames = (clone $query)->count();
  204. $normalGames = (clone $query)->where('status', self::STATUS_NORMAL)->count();
  205. $maintainGames = (clone $query)->where('status', self::STATUS_MAINTAIN)->count();
  206. $disabledGames = (clone $query)->where('status', self::STATUS_DISABLED)->count();
  207. // 按平台统计
  208. $platformStats = [];
  209. $platforms = self::getAllPlatforms();
  210. foreach ($platforms as $platform) {
  211. $platformStats[$platform['platform_name']] = self::where('game_platform', $platform['id'])
  212. ->where('merchant_id', $merchantId)
  213. ->count();
  214. }
  215. return [
  216. 'total_games' => $totalGames,
  217. 'normal_games' => $normalGames,
  218. 'maintain_games' => $maintainGames,
  219. 'disabled_games' => $disabledGames,
  220. 'platform_stats' => $platformStats,
  221. 'avg_rtp' => (clone $query)->avg('rtp'),
  222. 'free_game_enabled' => (clone $query)->where('free_game_status', self::FREE_GAME_ENABLED)->count(),
  223. 'terminal_spin_enabled' => (clone $query)->where('terminal_spin', self::TERMINAL_SPIN_ENABLED)->count(),
  224. ];
  225. }
  226. /**
  227. * 获取所有游戏平台
  228. */
  229. public static function getAllPlatforms(): array
  230. {
  231. return self::table('merchant_game_platform')
  232. ->field(['id', 'platform_name'])
  233. ->order('id', 'asc')
  234. ->select()
  235. ->toArray();
  236. }
  237. /**
  238. * 解析押注配置
  239. */
  240. public function getDepositListAttr($value)
  241. {
  242. if (empty($value)) {
  243. return [];
  244. }
  245. // 如果是JSON字符串,解析它
  246. $decoded = json_decode($value, true);
  247. if (json_last_error() === JSON_ERROR_NONE) {
  248. return $decoded;
  249. }
  250. // 如果是逗号分隔的字符串,转换为数组
  251. if (strpos($value, ',') !== false) {
  252. return array_map('floatval', explode(',', $value));
  253. }
  254. return [];
  255. }
  256. /**
  257. * 检查游戏ID是否已存在
  258. */
  259. public static function checkGameIdExists($gameId, $merchantId, $excludeId = null): bool
  260. {
  261. $query = self::where('game_id', $gameId)
  262. ->where('merchant_id', $merchantId);
  263. if ($excludeId) {
  264. $query->where('id', '<>', $excludeId);
  265. }
  266. return $query->count() > 0;
  267. }
  268. /**
  269. * 删除游戏
  270. */
  271. public static function deleteGame($id, $merchantId): bool
  272. {
  273. return self::where('id', $id)
  274. ->where('merchant_id', $merchantId)
  275. ->delete() > 0;
  276. }
  277. }