DiagnosisOrderController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. $orderInfo = DiagnosisOrder::create_order($order, $data['pay_type'],$uid);
  132. if ($order['pay_price'] == 0){
  133. $res = DiagnosisOrder::yuPay($orderInfo['order_id']);
  134. if ($res){
  135. cache()->delete($data['cache']);
  136. return app('json')->status('success', '支付成功');
  137. }else{
  138. return app('json')->fail('支付失败');
  139. }
  140. }else{
  141. if ($orderInfo['pay_type'] == 1){
  142. $res = DiagnosisOrder::yuPay($orderInfo['order_id']);
  143. if ($res){
  144. cache()->delete($data['cache']);
  145. return app('json')->status('success', '余额支付成功');
  146. }else{
  147. return app('json')->fail('支付失败');
  148. }
  149. }elseif ($orderInfo['pay_type'] ==2){
  150. try {
  151. $jsConfig = OrderRepository::jsDiaPay($orderInfo['order_id']); //创建订单jspay
  152. } catch (\Exception $e) {
  153. return app('json')->status('pay_error', $e->getMessage());
  154. }
  155. $info['jsConfig'] = $jsConfig;
  156. cache()->delete($data['cache']);
  157. return app('json')->status('wechat_pay', '订单创建成功', $info);
  158. }elseif ($orderInfo['pay_type'] ==3){
  159. try {
  160. $jsConfig = OrderRepository::wxDiaPay($orderInfo['order_id']); //创建订单jspay
  161. } catch (\Exception $e) {
  162. return app('json')->status('pay_error', $e->getMessage());
  163. }
  164. $info['jsConfig'] = $jsConfig;
  165. cache()->delete($data['cache']);
  166. return app('json')->status('wechat_h5_pay', '订单创建成功', $info);
  167. }
  168. }
  169. return app('json')->fail('订单生成失败!');
  170. }
  171. /**
  172. * 用户订单
  173. * @param Request $request
  174. * @return mixed
  175. */
  176. public function order_list(Request $request)
  177. {
  178. $data = UtilService::getMore([
  179. ['status', ''],
  180. ['page', 1],
  181. ['limit', 10]
  182. ]);
  183. $list = DiagnosisOrder::alias('a')
  184. ->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')
  185. ->leftJoin('diagnosis_order_attr b', 'a.id = b.oid')
  186. ->order('a.id DESC')
  187. ->where('a.status', $data['status'])
  188. ->where('a.uid', $request->uid())
  189. ->page($data['page'], $data['limit'])->select();
  190. $list = count($list) > 0 ? $list->toArray() : [];
  191. if ($list){
  192. foreach ($list as &$item){
  193. $item['cate'] = DiagnosisCate::where('id', 'in', $item['cate_id'])->column('name');
  194. $item['service'] = DiagnosisService::where('id', 'in', $item['service_id'])->column('name');
  195. $item['receiving'] = DiagnosisApply::where('uid', $item['order_receiving'])->find();
  196. }
  197. }
  198. return app('json')->successful($list);
  199. }
  200. /**
  201. * 订单详情
  202. * @param Request $request
  203. * @return void
  204. */
  205. public function details(Request $request,$id)
  206. {
  207. if (!$id) return app('json')->fail('传入订单id!');
  208. $list = DiagnosisOrder::alias('a')
  209. ->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')
  210. ->leftJoin('diagnosis_order_attr b', 'a.id = b.oid')
  211. ->order('a.id DESC')
  212. ->where('a.id', $id)
  213. ->find()->toArray();
  214. $list['cate'] = DiagnosisCate::where('id', 'in', $list['cate_id'])->column('name');
  215. $list['service'] = DiagnosisService::where('id', 'in', $list['service_id'])->column('name');
  216. $list['receiving'] = DiagnosisApply::where('uid', $list['order_receiving'])->find();
  217. return app('json')->successful($list);
  218. }
  219. /**
  220. * 接单
  221. * @param Request $request
  222. * @param $id
  223. * @return mixed
  224. * @throws \think\db\exception\DataNotFoundException
  225. * @throws \think\db\exception\DbException
  226. * @throws \think\db\exception\ModelNotFoundException
  227. */
  228. public function order_receiving(Request $request,$id)
  229. {
  230. BaseModel::beginTrans();
  231. $user = User::where('uid', $request->uid())->find();
  232. if ($user['is_receiver'] != 1) return app('json')->fail('你不是接单员');
  233. if (!$id) return app('json')->fail('传入订单id!');
  234. $order = DiagnosisOrder::where('id', $id)->lock(true)->find();
  235. if ($order['status'] > 0) return app('json')->fail('该订单已被接');
  236. $order['order_receiving'] = $request->uid();
  237. $order['status'] = 1;
  238. $res = $order->save();
  239. if ($res){
  240. BaseModel::commitTrans();
  241. return app('json')->success('接单成功');
  242. }
  243. BaseModel::rollbackTrans();
  244. return app('json')->fail('接单失败');
  245. }
  246. /**
  247. * 接单员订单列表
  248. * @param Request $request
  249. * @return mixed
  250. * @throws \think\db\exception\DataNotFoundException
  251. * @throws \think\db\exception\DbException
  252. * @throws \think\db\exception\ModelNotFoundException
  253. */
  254. public function receiving_list(Request $request)
  255. {
  256. $data = UtilService::getMore([
  257. ['status', ''],
  258. ['page', 1],
  259. ['limit', 10]
  260. ]);
  261. $list = DiagnosisOrder::alias('a')
  262. ->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')
  263. ->leftJoin('diagnosis_order_attr b', 'a.id = b.oid')
  264. ->order('a.id DESC')
  265. ->where('a.status', $data['status'])
  266. ->where('a.order_receiving', $request->uid())
  267. ->page($data['page'], $data['limit'])->select();
  268. $list = count($list) > 0 ? $list->toArray() : [];
  269. if ($list){
  270. foreach ($list as &$item){
  271. $item['cate'] = DiagnosisCate::where('id', 'in', $item['cate_id'])->column('name');
  272. $item['service'] = DiagnosisService::where('id', 'in', $item['service_id'])->column('name');
  273. $item['receiving'] = DiagnosisApply::where('uid', $item['order_receiving'])->find();
  274. }
  275. }
  276. return app('json')->successful($list);
  277. }
  278. /**
  279. * 接单大厅
  280. * @param Request $request
  281. * @return mixed
  282. * @throws \think\db\exception\DataNotFoundException
  283. * @throws \think\db\exception\DbException
  284. * @throws \think\db\exception\ModelNotFoundException
  285. */
  286. public function order_receiving_hall(Request $request)
  287. {
  288. $data = UtilService::getMore([
  289. ['type'],
  290. ['page', 1],
  291. ['limit', 10],
  292. ]);
  293. if (!$data['type']) return app('json')->fail('传入类型');
  294. $list = DiagnosisOrder::alias('a')
  295. ->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')
  296. ->leftJoin('diagnosis_order_attr b', 'a.id = b.oid')
  297. ->order('a.id DESC')
  298. ->where('a.type', $data['type'])
  299. ->where('a.status', 0)
  300. ->page($data['page'], $data['limit'])->select();
  301. $list = count($list) > 0 ? $list->toArray() : [];
  302. return app('json')->successful($list);
  303. }
  304. /**
  305. * 放弃订单
  306. * @param Request $request
  307. * @param $id
  308. * @return mixed
  309. * @throws \think\db\exception\DataNotFoundException
  310. * @throws \think\db\exception\DbException
  311. * @throws \think\db\exception\ModelNotFoundException、
  312. */
  313. public function discard_order(Request $request,$id)
  314. {
  315. if (!$id) return app('json')->fail('传入订单id!');
  316. $order = DiagnosisOrder::where('id', $id)->where('order_receiving', $request->uid())->lock(true)->find();
  317. if (!$order) return app('json')->fail('订单不存在!');
  318. $order['status'] = 0;
  319. $order['order_receiving'] = 0;
  320. $res = $order->save();
  321. if ($res) return app('json')->success('放弃订单成功');
  322. return app('json')->fail('放弃订单失败');
  323. }
  324. /**
  325. * 取消订单
  326. * @param Request $request
  327. * @param $id
  328. * @return mixed
  329. * @throws \think\db\exception\DataNotFoundException
  330. * @throws \think\db\exception\DbException
  331. * @throws \think\db\exception\ModelNotFoundException
  332. */
  333. public function cancel_order(Request $request,$id)
  334. {
  335. if (!$id) return app('json')->fail('传入订单id!');
  336. $order = DiagnosisOrder::where('id', $id)->where('uid', $request->uid())->find();
  337. if (!$order) return app('json')->fail('订单不存在!');
  338. if ($order['status'] > 2) return app('json')->fail('订单已完成无法取消!');
  339. $order['status'] = -1;
  340. $res = $order->save();
  341. if ($res) return app('json')->success('取消订单成功');
  342. return app('json')->fail('取消订单失败');
  343. }
  344. /**
  345. * 提交完成
  346. * @param Request $request
  347. * @param $id
  348. * @return void
  349. * @throws \think\db\exception\DataNotFoundException
  350. * @throws \think\db\exception\DbException
  351. * @throws \think\db\exception\ModelNotFoundException
  352. */
  353. public function submit(Request $request,$id)
  354. {
  355. $data = UtilService::postMore([
  356. ['images', '']
  357. ]);
  358. if (!$id) return app('json')->fail('传入订单id!');
  359. $order = DiagnosisOrder::where('id', $id)->where('order_receiving', $request->uid())->find();
  360. if (!$order) return app('json')->fail('订单不存在!');
  361. if ($order['status'] <> 1) return app('json')->fail('当前订单状态不对');
  362. $order['images'] = json_encode($data['images']);
  363. $order['status'] = 2;
  364. $res = $order->save();
  365. if ($res) return app('json')->success('提交订单成功');
  366. return app('json')->fail('提交订单失败');
  367. }
  368. /**
  369. * 用户确认完成
  370. * @param Request $request
  371. * @param $id
  372. * @return mixed
  373. * @throws \think\db\exception\DataNotFoundException
  374. * @throws \think\db\exception\DbException
  375. * @throws \think\db\exception\ModelNotFoundException
  376. */
  377. public function confirmation(Request $request,$id)
  378. {
  379. if (!$id) return app('json')->fail('传入订单id!');
  380. BaseModel::beginTrans();
  381. $order = DiagnosisOrder::where('id', $id)->where('uid', $request->uid())->lock(true)->find();
  382. if (!$order) return app('json')->fail('订单不存在!');
  383. if ($order['status'] <> 2) return app('json')->fail('当前订单状态不对');
  384. $order['status'] = 3;
  385. $res1 = $order->save();
  386. $user = User::where('uid', $order['order_receiving'])->find();
  387. $res2 = UserBill::income('完成订单佣金', $order['order_receiving'], 'now_money', 'brokerage', $order['commission'], '', $user['brokerage_price']+$order['commission'], '完成订单佣金');
  388. $res3 = User::where('uid', $order['order_receiving'])->inc('brokerage_price', $order['commission'])->update();
  389. if ($res1 && $res2 && $res3){
  390. BaseModel::commitTrans();
  391. return app('json')->success('成功');
  392. }
  393. BaseModel::rollbackTrans();
  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 after_sales(Request $request,$id)
  406. {
  407. $data = UtilService::postMore([
  408. ['after_remarks'],
  409. ['after_images']
  410. ]);
  411. if (!$id) return app('json')->fail('传入订单id!');
  412. $order = DiagnosisOrder::where('id', $id)->where('uid', $request->uid())->lock(true)->find();
  413. if (!$order) return app('json')->fail('订单不存在!');
  414. if ($order['status'] <> 3) return app('json')->fail('订单未完成');
  415. $order['after_remarks'] = $data['after_remarks'];
  416. $order['after_images'] = json_encode($data['after_images']);
  417. $order['after_sales'] = 1;
  418. $res = $order->save();
  419. if ($res) return app('json')->success('提交成功');
  420. return app('json')->fail('提交失败');
  421. }
  422. }