DiagnosisOrderController.php 19 KB

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