OrderController.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zilongs
  5. * Date: 20-9-30
  6. * Time: 下午10:58
  7. */
  8. namespace App\Http\Controllers\Api\V1;
  9. use App\Models\DocterServiceTime;
  10. use App\Models\DocterSetting;
  11. use App\Models\ImMessage;
  12. use App\Models\Nurse;
  13. use App\Models\Order;
  14. use App\Models\OrderNurse;
  15. use App\Models\OrderPack;
  16. use App\Models\OrderPatient;
  17. use App\Models\OrderVaccine;
  18. use App\Models\Patient;
  19. use App\Models\Payment;
  20. use App\Models\SchedulePeriod;
  21. use App\Models\ServicePack;
  22. use App\Models\Team;
  23. use App\Models\TeamDocter;
  24. use App\Models\TimePeriod;
  25. use App\Models\User;
  26. use App\Models\UserCoupon;
  27. use App\Models\Vaccine;
  28. use EasyWeChat\Factory;
  29. use DB;
  30. use Exception;
  31. class OrderController extends AuthController
  32. {
  33. public function consultPlaceOrder()
  34. {
  35. $req = request()->post();
  36. $this->validate(request(), [
  37. 'payment_type' => 'required|in:1,2,3',
  38. 'product_type' => 'required|in:1,2',
  39. 'docter_id' => 'required|integer',
  40. 'patient_id' => 'required|integer',
  41. 'total_amount' => 'required|integer',
  42. 'user_coupon_id' => 'integer',
  43. 'phone' => 'required_if:product_type,1',
  44. 'phone_minutes' => 'required_if:product_type,1|integer',
  45. 'symptoms' => 'required_if:product_type,2|max:2000',
  46. 'medical_imgs' => 'required_if:product_type,2|json|max:3000',
  47. 'order_pack_id' => 'required_if:payment_type,3|integer',
  48. ]);
  49. $user = $this->user;
  50. //判断是否在服务时间内
  51. $now_line = (int)date('Hi');
  52. if (!DocterServiceTime::where('docter_id', $req['docter_id'])->where('type', $req['product_type'])->where('start_time_line', '<=', $now_line)->where('end_time_line', '>', $now_line)->exists()) {
  53. return out(null, 10011, '当前不在医生服务时间内,不能下单');
  54. }
  55. $discount_amount = 0;
  56. if (!empty($req['user_coupon_id'])) {
  57. //计算优惠金额
  58. $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], $req['product_type']);
  59. }
  60. $payment_amount = $req['total_amount'] - $discount_amount;
  61. $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
  62. if (Order::where('docter_id', $req['docter_id'])->where('product_type', $req['product_type'])->where('user_id', $user['id'])->where('order_status', 1)->exists()) {
  63. return out(null, 10013, '您有一笔该医生的未支付订单,请先去支付');
  64. }
  65. //图文咨询订单未结束时不能针对同一医生再次下图文订单
  66. if ($req['product_type'] == 2 && Order::where('docter_id', $req['docter_id'])->where('product_type', 2)->where('user_id', $user['id'])->whereIn('order_status', [2,3])->exists()) {
  67. return out(null, 10012, '您已经下过该医生的图文订单了,并且订单还未完成');
  68. }
  69. $order_status = $payment_status = 1;
  70. $payment_time = 0;
  71. DB::beginTransaction();
  72. try {
  73. //保存订单数据
  74. $create = [
  75. 'user_id' => $user['id'],
  76. 'payment_type' => $req['payment_type'],
  77. 'product_type' => $req['product_type'],
  78. 'docter_id' => $req['docter_id'],
  79. 'total_amount' => $req['total_amount'],
  80. 'payment_amount' => $payment_amount,
  81. 'discount_amount' => $discount_amount,
  82. 'patient_id' => $req['patient_id'],
  83. 'order_status' => $order_status,
  84. 'payment_status' => $payment_status,
  85. 'payment_time' => $payment_time,
  86. ];
  87. if ($req['payment_type'] == 3) {
  88. $create['pay_order_pack_id'] = $req['order_pack_id'];
  89. }
  90. $order = Order::create($create);
  91. $order_sn = build_sn($order['id']);
  92. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  93. //保存订单患者信息
  94. $addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship_type', 'info', 'card_type', 'card_number', 'card_img_url', 'card_back_img_url', 'email', 'phone as patient_phone', 'social_card_number', 'born_hospital', 'guardian_name', 'address'])->where('id', $req['patient_id'])->first()->getOriginal();
  95. $addPatient['order_id'] = $order['id'];
  96. $addPatient['patient_id'] = $req['patient_id'];
  97. if ($req['product_type'] == 1) {
  98. $addPatient['phone'] = $req['phone'];
  99. $addPatient['phone_minutes'] = $req['phone_minutes'];
  100. }
  101. elseif ($req['product_type'] == 2) {
  102. $addPatient['symptoms'] = $req['symptoms'];
  103. $addPatient['medical_imgs'] = $req['medical_imgs'];
  104. }
  105. OrderPatient::create($addPatient);
  106. //如果有优惠券就标记优惠券为已使用
  107. if (!empty($req['user_coupon_id'])) {
  108. UserCoupon::where('id', $req['user_coupon_id'])->update(['order_id' => $order['id'], 'status' => 2, 'use_time' => time()]);
  109. }
  110. DB::commit();
  111. } catch (Exception $e) {
  112. DB::rollBack();
  113. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  114. }
  115. return out(['order_id' => $order['id']]);
  116. }
  117. public function appointPlaceOrder()
  118. {
  119. $req = request()->post();
  120. $this->validate(request(), [
  121. 'payment_type' => 'required|in:1,2,3',
  122. 'product_type' => 'required|in:3,4,5',
  123. 'patient_id' => 'required|integer',
  124. 'organization_id' => 'required|integer',
  125. 'schedule_date' => 'required|date',
  126. 'time_period_id' => 'required|integer',
  127. 'total_amount' => 'required|integer',
  128. 'user_coupon_id' => 'integer',
  129. 'docter_id' => 'required_if:product_type,3|integer',
  130. 'vaccine_id' => 'required_if:product_type,4|integer',
  131. 'nurse_ids' => 'required_if:product_type,5|json',
  132. 'order_pack_id' => 'required_if:payment_type,3|integer',
  133. ]);
  134. $user = $this->user;
  135. $product_type = $req['product_type'];
  136. $discount_amount = 0;
  137. if (!empty($req['user_coupon_id'])) {
  138. //计算优惠金额
  139. $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], $product_type);
  140. }
  141. $payment_amount = $req['total_amount'] - $discount_amount;
  142. $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
  143. //疫苗和儿保订单未结束时不能再次下单
  144. if (in_array($req['product_type'], [4,5]) && Order::whereIn('product_type', [4,5])->where('patient_id', $req['patient_id'])->whereIn('order_status', [2,3,7])->exists()) {
  145. return out(null, 10012, '该患者已经下过'.config('config.product_type_map')[$req['product_type']].'订单,并且订单还未完成');
  146. }
  147. //检查号源
  148. if ($product_type == 3) {
  149. $schedulePeriod = SchedulePeriod::where('docter_id', $req['docter_id'])->where('time_period_id', $req['time_period_id'])->where('schedule_date', $req['schedule_date'])->where('schedule_type', 1)->first();
  150. if (empty($schedulePeriod)) {
  151. return out(null, 10012, '医生无该时间段的排班');
  152. }
  153. $docterSettings = DocterSetting::select(['service_num'])->where('docter_id', $req['docter_id'])->where('type', 1)->first();
  154. if ($docterSettings['service_num'] <= $schedulePeriod['order_num']) {
  155. return out(null, 10014, '医生该时间段已经预约满了');
  156. }
  157. }
  158. elseif (in_array($product_type, [4,5])) {
  159. $schedule_type_map = [4 => 2, 5 => 3];
  160. $schedulePeriod = SchedulePeriod::where('time_period_id', $req['time_period_id'])->where('schedule_date', $req['schedule_date'])->where('organization_id', $req['organization_id'])->where('schedule_type', $schedule_type_map[$product_type])->first();
  161. if (empty($schedulePeriod)) {
  162. return out(null, 10013, '机构无该时间段的排班');
  163. }
  164. $docterSettings = DocterSetting::select(['service_num'])->where('org_id', $req['organization_id'])->where('type', $schedule_type_map[$product_type])->first();
  165. if ($docterSettings['service_num'] <= $schedulePeriod['order_num']) {
  166. return out(null, 10015, '机构该时间段已经预约满了');
  167. }
  168. }
  169. //疫苗预约检查库存是否足够
  170. if ($req['product_type'] == 4) {
  171. $stock = Vaccine::where('id', $req['vaccine_id'])->value('stock');
  172. if ($stock <= 0) {
  173. return out(null, 10009, '该疫苗库存不足');
  174. }
  175. }
  176. $order_status = $payment_status = 1;
  177. $payment_time = 0;
  178. DB::beginTransaction();
  179. try {
  180. $timePeriod = TimePeriod::where('id', $req['time_period_id'])->first();
  181. //保存订单数据
  182. $create = [
  183. 'user_id' => $user['id'],
  184. 'docter_id' => $req['docter_id'] ?? 0,
  185. 'patient_id' => $req['patient_id'],
  186. 'organization_id' => $req['organization_id'],
  187. 'payment_type' => $req['payment_type'],
  188. 'product_type' => $product_type,
  189. 'order_status' => $order_status,
  190. 'payment_status' => $payment_status,
  191. 'total_amount' => $req['total_amount'],
  192. 'payment_amount' => $payment_amount,
  193. 'discount_amount' => $discount_amount,
  194. 'payment_time' => $payment_time,
  195. 'appoint_date' => strtotime(date('Y-m-d 00:00:00')),
  196. 'appoint_start_time' => strtotime($req['schedule_date'].' '.$timePeriod['start_time_period'].':00'),
  197. 'appoint_end_time' => strtotime($req['schedule_date'].' '.$timePeriod['end_time_period'].':00'),
  198. ];
  199. if ($req['payment_type'] == 3) {
  200. $create['pay_order_pack_id'] = $req['order_pack_id'];
  201. }
  202. $order = Order::create($create);
  203. $order_sn = build_sn($order['id']);
  204. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  205. //保存订单患者信息
  206. $addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship_type', 'info', 'card_type', 'card_number', 'card_img_url', 'card_back_img_url', 'email', 'phone as patient_phone', 'social_card_number', 'born_hospital', 'guardian_name', 'address'])->where('id', $req['patient_id'])->first()->getOriginal();
  207. $addPatient['order_id'] = $order['id'];
  208. $addPatient['patient_id'] = $req['patient_id'];
  209. $addPatient['organization_id'] = $req['organization_id'];
  210. $addPatient['time_period_id'] = $req['time_period_id'];
  211. $addPatient['appoint_start_time'] = strtotime($req['schedule_date'].' '.$timePeriod['start_time_period'].':00');
  212. $addPatient['appoint_end_time'] = strtotime($req['schedule_date'].' '.$timePeriod['end_time_period'].':00');
  213. $orderPatient = OrderPatient::create($addPatient);
  214. //保存订单疫苗信息
  215. if ($req['product_type'] == 4) {
  216. $addVaccine = Vaccine::select(['id as vaccine_id', 'type as vaccine_type', 'price as vaccine_price', 'name as vaccine_name', 'remark as vaccine_remark', 'supplier as vaccine_supplier'])->where('id', $req['vaccine_id'])->where('org_id', $req['organization_id'])->first()->getOriginal();
  217. $addVaccine['order_id'] = $order['id'];
  218. $addVaccine['order_patient_id'] = $orderPatient['id'];
  219. $addVaccine = array_filter($addVaccine);
  220. OrderVaccine::create($addVaccine);
  221. }
  222. //保存儿保订单信息
  223. if ($req['product_type'] == 5) {
  224. $nurse_ids = json_decode($req['nurse_ids'], true);
  225. foreach ($nurse_ids as $k => $v) {
  226. $addNurse = Nurse::select(['id as nurse_id', 'price as nurse_price', 'name as nurse_name', 'remark as nurse_remark'])->where('id', $v)->first()->getOriginal();
  227. $addNurse['order_id'] = $order['id'];
  228. $addNurse['order_patient_id'] = $orderPatient['id'];
  229. OrderNurse::create($addNurse);
  230. }
  231. }
  232. //如果有优惠券就标记优惠券为已使用
  233. if (!empty($req['user_coupon_id'])) {
  234. UserCoupon::where('id', $req['user_coupon_id'])->update(['order_id' => $order['id'], 'status' => 2, 'use_time' => time()]);
  235. }
  236. //疫苗预约 订单直接完成
  237. if ($product_type == 4) {
  238. Order::payCompletedHandle($order['id']);
  239. }
  240. DB::commit();
  241. } catch (Exception $e) {
  242. DB::rollBack();
  243. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  244. }
  245. return out(['order_id' => $order['id']]);
  246. }
  247. public function packPlaceOrder()
  248. {
  249. $req = request()->post();
  250. $this->validate(request(), [
  251. 'payment_type' => 'required|in:1,2',
  252. 'patient_id' => 'required|integer',
  253. 'total_amount' => 'required|integer',
  254. 'user_coupon_id' => 'integer',
  255. 'service_pack_id' => 'required|integer',
  256. 'is_security' => 'required|in:0,1',
  257. 'guardian_name' => 'max:50',
  258. 'relationship_type' => 'integer',
  259. 'is_need_insurance' => 'in:0,1',
  260. 'team_id' => 'integer',
  261. ]);
  262. $user = $this->user;
  263. $discount_amount = 0;
  264. if (!empty($req['user_coupon_id'])) {
  265. //计算优惠金额
  266. $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], 6);
  267. }
  268. $addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship_type', 'info', 'card_type', 'card_number', 'card_img_url', 'card_back_img_url', 'email', 'phone as patient_phone', 'social_card_number', 'born_hospital', 'guardian_name', 'address'])->where('id', $req['patient_id'])->first()->getOriginal();
  269. $payment_amount = $req['total_amount'] - $discount_amount;
  270. $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
  271. $order_status = $payment_status = 1;
  272. $payment_time = 0;
  273. DB::beginTransaction();
  274. try {
  275. //保存订单数据
  276. $order = Order::create([
  277. 'user_id' => $user['id'],
  278. 'patient_id' => $req['patient_id'],
  279. 'payment_type' => $req['payment_type'],
  280. 'product_type' => 6,
  281. 'total_amount' => $req['total_amount'],
  282. 'payment_amount' => $payment_amount,
  283. 'discount_amount' => $discount_amount,
  284. 'order_status' => $order_status,
  285. 'payment_status' => $payment_status,
  286. 'payment_time' => $payment_time,
  287. ]);
  288. $order_sn = build_sn($order['id']);
  289. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  290. //保存订单患者信息
  291. $addPatient['order_id'] = $order['id'];
  292. $addPatient['patient_id'] = $req['patient_id'];
  293. OrderPatient::create($addPatient);
  294. //保存订单服务包表
  295. $addPack = ServicePack::select(['id as service_pack_id', 'name as pack_name', 'intro as pack_intro', 'price as pack_price', 'insurance_policy', 'insurance_img_url', 'team_id', 'phone_minutes', 'chat_num', 'appoint_num', 'vaccine_limit_amount', 'nurses_limit_amount', 'effective_days', 'label'])->where('id', $req['service_pack_id'])->first()->getOriginal();
  296. $addPack['user_id'] = $user['id'];
  297. $addPack['order_id'] = $order['id'];
  298. $addPack['is_security'] = $req['is_security'];
  299. $addPack['guardian_name'] = !empty($req['guardian_name']) ? $req['guardian_name'] : $addPatient['guardian_name'];
  300. $addPack['relationship_type'] = !empty($req['relationship_type']) ? $req['relationship_type'] : $addPatient['relationship_type'];
  301. $addPack['start_time'] = time();
  302. $addPack['end_time'] = time() + $addPack['effective_days']*24*3600;
  303. $addPack['total_phone_minutes'] = $addPack['phone_minutes'];
  304. $addPack['total_chat_num'] = $addPack['chat_num'];
  305. $addPack['total_appoint_num'] = $addPack['appoint_num'];
  306. $addPack['total_vaccine_limit_amount'] = $addPack['vaccine_limit_amount'];
  307. $addPack['total_nurses_limit_amount'] = $addPack['nurses_limit_amount'];
  308. $addPack['label'] = json_decode($addPack['label'], true);
  309. $addPack['is_need_insurance'] = !empty($req['is_need_insurance']) ? $req['is_need_insurance'] : 0;
  310. $pack_team_ids = json_decode($addPack['team_id'], true);
  311. if (!empty($req['team_id'])) {
  312. $pack_team_ids = [$req['team_id']];
  313. }
  314. $addPack['team_id'] = $pack_team_ids;
  315. OrderPack::create($addPack);
  316. //如果有优惠券就标记优惠券为已使用
  317. if (!empty($req['user_coupon_id'])) {
  318. UserCoupon::where('id', $req['user_coupon_id'])->update(['order_id' => $order['id'], 'status' => 2, 'use_time' => time()]);
  319. }
  320. DB::commit();
  321. } catch (Exception $e) {
  322. DB::rollBack();
  323. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  324. }
  325. return out(['order_id' => $order['id']]);
  326. }
  327. public function orderList()
  328. {
  329. $req = request()->post();
  330. $this->validate(request(), [
  331. 'list_type' => 'required|in:0,1,2',
  332. 'product_type' => 'integer',
  333. 'order_status' => 'integer',
  334. 'time_sort' => 'in:0,1',
  335. 'is_pack_expire' => 'in:0,1,2',
  336. ]);
  337. $user = $this->user;
  338. $builder = Order::with(['docter.office', 'docter.qualification', 'orderPatient', 'orderPack', 'orderNurse', 'orderVaccine', 'organization.docter', 'suggest'])->where('user_id', $user['id']);
  339. if (!empty($req['product_type'])) {
  340. $builder->where('product_type', $req['product_type']);
  341. }
  342. else {
  343. if (!empty($req['list_type'])) {
  344. if ($req['list_type'] == 1) {
  345. $builder->whereIn('product_type', [1,2]);
  346. }
  347. elseif ($req['list_type'] == 2) {
  348. $builder->whereIn('product_type', [3,4,5]);
  349. }
  350. }
  351. else {
  352. $builder->where('product_type', '<', 7);
  353. }
  354. }
  355. if (!empty($req['order_status'])) {
  356. $builder->where('order_status', $req['order_status']);
  357. }
  358. if (!empty($req['is_pack_expire'])) {
  359. $tmpBuilder = OrderPack::join('orders as o', 'o.id', '=', 'order_packs.order_id')->where('o.user_id', $user['id']);
  360. if ($req['is_pack_expire'] == 1) {
  361. $tmpBuilder->where('order_packs.end_time', '<', time());
  362. }
  363. else {
  364. $tmpBuilder->where('order_packs.end_time', '>=', time());
  365. }
  366. $order_ids = $tmpBuilder->pluck('o.id')->toArray();
  367. $builder->whereIn('id', $order_ids);
  368. }
  369. if (!empty($req['time_sort'])) {
  370. $builder->orderBy('id', 'desc');
  371. }
  372. else {
  373. $builder->orderBy('id', 'asc');
  374. }
  375. $data = $builder->paginate();
  376. return out($data);
  377. }
  378. public function orderDetail()
  379. {
  380. $req = request()->post();
  381. $this->validate(request(), [
  382. 'order_id' => 'required|integer'
  383. ]);
  384. $user = $this->user;
  385. $data = Order::with(['docter.office', 'docter.qualification', 'orderPatient', 'orderPack', 'orderNurse', 'orderVaccine', 'organization.docter', 'suggest'])->where('id', $req['order_id'])->where('user_id', $user['id'])->first()->toArray();
  386. if (!empty($data['order_pack'])) {
  387. $data['order_pack']['team'] = [];
  388. if (!empty($data['order_pack']['team_id'])) {
  389. $data['order_pack']['team'] = Team::with(['docter.office', 'docter.qualification'])->whereIn('id', $data['order_pack']['team_id'])->get()->toArray();
  390. }
  391. }
  392. return out($data);
  393. }
  394. public function topup()
  395. {
  396. $req = request()->post();
  397. $this->validate(request(), [
  398. 'amount' => 'required|integer',
  399. ]);
  400. $user = $this->user;
  401. DB::beginTransaction();
  402. try {
  403. //保存订单数据
  404. $order = Order::create([
  405. 'user_id' => $user['id'],
  406. 'product_type' => 7,
  407. 'total_amount' => $req['amount'],
  408. 'payment_amount' => $req['amount'],
  409. ]);
  410. $order_sn = build_sn($order['id']);
  411. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  412. //生成支付交易单
  413. $trade_sn = build_sn($order['id'], 3, 'T');
  414. $payBody = '超级宝妈-'.config('config.product_type_map')[7];
  415. Payment::create([
  416. 'user_id' => $user['id'],
  417. 'order_id' => $order['id'],
  418. 'trade_sn' => $trade_sn,
  419. 'amount' => $req['amount'],
  420. 'remark' => $payBody,
  421. ]);
  422. //请求支付
  423. $payment = Factory::payment(config('config.wechat_pay'));
  424. $result = $payment->order->unify([
  425. 'body' => $payBody,
  426. 'out_trade_no' => $trade_sn,
  427. 'total_fee' => $req['amount'],
  428. 'trade_type' => 'JSAPI',
  429. 'openid' => $user['openid'],
  430. ]);
  431. if (empty($result['prepay_id'])) {
  432. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  433. return out(null, 702, $errorMsg);
  434. }
  435. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  436. DB::commit();
  437. } catch (Exception $e) {
  438. DB::rollBack();
  439. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  440. }
  441. return out($config);
  442. }
  443. public function orderCancel()
  444. {
  445. $req = request()->post();
  446. $this->validate(request(), [
  447. 'order_id' => 'required|integer'
  448. ]);
  449. $user = $this->user;
  450. $order = Order::with(['orderPatient'])->where('id', $req['order_id'])->first()->toArray();
  451. if ($order['order_status'] == 4) {
  452. return out(null, 10001, '订单已完成,不能取消了');
  453. }
  454. if ($order['order_status'] == 5 || $order['order_status'] == 6) {
  455. return out(null, 10002, '订单已取消了,请勿重复操作');
  456. }
  457. if (in_array($order['product_type'], [1, 2])) {
  458. if ($order['order_status'] == 3) {
  459. return out(null, 10004, '医生已接单,不能再取消订单了');
  460. }
  461. }
  462. elseif (in_array($order['product_type'], [4])) {
  463. if ($order['order_patient']['appoint_start_time'] - 3600 < time()) {
  464. return out(null, 10003, '预约时间临近,不能取消订单了');
  465. }
  466. }
  467. DB::beginTransaction();
  468. try {
  469. Order::orderCancel($req['order_id'], '用户取消订单');
  470. //发送取消订单消息
  471. $order = Order::with(['docter', 'orderPatient', 'organization'])->where('id', $req['order_id'])->first()->toArray();
  472. $keyword2 = config('config.product_type_map')[$order['product_type']];
  473. if (in_array($order['product_type'], [1,2,3])) {
  474. $server_name = $order['docter']['name'].'医生';
  475. }
  476. else {
  477. $server_name = $order['organization']['name'];
  478. }
  479. $payment_status_text = '';
  480. if ($order['product_type'] != 4) {
  481. $payment_status_text = '支付状态:'.config('config.payment_status_map')[$order['payment_status']].' ';
  482. }
  483. $official_arr = [$user['openid'], $order['order_sn'], $keyword2, round($order['total_amount']/100, 2), $order['created_at'], $order['order_patient']['name'], $server_name, $order['order_notes'], $payment_status_text];
  484. $subscribe_arr = [$user['openid'], $order['order_sn'], $keyword2, $order['order_notes']];
  485. send_wechat_message(7, $official_arr, $subscribe_arr);
  486. DB::commit();
  487. } catch (Exception $e) {
  488. DB::rollBack();
  489. return out(null, 500, '取消订单失败,请稍后重试', $e->getMessage());
  490. }
  491. return out();
  492. }
  493. public function orderPackPayList()
  494. {
  495. $req = request()->post();
  496. $this->validate(request(), [
  497. 'docter_id' => 'required|integer',
  498. ]);
  499. $user = $this->user;
  500. $orderPacks = OrderPack::where('user_id', $user['id'])->where('end_time', '>', time())->get()->toArray();
  501. if (!empty($orderPacks)) {
  502. foreach ($orderPacks as $k => $v) {
  503. $order_status = Order::where('id', $v['order_id'])->value('order_status');
  504. if (!in_array($order_status, [3,4])) {
  505. unset($orderPacks[$k]);
  506. }
  507. if ($v['usable_status'] == 0) {
  508. unset($orderPacks[$k]);
  509. }
  510. if (!empty($v['team_id'])) {
  511. if (!TeamDocter::whereIn('team_id', $v['team_id'])->where('docter_id', $req['docter_id'])->exists()) {
  512. unset($orderPacks[$k]);
  513. }
  514. }
  515. }
  516. $orderPacks = array_values($orderPacks);
  517. }
  518. return out($orderPacks);
  519. }
  520. public function orderPay()
  521. {
  522. $req = request()->post();
  523. $this->validate(request(), [
  524. 'order_id' => 'required|integer',
  525. 'pay_password' => 'max:20',
  526. ]);
  527. $user = $this->user;
  528. $order = Order::where('id', $req['order_id'])->first()->toArray();
  529. if ($order['order_status'] != 1) {
  530. return out(null, 10001, '该订单不能支付了');
  531. }
  532. if (in_array($order['payment_type'], [2,3]) && $order['payment_amount'] > 0 && empty($req['pay_password'])) {
  533. return out(null, 10011, '请输入支付密码');
  534. }
  535. if (!empty($req['pay_password'])) {
  536. if (empty($user['pay_password'])) {
  537. return out(null, 60010, '未设置支付密码');
  538. }
  539. if (sha1(md5($req['pay_password'])) !== $user['pay_password']) {
  540. return out(null, 10001, '密码错误');
  541. }
  542. }
  543. if ($order['payment_type'] == 2) {
  544. if ($user['balance'] < $order['payment_amount']) {
  545. return out(null, 601, '余额不足');
  546. }
  547. }
  548. Order::checkOrder($req['order_id']);
  549. $config = null;
  550. DB::beginTransaction();
  551. try {
  552. //判断是微信支付
  553. if ($order['payment_type'] == 1) {
  554. //生成支付交易单
  555. if ($order['payment_amount'] > 0) {
  556. $trade_sn = build_sn($order['id'], 3, 'T');
  557. $payBody = '超级宝妈-'.config('config.product_type_map')[$order['product_type']];
  558. Payment::create([
  559. 'user_id' => $user['id'],
  560. 'order_id' => $order['id'],
  561. 'trade_sn' => $trade_sn,
  562. 'amount' => $order['payment_amount'],
  563. 'remark' => $payBody,
  564. ]);
  565. //请求支付
  566. $payment = Factory::payment(config('config.wechat_pay'));
  567. $result = $payment->order->unify([
  568. 'body' => $payBody,
  569. 'out_trade_no' => $trade_sn,
  570. 'total_fee' => $order['payment_amount'],
  571. 'trade_type' => 'JSAPI',
  572. 'openid' => $user['openid'],
  573. ]);
  574. if (empty($result['prepay_id'])) {
  575. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  576. return out(null, 702, $errorMsg);
  577. }
  578. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  579. }
  580. }
  581. //判断是余额支付
  582. elseif ($order['payment_type'] == 2) {
  583. if ($order['payment_amount'] > 0) {
  584. //改变用户余额
  585. $change_amount = 0 - $order['payment_amount'];
  586. User::changeBalance($user['id'], $change_amount, 1, $order['id'], '预约订单消费');
  587. }
  588. }
  589. if ($order['payment_amount'] == 0 || in_array($order['payment_type'], [2, 3])) {
  590. Order::payCompletedHandle($order['id']);
  591. }
  592. DB::commit();
  593. } catch (Exception $e) {
  594. DB::rollBack();
  595. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  596. }
  597. return out($config);
  598. }
  599. public function getChatRecord()
  600. {
  601. $req = request()->post();
  602. $this->validate(request(), [
  603. 'order_id' => 'required|integer'
  604. ]);
  605. $data = ImMessage::getChatRecord($req['order_id']);
  606. return out($data);
  607. }
  608. }