GameModel.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 = [
  43. 0 => "初始",
  44. 1 => "正常",
  45. 2 => "维护",
  46. ];
  47. // 止损止赢功能开关状态
  48. const FREE_STATUS = [
  49. 0 => '关闭',
  50. 1 => '开启',
  51. ];
  52. // 购买免费游戏功能开关状态
  53. const TERMINAL_SPIN_STATUS = [
  54. 0 => '关闭',
  55. 1 => '开启',
  56. ];
  57. /**
  58. * 获取状态文本
  59. */
  60. public static function getStatusText($status): string
  61. {
  62. return self::STATUS[$status] ?? '未知';
  63. }
  64. /**
  65. * 获取RTP类型文本
  66. */
  67. public static function getRtpTypeText($rtpType): string
  68. {
  69. $rtpTypeMap = $GLOBALS['gamePgGameConfig']['rtp_type_config'];
  70. return $rtpTypeMap[$rtpType]['name'] ?? '未知';
  71. }
  72. /**
  73. * 获取游戏平台文本
  74. */
  75. public static function getPlatFormText($platform): string
  76. {
  77. $platforms = self::getAllPlatforms();
  78. return $platforms[$platform] ?? '未知';
  79. }
  80. /**
  81. * 获取游戏列表
  82. */
  83. public static function getGameList($merchantId, $page = 1, $limit = 10, $filters = [])
  84. {
  85. $query = self::where('merchant_id', $merchantId); // 按商户ID过滤
  86. // 应用过滤条件
  87. if (!empty($filters['title'])) {
  88. $query->where(function($q) use ($filters) {
  89. $q->where('title', 'like', '%' . $filters['title'] . '%')
  90. ->whereOr('title_en', 'like', '%' . $filters['title'] . '%');
  91. });
  92. }
  93. if (!empty($filters['game_id'])) {
  94. $query->where('game_id', $filters['game_id']);
  95. }
  96. if (isset($filters['game_platform']) && $filters['game_platform'] !== '') {
  97. $query->where('game_platform', $filters['game_platform']);
  98. }
  99. if (isset($filters['status']) && $filters['status'] !== '') {
  100. $query->where('status', $filters['status']);
  101. }
  102. if (isset($filters['rtp_type']) && $filters['rtp_type'] !== '') {
  103. $query->where('rtp_type', $filters['rtp_type']);
  104. }
  105. if (isset($filters['free_game_status']) && $filters['free_game_status'] !== '') {
  106. $query->where('free_game_status', $filters['free_game_status']);
  107. }
  108. if (isset($filters['terminal_spin']) && $filters['terminal_spin'] !== '') {
  109. $query->where('terminal_spin', $filters['terminal_spin']);
  110. }
  111. // RTP范围
  112. if (!empty($filters['rtp_min'])) {
  113. $query->where('rtp', '>=', $filters['rtp_min']);
  114. }
  115. if (!empty($filters['rtp_max'])) {
  116. $query->where('rtp', '<=', $filters['rtp_max']);
  117. }
  118. // 最高倍数范围
  119. if (!empty($filters['max_multiple_min'])) {
  120. $query->where('max_multiple_count', '>=', $filters['max_multiple_min']);
  121. }
  122. if (!empty($filters['max_multiple_max'])) {
  123. $query->where('max_multiple_count', '<=', $filters['max_multiple_max']);
  124. }
  125. // 创建时间范围
  126. if (!empty($filters['create_time_start'])) {
  127. $query->where('create_time', '>=', strtotime($filters['create_time_start']));
  128. }
  129. if (!empty($filters['create_time_end'])) {
  130. $query->where('create_time', '<=', strtotime($filters['create_time_end']));
  131. }
  132. // 排序
  133. $order = $filters['order'] ?? 'id';
  134. $sort = $filters['sort'] ?? 'desc';
  135. $query->order($order, $sort);
  136. // 获取总数
  137. $total = $query->count();
  138. // 获取列表
  139. $list = $query->page($page, $limit)->select()->toArray();
  140. return [
  141. 'list' => $list,
  142. 'total' => $total,
  143. 'page' => $page,
  144. 'limit' => $limit
  145. ];
  146. }
  147. public static function getGames($merchantId, $filters = [], $field = ['id', 'game_id', 'title'])
  148. {
  149. $query = self::where('merchant_id', $merchantId);
  150. // 排序
  151. $order = $filters['order'] ?? 'id';
  152. $sort = $filters['sort'] ?? 'desc';
  153. $query->order($order, $sort);
  154. return $query->field($field)->select()->toArray();
  155. }
  156. /**
  157. * 获取游戏详情
  158. */
  159. public static function getGameDetail($merchantId, $filters = [])
  160. {
  161. $query = self::where('merchant_id', $merchantId);
  162. if (!empty($filters['id'])) {
  163. $query->where('id', $filters['id']);
  164. }
  165. if (!empty($filters['game_id'])) {
  166. $query->where('game_id', $filters['game_id']);
  167. }
  168. return $query->find()->toArray();
  169. }
  170. /**
  171. * 创建游戏
  172. */
  173. public static function createGame($merchantId, $data)
  174. {
  175. // 处理押注配置
  176. if (isset($data['deposit_list']) && is_array($data['deposit_list'])) {
  177. $data['deposit_list'] = json_encode($data['deposit_list']);
  178. }
  179. // 添加商户ID
  180. $data['merchant_id'] = $merchantId;
  181. return self::create($data);
  182. }
  183. /**
  184. * 更新游戏
  185. */
  186. public static function updateGame($merchantId, $id, $data)
  187. {
  188. // 处理押注配置
  189. if (isset($data['deposit_list']) && is_array($data['deposit_list'])) {
  190. $data['deposit_list'] = json_encode($data['deposit_list']);
  191. }
  192. return self::where('id', $id)
  193. ->where('merchant_id', $merchantId)
  194. ->update($data);
  195. }
  196. /**
  197. * 更新游戏状态
  198. */
  199. public static function updateGameStatus($merchantId, $ids, $status)
  200. {
  201. return self::whereIn('id', $ids)
  202. ->where('merchant_id', $merchantId)
  203. ->update(['status' => $status]);
  204. }
  205. /**
  206. * 获取所有游戏平台
  207. */
  208. public static function getAllPlatforms(): array
  209. {
  210. return $GLOBALS['gameGameTypeConfig'];
  211. }
  212. /**
  213. * 解析押注配置
  214. */
  215. public function getDepositListAttr($value)
  216. {
  217. if (empty($value)) {
  218. return [];
  219. }
  220. // 如果是JSON字符串,解析它
  221. $decoded = json_decode($value, true);
  222. if (json_last_error() === JSON_ERROR_NONE) {
  223. return $decoded;
  224. }
  225. // 如果是逗号分隔的字符串,转换为数组
  226. if (strpos($value, ',') !== false) {
  227. return array_map('floatval', explode(',', $value));
  228. }
  229. return [];
  230. }
  231. /**
  232. * 检查游戏ID是否已存在
  233. */
  234. public static function checkGameIdExists($merchantId, $gameId, $excludeId = null): bool
  235. {
  236. $query = self::where('game_id', $gameId)
  237. ->where('merchant_id', $merchantId);
  238. if ($excludeId) {
  239. $query->where('id', '<>', $excludeId);
  240. }
  241. return $query->count() > 0;
  242. }
  243. /**
  244. * 删除游戏
  245. */
  246. public static function deleteGame($merchantId, $id): bool
  247. {
  248. return self::where('id', $id)
  249. ->where('merchant_id', $merchantId)
  250. ->delete() > 0;
  251. }
  252. }