MidasGmMemModel.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * MidasGmMemModel.php UTF-8
  4. * 米大师游戏币模型
  5. *
  6. * @date : 2018/8/16 17:30
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huomp\model\wallet;
  13. use huolib\constant\CacheConst;
  14. use huomp\model\common\CommonModel;
  15. use think\Cache;
  16. class MidasGmMemModel extends CommonModel {
  17. protected $table = 'mp_midas_gm_mem';
  18. // 开启自动写入时间戳字段
  19. protected $autoWriteTimestamp = true;
  20. protected $cache_tag = CacheConst::TAG_CACHE_MGM;
  21. protected $cache_key_prefix = CacheConst::CACHE_GAME_MGM_PREFIX;
  22. /**
  23. * 关联game表
  24. *
  25. * @return mixed
  26. */
  27. public function game() {
  28. return $this->belongsTo('huo\model\game\GameModel', 'app_id', 'id')->field('id,name');
  29. }
  30. /**
  31. * 关联game_ext表
  32. *
  33. * @return mixed
  34. */
  35. public function mem() {
  36. return $this->belongsTo('huo\model\member\MemberModel', 'mem_id', 'id')->field('id,name');
  37. }
  38. /**
  39. * 添加数据
  40. *
  41. * @param array $data
  42. *
  43. * @return bool
  44. */
  45. public function addData($data) {
  46. if (empty($data) || empty($data['app_id']) || empty($data['mem_id'])) {
  47. return false;
  48. }
  49. $_data = $data;
  50. $_obj = self::create($_data, true);
  51. if ($_obj) {
  52. return true;
  53. }
  54. return false;
  55. }
  56. /**
  57. * 更新数据
  58. *
  59. * @param array $data 数据
  60. * @param int $id 应用ID
  61. *
  62. * @return bool
  63. */
  64. public function updateData($data, $id) {
  65. $_map['id'] = $id;
  66. $_data = $data;
  67. $_rs = self::update($_data, $_map, true);
  68. if (false == $_rs) {
  69. return false;
  70. }
  71. $_cache_key = $this->cache_key_prefix.$_data['mem_id'].$_data['app_id'];
  72. Cache::tag($this->cache_tag)->rm($_cache_key);
  73. return true;
  74. }
  75. /**
  76. * 获取游戏币数据
  77. *
  78. * @param int $mem_id 玩家ID
  79. * @param int $app_id 应用ID
  80. *
  81. * @return array|bool|false
  82. */
  83. public function getDataByMemApp($mem_id, $app_id) {
  84. if (empty($mem_id) || empty($app_id)) {
  85. return false;
  86. }
  87. $_cache_key = $this->cache_key_prefix.$mem_id.$app_id;
  88. $_data = Cache::tag($this->cache_tag)->get($_cache_key);
  89. if (!empty($_data)) {
  90. return $_data;
  91. }
  92. $_map['mem_id'] = $mem_id;
  93. $_map['app_id'] = $app_id;
  94. $_data = $this->where($_map)->find();
  95. if (is_object($_data)) {
  96. $_data = $_data->toArray();
  97. }
  98. Cache::tag($this->cache_tag)->set($_cache_key, $_data);
  99. return $_data;
  100. }
  101. /**
  102. * 无数据则插入数据
  103. *
  104. * @param int $mem_id 玩家ID
  105. * @param int $app_id 应用ID
  106. *
  107. * @return array|bool|false
  108. */
  109. public function getDataOrInsert($mem_id, $app_id) {
  110. $_data = $this->getDataByMemApp($mem_id, $app_id);
  111. if (empty($_data)) {
  112. $_data['mem_id'] = $mem_id;
  113. $_data['app_id'] = $app_id;
  114. $this->addData($_data);
  115. }
  116. return $this->getDataByMemApp($mem_id, $app_id);
  117. }
  118. }