Template.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\admin\model\ps;
  3. use think\Model;
  4. class Template extends Model
  5. {
  6. // 表名
  7. protected $name = 'ps_template';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'integer';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'locked_time_text',
  17. 'status_text'
  18. ];
  19. protected static function init()
  20. {
  21. self::beforeInsert(function ($row) {
  22. if (!isset($row['factory_id']) || $row['factory_id'] === '' || $row['factory_id'] === null) {
  23. $row['factory_id'] = 0;
  24. }
  25. if ((int)$row['factory_id'] === 0) {
  26. $row['is_system'] = 1;
  27. if (empty($row['source']) || $row['source'] === 'manual') {
  28. $row['source'] = 'system';
  29. }
  30. }
  31. });
  32. }
  33. public function getStatusList()
  34. {
  35. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  36. }
  37. public function getSourceList()
  38. {
  39. return ['manual' => __('Manual'), 'ai' => __('Ai'), 'system' => __('System'), 'market' => __('Market')];
  40. }
  41. public function getLicenseTypeList()
  42. {
  43. return [
  44. 'self' => __('Self'),
  45. 'system_copy' => __('System_copy'),
  46. 'market_single' => __('Market_single'),
  47. 'market_buyout' => __('Market_buyout'),
  48. ];
  49. }
  50. public function factory()
  51. {
  52. return $this->belongsTo('Factory', 'factory_id', 'id', [], 'LEFT')->setEagerlyType(0);
  53. }
  54. public function getLockedTimeTextAttr($value, $data)
  55. {
  56. $value = $value ?: ($data['locked_time'] ?? '');
  57. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  58. }
  59. public function getStatusTextAttr($value, $data)
  60. {
  61. $value = $value ?: ($data['status'] ?? '');
  62. $list = $this->getStatusList();
  63. return $list[$value] ?? '';
  64. }
  65. protected function setLockedTimeAttr($value)
  66. {
  67. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  68. }
  69. }