Order.php 19 KB

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