DiagnosisOrderController.php 16 KB

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