DiagnosisOrderController.php 16 KB

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