HpVisitorModel.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * HpVisitorModel.php UTF-8
  4. * 首页访问表
  5. *
  6. * @date : 2018/9/20 15:04
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huomp\model\homepage;
  13. use huolib\constant\CacheConst;
  14. use huomp\model\common\CommonModel;
  15. use think\Cache;
  16. class HpVisitorModel extends CommonModel {
  17. protected $table = 'mp_hp_visitor';
  18. // 开启自动写入时间戳字段
  19. protected $autoWriteTimestamp = true;
  20. protected $cache_key_prefix = CacheConst::CACHE_HOME_VISITOR_PREFIX;
  21. public function visitor() {
  22. return $this->belongsTo('huo\model\member\MemberModel', 'mem_id', 'id')->setEagerlyType(0);
  23. }
  24. /**
  25. * 添加数据
  26. *
  27. * @param $data
  28. *
  29. * @return bool
  30. */
  31. public function addData($data) {
  32. if (empty($data)) {
  33. return false;
  34. }
  35. $_data = $data;
  36. $_obj = self::create($_data, true);
  37. if ($_obj) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. /**
  43. * 更新数据
  44. *
  45. * @param int $parent_mem_id
  46. * @param int $mem_id
  47. *
  48. * @return bool
  49. */
  50. public function replaceData($parent_mem_id, $mem_id) {
  51. $_data = $this->getInfoByMemSub($parent_mem_id, $mem_id);
  52. if (!empty($_data)) {
  53. $_data['update_time'] = time();
  54. return $this->updateData($_data, $_data['id']);
  55. } else {
  56. $_data['parent_mem_id'] = $parent_mem_id;
  57. $_data['mem_id'] = $mem_id;
  58. $_data['create_time'] = time();
  59. $_data['update_time'] = $_data['create_time'];
  60. return $this->addData($_data);
  61. }
  62. }
  63. /**
  64. * 更新数据
  65. *
  66. * @param array $data 数据
  67. * @param int $id ID
  68. *
  69. * @return bool
  70. */
  71. public function updateData($data, $id) {
  72. $_map['id'] = $id;
  73. $_data = $data;
  74. $_rs = self::update($_data, $_map, true);
  75. if (false == $_rs) {
  76. return false;
  77. }
  78. $_cache_data = $this->getInfoByMemSub($_data['parent_mem_id'], $_data['mem_id']);
  79. $_cache_data = array_merge($_cache_data, $_data);
  80. $_cache_key = $this->cache_key_prefix.$data['parent_mem_id'].$data['mem_id'];
  81. Cache::set($_cache_key, $_cache_data);
  82. return true;
  83. }
  84. /**
  85. * 获取Banner图
  86. *
  87. * @param int $parent_mem_id
  88. * @param int $mem_id
  89. *
  90. * @return array|bool|false
  91. */
  92. public function getInfoByMemSub($parent_mem_id, $mem_id) {
  93. $_cache_key = $this->cache_key_prefix.$parent_mem_id.$mem_id;
  94. $_data = Cache::get($_cache_key);
  95. if (!empty($_data)) {
  96. return $_data;
  97. }
  98. $_map['parent_mem_id'] = $parent_mem_id;
  99. $_map['mem_id'] = $mem_id;
  100. $_info = $this->where($_map)->find();
  101. if (false === $_info) {
  102. return false;
  103. }
  104. if (is_object($_info)) {
  105. $_info = $_info->toArray();
  106. }
  107. Cache::set($_cache_key, $_info);
  108. return $_info;
  109. }
  110. }