* @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; } }