123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- /**
- * MgRoleModel.php UTF-8
- * 玩家角色类
- *
- * @date : 2018/5/10 10:48
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : chenbingling <cbl@huosdk.com>
- * @version : HUOSDK 8.0
- */
- namespace huo\model\member;
- use huo\model\common\CommonModel;
- use huolib\constant\CacheConst;
- use huolib\constant\CommonConst;
- use huolib\tool\Time;
- use think\Cache;
- class MgRoleModel extends CommonModel {
- protected $name = 'mg_role';
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = true;
- protected $cache_tag = CacheConst::TAG_CACHE_SERVER_ROLE;
- public function mg() {
- return $this->belongsTo('huo\model\member\MemGameModel', 'mg_mem_id', 'id', [], 'left')->setEagerlyType(0);
- }
- public function game() {
- return $this->belongsTo('huo\model\game\GameModel', 'app_id', 'id')->field('id, name,icon');
- }
- /**
- * 游戏游戏图标获取器
- *
- * @param $value
- *
- * @return mixed
- */
- public function getExtAttr($value) {
- if (!empty($value)) {
- return json_decode($value, true);
- } else {
- return [];
- }
- }
- public function setExtAttr($value) {
- if (!empty($value)) {
- return json_encode($value);
- }
- return $value;
- }
- /**
- *
- *
- * @param int $mg_mem_id
- * @param int $app_id
- * @param string $server_id
- * @param string $role_id
- *
- * @return array|bool|false
- */
- public function getDetailByMemGameServerRole($mg_mem_id, $app_id, $server_id, $role_id) {
- $_map['mg_mem_id'] = $mg_mem_id;
- $_map['app_id'] = $app_id;
- $_map['server_id'] = $server_id;
- $_map['role_id'] = $role_id;
- $_data = $this->where($_map)->find();
- if (is_object($_data)) {
- $_data = $_data->toArray();
- }
- if (empty($_data)) {
- return false;
- }
- return $_data;
- }
- /**
- * 新增数据
- *
- * @param $data
- *
- * @return bool|mixed
- */
- public function addData($data) {
- $_data = $data;
- if ($_obj = self::create($_data, true)) {
- Cache::clear($this->cache_tag);
- return $_obj->id;
- } else {
- return false;
- }
- }
- /**
- * 更新数据
- *
- * @param array $data
- *
- * @param int $id
- *
- * @return bool
- */
- public function updateData($data, $id) {
- $_map['id'] = $id;
- $_data = $data;
- $_rs = self::update($_data, $_map, true);
- if (false == $_rs) {
- return false;
- } else {
- Cache::clear($this->cache_tag);
- return true;
- }
- }
- /**
- * 获取区服角色列表
- *
- * @param $map
- *
- * @param string $order
- *
- * @return array|false
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getServerRoles($map, $order = '-create_time') {
- $_order = $this->orderFilter($order);
- $_tag = $this->cache_tag;
- $_cache_key = md5($_tag.json_encode($map).json_encode($_order));
- $_field = 'id mg_role_id,mg_mem_id,server_id,server_name,role_id,role_name';
- $_srs = $this->field($_field)
- ->where($map)
- ->order($_order)
- ->cache($_cache_key, CommonConst::CONST_DAY_SECONDS, $_tag)
- ->select();
- if (is_object($_srs)) {
- $_srs = $_srs->toArray();
- }
- return $_srs;
- }
- /**
- * 获取最早创角天数
- *
- * @param $mg_mem_id
- *
- * @return int
- */
- public function getRoleDays($mg_mem_id) {
- $_map = ['mg_mem_id' => $mg_mem_id];
- $_create_time = $this->where($_map)->order('create_time asc')->value('create_time');
- if (empty($_create_time)) {
- return 0;
- }
- return Time::timeDateDiff($_create_time, time());
- }
- public function getFirstMgRoleData($mg_mem_ids) {
- $_map = ['mg_mem_id' => ['in', $mg_mem_ids]];
- $_mg_role_data = $this->where($_map)->order('id ASC')->find();
- if (is_object($_mg_role_data)) {
- $_mg_role_data = $_mg_role_data->toArray();
- }
- return $_mg_role_data;
- }
- public function getMaxMoneyMgRoleData($mg_mem_ids) {
- $_map = ['mg_mem_id' => ['in', $mg_mem_ids]];
- $_mg_role_data = $this->where($_map)->order('money DESC')->find();
- if (is_object($_mg_role_data)) {
- $_mg_role_data = $_mg_role_data->toArray();
- }
- return $_mg_role_data;
- }
- }
|