AgentGameModel.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. /**
  3. * AgentGameModel.php UTF-8
  4. * 渠道游戏MOdel
  5. *
  6. * @date : 2018/4/26 14:53
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\agent;
  13. use huo\model\common\CommonModel;
  14. use huo\model\game\GameextModel;
  15. use huo\model\game\GameModel;
  16. use huo\model\rate\AgentGameRateModel;
  17. use huo\model\rate\GameRateModel;
  18. use huo\model\user\UserModel;
  19. use huolib\constant\AgentConst;
  20. use huolib\constant\CacheConst;
  21. use huolib\constant\GameConst;
  22. use huomp\model\game\GameMiniModel;
  23. use think\Cache;
  24. class AgentGameModel extends CommonModel {
  25. protected $name = 'agent_game';
  26. // 开启自动写入时间戳字段
  27. protected $autoWriteTimestamp = true;
  28. protected $agent_cache_tag = CacheConst::TAG_AGENT_GAME_LIST;
  29. protected $cache_key_prefix = CacheConst::CACHE_AGENT_GAME_PREFIX;
  30. /**
  31. * 连接游戏
  32. *
  33. * @return \think\model\relation\BelongsTo
  34. */
  35. public function game() {
  36. return $this->belongsTo(GameModel::className(), 'app_id', 'id', 'game')->setEagerlyType(0);
  37. }
  38. /**
  39. * 关联game_version表
  40. *
  41. * @return \think\model\relation\HasMany
  42. */
  43. public function gv() {
  44. return $this->hasMany('huo\model\game\GameversionModel', 'app_id', 'app_id')->field('app_id,package_url');
  45. }
  46. /**
  47. * 连接游戏折扣
  48. */
  49. public function gamerate() {
  50. return $this->belongsTo(GameRateModel::className(), 'app_id', 'app_id')->field('app_id,benefit_type');
  51. }
  52. /**
  53. * 连接游戏扩展
  54. */
  55. public function gameext() {
  56. return $this->belongsTo(GameextModel::className(), 'app_id', 'app_id')->setEagerlyType(0);
  57. }
  58. /**
  59. * 连接小程序游戏
  60. */
  61. public function gamemini() {
  62. return $this->belongsTo(GameMiniModel::className(), 'app_id', 'app_id');
  63. }
  64. /**
  65. * 连接渠道
  66. *
  67. * @return \think\model\relation\BelongsTo
  68. */
  69. public function agent() {
  70. return $this->belongsTo(UserModel::className(), 'agent_id', 'id')->field('id,user_login,user_nicename,role_id,parent_id');
  71. }
  72. /**
  73. * 关联折扣
  74. *
  75. * @return \think\model\relation\HasOne
  76. */
  77. public function rate() {
  78. return $this->hasOne(AgentGameRateModel::className(), 'ag_id', 'id');
  79. }
  80. public function getBenefitTypeAttr($value) {
  81. if (GameConst::GAME_PROMOTE_SWITCH_NO == $value) {
  82. return '不可推广';
  83. } elseif (GameConst::GAME_PROMOTE_SWITCH_CAN == $value) {
  84. return '可推广';
  85. } elseif (GameConst::GAME_PROMOTE_SWITCH_CHECK == $value) {
  86. return '需审核';
  87. }
  88. return '不可推广';
  89. }
  90. /**
  91. * 添加渠道游戏
  92. *
  93. * @param $data
  94. *
  95. * @return bool|string
  96. */
  97. public function addData($data) {
  98. if (empty($data)) {
  99. return false;
  100. }
  101. if ($_obj = self::create($data, true)) {
  102. Cache::clear($this->agent_cache_tag);
  103. return $_obj->id;
  104. } else {
  105. return false;
  106. }
  107. }
  108. /**
  109. * 更新渠道游戏
  110. *
  111. * @param $ag_data
  112. * @param $ag_id
  113. *
  114. * @return bool
  115. */
  116. public function updateData($ag_data, $ag_id) {
  117. $_map['id'] = $ag_id;
  118. $_data = $ag_data;
  119. $_rs = self::update($_data, $_map, true);
  120. if (false == $_rs) {
  121. return false;
  122. } else {
  123. Cache::clear($this->agent_cache_tag);
  124. return true;
  125. }
  126. }
  127. /**
  128. * 获取渠道ID
  129. *
  130. * @param string $agent_game
  131. *
  132. * @return bool|int|mixed
  133. */
  134. public function getAgentIdByAg($agent_game = '') {
  135. if (empty($agent_game)) {
  136. return 0;
  137. }
  138. $_map['agent_game'] = $agent_game;
  139. $_agent_id = $this->where($_map)->value('agent_id');
  140. if (false === $_agent_id) {
  141. return false;
  142. }
  143. return $_agent_id;
  144. }
  145. /**
  146. * 获取AG_ID
  147. *
  148. * @param string $agent_game
  149. *
  150. * @return bool|int|mixed
  151. */
  152. public function getAgIdByAg($agent_game = '') {
  153. if (empty($agent_game)) {
  154. return 0;
  155. }
  156. $_map['agent_game'] = $agent_game;
  157. $_ag_id = $this->where($_map)->value('id');
  158. if (false === $_ag_id) {
  159. return false;
  160. }
  161. return $_ag_id;
  162. }
  163. /**
  164. * 获取渠道ID
  165. *
  166. * @param int $ag_id
  167. *
  168. * @return bool|int|mixed
  169. */
  170. public function getAgentIdByAgId($ag_id = 0) {
  171. if (empty($ag_id)) {
  172. return 0;
  173. }
  174. $_map['id'] = $ag_id;
  175. $_agent_id = $this->where($_map)->value('agent_id');
  176. if (false === $_agent_id) {
  177. return false;
  178. }
  179. return $_agent_id;
  180. }
  181. /**
  182. * 通过渠道ID 获取已申请的游戏
  183. *
  184. * @param $agent_id
  185. *
  186. * @return array
  187. */
  188. public function getGameIdsByAgentId($agent_id) {
  189. if (empty($agent_id)) {
  190. return [];
  191. }
  192. $_map['agent_id'] = $agent_id;
  193. $_game_ids = $this->where($_map)->column('app_id');
  194. return $_game_ids;
  195. }
  196. /**
  197. * @param $app_id
  198. * @param $agent_id
  199. *
  200. * @return int
  201. */
  202. public function getAgIdByAgentIdAppId($agent_id, $app_id) {
  203. $_map['agent_id'] = $agent_id;
  204. $_map['app_id'] = $app_id;
  205. $_ag_id = $this->where($_map)->value('id');
  206. if (false == $_ag_id) {
  207. $_ag_id = 0;
  208. }
  209. return $_ag_id;
  210. }
  211. /**
  212. * @param $agent_id
  213. *
  214. * @param $app_id
  215. *
  216. * @return array|bool|false|\PDOStatement|string|\think\Model
  217. */
  218. public function getInfoByAgentIdAppId($agent_id, $app_id) {
  219. $_map['agent_id'] = $agent_id;
  220. $_map['app_id'] = $app_id;
  221. $_ag_info = $this->where($_map)->find();
  222. if (false === $_ag_info) {
  223. return false;
  224. }
  225. if (is_object($_ag_info)) {
  226. $_ag_info = $_ag_info->toArray();
  227. }
  228. return $_ag_info;
  229. }
  230. /**
  231. * @param $ag_id
  232. *
  233. * @return array|bool|false
  234. */
  235. public function getAgInfoByAgId($ag_id) {
  236. if (empty($ag_id)) {
  237. return false;
  238. }
  239. $_map['id'] = $ag_id;
  240. $_ag_info = $this->where($_map)->find();
  241. if (false === $_ag_info) {
  242. return false;
  243. }
  244. if (is_object($_ag_info)) {
  245. $_ag_info = $_ag_info->toArray();
  246. }
  247. return $_ag_info;
  248. }
  249. /**
  250. * 获取单条记录缓存key
  251. *
  252. * @param int $agent_id 渠道ID
  253. * @param int $app_id 应用ID
  254. *
  255. * @return string
  256. */
  257. protected function getCacheKeyByAgentApp($agent_id, $app_id) {
  258. return $this->cache_key_prefix.'a_'.$agent_id.'g_'.$app_id;
  259. }
  260. /**
  261. * 根据Agent_id app_id 获取ID
  262. *
  263. * @param int $agent_id 渠道ID
  264. * @param int $app_id 应用ID
  265. *
  266. * @return int|mixed
  267. */
  268. public function getIdByAgentApp($agent_id, $app_id) {
  269. /* 缓存操作 */
  270. $_cache_key = $this->getCacheKeyByAgentApp($agent_id, $app_id);
  271. $_id = Cache::get($_cache_key);
  272. if (!empty($_id)) {
  273. return $_id;
  274. }
  275. $_map = [
  276. 'agent_id' => $agent_id,
  277. 'app_id' => $app_id,
  278. ];
  279. $_id = $this->where($_map)->value('id');
  280. if (empty($_id)) {
  281. return 0;
  282. }
  283. Cache::set($_cache_key, $_id);
  284. return $_id;
  285. }
  286. /**
  287. * 根据Agent_id app_id 获取数据
  288. *
  289. * @param int $agent_id 渠道ID
  290. * @param int $app_id 应用ID
  291. *
  292. * @return int|mixed
  293. */
  294. public function getInfoByAgentApp($agent_id, $app_id) {
  295. $_id = $this->getIdByAgentApp($agent_id, $app_id);
  296. if (empty($_id)) {
  297. return [];
  298. }
  299. return $this->getInfoById($_id);
  300. }
  301. /**
  302. * 获取渠道游戏是否切量
  303. *
  304. * @param int $agent_id 渠道ID
  305. * @param int $app_id 应用ID
  306. *
  307. * @return int
  308. */
  309. public function getIsSwitchByAgentApp($agent_id, $app_id) {
  310. $_info = $this->getInfoByAgentApp($agent_id, $app_id);
  311. return get_val($_info, 'is_switch', AgentConst::USER_SWITCH_NO);
  312. }
  313. }