Order.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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
  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::with(['organization', 'orderVaccine', 'orderNurse', 'orderPack'])->where('id', $order_id)->first()->toArray();
  82. $orderPatient = OrderPatient::where('order_id', $order_id)->first();
  83. $user = User::select(['balance', 'openid'])->where('id', $order['user_id'])->first();
  84. //发送下单消息
  85. if ($order['product_type'] < 6) {
  86. $product_type_text = config('config.product_type_map')[$order['product_type']];
  87. UserMessage::saveMessage($order['user_id'], 4, $order_id, [$product_type_text]);
  88. }
  89. elseif ($order['product_type'] == 6) {
  90. $orderPack = OrderPack::select(['pack_name', 'end_time'])->where('order_id', $order_id)->first();
  91. UserMessage::saveMessage($order['user_id'], 5, $order_id, [$orderPack['pack_name'], date('Y-m-d', $orderPack['end_time'])]);
  92. //更新用户为服务包用户
  93. User::where('id', $order['user_id'])->update(['is_pack' => 1]);
  94. }
  95. elseif ($order['product_type'] == 7) {
  96. UserMessage::saveMessage($order['user_id'], 7, $order_id, [round($order['total_amount']/100, 2), round($user['balance']/100, 2)]);
  97. }
  98. //发送余额付款成功消息
  99. if ($order['payment_type'] == 2) {
  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']);
  107. }
  108. //如果是门诊预约,预约时间段的订单数要加1
  109. if ($order['product_type'] == 3) {
  110. $schedule_date = date('Y-m-d', $orderPatient['appoint_start_time']);
  111. 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');
  112. }
  113. //如果是儿保和疫苗预约,预约时间段的订单数要加1
  114. if (in_array($order['product_type'], [4,5])) {
  115. $schedule_date = date('Y-m-d', $orderPatient['appoint_start_time']);
  116. $schedule_type = $order['product_type'] == 4 ? 2 : 3;
  117. 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');
  118. }
  119. //如果是疫苗就减少疫苗库存
  120. if ($order['product_type'] == 4) {
  121. $orderVaccine = OrderVaccine::where('order_id', $order_id)->first();
  122. OrganizationVaccine::where('org_id', $order['organization_id'])->where('vaccine_id', $orderVaccine['vaccine_id'])->decrement('stock');
  123. }
  124. //发送微信消息
  125. $docter = Docter::where('id', $order['docter_id'])->first();
  126. if ($order['product_type'] == 1) {
  127. $official_arr = [$user['openid'], $docter['name'], date('Y-m-d H:i:s'), round($order['payment_amount']/100, 2)];
  128. $subscribe_arr = [];
  129. send_wechat_message(1, $official_arr, $subscribe_arr);
  130. }
  131. if ($order['product_type'] == 2) {
  132. $official_arr = [$user['openid'], $docter['name'], $orderPatient['name'], $orderPatient['symptoms'], date('Y-m-d H:i:s'), round($order['payment_amount']/100, 2)];
  133. $subscribe_arr = [];
  134. send_wechat_message(2, $official_arr, $subscribe_arr);
  135. }
  136. if ($order['product_type'] == 3) {
  137. $keyword2 = date('Y-m-d H:i', $orderPatient['appoint_start_time']).' - '.date('H:i', $orderPatient['appoint_end_time']);
  138. $official_arr = [$user['openid'], $docter['name'], $keyword2, $orderPatient['name'], $orderPatient['phone'], $order['organization']['name'], $order['organization']['address']];
  139. $subscribe_arr = [];
  140. send_wechat_message(3, $official_arr, $subscribe_arr);
  141. }
  142. if ($order['product_type'] == 4) {
  143. $keyword2 = date('Y-m-d H:i', $orderPatient['appoint_start_time']).' - '.date('H:i', $orderPatient['appoint_end_time']);
  144. $official_arr = [$user['openid'], $order['organization']['name'], $order['order_vaccine']['vaccine_name'], $keyword2, $orderPatient['name'], $orderPatient['phone'], $order['organization']['name'], $order['organization']['address']];
  145. $subscribe_arr = [];
  146. send_wechat_message(4, $official_arr, $subscribe_arr);
  147. }
  148. if ($order['product_type'] == 5) {
  149. $keyword2 = date('Y-m-d H:i', $orderPatient['appoint_start_time']).' - '.date('H:i', $orderPatient['appoint_end_time']);
  150. $official_arr = [$user['openid'], $order['organization']['name'], $order['order_nurse'][0]['nurse_name'], $keyword2, $orderPatient['name'], $orderPatient['phone'], $order['organization']['name'], $order['organization']['address']];
  151. $subscribe_arr = [];
  152. send_wechat_message(5, $official_arr, $subscribe_arr);
  153. }
  154. if ($order['product_type'] == 6) {
  155. $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'])];
  156. $subscribe_arr = [];
  157. send_wechat_message(6, $official_arr, $subscribe_arr);
  158. }
  159. return true;
  160. }
  161. public function suggest()
  162. {
  163. return $this->hasOne(Suggest::class)->select(['id', 'order_id']);
  164. }
  165. public function getIsEvaluateAttribute()
  166. {
  167. $is_evaluate = 0;
  168. $user = User::getUserByToken(false);
  169. if (!empty($user)) {
  170. if (Evaluate::where('order_id', $this->id)->where('user_id', $user['id'])->exists()) {
  171. $is_evaluate = 1;
  172. }
  173. }
  174. return $is_evaluate;
  175. }
  176. //取消订单,建议是在事务里面去调用
  177. public static function orderCancel($order_id, $order_notes = '')
  178. {
  179. $order = Order::with(['orderPatient'])->where('id', $order_id)->first()->toArray();
  180. //判断预约时间是否还剩1小时内
  181. $can_refund = true;
  182. if (in_array($order['product_type'], [3,4,5])) {
  183. $order_notes = '用户取消预约';
  184. if ($order['order_patient']['appoint_start_time'] - 3600 < time()) {
  185. $order_notes = '用户超时取消';
  186. $can_refund = false;
  187. }
  188. }
  189. //改变订单状态
  190. $updateOrder = ['order_status' => 5, 'order_notes' => $order_notes];
  191. if ($order['payment_status'] > 1) {
  192. //判断是余额支付还是服务包支付
  193. if ($order['payment_type'] == 2) {
  194. //退服务包的次数
  195. $map = [1 => 'phone_minutes', 2 => 'chat_num', 3 => 'appoint_num', 4 => 'vaccine_limit_amount', 5 => 'nurses_limit_amount'];
  196. $addNum = $order['product_type'] == 1 ? 10 : 1;
  197. OrderPack::where('id', $order['pay_order_pack_id'])->increment($map[$order['product_type']], $addNum);
  198. $updateOrder['cancel_time'] = time();
  199. $updateOrder['payment_status'] = 4;
  200. if ($order['product_type'] == 3 && !$can_refund) {
  201. $updateOrder['payment_status'] = 2;
  202. }
  203. }
  204. else {
  205. //退钱到余额
  206. if (!empty($order['payment_amount']) && $can_refund && $order['product_type'] != 5) {
  207. User::changeBalance($order['user_id'], $order['payment_amount'], 4, $order['id'], '取消订单退款');
  208. }
  209. $updateOrder['cancel_time'] = time();
  210. if ($can_refund) {
  211. $updateOrder['payment_status'] = 4;
  212. }
  213. if ($order['product_type'] == 5) {
  214. $updateOrder['payment_status'] = 5;
  215. }
  216. if ($order['product_type'] == 3 && !$can_refund) {
  217. $updateOrder['payment_status'] = 2;
  218. }
  219. }
  220. }
  221. Order::where('id', $order_id)->update($updateOrder);
  222. //如果是门诊预约且距离预约时间还有1个小时以上,那么预约时间段的订单数减1
  223. if ($order['product_type'] == 3 && $can_refund) {
  224. $schedule_date = date('Y-m-d', $order['order_patient']['appoint_start_time']);
  225. 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');
  226. }
  227. //如果是疫苗儿保预约且距离预约时间还有1个小时以上,那么预约时间段的订单数减1
  228. if (in_array($order['product_type'], [4,5]) && $can_refund) {
  229. $schedule_type_map = [4 => 2, 5 => 3];
  230. $schedule_date = date('Y-m-d', $order['order_patient']['appoint_start_time']);
  231. 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']])->decrement('order_num');
  232. }
  233. //如果是疫苗预约那么取消订单就增加疫苗库存
  234. if ($order['product_type'] == 4) {
  235. $orderVaccine = OrderVaccine::where('order_id', $order_id)->first();
  236. OrganizationVaccine::where('org_id', $order['organization_id'])->where('vaccine_id', $orderVaccine['vaccine_id'])->increment('stock');
  237. }
  238. return true;
  239. }
  240. public function getConsultDurationAttribute()
  241. {
  242. $duration = 0;
  243. if ($this->product_type == 1) {
  244. $duration = CallLog::where('order_id', $this->id)->where('talk_time', '<>', null)->sum('talk_time');
  245. $duration = !empty($duration) ? $duration : 0;
  246. }
  247. elseif ($this->product_type == 2) {
  248. $duration = $this->end_time - $this->receiving_time;
  249. }
  250. return $duration > 0 ? $duration : 0;
  251. }
  252. public function getCallbackPhoneAttribute()
  253. {
  254. $secret_no = '';
  255. if ($this->product_type == 1) {
  256. $secret_no = CallLog::where('order_id', $this->id)->orderBy('id', 'desc')->value('secret_no');
  257. }
  258. return !empty($secret_no) ? $secret_no : '';
  259. }
  260. public static function checkOrder($order_id)
  261. {
  262. $order = Order::with(['orderPatient', 'orderVaccine'])->where('id', $order_id)->first()->toArray();
  263. $product_type = $order['product_type'];
  264. if (in_array($product_type, [1,2])) {
  265. //判断是否在服务时间内
  266. $now_line = (int)date('Hi');
  267. 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()) {
  268. exit_out(null, 10011, '当前不在医生服务时间内,不能下单');
  269. }
  270. //图文咨询订单未结束时不能针对同一医生再次下图文订单
  271. 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()) {
  272. exit_out(null, 10012, '您已经下过该医生的图文订单了,并且订单还未完成');
  273. }
  274. }
  275. //检查号源
  276. elseif ($product_type == 3) {
  277. $schedule_date = date('Y-m-d', $order['order_patient']['appoint_start_time']);
  278. $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();
  279. if (empty($schedulePeriod)) {
  280. exit_out(null, 10012, '医生无该时间段的排班');
  281. }
  282. $docterSettings = DocterSetting::select(['service_num'])->where('docter_id', $order['docter_id'])->where('type', 1)->first();
  283. if ($docterSettings['service_num'] <= $schedulePeriod['order_sn']) {
  284. exit_out(null, 10014, '医生该时间段已经预约满了');
  285. }
  286. }
  287. elseif (in_array($product_type, [4,5])) {
  288. $schedule_date = date('Y-m-d', $order['order_patient']['appoint_start_time']);
  289. $schedule_type_map = [4 => 2, 5 => 3];
  290. $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();
  291. if (empty($schedulePeriod)) {
  292. exit_out(null, 10013, '机构无该时间段的排班');
  293. }
  294. $docterSettings = DocterSetting::select(['service_num'])->where('org_id', $order['organization_id'])->where('type', $schedule_type_map[$product_type])->first();
  295. if ($docterSettings['service_num'] <= $schedulePeriod['order_sn']) {
  296. exit_out(null, 10015, '机构该时间段已经预约满了');
  297. }
  298. }
  299. //疫苗预约检查库存是否足够
  300. if ($product_type == 4) {
  301. $stock = OrganizationVaccine::where('org_id', $order['organization_id'])->where('vaccine_id', $order['order_vaccine']['vaccine_id'])->value('stock');
  302. if ($stock <= 0) {
  303. exit_out(null, 10009, '该疫苗库存不足');
  304. }
  305. }
  306. if ($order['payment_type'] == 3) {
  307. OrderPack::checkUserServicePack($order['pay_order_pack_id'], $order['user_id'], $product_type);
  308. }
  309. return true;
  310. }
  311. }