StoreOrderController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <?php
  2. namespace app\api\controller\admin;
  3. use app\Request;
  4. use app\models\user\{
  5. User, UserBill
  6. };
  7. use crmeb\repositories\OrderRepository;
  8. use crmeb\repositories\ShortLetterRepositories;
  9. use crmeb\services\{
  10. MiniProgramService, UtilService, WechatService
  11. };
  12. use app\models\store\{StoreCart, StoreCoupon, StoreCouponUser, StoreOrder, StoreOrderStatus, StorePink, StoreService};
  13. use app\models\system\SystemStoreStaff;
  14. /**
  15. * 订单类
  16. * Class StoreOrderController
  17. * @package app\api\controller\admin\order
  18. */
  19. class StoreOrderController
  20. {
  21. /**
  22. * 订单数据统计
  23. * @param Request $request
  24. * @return mixed
  25. */
  26. public function statistics(Request $request)
  27. {
  28. $uid = $request->uid();
  29. if (!StoreService::orderServiceStatus($uid))
  30. return app('json')->fail('权限不足');
  31. $dataCount = StoreOrder::getOrderDataAdmin();
  32. $dataPrice = StoreOrder::getOrderTimeData();
  33. $data = array_merge($dataCount, $dataPrice);
  34. return app('json')->successful($data);
  35. }
  36. /**
  37. * 订单每月统计数据
  38. * @param Request $request
  39. * @return mixed
  40. */
  41. public function data(Request $request)
  42. {
  43. $uid = $request->uid();
  44. if (!StoreService::orderServiceStatus($uid))
  45. return app('json')->fail('权限不足');
  46. list($page, $limit, $start, $stop) = UtilService::getMore([
  47. ['page', 1],
  48. ['limit', 7],
  49. ['start', ''],
  50. ['stop', '']
  51. ], $request, true);
  52. if (!$limit) return app('json')->successful([]);
  53. $data = StoreOrder::getOrderDataPriceCount($page, $limit, $start, $stop);
  54. if ($data) return app('json')->successful($data->toArray());
  55. return app('json')->successful([]);
  56. }
  57. /**
  58. * 订单列表
  59. * @param Request $request
  60. * @return mixed
  61. */
  62. public function lst(Request $request)
  63. {
  64. $uid = $request->uid();
  65. if (!StoreService::orderServiceStatus($uid))
  66. return app('json')->fail('权限不足');
  67. $where = UtilService::getMore([
  68. ['status', ''],
  69. ['is_del', 0],
  70. ['data', ''],
  71. ['type', ''],
  72. ['order', ''],
  73. ['page', 0],
  74. ['limit', 0]
  75. ], $request);
  76. if (!$where['limit']) return app('json')->successful([]);
  77. return app('json')->successful(StoreOrder::orderList($where));
  78. }
  79. /**
  80. * 订单详情
  81. * @param Request $request
  82. * @param $orderId
  83. * @return mixed
  84. * @throws \think\Exception
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. * @throws \think\exception\DbException
  88. */
  89. public function detail(Request $request, $orderId)
  90. {
  91. $uid = $request->uid();
  92. if (!StoreService::orderServiceStatus($uid))
  93. return app('json')->fail('权限不足');
  94. $order = StoreOrder::getAdminOrderDetail($orderId, 'id,uid,order_id,add_time,status,total_num,total_price,total_postage,pay_price,pay_postage,paid,refund_status,remark,pink_id,combination_id,mark,seckill_id,bargain_id,delivery_type,pay_type,real_name,user_phone,user_address,coupon_price,freight_price,delivery_name,delivery_type,delivery_id');
  95. if (!$order) return app('json')->fail('订单不存在');
  96. $order = $order->toArray();
  97. $nickname = User::getUserInfo($order['uid'], 'nickname')['nickname'];
  98. $orderInfo = StoreOrder::tidyAdminOrder([$order], true)[0];
  99. unset($orderInfo['uid'], $orderInfo['seckill_id'], $orderInfo['pink_id'], $orderInfo['combination_id'], $orderInfo['bargain_id'], $orderInfo['status'], $orderInfo['total_postage']);
  100. $orderInfo['nickname'] = $nickname;
  101. return app('json')->successful('ok', $orderInfo);
  102. }
  103. /**
  104. * 订单发货获取订单信息
  105. * @param Request $request
  106. * @param $orderId
  107. * @return mixed
  108. * @throws \think\Exception
  109. * @throws \think\db\exception\DataNotFoundException
  110. * @throws \think\db\exception\ModelNotFoundException
  111. * @throws \think\exception\DbException
  112. */
  113. public function delivery_gain(Request $request, $orderId)
  114. {
  115. $uid = $request->uid();
  116. if (!StoreService::orderServiceStatus($uid))
  117. return app('json')->fail('权限不足');
  118. $order = StoreOrder::getAdminOrderDetail($orderId, 'real_name,user_phone,user_address,order_id,uid,status,paid');
  119. if (!$order) return app('json')->fail('订单不存在');
  120. if ($order['paid']) {
  121. $order['nickname'] = User::getUserInfo($order['uid'], 'nickname')['nickname'];
  122. $order = $order->hidden(['uid', 'status', 'paid'])->toArray();
  123. return app('json')->successful('ok', $order);
  124. }
  125. return app('json')->fail('状态错误');
  126. }
  127. /**
  128. * 订单发货
  129. * @param Request $request
  130. * @return mixed
  131. * @throws \think\db\exception\DataNotFoundException
  132. * @throws \think\db\exception\ModelNotFoundException
  133. * @throws \think\exception\DbException
  134. */
  135. public function delivery_keep(Request $request)
  136. {
  137. $uid = $request->uid();
  138. if (!StoreService::orderServiceStatus($uid))
  139. return app('json')->fail('权限不足');
  140. list($order_id, $delivery_type, $delivery_name, $delivery_id) = UtilService::postMore([
  141. ['order_id', ''],
  142. ['delivery_type', 0],
  143. ['delivery_name', ''],
  144. ['delivery_id', ''],
  145. ], $request, true);
  146. $order = StoreOrder::getAdminOrderDetail($order_id, 'id,status,paid');
  147. if (!$order) return app('json')->fail('订单不存在');
  148. if (!$order['status'] && $order['paid']) {
  149. $deliveryTypeArr = ['send', 'express', 'fictitious'];
  150. if (!strlen(trim($delivery_type))) return app('json')->fail('请填写发货方式');
  151. if (!in_array($delivery_type, $deliveryTypeArr)) return app('json')->fail('发货方式错误');
  152. if ($delivery_type == 'express') {
  153. if (!strlen(trim($delivery_name))) return app('json')->fail('请选择快递公司');
  154. if (!strlen(trim($delivery_id))) return app('json')->fail('请填写快递单号');
  155. }
  156. if ($delivery_type == 'send') {
  157. if (!strlen(trim($delivery_name))) return app('json')->fail('请填写发货人');
  158. if (!strlen(trim($delivery_id))) return app('json')->fail('请填写发货手机号');
  159. }
  160. $data['status'] = 1;
  161. $data['delivery_type'] = $delivery_type;
  162. $data['delivery_name'] = $delivery_name;
  163. $data['delivery_id'] = $delivery_id;
  164. $res = StoreOrder::edit($data, $order['id'], 'id');
  165. if ($res) {
  166. if ($delivery_type == 'express') {
  167. StoreOrderStatus::status($order['id'], 'delivery_goods', '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']);
  168. } else if ($delivery_type == 'send') {
  169. StoreOrderStatus::status($order['id'], 'delivery', '已配送 发货人:' . $delivery_name . ' 发货人电话:' . $delivery_id);
  170. } else if ($delivery_type == 'fictitious') {
  171. StoreOrderStatus::status($order['id'], 'delivery_fictitious', '虚拟产品已发货');
  172. }
  173. }
  174. event('StoreProductOrderDeliveryGoodsAfter', [$data, $order['id']]);
  175. return app('json')->successful('发货成功!');
  176. }
  177. return app('json')->fail('状态错误');
  178. }
  179. /**
  180. * 订单改价
  181. * @param Request $request
  182. * @return mixed
  183. * @throws \think\db\exception\DataNotFoundException
  184. * @throws \think\db\exception\ModelNotFoundException
  185. * @throws \think\exception\DbException
  186. */
  187. public function price(Request $request)
  188. {
  189. $uid = $request->uid();
  190. if (!StoreService::orderServiceStatus($uid))
  191. return app('json')->fail('权限不足');
  192. list($order_id, $price) = UtilService::postMore([
  193. ['order_id', ''],
  194. ['price', '']
  195. ], $request, true);
  196. $order = StoreOrder::getAdminOrderDetail($order_id, 'id,paid,user_phone,pay_price,order_id,total_price,total_postage,pay_postage,gain_integral');
  197. if (!$order) return app('json')->fail('订单不存在');
  198. $order = $order->toArray();
  199. if (!$order['paid']) {
  200. if ($price === '') return app('json')->fail('请填写实际支付金额');
  201. if ($price < 0) return app('json')->fail('实际支付金额不能小于0元');
  202. if ($order['pay_price'] == $price) return app('json')->successful('修改成功');
  203. $order['order_id'] = StoreOrder::changeOrderId($order['order_id']);
  204. if (!StoreOrder::edit(['pay_price' => $price, 'order_id' => $order['order_id']], $order['id'], 'id'))
  205. return app('json')->fail('状态错误');
  206. $order['pay_price'] = $price;
  207. event('StoreProductOrderEditAfter', [$order, $order['id']]);
  208. StoreOrderStatus::status($order['id'], 'order_edit', '修改实际支付金额' . $price);
  209. //改价短信提醒
  210. if ($price != $order['pay_price']) {
  211. $switch = sys_config('price_revision_switch') ? true : false;
  212. ShortLetterRepositories::send($switch, $order['user_phone'], ['order_id' => $order_id, 'pay_price' => $order['pay_price']], 'PRICE_REVISION_CODE');
  213. }
  214. return app('json')->successful('修改成功');
  215. }
  216. return app('json')->fail('状态错误');
  217. }
  218. /**
  219. * 订单备注
  220. * @param Request $request
  221. * @return mixed
  222. * @throws \think\db\exception\DataNotFoundException
  223. * @throws \think\db\exception\ModelNotFoundException
  224. * @throws \think\exception\DbException
  225. */
  226. public function remark(Request $request)
  227. {
  228. $uid = $request->uid();
  229. if (!StoreService::orderServiceStatus($uid))
  230. return app('json')->fail('权限不足');
  231. list($order_id, $remark) = UtilService::postMore([
  232. ['order_id', ''],
  233. ['remark', '']
  234. ], $request, true);
  235. $order = StoreOrder::getAdminOrderDetail($order_id, 'id');
  236. if (!$order) return app('json')->fail('订单不存在');
  237. $order = $order->toArray();
  238. if (!strlen(trim($remark))) return app('json')->fail('请填写备注内容');
  239. if (!StoreOrder::edit(['remark' => $remark], $order['id']))
  240. return app('json')->fail('备注失败');
  241. return app('json')->successful('备注成功');
  242. }
  243. /**
  244. * 订单交易额/订单数量时间统计
  245. * @param Request $request
  246. * @return bool
  247. */
  248. public function time(Request $request)
  249. {
  250. $uid = $request->uid();
  251. if (!StoreService::orderServiceStatus($uid))
  252. return app('json')->fail('权限不足');
  253. list($start, $stop, $type) = UtilService::getMore([
  254. ['start', strtotime(date('Y-m'))],
  255. ['stop', time()],
  256. ['type', 1]
  257. ], $request, true);
  258. if ($start == $stop) {
  259. return app('json')->fail('开始时间不能等于结束时间');
  260. }
  261. if ($start > $stop) {
  262. $middle = $stop;
  263. $stop = $start;
  264. $start = $middle;
  265. }
  266. $space = bcsub($stop, $start, 0);//间隔时间段
  267. $front = bcsub($start, $space, 0);//第一个时间段
  268. if ($type == 1) {//销售额
  269. $frontPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($front, $start);
  270. $afterPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($start, $stop);
  271. $chartInfo = StoreOrder::chartTimePrice($start, $stop);
  272. $data['chart'] = $chartInfo;//营业额图表数据
  273. $data['time'] = $afterPrice;//时间区间营业额
  274. $increase = (float)bcsub($afterPrice, $frontPrice, 2); //同比上个时间区间增长营业额
  275. $growthRate = abs($increase);
  276. if ($growthRate == 0) $data['growth_rate'] = 0;
  277. else if ($frontPrice == 0) $data['growth_rate'] = $growthRate;
  278. else $data['growth_rate'] = (int)bcmul(bcdiv($growthRate, $frontPrice, 2), 100, 0);//时间区间增长率
  279. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  280. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  281. } else {//订单数
  282. $frontNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($front, $start);
  283. $afterNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($start, $stop);
  284. $chartInfo = StoreOrder::chartTimeNumber($start, $stop);
  285. $data['chart'] = $chartInfo;//订单数图表数据
  286. $data['time'] = $afterNumber;//时间区间订单数
  287. $increase = (int)bcsub($afterNumber, $frontNumber, 0); //同比上个时间区间增长订单数
  288. $growthRate = abs($increase);
  289. if ($growthRate == 0) $data['growth_rate'] = 0;
  290. else if ($frontNumber == 0) $data['growth_rate'] = $growthRate;
  291. else $data['growth_rate'] = (int)bcmul(bcdiv($growthRate, $frontNumber, 2), 100, 0);//时间区间增长率
  292. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  293. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  294. }
  295. return app('json')->successful($data);
  296. }
  297. /**
  298. * 订单支付
  299. * @param Request $request
  300. * @return mixed
  301. * @throws \think\db\exception\DataNotFoundException
  302. * @throws \think\db\exception\ModelNotFoundException
  303. * @throws \think\exception\DbException
  304. */
  305. public function offline(Request $request)
  306. {
  307. list($orderId) = UtilService::postMore([['order_id', '']], $request, true);
  308. $orderInfo = StoreOrder::getAdminOrderDetail($orderId, 'id');
  309. if (!$orderInfo) return app('json')->fail('参数错误');
  310. $id = $orderInfo->id;
  311. $res = StoreOrder::updateOffline($id);
  312. if ($res) {
  313. event('StoreProductOrderOffline', [$id]);
  314. StoreOrderStatus::status($id, 'offline', '线下付款');
  315. return app('json')->successful('修改成功!');
  316. }
  317. return app('json')->fail(StoreOrder::getErrorInfo('修改失败!'));
  318. }
  319. /**
  320. * 订单退款
  321. * @param Request $request
  322. * @return mixed
  323. * @throws \think\Exception
  324. * @throws \think\db\exception\DataNotFoundException
  325. * @throws \think\db\exception\ModelNotFoundException
  326. * @throws \think\exception\DbException
  327. */
  328. public function refund(Request $request)
  329. {
  330. list($orderId, $price, $type) = UtilService::postMore([
  331. ['order_id', ''],
  332. ['price', 0],
  333. ['type', 1],
  334. ], $request, true);
  335. if (!strlen(trim($orderId))) return app('json')->fail('参数错误');
  336. $orderInfo = StoreOrder::getAdminOrderDetail($orderId);
  337. if (!$orderInfo) return app('json')->fail('数据不存在!');
  338. $orderInfo = $orderInfo->toArray();
  339. if ($type == 1)
  340. $data['refund_status'] = 2;
  341. else if ($type == 2)
  342. $data['refund_status'] = 0;
  343. else
  344. return app('json')->fail('退款修改状态错误');
  345. if ($orderInfo['pay_price'] == 0 || $type == 2) {
  346. StoreOrder::update($data, ['order_id' => $orderId]);
  347. return app('json')->successful('修改退款状态成功!');
  348. }
  349. if ($orderInfo['pay_price'] == $orderInfo['refund_price']) return app('json')->fail('已退完支付金额!不能再退款了');
  350. if (!$price) return app('json')->fail('请输入退款金额');
  351. $data['refund_price'] = bcadd($price, $orderInfo['refund_price'], 2);
  352. $bj = bccomp((float)$orderInfo['pay_price'], (float)$data['refund_price'], 2);
  353. if ($bj < 0) return app('json')->fail('退款金额大于支付金额,请修改退款金额');
  354. $refundData['pay_price'] = $orderInfo['pay_price'];
  355. $refundData['refund_price'] = $price;
  356. if ($orderInfo['pay_type'] == 'weixin') {
  357. if ($orderInfo['is_channel'] == 1) {// 小程序
  358. try {
  359. MiniProgramService::payOrderRefund($orderInfo['order_id'], $refundData);
  360. } catch (\Exception $e) {
  361. return app('json')->fail($e->getMessage());
  362. }
  363. } else {// 公众号
  364. try {
  365. WechatService::payOrderRefund($orderInfo['order_id'], $refundData);
  366. } catch (\Exception $e) {
  367. return app('json')->fail($e->getMessage());
  368. }
  369. }
  370. } else if ($orderInfo['pay_type'] == 'yue') {//余额
  371. StoreOrder::beginTrans();
  372. $userInfo = User::getUserInfo($orderInfo['uid'], 'now_money');
  373. if (!$userInfo) {
  374. StoreOrder::rollbackTrans();
  375. return app('json')->fail('订单用户不存在');
  376. }
  377. $res1 = User::bcInc($orderInfo['uid'], 'now_money', $price, 'uid');
  378. $res2 = $res2 = UserBill::income('商品退款', $orderInfo['uid'], 'now_money', 'pay_product_refund', $price, $orderInfo['id'], bcadd($userInfo['now_money'], $price, 2), '订单退款到余额' . floatval($price) . '元');
  379. try {
  380. OrderRepository::storeOrderYueRefund($orderInfo, $refundData);
  381. } catch (\Exception $e) {
  382. StoreOrder::rollbackTrans();
  383. return app('json')->fail($e->getMessage());
  384. }
  385. $res = $res1 && $res2;
  386. StoreOrder::checkTrans($res);
  387. if (!$res) return app('json')->fail('余额退款失败!');
  388. }
  389. $resEdit = StoreOrder::edit($data, $orderInfo['id'], 'id');
  390. if ($resEdit) {
  391. $data['type'] = $type;
  392. if ($data['type'] == 1) StorePink::setRefundPink($orderInfo['id']);
  393. try {
  394. OrderRepository::storeProductOrderRefundY($data, $orderInfo['id']);
  395. } catch (\Exception $e) {
  396. return app('json')->fail($e->getMessage());
  397. }
  398. StoreOrderStatus::status($orderInfo['id'], 'refund_price', '退款给用户' . $price . '元');
  399. //退佣金
  400. $brokerage_list = UserBill::where('category', 'now_money')
  401. ->where('type', 'brokerage')
  402. ->where('link_id', $orderId)
  403. ->where('pm', 1)
  404. ->select()->toArray();
  405. if ($brokerage_list)
  406. foreach ($brokerage_list as $item) {
  407. $usermoney = User::where('uid', $item['uid'])->value('brokerage_price');
  408. if ($item['number'] > $usermoney)
  409. $item['number'] = $usermoney;
  410. User::bcDec($item['uid'], 'brokerage_price', $item['number'], 'uid');
  411. UserBill::expend('退款退佣金', $item['uid'], 'now_money', 'brokerage', $item['number'], $orderId, bcsub($usermoney, $item['number'], 2), '订单退款扣除佣金' . floatval($item['number']) . '元');
  412. }
  413. return app('json')->successful('修改成功!');
  414. } else {
  415. StoreOrderStatus::status($orderInfo['id'], 'refund_price', '退款给用户' . $price . '元失败');
  416. return app('json')->successful('修改失败!');
  417. }
  418. }
  419. /**
  420. * 门店核销
  421. * @param Request $request
  422. */
  423. public function order_verific(Request $request)
  424. {
  425. list($verify_code, $is_confirm) = UtilService::postMore([
  426. ['verify_code', ''],
  427. ['is_confirm', 0]
  428. ], $request, true);
  429. if (!$verify_code) return app('json')->fail('缺少核销码');
  430. $orderInfo = StoreOrder::where('verify_code', $verify_code)->where('paid', 1)->where('refund_status', 0)->find();
  431. if (!$orderInfo) return app('json')->fail('核销的订单不存在或未支付或已退款');
  432. if ($orderInfo->status > 0) return app('json')->fail('订单已经核销');
  433. if ($orderInfo->combination_id && $orderInfo->pink_id) {
  434. $res = StorePink::where('id', $orderInfo->pink_id)->where('status', '<>', 2)->count();
  435. if ($res) return app('json')->fail('拼团订单暂未成功无法核销!');
  436. }
  437. if (!$is_confirm) {
  438. $orderInfo['image'] = StoreCart::getProductImage($orderInfo->cart_id);
  439. return app('json')->success($orderInfo->toArray());
  440. }
  441. StoreOrder::beginTrans();
  442. try {
  443. $uid = $request->uid();
  444. if (SystemStoreStaff::verifyStatus($uid) && ($storeStaff = SystemStoreStaff::where('uid', $uid)->field(['store_id', 'id'])->find())) {
  445. $orderInfo->store_id = $storeStaff['store_id'];
  446. $orderInfo->clerk_id = $storeStaff['id'];
  447. }
  448. $orderInfo->status = 2;
  449. if ($orderInfo->save()) {
  450. OrderRepository::storeProductOrderTakeDeliveryAdmin($orderInfo);
  451. StoreOrderStatus::status($orderInfo->id, 'take_delivery', '已核销');
  452. event('ShortMssageSend', [$orderInfo['order_id'], 'Receiving']);
  453. StoreOrder::commitTrans();
  454. return app('json')->success('核销成功');
  455. } else {
  456. StoreOrder::rollbackTrans();
  457. return app('json')->fail('核销失败');
  458. }
  459. } catch (\PDOException $e) {
  460. StoreOrder::rollbackTrans();
  461. return app('json')->fail($e->getMessage());
  462. }
  463. }
  464. /**
  465. * 优惠券核销
  466. * @param Request $request
  467. * @return mixed
  468. * @throws \think\db\exception\DataNotFoundException
  469. * @throws \think\db\exception\DbException
  470. * @throws \think\db\exception\ModelNotFoundException
  471. */
  472. public function coupon_verific(Request $request)
  473. {
  474. list($verify_code,) = UtilService::postMore([
  475. ['verify_code', ''],
  476. ], $request, true);
  477. if (!$verify_code) return app('json')->fail('缺少核销码');
  478. $coupon = StoreCouponUser::where('code', $verify_code)->where('status', 0)->find();
  479. $coupons = StoreCoupon::where('id', $coupon['cid'])->find();
  480. if (!$coupon) return app('json')->fail('核销的优惠券不存在或已使用');
  481. $user = SystemStoreStaff::where('uid', $request->uid())->find();
  482. if (!$user) return app('json')->fail('你不是店员');
  483. if ($coupons['store_id'] != $user['store_id']) return app('json')->fail('你不是门店优惠券指定店员,没有权限核销');
  484. StoreCouponUser::beginTrans();
  485. try {
  486. $coupon['status'] = 1;
  487. $coupon['use_time'] = time();
  488. $res = $coupon->save();
  489. $pon = StoreCouponUser::where('order_id', $coupon['order_id'])->where('code', '<>', $verify_code)->where('status', 0)->select();
  490. if (count($pon) == 0){
  491. StoreOrder::where('order_id', $coupon['order_id'])->update(['use' => 1]);
  492. }
  493. StoreCouponUser::commitTrans();
  494. if ($res) return app('json')->success('核销成功');
  495. } catch (\PDOException $e) {
  496. StoreCouponUser::rollbackTrans();
  497. return app('json')->fail($e->getMessage());
  498. }
  499. }
  500. }