DiagnosisOrderController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. if (empty($data['type'])) return app('json')->fail('传入type');
  51. if ($data['type'] == 1 and strstr($data['service_id'], '1')){
  52. $address = get_address($data['hospital']);
  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. if ($order['status'] > 0) return app('json')->fail('该订单已被接');
  250. $order['order_receiving'] = $request->uid();
  251. $order['status'] = 1;
  252. $res = $order->save();
  253. if ($res){
  254. BaseModel::commitTrans();
  255. return app('json')->success('接单成功');
  256. }
  257. BaseModel::rollbackTrans();
  258. return app('json')->fail('接单失败');
  259. }
  260. /**
  261. * 接单员订单列表
  262. * @param Request $request
  263. * @return mixed
  264. * @throws \think\db\exception\DataNotFoundException
  265. * @throws \think\db\exception\DbException
  266. * @throws \think\db\exception\ModelNotFoundException
  267. */
  268. public function receiving_list(Request $request)
  269. {
  270. $data = UtilService::getMore([
  271. ['status', ''],
  272. ['page', 1],
  273. ['limit', 10]
  274. ]);
  275. $list = DiagnosisOrder::alias('a')
  276. ->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')
  277. ->leftJoin('diagnosis_order_attr b', 'a.id = b.oid')
  278. ->order('a.id DESC')
  279. ->where('a.paid', 1)
  280. ->where('a.status', $data['status'])
  281. ->where('a.order_receiving', $request->uid())
  282. ->page($data['page'], $data['limit'])->select();
  283. $list = count($list) > 0 ? $list->toArray() : [];
  284. if ($list){
  285. foreach ($list as &$item){
  286. $item['images'] = json_decode($item['images']);
  287. $item['after_images'] = json_decode($item['after_images']);
  288. $item['cate'] = DiagnosisCate::where('id', 'in', $item['cate_id'])->column('name');
  289. $item['service'] = DiagnosisService::where('id', 'in', $item['service_id'])->column('name');
  290. $item['receiving'] = DiagnosisApply::where('uid', $item['order_receiving'])->find();
  291. }
  292. }
  293. return app('json')->successful($list);
  294. }
  295. /**
  296. * 接单大厅
  297. * @param Request $request
  298. * @return mixed
  299. * @throws \think\db\exception\DataNotFoundException
  300. * @throws \think\db\exception\DbException
  301. * @throws \think\db\exception\ModelNotFoundException
  302. */
  303. public function order_receiving_hall(Request $request)
  304. {
  305. $data = UtilService::getMore([
  306. ['type'],
  307. ['page', 1],
  308. ['limit', 10],
  309. ]);
  310. if (!$data['type']) return app('json')->fail('传入类型');
  311. $list = DiagnosisOrder::alias('a')
  312. ->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')
  313. ->leftJoin('diagnosis_order_attr b', 'a.id = b.oid')
  314. ->order('a.id DESC')
  315. ->where('a.paid', 1)
  316. ->where('a.type', $data['type'])
  317. ->where('a.status', 0)
  318. ->page($data['page'], $data['limit'])->select();
  319. $list = count($list) > 0 ? $list->toArray() : [];
  320. if ($list){
  321. foreach ($list as &$item){
  322. $item['images'] = json_decode($item['images']);
  323. $item['after_images'] = json_decode($item['after_images']);
  324. $item['cate'] = DiagnosisCate::where('id', 'in', $item['cate_id'])->column('name');
  325. $item['service'] = DiagnosisService::where('id', 'in', $item['service_id'])->column('name');
  326. $item['receiving'] = DiagnosisApply::where('uid', $item['order_receiving'])->find();
  327. }
  328. }
  329. return app('json')->successful($list);
  330. }
  331. /**
  332. * 放弃订单
  333. * @param Request $request
  334. * @param $id
  335. * @return mixed
  336. * @throws \think\db\exception\DataNotFoundException
  337. * @throws \think\db\exception\DbException
  338. * @throws \think\db\exception\ModelNotFoundException、
  339. */
  340. public function discard_order(Request $request,$id)
  341. {
  342. if (!$id) return app('json')->fail('传入订单id!');
  343. $order = DiagnosisOrder::where('id', $id)->where('order_receiving', $request->uid())->lock(true)->find();
  344. if (!$order) return app('json')->fail('订单不存在!');
  345. $order['status'] = 0;
  346. $order['order_receiving'] = 0;
  347. $res = $order->save();
  348. if ($res) return app('json')->success('放弃订单成功');
  349. return app('json')->fail('放弃订单失败');
  350. }
  351. /**
  352. * 取消订单
  353. * @param Request $request
  354. * @param $id
  355. * @return mixed
  356. * @throws \think\db\exception\DataNotFoundException
  357. * @throws \think\db\exception\DbException
  358. * @throws \think\db\exception\ModelNotFoundException
  359. */
  360. public function cancel_order(Request $request,$id)
  361. {
  362. if (!$id) return app('json')->fail('传入订单id!');
  363. $order = DiagnosisOrder::where('id', $id)->where('uid', $request->uid())->find();
  364. if (!$order) return app('json')->fail('订单不存在!');
  365. if ($order['status'] > 2) return app('json')->fail('订单已完成无法取消!');
  366. $order['status'] = -1;
  367. $res = $order->save();
  368. if ($res) return app('json')->success('取消订单成功');
  369. return app('json')->fail('取消订单失败');
  370. }
  371. /**
  372. * 提交完成
  373. * @param Request $request
  374. * @param $id
  375. * @return void
  376. * @throws \think\db\exception\DataNotFoundException
  377. * @throws \think\db\exception\DbException
  378. * @throws \think\db\exception\ModelNotFoundException
  379. */
  380. public function submit(Request $request,$id)
  381. {
  382. $data = UtilService::postMore([
  383. ['images', '']
  384. ]);
  385. if (!$id) return app('json')->fail('传入订单id!');
  386. $order = DiagnosisOrder::where('id', $id)->where('order_receiving', $request->uid())->find();
  387. if (!$order) return app('json')->fail('订单不存在!');
  388. if ($order['status'] <> 1) return app('json')->fail('当前订单状态不对');
  389. $order['images'] = json_encode($data['images']);
  390. $order['status'] = 2;
  391. $order['up_time'] = time();
  392. $res = $order->save();
  393. if ($res) return app('json')->success('提交订单成功');
  394. return app('json')->fail('提交订单失败');
  395. }
  396. /**
  397. * 用户确认完成
  398. * @param Request $request
  399. * @param $id
  400. * @return mixed
  401. * @throws \think\db\exception\DataNotFoundException
  402. * @throws \think\db\exception\DbException
  403. * @throws \think\db\exception\ModelNotFoundException
  404. */
  405. public function confirmation(Request $request,$id)
  406. {
  407. if (!$id) return app('json')->fail('传入订单id!');
  408. BaseModel::beginTrans();
  409. $order = DiagnosisOrder::where('id', $id)->where('uid', $request->uid())->lock(true)->find();
  410. if (!$order) return app('json')->fail('订单不存在!');
  411. if ($order['status'] <> 2) return app('json')->fail('当前订单状态不对');
  412. $order['status'] = 3;
  413. if ($order['after_sales'] == 1){
  414. $order['after_sales'] = 0;
  415. }
  416. $res1 = $order->save();
  417. $user = User::where('uid', $order['order_receiving'])->find();
  418. $res2 = UserBill::income('完成订单佣金', $order['order_receiving'], 'now_money', 'brokerage', $order['commission'], '', $user['brokerage_price']+$order['commission'], '完成订单佣金');
  419. $res3 = User::where('uid', $order['order_receiving'])->inc('brokerage_price', $order['commission'])->update();
  420. if ($res1 && $res2 && $res3){
  421. BaseModel::commitTrans();
  422. return app('json')->success('成功');
  423. }
  424. BaseModel::rollbackTrans();
  425. return app('json')->fail('失败');
  426. }
  427. /**
  428. * 申请售后
  429. * @param Request $request
  430. * @param $id
  431. * @return mixed
  432. * @throws \think\db\exception\DataNotFoundException
  433. * @throws \think\db\exception\DbException
  434. * @throws \think\db\exception\ModelNotFoundException
  435. */
  436. public function after_sales(Request $request,$id)
  437. {
  438. $data = UtilService::postMore([
  439. ['after_remarks'],
  440. ['after_images']
  441. ]);
  442. if (!$id) return app('json')->fail('传入订单id!');
  443. $order = DiagnosisOrder::where('id', $id)->where('uid', $request->uid())->lock(true)->find();
  444. if (!$order) return app('json')->fail('订单不存在!');
  445. if (empty($data['after_images'])) return app('json')->fail('传入售后图片!');
  446. // if ($order['status'] <> 3) return app('json')->fail('订单未完成');
  447. $order['after_remarks'] = $data['after_remarks'];
  448. $order['after_images'] = json_encode($data['after_images']);
  449. $order['after_sales'] = 1;
  450. $res = $order->save();
  451. if ($res) return app('json')->success('提交成功');
  452. return app('json')->fail('提交失败');
  453. }
  454. }