DiagnosisOrderController.php 16 KB

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