Trainactive.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. use think\Db;
  6. class Trainactive extends Model
  7. {
  8. use SoftDelete;
  9. // 表名
  10. protected $table = 'train_active';
  11. // 自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'integer';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = false;
  16. protected $deleteTime = 'deletetime';
  17. // 追加属性
  18. protected $append = [
  19. 'starttime_text',
  20. 'endtime_text',
  21. 'pingjia_time_text',
  22. 'sign_starttime_text',
  23. 'sign_endtime_text',
  24. 'status_text',
  25. 'userauth_status_text'
  26. ];
  27. protected static function init(){
  28. //文件名
  29. self::afterUpdate(function ($row) {
  30. $update = [];
  31. $changed = $row->getChangedData();
  32. if (isset($changed['files'])) {
  33. //文件
  34. $files = Db::name('attachment')->where('url','IN',$row['files'])->field('url,filename')->select();
  35. $update['files_json'] = json_encode($files);
  36. }
  37. //评价时间不能是null
  38. if(empty($row['pingjia_time'])){
  39. $update['pingjia_time'] = 0;
  40. }
  41. if(!empty($update)){
  42. Db::name('train_active')->where('id',$row['id'])->update($update);
  43. }
  44. });
  45. //文件名
  46. self::afterInsert(function ($row) {
  47. if (!empty($row['files'])) {
  48. $files = Db::name('attachment')->where('url','IN',$row['files'])->field('url,filename')->select();
  49. $update = ['files_json'=>json_encode($files)];
  50. Db::name('train_active')->where('id',$row['id'])->update($update);
  51. }
  52. });
  53. }
  54. public function getStatusList()
  55. {
  56. return ['1' => __('Status 1'), '0' => __('Status 0')];
  57. }
  58. public function getUserauthStatusList()
  59. {
  60. return ['0' => __('Userauth_status 0'), '1' => __('Userauth_status 1')];
  61. }
  62. public function getStarttimeTextAttr($value, $data)
  63. {
  64. $value = $value ? $value : (isset($data['starttime']) ? $data['starttime'] : '');
  65. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  66. }
  67. public function getEndtimeTextAttr($value, $data)
  68. {
  69. $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
  70. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  71. }
  72. public function getPingjiaTimeTextAttr($value, $data)
  73. {
  74. $value = $value ? $value : (isset($data['pingjia_time']) ? $data['pingjia_time'] : '');
  75. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  76. }
  77. public function getSignStarttimeTextAttr($value, $data)
  78. {
  79. $value = $value ? $value : (isset($data['sign_starttime']) ? $data['sign_starttime'] : '');
  80. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  81. }
  82. public function getSignEndtimeTextAttr($value, $data)
  83. {
  84. $value = $value ? $value : (isset($data['sign_endtime']) ? $data['sign_endtime'] : '');
  85. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  86. }
  87. public function getStatusTextAttr($value, $data)
  88. {
  89. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  90. $list = $this->getStatusList();
  91. return isset($list[$value]) ? $list[$value] : '';
  92. }
  93. public function getUserauthStatusTextAttr($value, $data)
  94. {
  95. $value = $value ? $value : (isset($data['userauth_status']) ? $data['userauth_status'] : '');
  96. $list = $this->getUserauthStatusList();
  97. return isset($list[$value]) ? $list[$value] : '';
  98. }
  99. protected function setStarttimeAttr($value)
  100. {
  101. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  102. }
  103. protected function setEndtimeAttr($value)
  104. {
  105. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  106. }
  107. protected function setPingjiaTimeAttr($value)
  108. {
  109. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  110. }
  111. protected function setSignStarttimeAttr($value)
  112. {
  113. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  114. }
  115. protected function setSignEndtimeAttr($value)
  116. {
  117. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  118. }
  119. public function cate()
  120. {
  121. return $this->belongsTo('app\admin\model\Traincate', 'cate_id', 'id', [], 'LEFT')->setEagerlyType(0);
  122. }
  123. public function level()
  124. {
  125. return $this->belongsTo('app\admin\model\Trainlevel', 'level_id', 'id', [], 'LEFT')->setEagerlyType(0);
  126. }
  127. public function type()
  128. {
  129. return $this->belongsTo('app\admin\model\Traintype', 'type_id', 'id', [], 'LEFT')->setEagerlyType(0);
  130. }
  131. }