BookChargeSetting.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Redis;
  4. use app\main\constants\CacheConstants;
  5. use think\Model;
  6. class BookChargeSetting extends Model
  7. {
  8. // 表名
  9. protected $table = 'book_charge_setting';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = 'updatetime';
  15. // 追加属性
  16. protected $append = [
  17. 'status_text'
  18. ];
  19. public function getStatusList()
  20. {
  21. return ['normal' => __('Status normal'),'hide' => __('Status hide')];
  22. }
  23. public function getStatusTextAttr($value, $data)
  24. {
  25. $value = $value ? $value : $data['status'];
  26. $list = $this->getStatusList();
  27. return isset($list[$value]) ? $list[$value] : '';
  28. }
  29. /**
  30. * 获取收费书籍信息
  31. * @param $channel_id
  32. * @param $book_id
  33. * @return array
  34. */
  35. public function getInfo($channel_id, $book_id)
  36. {
  37. $cache = CacheConstants::getChapterFeeCache($channel_id, $book_id);
  38. $data = Redis::instance()->hGetAll($cache);
  39. if (!$data) {
  40. $info = $this->where('book_id', $book_id)
  41. ->where('admin_id', $channel_id)
  42. ->where('status', 'normal')
  43. ->find();
  44. if ($info) {
  45. $data = [
  46. 'id' => $info['id'],
  47. 'chapter_charge_start' => $info->getData('chapter_charge_start'),
  48. 'chapter_kandian' => $info->getData('chapter_kandian'),
  49. ];
  50. } else {
  51. $data = ['id' => 0];
  52. }
  53. Redis::instance()->hMSet($cache, $data);
  54. Redis::instance()->expire($cache, 600);
  55. }
  56. if (!$data['id']) {
  57. return [];
  58. } else {
  59. return $data;
  60. }
  61. }
  62. /**
  63. * 清除缓存
  64. * @param $channel_id
  65. * @param $book_id
  66. */
  67. public function clearInfo($channel_id, $book_id)
  68. {
  69. $cache = CacheConstants::getChapterFeeCache($channel_id, $book_id);
  70. Redis::instance()->del($cache);
  71. }
  72. }