Order.php 20 KB

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