MgRoleModel.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * MgRoleModel.php UTF-8
  4. * 玩家角色类
  5. *
  6. * @date : 2018/5/10 10:48
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\member;
  13. use huo\model\common\CommonModel;
  14. use huolib\constant\CacheConst;
  15. use huolib\constant\CommonConst;
  16. use huolib\tool\Time;
  17. use think\Cache;
  18. class MgRoleModel extends CommonModel {
  19. protected $name = 'mg_role';
  20. // 开启自动写入时间戳字段
  21. protected $autoWriteTimestamp = true;
  22. protected $cache_tag = CacheConst::TAG_CACHE_SERVER_ROLE;
  23. public function mg() {
  24. return $this->belongsTo('huo\model\member\MemGameModel', 'mg_mem_id', 'id', [], 'left')->setEagerlyType(0);
  25. }
  26. public function game() {
  27. return $this->belongsTo('huo\model\game\GameModel', 'app_id', 'id')->field('id, name,icon');
  28. }
  29. /**
  30. * 游戏游戏图标获取器
  31. *
  32. * @param $value
  33. *
  34. * @return mixed
  35. */
  36. public function getExtAttr($value) {
  37. if (!empty($value)) {
  38. return json_decode($value, true);
  39. } else {
  40. return [];
  41. }
  42. }
  43. public function setExtAttr($value) {
  44. if (!empty($value)) {
  45. return json_encode($value);
  46. }
  47. return $value;
  48. }
  49. /**
  50. *
  51. *
  52. * @param int $mg_mem_id
  53. * @param int $app_id
  54. * @param string $server_id
  55. * @param string $role_id
  56. *
  57. * @return array|bool|false
  58. */
  59. public function getDetailByMemGameServerRole($mg_mem_id, $app_id, $server_id, $role_id) {
  60. $_map['mg_mem_id'] = $mg_mem_id;
  61. $_map['app_id'] = $app_id;
  62. $_map['server_id'] = $server_id;
  63. $_map['role_id'] = $role_id;
  64. $_data = $this->where($_map)->find();
  65. if (is_object($_data)) {
  66. $_data = $_data->toArray();
  67. }
  68. if (empty($_data)) {
  69. return false;
  70. }
  71. return $_data;
  72. }
  73. /**
  74. * 新增数据
  75. *
  76. * @param $data
  77. *
  78. * @return bool|mixed
  79. */
  80. public function addData($data) {
  81. $_data = $data;
  82. if ($_obj = self::create($_data, true)) {
  83. Cache::clear($this->cache_tag);
  84. return $_obj->id;
  85. } else {
  86. return false;
  87. }
  88. }
  89. /**
  90. * 更新数据
  91. *
  92. * @param array $data
  93. *
  94. * @param int $id
  95. *
  96. * @return bool
  97. */
  98. public function updateData($data, $id) {
  99. $_map['id'] = $id;
  100. $_data = $data;
  101. $_rs = self::update($_data, $_map, true);
  102. if (false == $_rs) {
  103. return false;
  104. } else {
  105. Cache::clear($this->cache_tag);
  106. return true;
  107. }
  108. }
  109. /**
  110. * 获取区服角色列表
  111. *
  112. * @param $map
  113. *
  114. * @param string $order
  115. *
  116. * @return array|false
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\ModelNotFoundException
  119. * @throws \think\exception\DbException
  120. */
  121. public function getServerRoles($map, $order = '-create_time') {
  122. $_order = $this->orderFilter($order);
  123. $_tag = $this->cache_tag;
  124. $_cache_key = md5($_tag.json_encode($map).json_encode($_order));
  125. $_field = 'id mg_role_id,mg_mem_id,server_id,server_name,role_id,role_name';
  126. $_srs = $this->field($_field)
  127. ->where($map)
  128. ->order($_order)
  129. ->cache($_cache_key, CommonConst::CONST_DAY_SECONDS, $_tag)
  130. ->select();
  131. if (is_object($_srs)) {
  132. $_srs = $_srs->toArray();
  133. }
  134. return $_srs;
  135. }
  136. /**
  137. * 获取最早创角天数
  138. *
  139. * @param $mg_mem_id
  140. *
  141. * @return int
  142. */
  143. public function getRoleDays($mg_mem_id) {
  144. $_map = ['mg_mem_id' => $mg_mem_id];
  145. $_create_time = $this->where($_map)->order('create_time asc')->value('create_time');
  146. if (empty($_create_time)) {
  147. return 0;
  148. }
  149. return Time::timeDateDiff($_create_time, time());
  150. }
  151. public function getFirstMgRoleData($mg_mem_ids) {
  152. $_map = ['mg_mem_id' => ['in', $mg_mem_ids]];
  153. $_mg_role_data = $this->where($_map)->order('id ASC')->find();
  154. if (is_object($_mg_role_data)) {
  155. $_mg_role_data = $_mg_role_data->toArray();
  156. }
  157. return $_mg_role_data;
  158. }
  159. public function getMaxMoneyMgRoleData($mg_mem_ids) {
  160. $_map = ['mg_mem_id' => ['in', $mg_mem_ids]];
  161. $_mg_role_data = $this->where($_map)->order('money DESC')->find();
  162. if (is_object($_mg_role_data)) {
  163. $_mg_role_data = $_mg_role_data->toArray();
  164. }
  165. return $_mg_role_data;
  166. }
  167. }