AgentGameRateModel.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <?php
  2. /**
  3. * AgentGameRateModel.php UTF-8
  4. * http://doc.1tsdk.com/170?page_id=5613
  5. * 渠道游戏折扣返利表
  6. *
  7. *
  8. * @date : 2019/4/20 16:23
  9. *
  10. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  11. * @author : wuyonghong <wyh@huosdk.com>
  12. * @version : HUOSDK 8.5
  13. */
  14. namespace huoRate\model;
  15. use huo\model\common\CommonModel;
  16. use huolib\constant\CacheConst;
  17. use huolib\constant\CommonConst;
  18. use huolib\constant\GameConst;
  19. use think\Cache;
  20. class AgentGameRateModel extends CommonModel {
  21. protected $name = 'agent_game_rate';
  22. protected $pk = 'ag_id';
  23. protected $type = ['benefit_type' => 'int'];
  24. /* 开启自动写入时间戳字段 */
  25. protected $autoWriteTimestamp = true;
  26. protected $cache_key_prefix = CacheConst::CACHE_AGENT_GAME_RATE_PREFIX;
  27. protected $agent_cache_tag = CacheConst::TAG_AGENT_GAME_LIST;
  28. /**
  29. * 获取单条记录缓存key
  30. *
  31. * @param int $id ID
  32. *
  33. * @return string
  34. */
  35. protected function getSingleCacheKey($id) {
  36. return $this->cache_key_prefix.$id;
  37. }
  38. /**
  39. * 获取单条记录缓存key
  40. *
  41. * @param int $app_id 应用ID
  42. * @param int $agent_id 渠道ID
  43. *
  44. * @return string
  45. */
  46. protected function getCacheKeyByGameAgent($app_id, $agent_id) {
  47. return $this->cache_key_prefix.$app_id.'_'.$agent_id;
  48. }
  49. /**
  50. * 添加数据
  51. *
  52. * @param array $data 需要添加的数据
  53. *
  54. * @return false|int 添加失败返回 false 添加成功 返回添加的ID
  55. */
  56. public function addData($data) {
  57. $_data = $data;
  58. $_id = parent::addData($_data);
  59. if (false === $_id) {
  60. return false;
  61. }
  62. {
  63. $_app_id = get_val($data, 'app_id', 0);
  64. $_agent_id = get_val($data, 'agent_id', 0);
  65. /* 添加数据 APP VIP 缓存 */
  66. $_cache_key = $this->getCacheKeyByGameAgent($_app_id, $_agent_id);
  67. Cache::set($_cache_key, $_id);
  68. }
  69. /* TAG缓存操作 */
  70. return $_id;
  71. }
  72. /**
  73. * 通过ID获取信息
  74. *
  75. * @param int $id 主键ID
  76. *
  77. * @return array|false
  78. */
  79. public function getInfoById($id) {
  80. /* 缓存操作 */
  81. $_single_cache_key = $this->getSingleCacheKey($id);
  82. $_data = Cache::get($_single_cache_key);
  83. if (!empty($_data)) {
  84. return $_data;
  85. }
  86. $_data = parent::getInfoById($id);
  87. if (empty($_data)) {
  88. return [];
  89. }
  90. Cache::set($_single_cache_key, $_data);
  91. return $_data;
  92. }
  93. /**
  94. * 更新单条数据
  95. *
  96. * @param array $data 数据
  97. * @param int $id ID
  98. *
  99. * @return bool
  100. */
  101. public function updateData($data, $id) {
  102. $_data = $data;
  103. $_rs = parent::updateData($_data, $id);
  104. if (false === $_rs) {
  105. return false;
  106. }
  107. /* 缓存操作 */
  108. $_single_cache_key = $this->getSingleCacheKey($id);
  109. Cache::rm($_single_cache_key);
  110. /* TAG缓存操作 */
  111. Cache::clear($this->agent_cache_tag);
  112. /* 更新轮播图列表缓存数据 */
  113. if (!empty($data['app_id'])) {
  114. //(new SlideItemModel())->clCacheByAppId($data['app_id']);
  115. }
  116. return true;
  117. }
  118. /**
  119. * 删除单条数据
  120. *
  121. * @param int $id ID
  122. * @param bool $is_complete 是否完成删除
  123. *
  124. * @return bool
  125. */
  126. public function deleteData($id, $is_complete = false) {
  127. $_rs = parent::deleteData($id, $is_complete);
  128. if (false == $_rs) {
  129. return false;
  130. }
  131. /* 缓存操作 */
  132. $_single_cache_key = $this->getSingleCacheKey($id);
  133. Cache::rm($_single_cache_key);
  134. /* TAG缓存操作 */
  135. Cache::clear($this->agent_cache_tag);
  136. return $_rs;
  137. }
  138. /**
  139. * 更新多条数据
  140. *
  141. * @param array $data 数据
  142. * @param array $ids 主集ID
  143. *
  144. * @return bool
  145. */
  146. public function updateDatas($data, $ids) {
  147. $_data = $data;
  148. foreach ($ids as $_id) {
  149. $_rs = $this->updateData($_data, $_id);
  150. if (false == $_rs) {
  151. return false;
  152. }
  153. }
  154. return true;
  155. }
  156. /**
  157. * 根据游戏删除所有数据
  158. *
  159. * @param int $app_id 应用ID
  160. *
  161. * @return bool
  162. */
  163. public function deleteDataByAppId($app_id) {
  164. $_map = [
  165. 'app_id' => $app_id,
  166. ];
  167. $_ids = $this->where($_map)->column('id');
  168. foreach ($_ids as $_id) {
  169. $this->deleteData($_id);
  170. }
  171. return true;
  172. }
  173. /**
  174. * 根据应用渠道获取ID
  175. *
  176. * @param int $app_id 应用ID
  177. * @param int $agent_id 渠道ID
  178. *
  179. * @return int
  180. */
  181. public function getIdByGameAgent($app_id, $agent_id) {
  182. if (empty($app_id) && empty($agent_id)) {
  183. return 0;
  184. }
  185. $_cache_key = $this->getCacheKeyByGameAgent($app_id, $agent_id);
  186. $_id = Cache::get($_cache_key);
  187. if (!empty($_id)) {
  188. if (CommonConst::CACHE_HAS_READ == $_id) {
  189. return 0;
  190. }
  191. return $_id;
  192. }
  193. $_map = [
  194. 'app_id' => $app_id,
  195. 'agent_id' => $agent_id,
  196. ];
  197. $_id = $this->where($_map)->value($this->pk);
  198. if (empty($_id)) {
  199. Cache::set($_cache_key, CommonConst::CACHE_HAS_READ);
  200. return 0;
  201. }
  202. Cache::set($_cache_key, $_id);
  203. return $_id;
  204. }
  205. /**
  206. * 根据应用渠道获取信息
  207. *
  208. * @param int $app_id 应用ID
  209. * @param int $agent_id 渠道ID
  210. *
  211. *
  212. * @return array|false
  213. */
  214. public function getInfoByGameAgent($app_id, $agent_id) {
  215. $_id = $this->getIdByGameAgent($app_id, $agent_id);
  216. if (empty($_id)) {
  217. return false;
  218. }
  219. return $this->getInfoById($_id);
  220. }
  221. /**
  222. * 根据ID获取应用ID
  223. *
  224. * @param int $id ID
  225. *
  226. *
  227. * @return int
  228. */
  229. public function getAppIdById($id) {
  230. $_data = $this->getInfoById($id);
  231. return get_val($_data, 'app_id', CommonConst::CONST_ZERO);
  232. }
  233. /**
  234. * 根据ID获取渠道ID
  235. *
  236. * @param int $id ID
  237. *
  238. *
  239. * @return int
  240. */
  241. public function getAgentIdById($id) {
  242. $_data = $this->getInfoById($id);
  243. return get_val($_data, 'agent_id', CommonConst::CONST_ZERO);
  244. }
  245. /**
  246. * 根据应用渠道获取信息
  247. *
  248. * @param int $app_id 应用ID
  249. * @param int $agent_id 渠道ID
  250. *
  251. *
  252. * @return array|false
  253. */
  254. public function getPromoteSwitchByGameAgent($app_id, $agent_id = 0) {
  255. $_data = $this->getInfoByGameAgent($app_id, $agent_id);
  256. return get_val($_data, 'promote_switch', 0);
  257. }
  258. /**
  259. * 根据应用渠道游戏折扣
  260. *
  261. * @param int $app_id 应用ID
  262. * @param int $agent_id 渠道ID
  263. *
  264. *
  265. * @return array|false
  266. */
  267. public function getGameRateByGameAgent($app_id, $agent_id = 0) {
  268. $_data = $this->getInfoByGameAgent($app_id, 0);
  269. return get_val($_data, 'game_rate', CommonConst::CONST_ZERO);
  270. }
  271. /**
  272. * 根据应用渠道获取 优惠类型
  273. *
  274. * @param int $app_id 应用ID
  275. * @param int $agent_id 渠道ID
  276. *
  277. *
  278. * @return int 优惠类型,0 无优惠 1 折扣 2 返利
  279. */
  280. public function getBenefitTypeByGameAgent($app_id, $agent_id = 0) {
  281. $_data = $this->getInfoByGameAgent($app_id, 0);
  282. return get_val($_data, 'benefit_type', GameConst::RATE_BENEFIT_NO);
  283. }
  284. /**
  285. * 关联game表
  286. */
  287. public function game() {
  288. return $this->belongsTo('huo\model\game\GameModel', 'app_id')->field('id,name,icon,classify');
  289. }
  290. /**
  291. * 关联user表
  292. */
  293. public function user() {
  294. return $this->belongsTo('huo\model\user\UserModel', 'agent_id')->field('id,user_login,user_nicename,role_id');
  295. }
  296. //
  297. // /**
  298. // * 根据应用渠道获取一级渠道返点比例
  299. // *
  300. // * @param int $app_id 应用ID
  301. // * @param int $agent_id 渠道ID
  302. // *
  303. // *
  304. // * @return FLOAT
  305. // */
  306. // public function getAgentRebateByGameAgent($app_id, $agent_id) {
  307. // $_data = $this->getInfoByGameAgent($app_id, $agent_id);
  308. //
  309. // return get_val($_data, 'agent_rebate', CommonConst::CONST_ZERO);
  310. // }
  311. //
  312. // /**
  313. // * 根据应用渠道获取 二级渠道返点比例
  314. // *
  315. // * @param int $app_id 应用ID
  316. // * @param int $agent_id 渠道ID
  317. // *
  318. // *
  319. // * @return FLOAT
  320. // */
  321. // public function getSubAgentRebateByGameAgent($app_id, $agent_id) {
  322. // $_data = $this->getInfoByGameAgent($app_id, $agent_id);
  323. //
  324. // return get_val($_data, 'sub_agent_rebate', CommonConst::CONST_ZERO);
  325. // }
  326. //
  327. // /**
  328. // * 根据应用渠道获取 一级渠道分成比例
  329. // *
  330. // * @param int $app_id 应用ID
  331. // * @param int $agent_id 渠道ID
  332. // *
  333. // *
  334. // * @return FLOAT
  335. // */
  336. // public function getAgentRateByGameAgent($app_id, $agent_id) {
  337. // $_data = $this->getInfoByGameAgent($app_id, $agent_id);
  338. //
  339. // return get_val($_data, 'agent_rate', CommonConst::CONST_ONE);
  340. // }
  341. //
  342. // /**
  343. // * 根据应用渠道获取下级渠道分成比例
  344. // *
  345. // * @param int $app_id 应用ID
  346. // * @param int $agent_id 渠道ID
  347. // *
  348. // *
  349. // * @return FLOAT
  350. // */
  351. // public function getSubAgentRateByGameAgent($app_id, $agent_id) {
  352. // $_data = $this->getInfoByGameAgent($app_id, $agent_id);
  353. //
  354. // return get_val($_data, 'sub_agent_rate', CommonConst::CONST_ONE);
  355. // }
  356. //
  357. //
  358. // /**
  359. // * 根据应用渠道获取 玩家折扣比例
  360. // *
  361. // * @param int $app_id 应用ID
  362. // * @param int $agent_id 渠道ID
  363. // *
  364. // *
  365. // * @return FLOAT
  366. // */
  367. // public function getMemRateByGameAgent($app_id, $agent_id) {
  368. // $_data = $this->getInfoByGameAgent($app_id, $agent_id);
  369. //
  370. // return get_val($_data, 'mem_rate', CommonConst::CONST_ZERO);
  371. // }
  372. //
  373. // /**
  374. // * 根据应用渠道获取 玩家首充折扣比例
  375. // *
  376. // * @param int $app_id 应用ID
  377. // * @param int $agent_id 渠道ID
  378. // *
  379. // *
  380. // * @return FLOAT
  381. // */
  382. // public function getFirstMemRateByGameAgent($app_id, $agent_id) {
  383. // $_data = $this->getInfoByGameAgent($app_id, $agent_id);
  384. //
  385. // return get_val($_data, 'first_mem_rate', CommonConst::CONST_ZERO);
  386. // }
  387. //
  388. // /**
  389. // * 根据应用渠道获取 玩家返利比例
  390. // *
  391. // * @param int $app_id 应用ID
  392. // * @param int $agent_id 渠道ID
  393. // *
  394. // *
  395. // * @return FLOAT
  396. // */
  397. // public function getMemRebateByGameAgent($app_id, $agent_id) {
  398. // $_data = $this->getInfoByGameAgent($app_id, $agent_id);
  399. //
  400. // return get_val($_data, 'mem_rebate', CommonConst::CONST_ZERO);
  401. // }
  402. //
  403. // /**
  404. // * 根据应用渠道获取 玩家首充返利比例
  405. // *
  406. // * @param int $app_id 应用ID
  407. // * @param int $agent_id 渠道ID
  408. // *
  409. // *
  410. // * @return FLOAT
  411. // */
  412. // public function getFirstMemRebateByGameAgent($app_id, $agent_id) {
  413. // $_data = $this->getInfoByGameAgent($app_id, $agent_id);
  414. //
  415. // return get_val($_data, 'first_mem_rebate', CommonConst::CONST_ZERO);
  416. // }
  417. //
  418. // /**
  419. // * 根据应用渠道获取渠道奖励
  420. // *
  421. // * @param int $app_id 应用ID
  422. // * @param int $agent_id 渠道ID
  423. // *
  424. // *
  425. // * @return int CPA计价
  426. // */
  427. // public function getAgentRewardByGameAgent($app_id, $agent_id) {
  428. // $_data = $this->getInfoByGameAgent($app_id, $agent_id);
  429. //
  430. // return get_val($_data, 'agent_reward', 0);
  431. // }
  432. //
  433. // /**
  434. // * 根据应用渠道获取玩家奖励
  435. // *
  436. // * @param int $app_id 应用ID
  437. // * @param int $agent_id 渠道ID
  438. // *
  439. // *
  440. // * @return int CPA计价
  441. // */
  442. // public function getMemRewardByGameAgent($app_id, $agent_id) {
  443. // $_data = $this->getInfoByGameAgent($app_id, $agent_id);
  444. //
  445. // return get_val($_data, 'mem_reward', 0);
  446. // }
  447. }