Order.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. // 支付状态(1.待付款 2.已付款 3.退款中 4.已退款 5.待退款)
  27. public static $_order_pay_status = [
  28. 1=>'待支付',
  29. 2=>'已付款',
  30. 3=>'退款中',
  31. 4=>'已退款',
  32. 5=>'待退款',
  33. ];
  34. public static function getPayStatus()
  35. {
  36. return self::$_order_pay_status;
  37. }
  38. public function docter()
  39. {
  40. return $this->belongsTo(Docter::class);
  41. }
  42. public function calllog()
  43. {
  44. return $this->hasMany(CallLog::class);
  45. }
  46. /**
  47. * 用户医生关注表
  48. * @return
  49. * @author Liu-Yh
  50. * Create By 2020/11/18 11:06
  51. */
  52. public function userDocter(){
  53. return $this->hasOne(UserDocter::class,'user_id','user_id')->select(['id', 'remark']);
  54. }
  55. public function patients(){
  56. return $this->belongsTo(Patient::class);
  57. }
  58. public function orderPatient()
  59. {
  60. return $this->hasOne(OrderPatient::class);
  61. }
  62. public function orderPack()
  63. {
  64. return $this->hasOne(OrderPack::class);
  65. }
  66. public function orderNurse()
  67. {
  68. return $this->hasMany(OrderNurse::class);
  69. }
  70. public function orderVaccine()
  71. {
  72. return $this->hasOne(OrderVaccine::class);
  73. }
  74. public function orderUser()
  75. {
  76. return $this->hasOne(User::class,'id','user_id');
  77. }
  78. public function organization()
  79. {
  80. return $this->belongsTo(Organization::class);
  81. }
  82. public function user()
  83. {
  84. return $this->belongsTo(User::class);
  85. }
  86. public function evaluate()
  87. {
  88. return $this->hasOne(Evaluate::class);
  89. }
  90. //支付完成的处理方法
  91. public static function payCompletedHandle($order_id)
  92. {
  93. $order = Order::with(['organization', 'orderVaccine', 'orderNurse', 'orderPack'])->where('id', $order_id)->first()->toArray();
  94. $orderPatient = OrderPatient::where('order_id', $order_id)->first();
  95. $user = User::where('id', $order['user_id'])->first();
  96. //更新订单
  97. $order_status = 2;
  98. if (in_array($order['product_type'], [3,4,5])) {
  99. $order_status = 7;
  100. }
  101. elseif ($order['product_type'] == 6) {
  102. $order_status = 3;
  103. }
  104. elseif ($order['product_type'] == 7) {
  105. $order_status = 4;
  106. //改变用户余额
  107. User::changeBalance($order['user_id'], $order['total_amount'], 2, $order['id'], '充值');
  108. }
  109. Order::where('id', $order_id)->update([
  110. 'order_status' => $order_status,
  111. 'payment_status' => 2,
  112. 'payment_time' => time(),
  113. ]);
  114. //发送下单消息
  115. if ($order['product_type'] < 6) {
  116. $product_type_text = config('config.product_type_map')[$order['product_type']];
  117. UserMessage::saveMessage($order['user_id'], 4, $order_id, [$product_type_text]);
  118. }
  119. elseif ($order['product_type'] == 6) {
  120. $orderPack = OrderPack::select(['pack_name', 'end_time'])->where('order_id', $order_id)->first();
  121. UserMessage::saveMessage($order['user_id'], 5, $order_id, [$orderPack['pack_name'], date('Y-m-d', $orderPack['end_time'])]);
  122. //更新用户为服务包用户
  123. User::where('id', $order['user_id'])->update(['is_pack' => 1]);
  124. }
  125. elseif ($order['product_type'] == 7) {
  126. UserMessage::saveMessage($order['user_id'], 7, $order_id, [round($order['total_amount']/100, 2), round($user['balance']/100, 2)]);
  127. }
  128. //发送余额付款成功消息
  129. if ($order['payment_type'] == 2) {
  130. UserMessage::saveMessage($order['user_id'], 8, $order_id, [round($order['payment_amount']/100, 2), round($user['balance']/100, 2)]);
  131. }
  132. //发送医生端消息
  133. DocterMessage::saveMessage($order['docter_id'], $order['user_id'], 1, $order_id, [$order['order_sn']]);
  134. //如果是服务包支付的,就扣服务包的次数
  135. if ($order['payment_type'] == 3) {
  136. OrderPack::deductPackData($order['pay_order_pack_id'], $order['product_type']);
  137. }
  138. //如果是门诊预约,预约时间段的订单数要加1
  139. if ($order['product_type'] == 3) {
  140. $schedule_date = date('Y-m-d', $orderPatient['appoint_start_time']);
  141. 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');
  142. }
  143. //如果是儿保和疫苗预约,预约时间段的订单数要加1
  144. if (in_array($order['product_type'], [4,5])) {
  145. $schedule_date = date('Y-m-d', $orderPatient['appoint_start_time']);
  146. $schedule_type = $order['product_type'] == 4 ? 2 : 3;
  147. 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');
  148. }
  149. //如果是疫苗就减少疫苗库存
  150. if ($order['product_type'] == 4) {
  151. $orderVaccine = OrderVaccine::where('order_id', $order_id)->first();
  152. Vaccine::where('id', $orderVaccine['vaccine_id'])->decrement('stock');
  153. }
  154. //给用户发送微信消息
  155. $docter = Docter::where('id', $order['docter_id'])->first();
  156. if ($order['product_type'] == 1) {
  157. $official_arr = [$user['openid'], $docter['name'], date('Y-m-d H:i:s'), round($order['payment_amount']/100, 2)];
  158. $product_type_text = config('config.product_type_map')[$order['product_type']];
  159. $subscribe_arr = [$user['openid'], $product_type_text, $user['nickname'], date('Y-m-d H:i:s'), $product_type_text, round($order['payment_amount']/100, 2)];
  160. send_wechat_message(1, $official_arr, $subscribe_arr);
  161. }
  162. elseif ($order['product_type'] == 2) {
  163. $official_arr = [$user['openid'], $docter['name'], $orderPatient['name'], $orderPatient['symptoms'], date('Y-m-d H:i:s'), round($order['payment_amount']/100, 2)];
  164. $product_type_text = config('config.product_type_map')[$order['product_type']];
  165. $subscribe_arr = [$user['openid'], $product_type_text, $user['nickname'], date('Y-m-d H:i:s'), $product_type_text, round($order['payment_amount']/100, 2)];
  166. send_wechat_message(2, $official_arr, $subscribe_arr);
  167. }
  168. elseif ($order['product_type'] == 3) {
  169. $keyword2 = date('Y-m-d H:i', $orderPatient['appoint_start_time']).' - '.date('H:i', $orderPatient['appoint_end_time']);
  170. $official_arr = [$user['openid'], $docter['name'], $keyword2, $orderPatient['name'], $orderPatient['phone'], $order['organization']['name'], $order['organization']['address']];
  171. $subscribe_arr = [$user['openid'], $orderPatient['name'], $order['organization']['name'], $docter['name'], $keyword2];
  172. send_wechat_message(3, $official_arr, $subscribe_arr);
  173. }
  174. elseif ($order['product_type'] == 4) {
  175. $keyword2 = date('Y-m-d H:i', $orderPatient['appoint_start_time']).' - '.date('H:i', $orderPatient['appoint_end_time']);
  176. $official_arr = [$user['openid'], $order['organization']['name'], $order['order_vaccine']['vaccine_name'], $keyword2, $orderPatient['name'], $orderPatient['phone'], $order['organization']['name'], $order['organization']['address']];
  177. $subscribe_arr = [$user['openid'], $orderPatient['name'], $order['order_vaccine']['vaccine_name'], $order['organization']['name'], $keyword2];
  178. send_wechat_message(4, $official_arr, $subscribe_arr);
  179. }
  180. elseif ($order['product_type'] == 5) {
  181. $keyword2 = date('Y-m-d H:i', $orderPatient['appoint_start_time']).' - '.date('H:i', $orderPatient['appoint_end_time']);
  182. $official_arr = [$user['openid'], $order['organization']['name'], $order['order_nurse'][0]['nurse_name'], $keyword2, $orderPatient['name'], $orderPatient['phone'], $order['organization']['name'], $order['organization']['address']];
  183. $subscribe_arr = [$user['openid'], $orderPatient['name'], $order['order_nurse'][0]['nurse_name'], $order['organization']['name'], $keyword2];
  184. send_wechat_message(5, $official_arr, $subscribe_arr);
  185. }
  186. elseif ($order['product_type'] == 6) {
  187. $official_arr = [$user['openid'], $order['order_pack']['pack_name'], date('Y-m-d H:i:s', $order['order_pack']['start_time']), date('Y-m-d H:i:s', $order['order_pack']['end_time'])];
  188. $service_time = date('Y/m/d', $order['order_pack']['start_time']). ' - '. date('Y/m/d', $order['order_pack']['end_time']);
  189. $subscribe_arr = [$user['openid'], $order['order_pack']['pack_name'], $service_time, $user['nickname']];
  190. send_wechat_message(6, $official_arr, $subscribe_arr);
  191. }
  192. //给医生发送消息
  193. if (in_array($order['product_type'], [1,2])) {
  194. $official_arr = [$user['openid'], $order['order_sn'], config('config.product_type_map')[$order['product_type']], $orderPatient['name'], $orderPatient['phone']];
  195. send_wechat_message(10, $official_arr);
  196. }
  197. elseif ($order['product_type'] == 3) {
  198. $keyword4 = date('Y-m-d H:i', $orderPatient['appoint_start_time']).' - '.date('H:i', $orderPatient['appoint_end_time']);
  199. $official_arr = [$user['openid'], $orderPatient['name'], $orderPatient['phone'], $keyword4, $order['organization']['name'], $docter['name']];
  200. send_wechat_message(11, $official_arr);
  201. }
  202. return true;
  203. }
  204. public function suggest()
  205. {
  206. return $this->hasOne(Suggest::class)->select(['id', 'order_id']);
  207. }
  208. public function getIsEvaluateAttribute()
  209. {
  210. $is_evaluate = 0;
  211. $user = User::getUserByToken(false);
  212. if (!empty($user)) {
  213. if (Evaluate::where('order_id', $this->id)->where('user_id', $user['id'])->exists()) {
  214. $is_evaluate = 1;
  215. }
  216. }
  217. return $is_evaluate;
  218. }
  219. //取消订单,建议是在事务里面去调用
  220. public static function orderCancel($order_id, $order_notes = '')
  221. {
  222. $order = Order::with(['orderPatient'])->where('id', $order_id)->first()->toArray();
  223. //判断预约时间是否还剩1小时内
  224. $can_refund = true;
  225. if (in_array($order['product_type'], [3,4,5])) {
  226. $order_notes = '用户取消预约';
  227. if ($order['order_patient']['appoint_start_time'] - 3600 < time()) {
  228. $order_notes = '用户超时取消';
  229. $can_refund = false;
  230. }
  231. }
  232. //改变订单状态
  233. $updateOrder = ['order_status' => 5, 'order_notes' => $order_notes];
  234. if ($order['payment_status'] > 1) {
  235. //判断是余额支付还是服务包支付
  236. if ($order['payment_type'] == 3) {
  237. //退服务包的次数
  238. $map = [1 => 'phone_minutes', 2 => 'chat_num', 3 => 'appoint_num', 4 => 'vaccine_limit_amount', 5 => 'nurses_limit_amount'];
  239. OrderPack::where('id', $order['pay_order_pack_id'])->increment($map[$order['product_type']]);
  240. $updateOrder['cancel_time'] = time();
  241. $updateOrder['payment_status'] = 4;
  242. if ($order['product_type'] == 3 && !$can_refund) {
  243. $updateOrder['payment_status'] = 2;
  244. }
  245. }
  246. else {
  247. //退钱到余额
  248. if (!empty($order['payment_amount']) && $can_refund && $order['product_type'] != 5) {
  249. User::changeBalance($order['user_id'], $order['payment_amount'], 4, $order['id'], '取消订单退款');
  250. }
  251. $updateOrder['cancel_time'] = time();
  252. if ($can_refund) {
  253. $updateOrder['payment_status'] = 4;
  254. }
  255. if ($order['product_type'] == 5) {
  256. $updateOrder['payment_status'] = 5;
  257. }
  258. if ($order['product_type'] == 3 && !$can_refund) {
  259. $updateOrder['payment_status'] = 2;
  260. }
  261. }
  262. }
  263. Order::where('id', $order_id)->update($updateOrder);
  264. //如果是门诊预约且距离预约时间还有1个小时以上,那么预约时间段的订单数减1
  265. if ($order['product_type'] == 3 && $can_refund) {
  266. $schedule_date = date('Y-m-d', $order['order_patient']['appoint_start_time']);
  267. 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)->where('order_num', '>', 0)->decrement('order_num');
  268. }
  269. //如果是疫苗儿保预约且距离预约时间还有1个小时以上,那么预约时间段的订单数减1
  270. if (in_array($order['product_type'], [4,5]) && $can_refund) {
  271. $schedule_type_map = [4 => 2, 5 => 3];
  272. $schedule_date = date('Y-m-d', $order['order_patient']['appoint_start_time']);
  273. SchedulePeriod::where('organization_id', $order['organization_id'])->where('time_period_id', $order['order_patient']['time_period_id'])->where('schedule_date', $schedule_date)->where('schedule_type', $schedule_type_map[$order['product_type']])->where('order_num', '>', 0)->decrement('order_num');
  274. }
  275. //如果是疫苗预约那么取消订单就增加疫苗库存
  276. if ($order['product_type'] == 4) {
  277. $orderVaccine = OrderVaccine::where('order_id', $order_id)->first();
  278. Vaccine::where('id', $orderVaccine['vaccine_id'])->increment('stock');
  279. }
  280. return true;
  281. }
  282. public function getConsultDurationAttribute()
  283. {
  284. $duration = 0;
  285. if ($this->product_type == 1) {
  286. $duration = CallLog::where('order_id', $this->id)->where('talk_time', '<>', null)->sum('talk_time');
  287. $duration = !empty($duration) ? $duration : 0;
  288. }
  289. elseif ($this->product_type == 2) {
  290. $duration = $this->end_time - $this->receiving_time;
  291. }
  292. return $duration > 0 ? $duration : 0;
  293. }
  294. public function getCallbackPhoneAttribute()
  295. {
  296. $secret_no = '';
  297. if ($this->product_type == 1) {
  298. $secret_no = CallLog::where('order_id', $this->id)->orderBy('id', 'desc')->value('secret_no');
  299. }
  300. return !empty($secret_no) ? $secret_no : '';
  301. }
  302. public static function checkOrder($order_id)
  303. {
  304. $order = Order::with(['orderPatient', 'orderVaccine'])->where('id', $order_id)->first()->toArray();
  305. $product_type = $order['product_type'];
  306. if (in_array($product_type, [1,2])) {
  307. //判断是否在服务时间内
  308. $now_line = (int)date('Hi');
  309. if (!DocterServiceTime::where('docter_id', $order['docter_id'])->where('type', $product_type)->where('start_time_line', '<=', $now_line)->where('end_time_line', '>', $now_line)->exists()) {
  310. exit_out(null, 10011, '当前不在医生服务时间内,不能下单');
  311. }
  312. //图文咨询订单未结束时不能针对同一医生再次下图文订单
  313. if ($product_type == 2 && Order::where('docter_id', $order['docter_id'])->where('product_type', 2)->where('user_id', $order['user_id'])->whereIn('order_status', [2,3])->exists()) {
  314. exit_out(null, 10012, '您已经下过该医生的图文订单了,并且订单还未完成');
  315. }
  316. }
  317. //检查号源
  318. elseif ($product_type == 3) {
  319. $schedule_date = date('Y-m-d', $order['order_patient']['appoint_start_time']);
  320. $schedulePeriod = SchedulePeriod::where('docter_id', $order['docter_id'])->where('time_period_id', $order['order_patient']['time_period_id'])->where('schedule_date', $schedule_date)->where('schedule_type', 1)->first();
  321. if (empty($schedulePeriod)) {
  322. exit_out(null, 10012, '医生无该时间段的排班');
  323. }
  324. $docterSettings = DocterSetting::select(['service_num'])->where('docter_id', $order['docter_id'])->where('type', 1)->first();
  325. if ($docterSettings['service_num'] <= $schedulePeriod['order_sn']) {
  326. exit_out(null, 10014, '医生该时间段已经预约满了');
  327. }
  328. }
  329. elseif (in_array($product_type, [4,5])) {
  330. $schedule_date = date('Y-m-d', $order['order_patient']['appoint_start_time']);
  331. $schedule_type_map = [4 => 2, 5 => 3];
  332. $schedulePeriod = SchedulePeriod::where('time_period_id', $order['order_patient']['time_period_id'])->where('schedule_date', $schedule_date)->where('organization_id', $order['organization_id'])->where('schedule_type', $schedule_type_map[$product_type])->first();
  333. if (empty($schedulePeriod)) {
  334. exit_out(null, 10013, '机构无该时间段的排班');
  335. }
  336. $docterSettings = DocterSetting::select(['service_num'])->where('org_id', $order['organization_id'])->where('type', $schedule_type_map[$product_type])->first();
  337. if ($docterSettings['service_num'] <= $schedulePeriod['order_sn']) {
  338. exit_out(null, 10015, '机构该时间段已经预约满了');
  339. }
  340. }
  341. //疫苗预约检查库存是否足够
  342. if ($product_type == 4) {
  343. $stock = Vaccine::where('id', $order['order_vaccine']['vaccine_id'])->value('stock');
  344. if ($stock <= 0) {
  345. exit_out(null, 10009, '该疫苗库存不足');
  346. }
  347. }
  348. if ($order['payment_type'] == 3) {
  349. OrderPack::checkUserServicePack($order['pay_order_pack_id'], $order['user_id'], $product_type);
  350. }
  351. return true;
  352. }
  353. }