AgentCpaLogModel.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * AgentCpaLogModel.php UTF-8
  4. * 渠道CPA记录表
  5. *
  6. * @date : 2020/3/7 16:15
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOOA 1.0
  11. */
  12. namespace huo\model\agent;
  13. use huo\model\common\CommonModel;
  14. use huolib\constant\CacheConst;
  15. use huolib\constant\CommonConst;
  16. use think\Cache;
  17. class AgentCpaLogModel extends CommonModel {
  18. protected $name = 'agent_cpa_log';
  19. // 开启自动写入时间戳字段
  20. protected $autoWriteTimestamp = true;
  21. protected $cache_key_prefix = CacheConst::CACHE_AGENT_CPA_LOG_PREFIX;
  22. /**
  23. * 获取单条记录缓存key
  24. *
  25. * @param int $id ID
  26. *
  27. * @return string
  28. */
  29. protected function getSingleCacheKey($id) {
  30. return $this->cache_key_prefix.$id;
  31. }
  32. /**
  33. * 获取单条记录缓存key
  34. *
  35. * @param $ip
  36. *
  37. * @return string
  38. */
  39. protected function getCacheKeyByIp($ip) {
  40. if (false == is_int($ip)) {
  41. $ip = ip2long($ip);
  42. }
  43. return $this->cache_key_prefix.'ip_'.$ip;
  44. }
  45. /**
  46. * 添加数据
  47. *
  48. * @param array $data 需要添加的数据
  49. *
  50. * @return false|int 添加失败返回 false 添加成功 返回添加的ID
  51. */
  52. public function addData($data) {
  53. if (empty($data)) {
  54. return false;
  55. }
  56. $_data = $data;
  57. $_model = new static();
  58. $_rs = $_model->allowField(true)->isUpdate(false)->save($_data, []);
  59. if (false === $_rs) {
  60. return false;
  61. }
  62. $_id = $_model->getLastInsID();
  63. /* TAG缓存操作 */
  64. return $_id;
  65. }
  66. /**
  67. * 通过ID获取信息
  68. *
  69. * @param int $id 主键ID
  70. *
  71. * @return array|false
  72. */
  73. public function getInfoById($id) {
  74. /* 缓存操作 */
  75. $_single_cache_key = $this->getSingleCacheKey($id);
  76. $_data = Cache::get($_single_cache_key);
  77. if (!empty($_data)) {
  78. return $_data;
  79. }
  80. $_data = parent::getInfoById($id);
  81. if (empty($_data)) {
  82. return [];
  83. }
  84. Cache::set($_single_cache_key, $_data);
  85. return $_data;
  86. }
  87. /**
  88. * 更新单条数据
  89. *
  90. * @param array $data 数据
  91. * @param int $id ID
  92. *
  93. * @return bool
  94. */
  95. public function updateData($data, $id) {
  96. $_old_data = $this->getInfoById($id);
  97. $_data = $data;
  98. $_map = ['id' => $id];
  99. $_model = new static();
  100. $_rs = $_model->allowField(true)->isUpdate(true)->save($_data, $_map);
  101. if (false === $_rs) {
  102. return false;
  103. }
  104. /* 缓存操作 */
  105. $_single_cache_key = $this->getSingleCacheKey($id);
  106. Cache::rm($_single_cache_key);
  107. $_cache_key = $this->getCacheKeyByIp($_old_data['ip']);
  108. Cache::rm($_cache_key);
  109. /* TAG缓存操作 */
  110. return true;
  111. }
  112. /**
  113. * 删除单条数据
  114. *
  115. * @param int $id ID
  116. * @param bool $is_complete 是否完成删除
  117. *
  118. * @return bool
  119. */
  120. public function deleteData($id, $is_complete = true) {
  121. $_old_data = $this->getInfoById($id);
  122. $_map['id'] = $id;
  123. $_rs = $this->useGlobalScope(false)->where($_map)->delete();
  124. if (false == $_rs) {
  125. return false;
  126. }
  127. /* 缓存操作 */
  128. $_single_cache_key = $this->getSingleCacheKey($id);
  129. Cache::rm($_single_cache_key);
  130. $_cache_key = $this->getCacheKeyByIp($_old_data['ip']);
  131. Cache::rm($_cache_key);
  132. return $_rs;
  133. }
  134. /***
  135. * 根据玩家id获取id
  136. *
  137. * @param $mem_id
  138. *
  139. * @return int
  140. */
  141. public function getIdByMemId($mem_id) {
  142. if (empty($mem_id)) {
  143. return CommonConst::CONST_ZERO;
  144. }
  145. $_map = [
  146. 'mem_id' => $mem_id
  147. ];
  148. return $this->where($_map)->value('id');
  149. }
  150. /**
  151. * 根据ip获取id
  152. *
  153. * @param $ip
  154. *
  155. * @return mixed
  156. */
  157. public function getIdByIp($ip) {
  158. $_cache_key = $this->getCacheKeyByIp($ip);
  159. $_id = Cache::get($_cache_key);
  160. if (!empty($_id)) {
  161. return $_id;
  162. }
  163. $_map = [
  164. 'ip' => $ip
  165. ];
  166. $_id = $this->where($_map)->value('id');
  167. if (empty($_id)) {
  168. return CommonConst::CONST_ZERO;
  169. }
  170. Cache::set($_cache_key, $_id, CommonConst::CONST_DAY_SECONDS);
  171. return $_id;
  172. }
  173. }