StoreOrderDeliveryServices.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\order;
  12. use app\services\activity\coupon\StoreCouponIssueServices;
  13. use app\services\BaseServices;
  14. use app\dao\order\StoreOrderDao;
  15. use app\services\message\MessageSystemServices;
  16. use app\services\product\sku\StoreProductAttrValueServices;
  17. use app\services\product\sku\StoreProductVirtualServices;
  18. use app\services\serve\ServeServices;
  19. use crmeb\exceptions\AdminException;
  20. use crmeb\exceptions\ApiException;
  21. use crmeb\services\FormBuilder as Form;
  22. use app\services\shipping\ExpressServices;
  23. /**
  24. * 订单发货
  25. * Class StoreOrderDeliveryServices
  26. * @package app\services\order
  27. */
  28. class StoreOrderDeliveryServices extends BaseServices
  29. {
  30. /**
  31. * 构造方法
  32. * StoreOrderDeliveryServices constructor.
  33. * @param StoreOrderDao $dao
  34. */
  35. public function __construct(StoreOrderDao $dao)
  36. {
  37. $this->dao = $dao;
  38. }
  39. /**
  40. * 订单发货
  41. * @param int $id
  42. * @param array $data
  43. * @return bool
  44. */
  45. public function delivery(int $id, array $data)
  46. {
  47. $orderInfo = $this->dao->get($id, ['*'], ['pink']);
  48. if (!$orderInfo) {
  49. throw new AdminException(400470);
  50. }
  51. if ($orderInfo->is_del) {
  52. throw new AdminException(400471);
  53. }
  54. if ($orderInfo->status) {
  55. throw new AdminException(400472);
  56. }
  57. if ($orderInfo->shipping_type == 2) {
  58. throw new AdminException(400473);
  59. }
  60. if (isset($orderInfo['pinkStatus']) && $orderInfo['pinkStatus'] != 2) {
  61. throw new AdminException(400474);
  62. }
  63. if ($data['type'] == 1) {
  64. // 检测快递公司编码
  65. /** @var ExpressServices $expressServices */
  66. $expressServices = app()->make(ExpressServices::class);
  67. if (!$expressServices->be(['code' => $data['delivery_code']])) {
  68. throw new AdminException(410324);
  69. }
  70. }
  71. /** @var StoreOrderRefundServices $storeOrderRefundServices */
  72. $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
  73. if ($storeOrderRefundServices->count(['store_order_id' => $id, 'refund_type' => [1, 2, 4, 5], 'is_cancel' => 0, 'is_del' => 0])) {
  74. throw new AdminException(400475);
  75. }
  76. $this->doDelivery($id, $orderInfo, $data);
  77. return true;
  78. }
  79. /**
  80. * 订单快递发货
  81. * @param int $id
  82. * @param array $data
  83. */
  84. public function orderDeliveryGoods(int $id, array $data, $orderInfo, $storeTitle)
  85. {
  86. /** @var StoreOrderCartInfoServices $orderInfoServices */
  87. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  88. if (!$data['delivery_name']) {
  89. throw new AdminException(400007);
  90. }
  91. $data['delivery_type'] = 'express';
  92. if ($data['express_record_type'] == 2) {//电子面单
  93. if (!$data['delivery_code']) {
  94. throw new AdminException(400476);
  95. }
  96. if (!$data['express_temp_id']) {
  97. throw new AdminException(400527);
  98. }
  99. if (!$data['to_name']) {
  100. throw new AdminException(400008);
  101. }
  102. if (!$data['to_tel']) {
  103. throw new AdminException(400477);
  104. }
  105. if (!$data['to_addr']) {
  106. throw new AdminException(400478);
  107. }
  108. /** @var ServeServices $expressService */
  109. $expressService = app()->make(ServeServices::class);
  110. $expData['com'] = $data['delivery_code'];
  111. $expData['to_name'] = $orderInfo->real_name;
  112. $expData['to_tel'] = $orderInfo->user_phone;
  113. $expData['to_addr'] = $orderInfo->user_address;
  114. $expData['from_name'] = $data['to_name'];
  115. $expData['from_tel'] = $data['to_tel'];
  116. $expData['from_addr'] = $data['to_addr'];
  117. $expData['siid'] = sys_config('config_export_siid');
  118. $expData['temp_id'] = $data['express_temp_id'];
  119. $expData['count'] = $orderInfo->total_num;
  120. $expData['cargo'] = $orderInfoServices->getCarIdByProductTitle($orderInfo->id, $orderInfo->cart_id, true);
  121. $expData['order_id'] = $orderInfo->order_id;
  122. if (!sys_config('config_export_open', 0)) {
  123. throw new AdminException(400528);
  124. }
  125. $dump = $expressService->express()->dump($expData);
  126. $orderInfo->delivery_id = $dump['kuaidinum'];
  127. $data['express_dump'] = json_encode([
  128. 'com' => $expData['com'],
  129. 'from_name' => $expData['from_name'],
  130. 'from_tel' => $expData['from_tel'],
  131. 'from_addr' => $expData['from_addr'],
  132. 'temp_id' => $expData['temp_id'],
  133. 'cargo' => $expData['cargo'],
  134. ]);
  135. $data['delivery_id'] = $dump['kuaidinum'];
  136. } else {
  137. if (!$data['delivery_id']) {
  138. throw new AdminException(400531);
  139. }
  140. $orderInfo->delivery_id = $data['delivery_id'];
  141. }
  142. $data['status'] = 1;
  143. $orderInfo->delivery_type = $data['delivery_type'];
  144. $orderInfo->delivery_name = $data['delivery_name'];
  145. $orderInfo->status = $data['status'];
  146. /** @var StoreOrderStatusServices $services */
  147. $services = app()->make(StoreOrderStatusServices::class);
  148. $this->transaction(function () use ($id, $data, $services) {
  149. $res = $this->dao->update($id, $data);
  150. $res = $res && $services->save([
  151. 'oid' => $id,
  152. 'change_time' => time(),
  153. 'change_type' => 'delivery_goods',
  154. 'change_message' => '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']
  155. ]);
  156. if (!$res) {
  157. throw new AdminException(400529);
  158. }
  159. });
  160. return true;
  161. }
  162. /**
  163. * 订单配送
  164. * @param int $id
  165. * @param array $data
  166. */
  167. public function orderDelivery(int $id, array $data, $orderInfo, string $storeTitle)
  168. {
  169. $data['delivery_type'] = 'send';
  170. $data['delivery_name'] = $data['sh_delivery_name'];
  171. $data['delivery_id'] = $data['sh_delivery_id'];
  172. $data['delivery_uid'] = $data['sh_delivery_uid'];
  173. $data['shipping_type'] = 1;
  174. //获取核销码
  175. /** @var StoreOrderCreateServices $storeOrderCreateService */
  176. $storeOrderCreateService = app()->make(StoreOrderCreateServices::class);
  177. $data['verify_code'] = $storeOrderCreateService->getStoreCode();
  178. unset($data['sh_delivery_name'], $data['sh_delivery_id'], $data['sh_delivery_uid']);
  179. if (!$data['delivery_name']) {
  180. throw new AdminException(400523);
  181. }
  182. if (!$data['delivery_id']) {
  183. throw new AdminException(400524);
  184. }
  185. if (!$data['delivery_uid']) {
  186. throw new AdminException(400525);
  187. }
  188. if (!preg_match("/^1[3456789]{1}\d{9}$/", $data['delivery_id'])) {
  189. throw new AdminException(400526);
  190. }
  191. $data['status'] = 1;
  192. $orderInfo->delivery_type = $data['delivery_type'];
  193. $orderInfo->delivery_name = $data['delivery_name'];
  194. $orderInfo->delivery_id = $data['delivery_id'];
  195. $orderInfo->status = $data['status'];
  196. /** @var StoreOrderStatusServices $services */
  197. $services = app()->make(StoreOrderStatusServices::class);
  198. $this->transaction(function () use ($id, $data, $services) {
  199. $this->dao->update($id, $data);
  200. //记录订单状态
  201. $services->save([
  202. 'oid' => $id,
  203. 'change_type' => 'delivery',
  204. 'change_time' => time(),
  205. 'change_message' => '已配送 发货人:' . $data['delivery_name'] . ' 发货人电话:' . $data['delivery_id']
  206. ]);
  207. });
  208. return true;
  209. }
  210. /**
  211. * 虚拟发货
  212. * @param int $id
  213. * @param array $data
  214. */
  215. public function orderVirtualDelivery(int $id, array $data)
  216. {
  217. $data['delivery_type'] = 'fictitious';
  218. $data['status'] = 1;
  219. unset($data['sh_delivery_name'], $data['sh_delivery_id'], $data['delivery_name'], $data['delivery_id']);
  220. //保存信息
  221. /** @var StoreOrderStatusServices $services */
  222. $services = app()->make(StoreOrderStatusServices::class);
  223. $this->transaction(function () use ($id, $data, $services) {
  224. $this->dao->update($id, $data);
  225. $services->save([
  226. 'oid' => $id,
  227. 'change_type' => 'delivery_fictitious',
  228. 'change_message' => '已虚拟发货',
  229. 'change_time' => time()
  230. ]);
  231. });
  232. }
  233. /**
  234. * 获取修改配送信息表单结构
  235. * @param int $id
  236. * @return array
  237. * @throws \FormBuilder\Exception\FormBuilderException
  238. */
  239. public function distributionForm(int $id)
  240. {
  241. if (!$orderInfo = $this->dao->get($id))
  242. throw new AdminException(400118);
  243. $f[] = Form::input('order_id', '订单号', $orderInfo->getData('order_id'))->disabled(1);
  244. switch ($orderInfo['delivery_type']) {
  245. case 'send':
  246. $f[] = Form::input('delivery_name', '送货人姓名', $orderInfo->getData('delivery_name'))->required('请输入送货人姓名');
  247. $f[] = Form::input('delivery_id', '送货人电话', $orderInfo->getData('delivery_id'))->required('请输入送货人电话');
  248. break;
  249. case 'express':
  250. /** @var ExpressServices $expressServices */
  251. $expressServices = app()->make(ExpressServices::class);
  252. $f[] = Form::select('delivery_code', '快递公司', (string)$orderInfo->getData('delivery_code'))->setOptions($expressServices->expressSelectForm(['is_show' => 1]))->required('请选择快递公司');
  253. $f[] = Form::input('delivery_id', '快递单号', $orderInfo->getData('delivery_id'))->required('请填写快递单号');
  254. break;
  255. }
  256. return create_form('配送信息', $f, $this->url('/order/distribution/' . $id), 'PUT');
  257. }
  258. /**
  259. * 修改配送信息
  260. * @param int $id 订单id
  261. * @return mixed
  262. */
  263. public function updateDistribution(int $id, array $data)
  264. {
  265. $order = $this->dao->get($id);
  266. if (!$order) {
  267. throw new AdminException(100026);
  268. }
  269. switch ($order['delivery_type']) {
  270. case 'send':
  271. if (!$data['delivery_name']) {
  272. throw new AdminException(400523);
  273. }
  274. if (!$data['delivery_id']) {
  275. throw new AdminException(400524);
  276. }
  277. if (!preg_match("/^1[3456789]{1}\d{9}$/", $data['delivery_id'])) {
  278. throw new AdminException(400526);
  279. }
  280. break;
  281. case 'express':
  282. if (!$data['delivery_id']) {
  283. throw new AdminException(400531);
  284. }
  285. // 检测快递公司编码
  286. /** @var ExpressServices $expressServices */
  287. $expressServices = app()->make(ExpressServices::class);
  288. if ($name = $expressServices->value(['code' => $data['delivery_code']], 'name')) {
  289. $data['delivery_name'] = $name;
  290. } else {
  291. throw new AdminException(410324);
  292. }
  293. break;
  294. case 'fictitious':
  295. throw new AdminException(400479);
  296. break;
  297. default:
  298. throw new AdminException(400480);
  299. break;
  300. }
  301. /** @var StoreOrderStatusServices $statusService */
  302. $statusService = app()->make(StoreOrderStatusServices::class);
  303. $statusService->save([
  304. 'oid' => $id,
  305. 'change_type' => 'distribution',
  306. 'change_message' => '修改发货信息为' . $data['delivery_name'] . '号' . $data['delivery_id'],
  307. 'change_time' => time()
  308. ]);
  309. return $this->dao->update($id, $data);
  310. }
  311. /**订单发货后打印电子面单
  312. * @param $orderId
  313. * @return bool|mixed
  314. */
  315. public function orderDump($orderId)
  316. {
  317. if (!$orderId) throw new AdminException(10100);
  318. /** @var StoreOrderServices $orderService */
  319. $orderService = app()->make(StoreOrderServices::class);
  320. $orderInfo = $orderService->getOne(['id' => $orderId]);
  321. if (!$orderInfo) throw new AdminException(400118);
  322. if ($orderInfo->shipping_type != 1) throw new AdminException(400481);
  323. if (!$orderInfo->express_dump) throw new AdminException(400482);
  324. if (!sys_config('config_export_open', 0)) {
  325. throw new AdminException(400483);
  326. }
  327. $dumpInfo = json_decode($orderInfo->express_dump, true);
  328. /** @var ServeServices $expressService */
  329. $expressService = app()->make(ServeServices::class);
  330. $expData['com'] = $dumpInfo['com'];
  331. $expData['to_name'] = $orderInfo->real_name;
  332. $expData['to_tel'] = $orderInfo->user_phone;
  333. $expData['to_addr'] = $orderInfo->user_address;
  334. $expData['from_name'] = $dumpInfo['from_name'];
  335. $expData['from_tel'] = $dumpInfo['from_tel'];
  336. $expData['from_addr'] = $dumpInfo['from_addr'];
  337. $expData['siid'] = sys_config('config_export_siid');
  338. $expData['temp_id'] = $dumpInfo['temp_id'];
  339. $expData['cargo'] = $dumpInfo['cargo'];
  340. $expData['count'] = $orderInfo->total_num;
  341. $expData['order_id'] = $orderInfo->order_id;
  342. return $expressService->express()->dump($expData);
  343. }
  344. /**
  345. * 订单拆单发货
  346. * @param int $id
  347. * @param array $data
  348. * @return bool
  349. */
  350. public function splitDelivery(int $id, array $data)
  351. {
  352. $orderInfo = $this->dao->get($id, ['*'], ['pink']);
  353. if (!$orderInfo) {
  354. throw new AdminException(400470);
  355. }
  356. if ($orderInfo->is_del) {
  357. throw new AdminException(400471);
  358. }
  359. if ($orderInfo->shipping_type == 2) {
  360. throw new AdminException(400473);
  361. }
  362. if (isset($orderInfo['pinkStatus']) && $orderInfo['pinkStatus'] != 2) {
  363. throw new AdminException(400474);
  364. }
  365. /** @var StoreOrderRefundServices $storeOrderRefundServices */
  366. $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
  367. if ($storeOrderRefundServices->count(['store_order_id' => $id, 'refund_type' => [1, 2, 4, 5], 'is_cancel' => 0, 'is_del' => 0])) {
  368. throw new AdminException(400475);
  369. }
  370. if ($data['type'] == 1) {
  371. // 检测快递公司编码
  372. /** @var ExpressServices $expressServices */
  373. $expressServices = app()->make(ExpressServices::class);
  374. if (!$expressServices->be(['code' => $data['delivery_code']])) {
  375. throw new AdminException(410324);
  376. }
  377. }
  378. $cart_ids = $data['cart_ids'];
  379. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  380. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  381. unset($data['cart_ids']);
  382. $this->transaction(function () use ($id, $cart_ids, $orderInfo, $data, $storeOrderCartInfoServices) {
  383. /** @var StoreOrderSplitServices $storeOrderSplitServices */
  384. $storeOrderSplitServices = app()->make(StoreOrderSplitServices::class);
  385. //订单拆单
  386. [$splitOrderInfo, $otherOrder] = $storeOrderSplitServices->equalSplit($id, $cart_ids, $orderInfo);
  387. if ($splitOrderInfo) {
  388. $splitOrderInfo['refund_status'] = 0;
  389. //拆分订单执行发货
  390. $this->doDelivery((int)$splitOrderInfo->id, $splitOrderInfo, $data);
  391. /** @var StoreOrderStatusServices $services */
  392. $services = app()->make(StoreOrderStatusServices::class);
  393. //记录原订单状态
  394. $status_data = ['oid' => $id, 'change_time' => time()];
  395. $status_data['change_type'] = 'delivery_split';
  396. $status_data['change_message'] = '已拆分发货';
  397. $services->save($status_data);
  398. } else {
  399. $this->doDelivery($id, $orderInfo, $data);
  400. }
  401. });
  402. return true;
  403. }
  404. /**
  405. * 具体执行发货
  406. * @param int $id
  407. * @param $orderInfo
  408. * @param array $data
  409. * @return bool
  410. */
  411. public function doDelivery(int $id, $orderInfo, array $data)
  412. {
  413. $type = (int)$data['type'];
  414. unset($data['type']);
  415. //获取购物车内的商品标题
  416. /** @var StoreOrderCartInfoServices $orderInfoServices */
  417. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  418. $storeName = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo->id, $orderInfo->cart_id);
  419. switch ($type) {
  420. case 1://快递发货
  421. $this->orderDeliverGoods($id, $data, $orderInfo, $storeName);
  422. break;
  423. case 2://配送
  424. $this->orderDelivery($id, $data, $orderInfo, $storeName);
  425. break;
  426. case 3://虚拟发货
  427. $this->orderVirtualDelivery($id, $data, $orderInfo, $storeName);
  428. break;
  429. default:
  430. throw new AdminException(400522);
  431. }
  432. //用户推送消息事件
  433. event('notice.notice', [['orderInfo' => $orderInfo, 'storeName' => $storeName, 'data' => $data], 'order_deliver_success']);
  434. event('order.delivery', [$orderInfo, $storeName, $data, $type]);
  435. return true;
  436. }
  437. /**
  438. * 订单快递发货
  439. * @param int $id
  440. * @param array $data
  441. */
  442. public function orderDeliverGoods(int $id, array $data, $orderInfo, $storeTitle)
  443. {
  444. /** @var StoreOrderCartInfoServices $orderInfoServices */
  445. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  446. if (!$data['delivery_name']) {
  447. throw new AdminException(400007);
  448. }
  449. $data['delivery_type'] = 'express';
  450. if ($data['express_record_type'] == 2) {//电子面单
  451. if (!$data['delivery_code']) {
  452. throw new AdminException(400476);
  453. }
  454. if (!$data['express_temp_id']) {
  455. throw new AdminException(400527);
  456. }
  457. if (!$data['to_name']) {
  458. throw new AdminException(400008);
  459. }
  460. if (!$data['to_tel']) {
  461. throw new AdminException(400477);
  462. }
  463. if (!$data['to_addr']) {
  464. throw new AdminException(400478);
  465. }
  466. /** @var ServeServices $expressService */
  467. $expressService = app()->make(ServeServices::class);
  468. $expData['com'] = $data['delivery_code'];
  469. $expData['to_name'] = $orderInfo->real_name;
  470. $expData['to_tel'] = $orderInfo->user_phone;
  471. $expData['to_addr'] = $orderInfo->user_address;
  472. $expData['from_name'] = $data['to_name'];
  473. $expData['from_tel'] = $data['to_tel'];
  474. $expData['from_addr'] = $data['to_addr'];
  475. $expData['siid'] = sys_config('config_export_siid');
  476. $expData['temp_id'] = $data['express_temp_id'];
  477. $expData['count'] = $orderInfo->total_num;
  478. $expData['weight'] = $this->getOrderSumWeight($id);
  479. $expData['cargo'] = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo->id, $orderInfo->cart_id, true);
  480. $expData['order_id'] = $orderInfo->order_id;
  481. if (!sys_config('config_export_open', 0)) {
  482. throw new AdminException(400528);
  483. }
  484. $dump = $expressService->express()->dump($expData);
  485. $orderInfo->delivery_id = $dump['kuaidinum'];
  486. $data['express_dump'] = json_encode([
  487. 'com' => $expData['com'],
  488. 'from_name' => $expData['from_name'],
  489. 'from_tel' => $expData['from_tel'],
  490. 'from_addr' => $expData['from_addr'],
  491. 'temp_id' => $expData['temp_id'],
  492. 'cargo' => $expData['cargo'],
  493. ]);
  494. $data['delivery_id'] = $dump['kuaidinum'];
  495. } else {
  496. if (!$data['delivery_id']) {
  497. throw new AdminException(400531);
  498. }
  499. $orderInfo->delivery_id = $data['delivery_id'];
  500. }
  501. $data['status'] = 1;
  502. $orderInfo->delivery_type = $data['delivery_type'];
  503. $orderInfo->delivery_name = $data['delivery_name'];
  504. $orderInfo->status = $data['status'];
  505. /** @var StoreOrderStatusServices $services */
  506. $services = app()->make(StoreOrderStatusServices::class);
  507. $this->transaction(function () use ($id, $data, $services) {
  508. $res = $this->dao->update($id, $data);
  509. $res = $res && $services->save([
  510. 'oid' => $id,
  511. 'change_time' => time(),
  512. 'change_type' => 'delivery_goods',
  513. 'change_message' => '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']
  514. ]);
  515. if (!$res) {
  516. throw new AdminException(400529);
  517. }
  518. });
  519. return true;
  520. }
  521. /**
  522. * 返回订单商品总重量
  523. * @param int $id
  524. * @return int|string
  525. */
  526. public function getOrderSumWeight(int $id, $default = false)
  527. {
  528. /** @var StoreOrderCartInfoServices $services */
  529. $services = app()->make(StoreOrderCartInfoServices::class);
  530. $orderGoodInfo = $services->getOrderCartInfo((int)$id);
  531. $weight = 0;
  532. foreach ($orderGoodInfo as $cartInfo) {
  533. $cart = $cartInfo['cart_info'] ?? [];
  534. if ($cart) {
  535. $weight = bcadd((string)$weight, (string)bcmul((string)$cart['cart_num'] ?? '0', (string)$cart['productInfo']['attrInfo']['weight'] ?? '0', 4), 2);
  536. }
  537. }
  538. return $weight ?: ($default === false ? 0 : $default);
  539. }
  540. /**
  541. * 虚拟商品自动发货
  542. * @param $orderInfo
  543. */
  544. public function virtualSend($orderInfo)
  545. {
  546. /** @var StoreOrderStatusServices $statusService */
  547. $statusService = app()->make(StoreOrderStatusServices::class);
  548. /** @var StoreOrderCartInfoServices $services */
  549. $services = app()->make(StoreOrderCartInfoServices::class);
  550. $orderInfo['cart_info'] = $services->getOrderCartInfo((int)$orderInfo['id']);
  551. $activityStatus = $orderInfo['combination_id'] || $orderInfo['seckill_id'] || $orderInfo['bargain_id'];
  552. if ($orderInfo['virtual_type'] == 1) {
  553. /** @var StoreOrderServices $orderService */
  554. $orderService = app()->make(StoreOrderServices::class);
  555. $sku = $orderInfo['cart_info'][$orderInfo['cart_id'][0]]['cart_info']['productInfo']['attrInfo']['suk'];
  556. if ($activityStatus) {
  557. $product_id = $orderInfo['cart_info'][$orderInfo['cart_id'][0]]['cart_info']['productInfo']['product_id'];
  558. /** @var StoreProductAttrValueServices $attrValue */
  559. $attrValue = app()->make(StoreProductAttrValueServices::class);
  560. $disk_info = $attrValue->value(['product_id' => $product_id, 'suk' => $sku, 'type' => 0, 'is_virtual' => 1], 'disk_info');
  561. } else {
  562. $disk_info = $orderInfo['cart_info'][$orderInfo['cart_id'][0]]['cart_info']['productInfo']['attrInfo']['disk_info'];
  563. }
  564. if ($disk_info != '') {
  565. $orderService->update(['id' => $orderInfo['id']], ['status' => 1, 'delivery_type' => 'fictitious', 'virtual_info' => $disk_info, 'remark' => '密钥自动发放:' . $disk_info]);
  566. $this->SystemSend($orderInfo['uid'], [
  567. 'mark' => 'virtual_info',
  568. 'title' => '虚拟密钥发放',
  569. 'content' => '您购买的密钥商品已支付成功,支付金额' . $orderInfo['pay_price'] . '元,订单号:' . $orderInfo['order_id'] . ',密钥:' . $disk_info . ',感谢您的光临!'
  570. ]);
  571. } else {
  572. if ($activityStatus) {
  573. $product_id = $orderInfo['cart_info'][$orderInfo['cart_id'][0]]['cart_info']['productInfo']['product_id'];
  574. /** @var StoreProductAttrValueServices $attrValue */
  575. $attrValue = app()->make(StoreProductAttrValueServices::class);
  576. $unique = $attrValue->value(['product_id' => $product_id, 'suk' => $sku, 'type' => 0, 'is_virtual' => 1], 'unique');
  577. } else {
  578. $unique = $orderInfo['cart_info'][$orderInfo['cart_id'][0]]['cart_info']['productInfo']['attrInfo']['unique'];
  579. }
  580. /** @var StoreProductVirtualServices $virtualService */
  581. $virtualService = app()->make(StoreProductVirtualServices::class);
  582. $virtual = $virtualService->get(['attr_unique' => $unique, 'uid' => 0]);
  583. $virtual->order_id = $orderInfo['order_id'];
  584. $virtual->uid = $orderInfo['uid'];
  585. $virtual->save();
  586. $orderService->update(['id' => $orderInfo['id']], ['status' => 1, 'delivery_type' => 'fictitious', 'virtual_info' => $virtual->card_unique, 'remark' => '卡密已自动发放,卡号:' . $virtual->card_no . ';密码:' . $virtual->card_pwd]);
  587. $this->SystemSend($orderInfo['uid'], [
  588. 'mark' => 'virtual_info',
  589. 'title' => '虚拟卡密发放',
  590. 'content' => '您购买的卡密商品已支付成功,支付金额' . $orderInfo['pay_price'] . '元,订单号:' . $orderInfo['order_id'] . ',卡号:' . $virtual->card_no . ';密码:' . $virtual->card_pwd . ',感谢您的光临!'
  591. ]);
  592. }
  593. $statusService->save([
  594. 'oid' => $orderInfo['id'],
  595. 'change_type' => 'delivery_fictitious',
  596. 'change_message' => '卡密自动发货',
  597. 'change_time' => time()
  598. ]);
  599. } elseif ($orderInfo['virtual_type'] == 2) {
  600. if ($activityStatus) {
  601. $sku = $orderInfo['cart_info'][$orderInfo['cart_id'][0]]['cart_info']['productInfo']['attrInfo']['suk'];
  602. $product_id = $orderInfo['cart_info'][$orderInfo['cart_id'][0]]['cart_info']['productInfo']['product_id'];
  603. /** @var StoreProductAttrValueServices $attrValue */
  604. $attrValue = app()->make(StoreProductAttrValueServices::class);
  605. $coupon_id = $attrValue->value(['product_id' => $product_id, 'suk' => $sku, 'type' => 0, 'is_virtual' => 1], 'coupon_id');
  606. } else {
  607. $coupon_id = $orderInfo['cart_info'][$orderInfo['cart_id'][0]]['cart_info']['productInfo']['attrInfo']['coupon_id'];
  608. }
  609. /** @var StoreCouponIssueServices $issueService */
  610. $issueService = app()->make(StoreCouponIssueServices::class);
  611. $coupon = $issueService->get($coupon_id);
  612. if ($issueService->setCoupon($coupon, [$orderInfo['uid']])) {
  613. /** @var StoreOrderServices $orderService */
  614. $orderService = app()->make(StoreOrderServices::class);
  615. $orderService->update(['id' => $orderInfo['id']], ['status' => 1, 'delivery_type' => 'fictitious', 'virtual_info' => $coupon_id, 'remark' => '优惠券已自动发放']);
  616. $this->SystemSend($orderInfo['uid'], [
  617. 'mark' => 'virtual_info',
  618. 'title' => '购买优惠券发放',
  619. 'content' => '您购买的优惠券已支付成功,支付金额' . $orderInfo['pay_price'] . '元,订单号' . $orderInfo['order_id'] . '请在个人中心优惠券中查看,感谢您的光临!'
  620. ]);
  621. } else {
  622. throw new ApiException(410323);
  623. }
  624. $statusService->save([
  625. 'oid' => $orderInfo['id'],
  626. 'change_type' => 'delivery_fictitious',
  627. 'change_message' => '优惠券自动发货',
  628. 'change_time' => time()
  629. ]);
  630. }
  631. }
  632. /**
  633. * 虚拟商品站内信
  634. * @param int $uid
  635. * @param array $noticeInfo
  636. */
  637. public function SystemSend(int $uid, array $noticeInfo)
  638. {
  639. /** @var MessageSystemServices $MessageSystemServices */
  640. $MessageSystemServices = app()->make(MessageSystemServices::class);
  641. $data = [];
  642. $data['mark'] = $noticeInfo['mark'];
  643. $data['uid'] = $uid;
  644. $data['title'] = $noticeInfo['title'];
  645. $data['content'] = $noticeInfo['content'];
  646. $data['type'] = 1;
  647. $data['add_time'] = time();
  648. $MessageSystemServices->save($data);
  649. }
  650. }