MemFakerModel.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * MemFakerModel.php UTF-8
  4. * 玩家假数据
  5. *
  6. * @date : 2018/6/27 17:57
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : luowei <lwl@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huomp\model\fill;
  13. use huolib\constant\CacheConst;
  14. use huolib\constant\CommonConst;
  15. use huomp\model\common\CommonModel;
  16. use think\Cache;
  17. class MemFakerModel extends CommonModel {
  18. protected $cache_key_prefix = CacheConst::CACHE_MEM_RANK_FAKER_PREFIX;
  19. protected $tag = CacheConst::CACHE_MEM_RANK_FAKER_TAG;
  20. protected $table = 'mp_mem_faker';
  21. // 开启自动写入时间戳字段
  22. protected $autoWriteTimestamp = true;
  23. /**
  24. * @param array $data
  25. *
  26. * @return bool|mixed
  27. */
  28. public function addData($data = []) {
  29. if ($_obj = self::create($data, true)) {
  30. Cache::clear($this->tag);
  31. return $_obj->id;
  32. } else {
  33. return false;
  34. }
  35. }
  36. /**
  37. * @param array $data
  38. * @param int $id
  39. *
  40. * @return bool|mixed
  41. */
  42. public function updateData($data, $id) {
  43. $_map['id'] = $id;
  44. $_data = $data;
  45. $_rs = self::update($_data, $_map, true);
  46. if (false === $_rs) {
  47. return false;
  48. } else {
  49. $_cache_key = $this->cache_key_prefix.$id;
  50. Cache::rm($_cache_key);
  51. return true;
  52. }
  53. }
  54. /**
  55. * @param int $id
  56. *
  57. * @return array|false
  58. */
  59. public function getDetail($id) {
  60. $_cache_key = $this->cache_key_prefix.$id;
  61. $_data = Cache::get($_cache_key);
  62. if (!empty($_data)) {
  63. return json_decode($_data, true);
  64. }
  65. $_map['id'] = $id;
  66. $_data = $this->where($_map)->find();
  67. if (empty($_data)) {
  68. return [];
  69. }
  70. if (is_object($_data)) {
  71. $_data = $_data->toArray();
  72. }
  73. if (!empty($_data)) {
  74. Cache::tag($this->tag, $_cache_key)->set($_cache_key, json_encode($_data));
  75. }
  76. return $_data;
  77. }
  78. /**
  79. * 获取列表
  80. *
  81. * @param array $ids
  82. *
  83. * @return array
  84. */
  85. public function getList($ids = []) {
  86. $_map = [];
  87. if (!empty($ids)) {
  88. $_map['id'] = ['in', $ids];
  89. }
  90. $_map['is_delete'] = CommonConst::CONST_NOT_DELETE;
  91. return self::where($_map)->select()->toArray();
  92. }
  93. /**
  94. * 获取头像列表
  95. */
  96. public function getAvatars() {
  97. $_cache_key = $this->cache_key_prefix.'avatars';
  98. $_data = Cache::get($_cache_key);
  99. if (!empty($_data)) {
  100. return json_decode($_data, true);
  101. }
  102. $_avatars = $this->column('avatar');
  103. if (!empty($_avatars)) {
  104. Cache::tag($this->tag, $_cache_key)->set($_cache_key, json_encode($_avatars));
  105. }
  106. return $_avatars;
  107. }
  108. }