Order.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 static function getStatus()
  21. {
  22. return self::$_order_status;
  23. }
  24. public function docter()
  25. {
  26. return $this->belongsTo(Docter::class);
  27. }
  28. public function patients(){
  29. return $this->belongsTo(Patient::class);
  30. }
  31. public function orderPatient()
  32. {
  33. return $this->hasOne(OrderPatient::class);
  34. }
  35. public function orderPack()
  36. {
  37. return $this->hasOne(OrderPack::class);
  38. }
  39. public function orderNurse()
  40. {
  41. return $this->hasMany(OrderNurse::class);
  42. }
  43. public function orderVaccine()
  44. {
  45. return $this->hasOne(OrderVaccine::class);
  46. }
  47. public function orderUser()
  48. {
  49. return $this->hasOne(User::class,'id','user_id');
  50. }
  51. public function organization()
  52. {
  53. return $this->belongsTo(Organization::class);
  54. }
  55. public function user()
  56. {
  57. return $this->belongsTo(User::class);
  58. }
  59. //支付完成的处理方法
  60. public static function payCompletedHandle($order_id)
  61. {
  62. $order = Order::select(['user_id', 'docter_id', 'product_type', 'total_amount', 'payment_type', 'payment_amount', 'order_sn'])->where('id', $order_id)->first();
  63. //发送下单消息
  64. if ($order['product_type'] < 6) {
  65. $product_type_text = config('config.product_type_map')[$order['product_type']];
  66. UserMessage::saveMessage($order['user_id'], 4, $order_id, [$product_type_text]);
  67. }
  68. elseif ($order['product_type'] == 6) {
  69. $orderPack = OrderPack::select(['pack_name', 'end_time'])->where('order_id', $order_id)->first();
  70. UserMessage::saveMessage($order['user_id'], 5, $order_id, [$orderPack['pack_name'], date('Y-m-d', $orderPack['end_time'])]);
  71. //更新用户为服务包用户
  72. User::where('id', $order['user_id'])->update(['is_pack' => 1]);
  73. }
  74. elseif ($order['product_type'] == 7) {
  75. $user = User::select(['balance'])->where('id', $order['user_id'])->first();
  76. UserMessage::saveMessage($order['user_id'], 7, $order_id, [round($order['total_amount']/100, 2), round($user['balance']/100, 2)]);
  77. }
  78. //发送余额付款成功消息
  79. if ($order['payment_type'] == 2) {
  80. $user = User::select(['balance'])->where('id', $order['user_id'])->first();
  81. UserMessage::saveMessage($order['user_id'], 8, $order_id, [round($order['payment_amount']/100, 2), round($user['balance']/100, 2)]);
  82. }
  83. //发送医生端消息
  84. DocterMessage::saveMessage($order['docter_id'], $order['user_id'], 1, $order_id, [$order['order_sn']]);
  85. return true;
  86. }
  87. public function suggest()
  88. {
  89. return $this->hasOne(Suggest::class)->select(['id', 'order_id']);
  90. }
  91. public function getIsEvaluateAttribute()
  92. {
  93. $is_evaluate = 0;
  94. $user = User::getUserByToken(false);
  95. if (!empty($user)) {
  96. if (Evaluate::where('order_id', $this->id)->where('user_id', $user['id'])->exists()) {
  97. $is_evaluate = 1;
  98. }
  99. }
  100. return $is_evaluate;
  101. }
  102. public function getOrderDurationAttribute()
  103. {
  104. if (!empty($this->outtime) && !empty($this->receiving_time)) {
  105. $diff = $this->outtime - $this->receiving_time;
  106. $hour = round($diff/3600);
  107. if ($hour == 0) {
  108. return round($diff/60).'分钟';
  109. }
  110. return $hour.'小时';
  111. }
  112. return '';
  113. }
  114. }