Order.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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', 'consult_duration', 'callback_phone'];
  12. CONST UNPAID = 1, NOTACCEPT = 2, ISING = 3, FINISHED = 4,CANCELED=5,ISOUT=6; //订单状态(1.未支付 2.进行中 3.已完成 4.已取消)
  13. public static $_order_status = [
  14. self::UNPAID=>'未支付',
  15. self::NOTACCEPT=>'待支付',
  16. self::ISING=>'进行中',
  17. self::FINISHED=>'已完成',
  18. self::CANCELED=>'已取消',
  19. self::ISOUT=>'已超时'
  20. ];
  21. //获取订单状态
  22. public static function getStatus()
  23. {
  24. return self::$_order_status;
  25. }
  26. public function docter()
  27. {
  28. return $this->belongsTo(Docter::class);
  29. }
  30. public function calllog()
  31. {
  32. return $this->hasMany(CallLog::class);
  33. }
  34. /**
  35. * 用户医生关注表
  36. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  37. * @author Liu-Yh
  38. * Create By 2020/11/18 11:06
  39. */
  40. public function userDocter(){
  41. return $this->hasOne(UserDocter::class,'user_id','user_id')->select(['id', 'remark']);
  42. }
  43. public function patients(){
  44. return $this->belongsTo(Patient::class);
  45. }
  46. public function orderPatient()
  47. {
  48. return $this->hasOne(OrderPatient::class);
  49. }
  50. public function orderPack()
  51. {
  52. return $this->hasOne(OrderPack::class);
  53. }
  54. public function orderNurse()
  55. {
  56. return $this->hasMany(OrderNurse::class);
  57. }
  58. public function orderVaccine()
  59. {
  60. return $this->hasOne(OrderVaccine::class);
  61. }
  62. public function orderUser()
  63. {
  64. return $this->hasOne(User::class,'id','user_id');
  65. }
  66. public function organization()
  67. {
  68. return $this->belongsTo(Organization::class);
  69. }
  70. public function user()
  71. {
  72. return $this->belongsTo(User::class);
  73. }
  74. public function evaluate()
  75. {
  76. return $this->hasOne(Evaluate::class);
  77. }
  78. //支付完成的处理方法
  79. public static function payCompletedHandle($order_id)
  80. {
  81. $order = Order::select(['user_id', 'docter_id', 'product_type', 'total_amount', 'payment_type', 'payment_amount', 'order_sn', 'pay_order_pack_id', 'organization_id'])->where('id', $order_id)->first();
  82. //发送下单消息
  83. if ($order['product_type'] < 6) {
  84. $product_type_text = config('config.product_type_map')[$order['product_type']];
  85. UserMessage::saveMessage($order['user_id'], 4, $order_id, [$product_type_text]);
  86. }
  87. elseif ($order['product_type'] == 6) {
  88. $orderPack = OrderPack::select(['pack_name', 'end_time'])->where('order_id', $order_id)->first();
  89. UserMessage::saveMessage($order['user_id'], 5, $order_id, [$orderPack['pack_name'], date('Y-m-d', $orderPack['end_time'])]);
  90. //更新用户为服务包用户
  91. User::where('id', $order['user_id'])->update(['is_pack' => 1]);
  92. }
  93. elseif ($order['product_type'] == 7) {
  94. $user = User::select(['balance'])->where('id', $order['user_id'])->first();
  95. UserMessage::saveMessage($order['user_id'], 7, $order_id, [round($order['total_amount']/100, 2), round($user['balance']/100, 2)]);
  96. }
  97. //发送余额付款成功消息
  98. if ($order['payment_type'] == 2) {
  99. $user = User::select(['balance'])->where('id', $order['user_id'])->first();
  100. UserMessage::saveMessage($order['user_id'], 8, $order_id, [round($order['payment_amount']/100, 2), round($user['balance']/100, 2)]);
  101. }
  102. //发送医生端消息
  103. DocterMessage::saveMessage($order['docter_id'], $order['user_id'], 1, $order_id, [$order['order_sn']]);
  104. //如果是服务包支付的,就扣服务包的次数
  105. if ($order['payment_type'] == 3) {
  106. OrderPack::deductPackData($order['pay_order_pack_id'], $order['product_type'], $order['payment_amount']);
  107. }
  108. /* if (!empty($order['docter_id'])) {
  109. //更新医生的服务人数
  110. Docter::where('id', $order['docter_id'])->increment('service_persons');
  111. }*/
  112. //如果是门诊预约,预约时间段的订单数要加1
  113. if ($order['product_type'] == 3) {
  114. $orderPatient = OrderPatient::select(['time_period_id', 'appoint_start_time'])->where('order_id', $order_id)->first();
  115. $schedule_date = date('Y-m-d', $orderPatient['appoint_start_time']);
  116. SchedulePeriod::where('docter_id', $order['docter_id'])->where('organization_id', $order['organization_id'])->where('schedule_type', 1)->where('time_period_id', $orderPatient['time_period_id'])->where('schedule_date', $schedule_date)->increment('order_num');
  117. }
  118. //如果是儿保和疫苗预约,预约时间段的订单数要加1
  119. if (in_array($order['product_type'], [4,5])) {
  120. $orderPatient = OrderPatient::select(['time_period_id', 'appoint_start_time'])->where('order_id', $order_id)->first();
  121. $schedule_date = date('Y-m-d', $orderPatient['appoint_start_time']);
  122. $schedule_type = $order['product_type'] == 4 ? 2 : 3;
  123. SchedulePeriod::where('organization_id', $order['organization_id'])->where('schedule_type', $schedule_type)->where('time_period_id', $orderPatient['time_period_id'])->where('schedule_date', $schedule_date)->increment('order_num');
  124. }
  125. //如果是疫苗就减少疫苗库存
  126. if ($order['product_type'] == 4) {
  127. $orderVaccine = OrderVaccine::where('order_id', $order_id)->first();
  128. OrganizationVaccine::where('org_id', $order['organization_id'])->where('vaccine_id', $orderVaccine['vaccine_id'])->decrement('stock');
  129. }
  130. return true;
  131. }
  132. public function suggest()
  133. {
  134. return $this->hasOne(Suggest::class)->select(['id', 'order_id']);
  135. }
  136. public function getIsEvaluateAttribute()
  137. {
  138. $is_evaluate = 0;
  139. $user = User::getUserByToken(false);
  140. if (!empty($user)) {
  141. if (Evaluate::where('order_id', $this->id)->where('user_id', $user['id'])->exists()) {
  142. $is_evaluate = 1;
  143. }
  144. }
  145. return $is_evaluate;
  146. }
  147. //取消订单,建议是在事务里面去调用
  148. public static function orderCancel($order_id, $order_notes = '')
  149. {
  150. $order = Order::with(['orderPatient'])->where('id', $order_id)->first();
  151. //改变订单状态
  152. $updateOrder = ['order_status' => 5, 'order_notes' => $order_notes];
  153. if ($order['payment_status'] > 1) {
  154. //退钱到余额
  155. if (!empty($order['payment_amount'])) {
  156. User::changeBalance($order_id, $order['payment_amount'], 4, $order['id'], '取消订单退款');
  157. }
  158. $updateOrder['refund_time'] = time();
  159. $updateOrder['payment_status'] = 4;
  160. }
  161. Order::where('id', $order_id)->update($updateOrder);
  162. //如果距离预约时间还有半个小时以上,那么预约时间段的订单数减1
  163. if ($order['order_patient']['appoint_start_time'] - 1800 > time()) {
  164. $schedule_date = date('Y-m-d', $order['order_patient']['appoint_start_time']);
  165. SchedulePeriod::where('docter_id', $order['docter_id'])->where('organization_id', $order['organization_id'])->where('time_period_id', $order['order_patient']['time_period_id'])->where('schedule_date', $schedule_date)->decrement('order_num');
  166. }
  167. //更新医生的服务人数
  168. if ($order['payment_status'] > 1) {
  169. Docter::where('id', $order['docter_id'])->decrement('service_persons');
  170. }
  171. return true;
  172. }
  173. public function getConsultDurationAttribute()
  174. {
  175. $duration = 0;
  176. if ($this->product_type == 1) {
  177. $duration = CallLog::where('order_id', $this->id)->where('talk_time', '<>', null)->sum('talk_time');
  178. $duration = !empty($duration) ? $duration : 0;
  179. }
  180. elseif ($this->product_type == 2) {
  181. $duration = $this->end_time - $this->receiving_time;
  182. }
  183. return $duration > 0 ? $duration : 0;
  184. }
  185. public function getCallbackPhoneAttribute()
  186. {
  187. $secret_no = '';
  188. if ($this->product_type == 1) {
  189. $secret_no = CallLog::where('order_id', $this->id)->orderBy('id', 'desc')->value('secret_no');
  190. }
  191. return !empty($secret_no) ? $secret_no : '';
  192. }
  193. }