DiagnosisOrderController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. namespace app\api\controller\diagnosis;
  3. use app\admin\model\diagnosis\DiagnosisApply;
  4. use app\admin\model\diagnosis\DiagnosisCate;
  5. use app\admin\model\diagnosis\DiagnosisOrder;
  6. use app\admin\model\diagnosis\DiagnosisService;
  7. use app\admin\model\user\UserEquity;
  8. use app\models\user\UserBill;
  9. use crmeb\basic\BaseModel;
  10. use crmeb\repositories\OrderRepository;
  11. use app\admin\model\system\{
  12. SystemAttachment, ShippingTemplates
  13. };
  14. use app\admin\model\user\User;
  15. use app\Request;
  16. use crmeb\services\{
  17. CacheService,
  18. ExpressService,
  19. SystemConfigService,
  20. UtilService
  21. };
  22. /**
  23. * 订单
  24. * Class StoreOrderController
  25. * @package app\api\controller\order
  26. */
  27. class DiagnosisOrderController
  28. {
  29. /**
  30. * 计算订单
  31. * @param Request $request
  32. * @return void
  33. */
  34. public function confirm(Request $request)
  35. {
  36. $data = UtilService::getMore([
  37. ['cate'],
  38. ['service_id'],
  39. ['type'],
  40. ['time'],
  41. ['name'],
  42. ['sex'],
  43. ['age'],
  44. ['phone'],
  45. ['hospital'],
  46. ['urgent_phone'],
  47. ['transfer_address'],
  48. ['remarks'],
  49. ]);
  50. $address = get_address($data['hospital']);
  51. if (empty($data['type'])) return app('json')->fail('传入type');
  52. if ($data['type'] == 1){
  53. if (!strstr($data['transfer_address'], $address['city'])){
  54. return app('json')->fail('接送地址和医院不在同一个市');
  55. }
  56. }
  57. $price = 0;// 金额
  58. $ot_price = 0;
  59. $commission = 0; //佣金
  60. $card_id = [];
  61. $data['cate'] = explode(',', $data['cate']);
  62. foreach ($data['cate'] as $item){
  63. $da = date('w');
  64. if ($da == 0) $da = 7;
  65. $equity = UserEquity::where('c_id', 'like', '%'.$item.'%')->where('uid', $request->uid())->order('number DESC')->where('use', 1)->where('start_week', '<=', $da)->where('end_week', '>=', $da)->find();
  66. $cate = DiagnosisCate::where('id', $item)->find();
  67. if ($equity){
  68. $price += 0;
  69. $card_id[] = $equity['user_card_id'];
  70. }else{
  71. $price += $cate['price'];
  72. }
  73. $ot_price += $cate['price'];
  74. $commission += $cate['reward'];
  75. }
  76. if ($data['type'] == 2){
  77. $ot_price = sys_config('basics_price');
  78. }
  79. if ($data['type'] == 2 and !$card_id){
  80. $price += sys_config('basics_price');
  81. }
  82. $card_id = array_unique($card_id);
  83. if ($card_id){
  84. $card_id = implode(',', $card_id);
  85. }
  86. if ($data['service_id']){
  87. $data['service_id'] = explode(',', $data['service_id']);
  88. foreach ($data['service_id'] as $item){
  89. $cate = DiagnosisService::where('id', $item)->find();
  90. $price += $cate['price'];
  91. $ot_price += $cate['price'];
  92. $commission += $cate['reward'];
  93. }
  94. }
  95. $order['cate_id'] = implode(',', $data['cate']);
  96. $order['ot_price'] = $ot_price;
  97. $order['pay_price'] = $price;
  98. $order['commission'] = $commission;
  99. $order['type'] = $data['type'];
  100. $order['user_card_id'] = $card_id;
  101. if (strtotime($data['time']) < time()){
  102. return app('json')->fail('选择正确的时间');
  103. }
  104. $order['attr'] = [
  105. 'uid' => $request->uid(),
  106. 'time' => strtotime($data['time']),
  107. 'service_id' => $data['service_id'] ? implode(',', $data['service_id']) : [],
  108. 'name' => $data['name'],
  109. 'sex' => $data['sex'],
  110. 'age' => $data['age'],
  111. 'phone' => $data['phone'],
  112. 'hospital' => $data['hospital'],
  113. 'urgent_phone' => $data['urgent_phone'],
  114. 'transfer_address' => $data['transfer_address'],
  115. 'remarks' => $data['remarks'],
  116. ];
  117. list($msec, $sec) = explode(' ', microtime());
  118. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  119. $orderId = $request->uid() . $msectime . mt_rand(10000, 99999);
  120. $order['cache'] = $orderId;
  121. cache($order['cache'], $order, 300);
  122. return app('json')->successful($order);
  123. }
  124. /**
  125. * 创建订单
  126. * @param Request $request
  127. * @return mixed
  128. */
  129. public function create(Request $request)
  130. {
  131. $data = UtilService::postMore([
  132. ['cache', ''],
  133. ['pay_type' , '']
  134. ]);
  135. $uid = $request->uid();
  136. $order = cache($data['cache']);
  137. if (!$order) return app('json')->fail('订单已过期');
  138. if (!$data['pay_type']) return app('json')->fail('支付方式不正确');
  139. $orderInfo = DiagnosisOrder::create_order($order, $data['pay_type'],$uid);
  140. if ($order['pay_price'] == 0){
  141. $res = DiagnosisOrder::yuPay($orderInfo['order_id']);
  142. if ($res){
  143. cache()->delete($data['cache']);
  144. return app('json')->status('success', '支付成功');
  145. }else{
  146. return app('json')->fail('支付失败');
  147. }
  148. }else{
  149. if ($orderInfo['pay_type'] == 1){
  150. $res = DiagnosisOrder::yuPay($orderInfo['order_id']);
  151. if ($res){
  152. cache()->delete($data['cache']);
  153. return app('json')->status('success', '余额支付成功');
  154. }else{
  155. return app('json')->fail('支付失败');
  156. }
  157. }elseif ($orderInfo['pay_type'] ==2){
  158. try {
  159. $jsConfig = OrderRepository::jsDiaPay($orderInfo['order_id']); //创建订单jspay
  160. } catch (\Exception $e) {
  161. return app('json')->status('pay_error', $e->getMessage());
  162. }
  163. $info['jsConfig'] = $jsConfig;
  164. cache()->delete($data['cache']);
  165. return app('json')->status('wechat_pay', '订单创建成功', $info);
  166. }elseif ($orderInfo['pay_type'] ==3){
  167. try {
  168. $jsConfig = OrderRepository::wxDiaPay($orderInfo['order_id']); //创建订单jspay
  169. } catch (\Exception $e) {
  170. return app('json')->status('pay_error', $e->getMessage());
  171. }
  172. $info['jsConfig'] = $jsConfig;
  173. cache()->delete($data['cache']);
  174. return app('json')->status('wechat_h5_pay', '订单创建成功', $info);
  175. }
  176. }
  177. return app('json')->fail('订单生成失败!');
  178. }
  179. /**
  180. * 用户订单
  181. * @param Request $request
  182. * @return mixed
  183. */
  184. public function order_list(Request $request)
  185. {
  186. $data = UtilService::getMore([
  187. ['status', ''],
  188. ['page', 1],
  189. ['limit', 10]
  190. ]);
  191. $list = DiagnosisOrder::alias('a')
  192. ->field('a.*,b.time,b.name,b.sex,b.age,b.phone,b.hospital,b.urgent_phone,b.service_id,b.transfer_address,b.remarks')
  193. ->leftJoin('diagnosis_order_attr b', 'a.id = b.oid')
  194. ->order('a.id DESC')
  195. ->where('a.paid', 1)
  196. ->where('a.status', $data['status'])
  197. ->where('a.uid', $request->uid())
  198. ->page($data['page'], $data['limit'])->select();
  199. $list = count($list) > 0 ? $list->toArray() : [];
  200. if ($list){
  201. foreach ($list as &$item){
  202. $item['images'] = json_decode($item['images']);
  203. $item['after_images'] = json_decode($item['after_images']);
  204. $item['cate'] = DiagnosisCate::where('id', 'in', $item['cate_id'])->column('name');
  205. $item['service'] = DiagnosisService::where('id', 'in', $item['service_id'])->column('name');
  206. $item['receiving'] = DiagnosisApply::where('uid', $item['order_receiving'])->find();
  207. }
  208. }
  209. return app('json')->successful($list);
  210. }
  211. /**
  212. * 订单详情
  213. * @param Request $request
  214. * @return void
  215. */
  216. public function details(Request $request,$id)
  217. {
  218. if (!$id) return app('json')->fail('传入订单id!');
  219. $list = DiagnosisOrder::alias('a')
  220. ->field('a.*,b.time,b.name,b.sex,b.age,b.phone,b.hospital,b.urgent_phone,b.service_id,b.transfer_address,b.remarks')
  221. ->leftJoin('diagnosis_order_attr b', 'a.id = b.oid')
  222. ->order('a.id DESC')
  223. ->where('a.id', $id)
  224. ->find()->toArray();
  225. $list['images'] = json_decode($list['images']);
  226. $list['after_images'] = json_decode($list['after_images']);
  227. $list['cate'] = DiagnosisCate::where('id', 'in', $list['cate_id'])->column('name');
  228. $list['service'] = DiagnosisService::where('id', 'in', $list['service_id'])->column('name');
  229. $list['receiving'] = DiagnosisApply::where('uid', $list['order_receiving'])->find();
  230. return app('json')->successful($list);
  231. }
  232. /**
  233. * 接单
  234. * @param Request $request
  235. * @param $id
  236. * @return mixed
  237. * @throws \think\db\exception\DataNotFoundException
  238. * @throws \think\db\exception\DbException
  239. * @throws \think\db\exception\ModelNotFoundException
  240. */
  241. public function order_receiving(Request $request,$id)
  242. {
  243. BaseModel::beginTrans();
  244. $user = User::where('uid', $request->uid())->find();
  245. if ($user['is_receiver'] != 1) return app('json')->fail('你不是接单员');
  246. if (!$id) return app('json')->fail('传入订单id!');
  247. $order = DiagnosisOrder::where('id', $id)->lock(true)->find();
  248. if ($order['status'] > 0) return app('json')->fail('该订单已被接');
  249. $order['order_receiving'] = $request->uid();
  250. $order['status'] = 1;
  251. $res = $order->save();
  252. if ($res){
  253. BaseModel::commitTrans();
  254. return app('json')->success('接单成功');
  255. }
  256. BaseModel::rollbackTrans();
  257. return app('json')->fail('接单失败');
  258. }
  259. /**
  260. * 接单员订单列表
  261. * @param Request $request
  262. * @return mixed
  263. * @throws \think\db\exception\DataNotFoundException
  264. * @throws \think\db\exception\DbException
  265. * @throws \think\db\exception\ModelNotFoundException
  266. */
  267. public function receiving_list(Request $request)
  268. {
  269. $data = UtilService::getMore([
  270. ['status', ''],
  271. ['page', 1],
  272. ['limit', 10]
  273. ]);
  274. $list = DiagnosisOrder::alias('a')
  275. ->field('a.*,b.time,b.name,b.sex,b.age,b.phone,b.hospital,b.urgent_phone,b.service_id,b.transfer_address,b.remarks')
  276. ->leftJoin('diagnosis_order_attr b', 'a.id = b.oid')
  277. ->order('a.id DESC')
  278. ->where('a.paid', 1)
  279. ->where('a.status', $data['status'])
  280. ->where('a.order_receiving', $request->uid())
  281. ->page($data['page'], $data['limit'])->select();
  282. $list = count($list) > 0 ? $list->toArray() : [];
  283. if ($list){
  284. foreach ($list as &$item){
  285. $item['images'] = json_decode($item['images']);
  286. $item['after_images'] = json_decode($item['after_images']);
  287. $item['cate'] = DiagnosisCate::where('id', 'in', $item['cate_id'])->column('name');
  288. $item['service'] = DiagnosisService::where('id', 'in', $item['service_id'])->column('name');
  289. $item['receiving'] = DiagnosisApply::where('uid', $item['order_receiving'])->find();
  290. }
  291. }
  292. return app('json')->successful($list);
  293. }
  294. /**
  295. * 接单大厅
  296. * @param Request $request
  297. * @return mixed
  298. * @throws \think\db\exception\DataNotFoundException
  299. * @throws \think\db\exception\DbException
  300. * @throws \think\db\exception\ModelNotFoundException
  301. */
  302. public function order_receiving_hall(Request $request)
  303. {
  304. $data = UtilService::getMore([
  305. ['type'],
  306. ['page', 1],
  307. ['limit', 10],
  308. ]);
  309. if (!$data['type']) return app('json')->fail('传入类型');
  310. $list = DiagnosisOrder::alias('a')
  311. ->field('a.*,b.time,b.name,b.sex,b.age,b.phone,b.hospital,b.urgent_phone,b.service_id,b.transfer_address,b.remarks')
  312. ->leftJoin('diagnosis_order_attr b', 'a.id = b.oid')
  313. ->order('a.id DESC')
  314. ->where('a.paid', 1)
  315. ->where('a.type', $data['type'])
  316. ->where('a.status', 0)
  317. ->page($data['page'], $data['limit'])->select();
  318. $list = count($list) > 0 ? $list->toArray() : [];
  319. if ($list){
  320. foreach ($list as &$item){
  321. $item['images'] = json_decode($item['images']);
  322. $item['after_images'] = json_decode($item['after_images']);
  323. $item['cate'] = DiagnosisCate::where('id', 'in', $item['cate_id'])->column('name');
  324. $item['service'] = DiagnosisService::where('id', 'in', $item['service_id'])->column('name');
  325. $item['receiving'] = DiagnosisApply::where('uid', $item['order_receiving'])->find();
  326. }
  327. }
  328. return app('json')->successful($list);
  329. }
  330. /**
  331. * 放弃订单
  332. * @param Request $request
  333. * @param $id
  334. * @return mixed
  335. * @throws \think\db\exception\DataNotFoundException
  336. * @throws \think\db\exception\DbException
  337. * @throws \think\db\exception\ModelNotFoundException、
  338. */
  339. public function discard_order(Request $request,$id)
  340. {
  341. if (!$id) return app('json')->fail('传入订单id!');
  342. $order = DiagnosisOrder::where('id', $id)->where('order_receiving', $request->uid())->lock(true)->find();
  343. if (!$order) return app('json')->fail('订单不存在!');
  344. $order['status'] = 0;
  345. $order['order_receiving'] = 0;
  346. $res = $order->save();
  347. if ($res) return app('json')->success('放弃订单成功');
  348. return app('json')->fail('放弃订单失败');
  349. }
  350. /**
  351. * 取消订单
  352. * @param Request $request
  353. * @param $id
  354. * @return mixed
  355. * @throws \think\db\exception\DataNotFoundException
  356. * @throws \think\db\exception\DbException
  357. * @throws \think\db\exception\ModelNotFoundException
  358. */
  359. public function cancel_order(Request $request,$id)
  360. {
  361. if (!$id) return app('json')->fail('传入订单id!');
  362. $order = DiagnosisOrder::where('id', $id)->where('uid', $request->uid())->find();
  363. if (!$order) return app('json')->fail('订单不存在!');
  364. if ($order['status'] > 2) return app('json')->fail('订单已完成无法取消!');
  365. $order['status'] = -1;
  366. $res = $order->save();
  367. if ($res) return app('json')->success('取消订单成功');
  368. return app('json')->fail('取消订单失败');
  369. }
  370. /**
  371. * 提交完成
  372. * @param Request $request
  373. * @param $id
  374. * @return void
  375. * @throws \think\db\exception\DataNotFoundException
  376. * @throws \think\db\exception\DbException
  377. * @throws \think\db\exception\ModelNotFoundException
  378. */
  379. public function submit(Request $request,$id)
  380. {
  381. $data = UtilService::postMore([
  382. ['images', '']
  383. ]);
  384. if (!$id) return app('json')->fail('传入订单id!');
  385. $order = DiagnosisOrder::where('id', $id)->where('order_receiving', $request->uid())->find();
  386. if (!$order) return app('json')->fail('订单不存在!');
  387. if ($order['status'] <> 1) return app('json')->fail('当前订单状态不对');
  388. $order['images'] = json_encode($data['images']);
  389. $order['status'] = 2;
  390. $res = $order->save();
  391. if ($res) return app('json')->success('提交订单成功');
  392. return app('json')->fail('提交订单失败');
  393. }
  394. /**
  395. * 用户确认完成
  396. * @param Request $request
  397. * @param $id
  398. * @return mixed
  399. * @throws \think\db\exception\DataNotFoundException
  400. * @throws \think\db\exception\DbException
  401. * @throws \think\db\exception\ModelNotFoundException
  402. */
  403. public function confirmation(Request $request,$id)
  404. {
  405. if (!$id) return app('json')->fail('传入订单id!');
  406. BaseModel::beginTrans();
  407. $order = DiagnosisOrder::where('id', $id)->where('uid', $request->uid())->lock(true)->find();
  408. if (!$order) return app('json')->fail('订单不存在!');
  409. if ($order['status'] <> 2) return app('json')->fail('当前订单状态不对');
  410. $order['status'] = 3;
  411. $res1 = $order->save();
  412. $user = User::where('uid', $order['order_receiving'])->find();
  413. $res2 = UserBill::income('完成订单佣金', $order['order_receiving'], 'now_money', 'brokerage', $order['commission'], '', $user['brokerage_price']+$order['commission'], '完成订单佣金');
  414. $res3 = User::where('uid', $order['order_receiving'])->inc('brokerage_price', $order['commission'])->update();
  415. if ($res1 && $res2 && $res3){
  416. BaseModel::commitTrans();
  417. return app('json')->success('成功');
  418. }
  419. BaseModel::rollbackTrans();
  420. return app('json')->fail('失败');
  421. }
  422. /**
  423. * 申请售后
  424. * @param Request $request
  425. * @param $id
  426. * @return mixed
  427. * @throws \think\db\exception\DataNotFoundException
  428. * @throws \think\db\exception\DbException
  429. * @throws \think\db\exception\ModelNotFoundException
  430. */
  431. public function after_sales(Request $request,$id)
  432. {
  433. $data = UtilService::postMore([
  434. ['after_remarks'],
  435. ['after_images']
  436. ]);
  437. if (!$id) return app('json')->fail('传入订单id!');
  438. $order = DiagnosisOrder::where('id', $id)->where('uid', $request->uid())->lock(true)->find();
  439. if (!$order) return app('json')->fail('订单不存在!');
  440. if (empty($data['after_images'])) return app('json')->fail('传入售后图片!');
  441. // if ($order['status'] <> 3) return app('json')->fail('订单未完成');
  442. $order['after_remarks'] = $data['after_remarks'];
  443. $order['after_images'] = json_encode($data['after_images']);
  444. $order['after_sales'] = 1;
  445. $res = $order->save();
  446. if ($res) return app('json')->success('提交成功');
  447. return app('json')->fail('提交失败');
  448. }
  449. }