Order.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\admin\model\order;
  3. use app\admin\model\User;
  4. use think\Model;
  5. class Order extends Model
  6. {
  7. // 表名
  8. protected $name = 'order';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'integer';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. protected $deleteTime = false;
  15. // 追加属性
  16. protected $append = [
  17. 'status_text',
  18. 'is_deleted_text',
  19. 'paytime_text',
  20. 'type_text',
  21. 'pay_type_text'
  22. ];
  23. public function getStatusList()
  24. {
  25. return ['-1' => __('已取消'), '0' => __('未支付'), '1' => __('已完成')];
  26. }
  27. public function getIsDeletedList()
  28. {
  29. return ['0' => __('是'), '1' => __('否')];
  30. }
  31. public function getTypeList()
  32. {
  33. return ['0' => __('书籍'), '1' => __('视频')];
  34. }
  35. public function getPayTypeList()
  36. {
  37. return ['0' => __('微信'), '1' => __('支付宝')];
  38. }
  39. public function getStatusTextAttr($value, $data)
  40. {
  41. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  42. $list = $this->getStatusList();
  43. return isset($list[$value]) ? $list[$value] : '';
  44. }
  45. public function getIsDeletedTextAttr($value, $data)
  46. {
  47. $value = $value ? $value : (isset($data['is_deleted']) ? $data['is_deleted'] : '');
  48. $list = $this->getIsDeletedList();
  49. return isset($list[$value]) ? $list[$value] : '';
  50. }
  51. public function getPaytimeTextAttr($value, $data)
  52. {
  53. $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
  54. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  55. }
  56. public function getTypeTextAttr($value, $data)
  57. {
  58. $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
  59. $list = $this->getTypeList();
  60. return isset($list[$value]) ? $list[$value] : '';
  61. }
  62. public function getPayTypeTextAttr($value, $data)
  63. {
  64. $value = $value ? $value : (isset($data['pay_type']) ? $data['pay_type'] : '');
  65. $list = $this->getPayTypeList();
  66. return isset($list[$value]) ? $list[$value] : '';
  67. }
  68. protected function setPaytimeAttr($value)
  69. {
  70. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  71. }
  72. public function goods(){
  73. return $this->hasMany(OrderGoods::class,'order_id','id');
  74. }
  75. public function user(){
  76. return $this->hasOne(User::class,'id','user_id');
  77. }
  78. }