BookListCh.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Redis;
  4. use think\Model;
  5. class BookListCh extends Model
  6. {
  7. // 表名
  8. protected $table = 'book_list_ch';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. // 追加属性
  15. protected $append = [
  16. 'status_text',
  17. ];
  18. public function getStatusList()
  19. {
  20. return ['normal' => __('Status normal'),'hidden' => __('Status hidden')];
  21. }
  22. public function getStatusTextAttr($value, $data)
  23. {
  24. $value = $value ? $value : $data['status'];
  25. $list = $this->getStatusList();
  26. return isset($list[$value]) ? $list[$value] : '';
  27. }
  28. /**
  29. * 返回详情
  30. * @param $id
  31. * @return array
  32. */
  33. public function getInfo($id)
  34. {
  35. $result = [];
  36. $redis = Redis::instance();
  37. $key = 'BLPC:'. $id; //书单信息缓存
  38. if ($redis->exists($key)) {
  39. $result = $redis->hGetAll($key);
  40. } else {
  41. $row = $this->where('id', 'EQ', $id)->find();
  42. if (!empty($row)) {
  43. $data = $row->toArray();
  44. //如果是vip 可以存一下所有的子账号
  45. $createAdminId = $row['admin_id'];
  46. $bindRows = model("VipAdminBind")
  47. ->where('admin_id_master', 'eq', $createAdminId)
  48. ->select();
  49. if ($bindRows) {
  50. $adminIds = array_column($bindRows, 'admin_id_slave');
  51. $data['admin_ids'] = json_encode($adminIds);
  52. } else {
  53. $data['admin_ids'] = json_encode([$createAdminId]);
  54. }
  55. $redis->hMSet($key, $data);
  56. $redis->expire($key, 600);
  57. $result = $data;
  58. }
  59. }
  60. return $result;
  61. }
  62. public function rmCache($id)
  63. {
  64. if (!empty($id)) {
  65. $redis = Redis::instance();
  66. $key = 'BLPC:'. $id; //书单信息缓存
  67. if ($redis->exists($key)) {
  68. $redis->del($key);
  69. }
  70. }
  71. return true;
  72. }
  73. }