Verify.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace app\admin\model\shopro\order;
  3. use think\Model;
  4. class Verify extends Model
  5. {
  6. // 表名
  7. protected $name = 'shopro_verify';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. // 追加属性
  14. protected $append = [
  15. 'type_text',
  16. 'status_code',
  17. 'status_name'
  18. ];
  19. public function getTypeList()
  20. {
  21. return [
  22. 'verify' => '核销券'
  23. ];
  24. }
  25. public function scopeCanUse($query)
  26. {
  27. return $query->where('usetime', null)->where(function ($query) {
  28. $query->where('expiretime', null)->whereOr('expiretime', '>', time());
  29. });
  30. }
  31. public function getTypeTextAttr($value, $data)
  32. {
  33. $value = isset($data['type']) ? $data['type'] : '';
  34. $list = $this->getTypeList();
  35. return isset($list[$value]) ? $list[$value] : '';
  36. }
  37. public function getStatusNameAttr($value, $data)
  38. {
  39. $status_name = '';
  40. switch ($this->status_code) {
  41. case 'used':
  42. $status_name = '已使用';
  43. break;
  44. case 'nouse':
  45. $status_name = '未使用';
  46. break;
  47. case 'expire':
  48. $status_name = '已过期';
  49. break;
  50. }
  51. return $status_name;
  52. }
  53. public function getStatusCodeAttr($value, $data)
  54. {
  55. $status_code = '';
  56. if ($data['usetime']) {
  57. // 已使用
  58. $status_code = 'used';
  59. } else {
  60. // 未使用
  61. if (is_null($data['expiretime']) || $data['expiretime'] > time()) {
  62. // 不过期,或者过期时间大于当前时间
  63. $status_code = 'nouse';
  64. } else {
  65. // 已过期
  66. $status_code = 'expire';
  67. }
  68. }
  69. return $status_code;
  70. }
  71. public function orderItem()
  72. {
  73. return $this->belongsTo(\app\admin\model\shopro\order\OrderItem::class, 'order_item_id', 'id');
  74. }
  75. public function order()
  76. {
  77. return $this->belongsTo(\app\admin\model\shopro\order\Order::class, 'order_id', 'id');
  78. }
  79. }