Order.php 19 KB

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