MemItgLogModel.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * MemItgLogModel.php UTF-8
  4. * 玩家积分记录
  5. *
  6. * @date : 2018/5/5 14:40
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\integral;
  13. use huo\model\common\CommonModel;
  14. use huolib\constant\CacheConst;
  15. use huolib\constant\IaConst;
  16. use huolib\constant\MemItgConst;
  17. use huolib\tool\Time;
  18. use think\Cache;
  19. class MemItgLogModel extends CommonModel {
  20. protected $name = 'mem_itg_log';
  21. protected $cache_prefix = CacheConst::CACHE_MEM_ITG_SUB_CNT_PREFIX;
  22. public function setTypeAttr($value) {
  23. }
  24. /**
  25. * 关联玩家表
  26. */
  27. public function mem() {
  28. return $this->hasOne('huo\model\member\MemberModel', 'id', 'mem_id', [], 'left');
  29. }
  30. /**
  31. * 关联玩家表
  32. */
  33. public function member() {
  34. return $this->belongsTo('huo\model\member\MemberModel', 'mem_id', 'id')->field(
  35. 'id,username,nickname,avatar,mobile,real_name'
  36. );
  37. }
  38. /**
  39. * 关联下级玩家表
  40. */
  41. public function submem() {
  42. return $this->belongsTo('huo\model\member\MemberModel', 'link_id', 'id')->field('id,nickname,avatar');
  43. }
  44. /**
  45. * 添加玩家积分LOG
  46. *
  47. * @param array $data
  48. *
  49. * @return bool|mixed
  50. */
  51. public function addMemIaLog($data) {
  52. $_data['mem_id'] = get_val($data, 'mem_id', 0);
  53. $_data['itg_type'] = get_val($data, 'itg_type', 0);
  54. $_data['integral_total'] = get_val($data, 'integral_total', 0);
  55. $_data['my_integral'] = get_val($data, 'my_integral', 0);
  56. $_data['integral'] = get_val($data, 'integral', 0);
  57. $_data['ia_id'] = get_val($data, 'ia_id', 0);
  58. $_data['sub_ia_id'] = get_val($data, 'sub_ia_id', 0);
  59. $_data['ia_name'] = get_val($data, 'ia_name', '');
  60. $_data['link_table'] = get_val($data, 'link_table', '');
  61. $_data['link_id'] = get_val($data, 'link_id', 0);
  62. $_data['create_time'] = time();
  63. $_app_id = get_val($data, 'app_id', 0);
  64. if (empty($_data)) {
  65. return false;
  66. }
  67. if ($_obj = self::create($_data)) {
  68. $_map['mem_id'] = $_data['mem_id'];
  69. $_map['ia_id'] = $_data['ia_id'];
  70. if ($_data['sub_ia_id'] != $_app_id) {
  71. $_map['sub_ia_id'] = $_data['sub_ia_id'];
  72. $_cnt = $this->getSubCnt($_map['mem_id'], $_map['ia_id'], $_map['sub_ia_id']);
  73. } else {
  74. $_cnt = $this->getSubCnt($_map['mem_id'], $_map['ia_id']);
  75. }
  76. $_cache_key = $this->cache_prefix.md5(json_encode($_map));
  77. $_cnt++;
  78. Cache::set($_cache_key, $_cnt);
  79. if (IaConst::IA_SHARE == $_data['ia_id']) {
  80. unset($_map);
  81. $_map['mem_id'] = $_data['mem_id'];
  82. $_map['itg_type'] = MemItgConst::MEM_ITG_ADD;
  83. $_map['ia_id'] = $_data['ia_id'];
  84. $_map['sub_ia_id'] = $_data['sub_ia_id'];
  85. $_map['link_table'] = $_data['link_table'];
  86. $_map['link_id'] = $_data['link_id'];
  87. $_share_cache_key = $this->cache_prefix.md5(json_encode($_map));
  88. $_cnt = $this->getShareCnt($_map['mem_id'], $_map['sub_ia_id'], $_map['link_id']);
  89. $_cnt++;
  90. Cache::set($_share_cache_key, $_cnt);
  91. }
  92. return $_obj->id;
  93. } else {
  94. return false;
  95. }
  96. }
  97. /**
  98. * 获取子任务数量
  99. *
  100. * @param int $mem_id
  101. * @param int $ia_id
  102. * @param int $sub_ia_id
  103. *
  104. * @return int|string
  105. */
  106. public function getSubCnt($mem_id, $ia_id, $sub_ia_id = 0) {
  107. $_map['mem_id'] = $mem_id;
  108. $_map['ia_id'] = $ia_id;
  109. if (!empty($sub_ia_id)) {
  110. $_map['sub_ia_id'] = $sub_ia_id;
  111. }
  112. $_cache_key = $this->cache_prefix.md5(json_encode($_map));
  113. $_cnt = Cache::get($_cache_key);
  114. if (is_numeric($_cnt)) {
  115. return $_cnt;
  116. }
  117. $_cnt = $this->where($_map)->count();
  118. if (empty($_cnt)) {
  119. $_cnt = 0;
  120. }
  121. Cache::set($_cache_key, $_cnt);
  122. return $_cnt;
  123. }
  124. /**
  125. * 判断是否获取过奖励
  126. *
  127. * @param int $mem_id
  128. * @param int $sub_ia_id
  129. * @param int $link_id
  130. *
  131. * @return int|string
  132. */
  133. public function getShareCnt($mem_id, $sub_ia_id = 0, $link_id) {
  134. $_ia_id = IaConst::IA_SHARE;
  135. $_map['mem_id'] = $mem_id;
  136. $_map['itg_type'] = MemItgConst::MEM_ITG_ADD;
  137. $_map['ia_id'] = $_ia_id;
  138. $_map['sub_ia_id'] = $sub_ia_id;
  139. $_map['link_table'] = 'member';
  140. $_map['link_id'] = $link_id;
  141. $_cache_key = $this->cache_prefix.md5(json_encode($_map));
  142. $_cnt = Cache::get($_cache_key);
  143. if (is_numeric($_cnt)) {
  144. return $_cnt;
  145. }
  146. $_cnt = $this->where($_map)->count();
  147. if (empty($_cnt)) {
  148. $_cnt = 0;
  149. }
  150. Cache::set($_cache_key, $_cnt);
  151. return $_cnt;
  152. }
  153. /**
  154. * 获取玩家今日任务领取奖励次数
  155. *
  156. * @param $ia_id
  157. * @param $mem_id
  158. * @param int $sub_ia_id
  159. *
  160. * @return int|string
  161. */
  162. public function getMemIaTodayCnt($ia_id, $mem_id, $sub_ia_id = 0) {
  163. list($today_start, $today_end) = Time::today();
  164. $_map['mem_id'] = $mem_id;
  165. $_map['itg_type'] = MemItgConst::MEM_ITG_ADD;
  166. $_map['ia_id'] = $ia_id;
  167. $_map['sub_ia_id'] = $sub_ia_id;
  168. $_map['create_time'] = ['gt', $today_start];
  169. $_cnt = $this->where($_map)->count();
  170. if (empty($_cnt)) {
  171. $_cnt = 0;
  172. }
  173. return $_cnt;
  174. }
  175. }