DiagnosisOrderController.php 18 KB

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