DiagnosisOrderController.php 16 KB

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