ExportFans.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Redis;
  4. use app\main\constants\CacheConstants;
  5. use app\main\constants\ExportFansConstants;
  6. use think\Model;
  7. class ExportFans extends Model
  8. {
  9. // 表名
  10. protected $table = 'export_fans';
  11. // 自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'int';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = 'updatetime';
  16. // 追加属性
  17. protected $append = [
  18. 'status_text',
  19. 'pay_status_text',
  20. 'limittime_text'
  21. ];
  22. public function getStatusList()
  23. {
  24. return ['1' => __('Status 1'),'2' => __('Status 2')];
  25. }
  26. public function getPayStatusList()
  27. {
  28. return ['1' => __('Pay_status 1'),'2' => __('Pay_status 2')];
  29. }
  30. public function getStatusTextAttr($value, $data)
  31. {
  32. $value = $value ? $value : $data['status'];
  33. $list = $this->getStatusList();
  34. return isset($list[$value]) ? $list[$value] : '';
  35. }
  36. public function getPayStatusTextAttr($value, $data)
  37. {
  38. $value = $value ? $value : $data['pay_status'];
  39. $list = $this->getPayStatusList();
  40. return isset($list[$value]) ? $list[$value] : '';
  41. }
  42. public function getLimittimeTextAttr($value, $data)
  43. {
  44. $value = $value ? $value : $data['limittime'];
  45. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  46. }
  47. protected function setLimittimeAttr($value)
  48. {
  49. return $value && !is_numeric($value) ? strtotime($value) : $value;
  50. }
  51. public function getStarttimeTextAttr($value, $data)
  52. {
  53. $value = $value ? $value : $data['starttime'];
  54. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  55. }
  56. protected function setStarttimeAttr($value)
  57. {
  58. return $value && !is_numeric($value) ? strtotime($value) : $value;
  59. }
  60. public function getOne($id)
  61. {
  62. $cacheKey = CacheConstants::getExportFansRow($id);
  63. if (!$data = Redis::instance()->hGetAll($cacheKey)) {
  64. $data = $this->where('id', $id)->where('status', ExportFansConstants::STATUS_YES)->find();
  65. if ($data) {
  66. $data = $data->getData();
  67. Redis::instance()->hMSet($cacheKey, $data);
  68. } else {
  69. $data = ['id' => 0];
  70. Redis::instance()->hMSet($cacheKey, $data);
  71. }
  72. Redis::instance()->expire($cacheKey, 1800);
  73. }
  74. if (!$data['id']) {
  75. $data = false;
  76. }
  77. return $data;
  78. }
  79. }