Order.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zilongs
  5. * Date: 20-9-30
  6. * Time: 下午10:56
  7. */
  8. namespace App\Models;
  9. class Order extends BaseModel
  10. {
  11. protected $appends = ['is_evaluate', 'order_duration'];
  12. CONST UNPAID = 1, ISING = 2, FINISHED = 3,CANCELED=4; //订单状态(1.未支付 2.进行中 3.已完成 4.已取消)
  13. public static $_order_status = [
  14. self::UNPAID=>'未支付',
  15. self::ISING=>'进行中',
  16. self::FINISHED=>'已完成',
  17. self::CANCELED=>'已取消',
  18. ];
  19. //获取订单状态
  20. public function getStatus()
  21. {
  22. return self::$_order_status;
  23. }
  24. public function docter()
  25. {
  26. return $this->belongsTo(Docter::class);
  27. }
  28. public function orderPatient()
  29. {
  30. return $this->hasOne(OrderPatient::class);
  31. }
  32. public function orderPack()
  33. {
  34. return $this->hasMany(OrderPack::class);
  35. }
  36. public function orderNurse()
  37. {
  38. return $this->hasMany(OrderNurse::class);
  39. }
  40. public function orderVaccine()
  41. {
  42. return $this->hasOne(OrderVaccine::class);
  43. }
  44. public function orderUser()
  45. {
  46. return $this->hasOne(User::class,'id','user_id');
  47. }
  48. public function organization()
  49. {
  50. return $this->belongsTo(Organization::class);
  51. }
  52. //支付完成的处理方法
  53. public static function payCompletedHandle($order_id)
  54. {
  55. $order = Order::select(['user_id', 'docter_id', 'product_type', 'total_amount', 'payment_type', 'payment_amount', 'order_sn'])->where('id', $order_id)->first();
  56. //发送下单消息
  57. if ($order['product_type'] < 6) {
  58. $product_type_text = config('config.product_type_map')[$order['product_type']];
  59. UserMessage::saveMessage($order['user_id'], 4, $order_id, [$product_type_text]);
  60. }
  61. elseif ($order['product_type'] == 6) {
  62. $orderPack = OrderPack::select(['pack_name', 'end_time'])->where('order_id', $order_id)->first();
  63. UserMessage::saveMessage($order['user_id'], 5, $order_id, [$orderPack['pack_name'], date('Y-m-d', $orderPack['end_time'])]);
  64. }
  65. elseif ($order['product_type'] == 7) {
  66. $user = User::select(['balance'])->where('id', $order['user_id'])->first();
  67. UserMessage::saveMessage($order['user_id'], 7, $order_id, [round($order['total_amount']/100, 2), round($user['balance']/100, 2)]);
  68. }
  69. //发送余额付款成功消息
  70. if ($order['payment_type'] == 2) {
  71. $user = User::select(['balance'])->where('id', $order['user_id'])->first();
  72. UserMessage::saveMessage($order['user_id'], 8, $order_id, [round($order['payment_amount']/100, 2), round($user['balance']/100, 2)]);
  73. }
  74. //发送医生端消息
  75. DocterMessage::saveMessage($order['docter_id'], $order['user_id'], 1, $order_id, [$order['order_sn']]);
  76. return true;
  77. }
  78. public function suggest()
  79. {
  80. return $this->hasOne(Suggest::class)->select(['id', 'order_id']);
  81. }
  82. public function getIsEvaluateAttribute()
  83. {
  84. $user = User::getUserByToken();
  85. $is_evaluate = 0;
  86. if (Evaluate::where('order_id', $this->id)->where('user_id', $user['id'])->exists()) {
  87. $is_evaluate = 1;
  88. }
  89. return $is_evaluate;
  90. }
  91. public function getOrderDurationAttribute()
  92. {
  93. if (!empty($this->outtime) && !empty($this->receiving_time)) {
  94. $diff = $this->outtime - $this->receiving_time;
  95. $hour = round($diff/3600);
  96. if ($hour == 0) {
  97. return round($diff/60).'分钟';
  98. }
  99. return $hour.'小时';
  100. }
  101. return '';
  102. }
  103. }