<?php
/**
 * MidasGmMemModel.php UTF-8
 * 米大师游戏币模型
 *
 * @date    : 2018/8/16 17:30
 *
 * @license 这不是一个自由软件,未经授权不许任何使用和传播。
 * @author  : wuyonghong <wyh@huosdk.com>
 * @version : HuoMp 1.0
 */

namespace huomp\model\wallet;

use huolib\constant\CacheConst;
use huomp\model\common\CommonModel;
use think\Cache;

class MidasGmMemModel extends CommonModel {
    protected $table = 'mp_midas_gm_mem';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    protected $cache_tag          = CacheConst::TAG_CACHE_MGM;
    protected $cache_key_prefix   = CacheConst::CACHE_GAME_MGM_PREFIX;

    /**
     * 关联game表
     *
     * @return mixed
     */
    public function game() {
        return $this->belongsTo('huo\model\game\GameModel', 'app_id', 'id')->field('id,name');
    }

    /**
     * 关联game_ext表
     *
     * @return mixed
     */
    public function mem() {
        return $this->belongsTo('huo\model\member\MemberModel', 'mem_id', 'id')->field('id,name');
    }

    /**
     * 添加数据
     *
     * @param array $data
     *
     * @return bool
     */
    public function addData($data) {
        if (empty($data) || empty($data['app_id']) || empty($data['mem_id'])) {
            return false;
        }
        $_data = $data;
        $_obj = self::create($_data, true);
        if ($_obj) {
            return true;
        }

        return false;
    }

    /**
     * 更新数据
     *
     * @param array $data 数据
     * @param int   $id   应用ID
     *
     * @return bool
     */
    public function updateData($data, $id) {
        $_map['id'] = $id;
        $_data = $data;
        $_rs = self::update($_data, $_map, true);
        if (false == $_rs) {
            return false;
        }
        $_cache_key = $this->cache_key_prefix.$_data['mem_id'].$_data['app_id'];
        Cache::tag($this->cache_tag)->rm($_cache_key);

        return true;
    }

    /**
     * 获取游戏币数据
     *
     * @param int $mem_id 玩家ID
     * @param int $app_id 应用ID
     *
     * @return array|bool|false
     */
    public function getDataByMemApp($mem_id, $app_id) {
        if (empty($mem_id) || empty($app_id)) {
            return false;
        }
        $_cache_key = $this->cache_key_prefix.$mem_id.$app_id;
        $_data = Cache::tag($this->cache_tag)->get($_cache_key);
        if (!empty($_data)) {
            return $_data;
        }
        $_map['mem_id'] = $mem_id;
        $_map['app_id'] = $app_id;
        $_data = $this->where($_map)->find();
        if (is_object($_data)) {
            $_data = $_data->toArray();
        }
        Cache::tag($this->cache_tag)->set($_cache_key, $_data);

        return $_data;
    }

    /**
     * 无数据则插入数据
     *
     * @param int $mem_id 玩家ID
     * @param int $app_id 应用ID
     *
     * @return array|bool|false
     */
    public function getDataOrInsert($mem_id, $app_id) {
        $_data = $this->getDataByMemApp($mem_id, $app_id);
        if (empty($_data)) {
            $_data['mem_id'] = $mem_id;
            $_data['app_id'] = $app_id;
            $this->addData($_data);
        }

        return $this->getDataByMemApp($mem_id, $app_id);
    }
}