StoreOrderDeliveryServices.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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. //用户推送消息事件
  423. event('notice.notice', [['orderInfo' => $orderInfo, 'storeName' => $storeName, 'data' => $data], 'order_postage_success']);
  424. break;
  425. case 2://配送
  426. $this->orderDelivery($id, $data, $orderInfo, $storeName);
  427. //用户推送消息事件
  428. event('notice.notice', [['orderInfo' => $orderInfo, 'storeName' => $storeName, 'data' => $data], 'order_deliver_success']);
  429. break;
  430. case 3://虚拟发货
  431. $this->orderVirtualDelivery($id, $data, $orderInfo, $storeName);
  432. break;
  433. default:
  434. throw new AdminException(400522);
  435. }
  436. event('order.delivery', [$orderInfo, $storeName, $data, $type]);
  437. return true;
  438. }
  439. /**
  440. * 订单快递发货
  441. * @param int $id
  442. * @param array $data
  443. */
  444. public function orderDeliverGoods(int $id, array $data, $orderInfo, $storeTitle)
  445. {
  446. /** @var StoreOrderCartInfoServices $orderInfoServices */
  447. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  448. if (!$data['delivery_name']) {
  449. throw new AdminException(400007);
  450. }
  451. $data['delivery_type'] = 'express';
  452. if ($data['express_record_type'] == 2) {//电子面单
  453. if (!$data['delivery_code']) {
  454. throw new AdminException(400476);
  455. }
  456. if (!$data['express_temp_id']) {
  457. throw new AdminException(400527);
  458. }
  459. if (!$data['to_name']) {
  460. throw new AdminException(400008);
  461. }
  462. if (!$data['to_tel']) {
  463. throw new AdminException(400477);
  464. }
  465. if (!$data['to_addr']) {
  466. throw new AdminException(400478);
  467. }
  468. /** @var ServeServices $expressService */
  469. $expressService = app()->make(ServeServices::class);
  470. $expData['com'] = $data['delivery_code'];
  471. $expData['to_name'] = $orderInfo->real_name;
  472. $expData['to_tel'] = $orderInfo->user_phone;
  473. $expData['to_addr'] = $orderInfo->user_address;
  474. $expData['from_name'] = $data['to_name'];
  475. $expData['from_tel'] = $data['to_tel'];
  476. $expData['from_addr'] = $data['to_addr'];
  477. $expData['siid'] = sys_config('config_export_siid');
  478. $expData['temp_id'] = $data['express_temp_id'];
  479. $expData['count'] = $orderInfo->total_num;
  480. $expData['weight'] = $this->getOrderSumWeight($id);
  481. $expData['cargo'] = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo->id, $orderInfo->cart_id, true);
  482. $expData['order_id'] = $orderInfo->order_id;
  483. if (!sys_config('config_export_open', 0)) {
  484. throw new AdminException(400528);
  485. }
  486. $dump = $expressService->express()->dump($expData);
  487. $orderInfo->delivery_id = $dump['kuaidinum'];
  488. $data['express_dump'] = json_encode([
  489. 'com' => $expData['com'],
  490. 'from_name' => $expData['from_name'],
  491. 'from_tel' => $expData['from_tel'],
  492. 'from_addr' => $expData['from_addr'],
  493. 'temp_id' => $expData['temp_id'],
  494. 'cargo' => $expData['cargo'],
  495. ]);
  496. $data['delivery_id'] = $dump['kuaidinum'];
  497. } else {
  498. if (!$data['delivery_id']) {
  499. throw new AdminException(400531);
  500. }
  501. $orderInfo->delivery_id = $data['delivery_id'];
  502. }
  503. $data['status'] = 1;
  504. $orderInfo->delivery_type = $data['delivery_type'];
  505. $orderInfo->delivery_name = $data['delivery_name'];
  506. $orderInfo->status = $data['status'];
  507. /** @var StoreOrderStatusServices $services */
  508. $services = app()->make(StoreOrderStatusServices::class);
  509. $this->transaction(function () use ($id, $data, $services) {
  510. $res = $this->dao->update($id, $data);
  511. $res = $res && $services->save([
  512. 'oid' => $id,
  513. 'change_time' => time(),
  514. 'change_type' => 'delivery_goods',
  515. 'change_message' => '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']
  516. ]);
  517. if (!$res) {
  518. throw new AdminException(400529);
  519. }
  520. });
  521. return true;
  522. }
  523. /**
  524. * 返回订单商品总重量
  525. * @param int $id
  526. * @return int|string
  527. */
  528. public function getOrderSumWeight(int $id, $default = false)
  529. {
  530. /** @var StoreOrderCartInfoServices $services */
  531. $services = app()->make(StoreOrderCartInfoServices::class);
  532. $orderGoodInfo = $services->getOrderCartInfo((int)$id);
  533. $weight = 0;
  534. foreach ($orderGoodInfo as $cartInfo) {
  535. $cart = $cartInfo['cart_info'] ?? [];
  536. if ($cart) {
  537. $weight = bcadd((string)$weight, (string)bcmul((string)$cart['cart_num'] ?? '0', (string)$cart['productInfo']['attrInfo']['weight'] ?? '0', 4), 2);
  538. }
  539. }
  540. return $weight ?: ($default === false ? 0 : $default);
  541. }
  542. /**
  543. * 虚拟商品自动发货
  544. * @param $orderInfo
  545. */
  546. public function virtualSend($orderInfo)
  547. {
  548. /** @var StoreOrderStatusServices $statusService */
  549. $statusService = app()->make(StoreOrderStatusServices::class);
  550. /** @var StoreOrderCartInfoServices $services */
  551. $services = app()->make(StoreOrderCartInfoServices::class);
  552. $orderInfo['cart_info'] = $services->getOrderCartInfo((int)$orderInfo['id']);
  553. $activityStatus = $orderInfo['combination_id'] || $orderInfo['seckill_id'] || $orderInfo['bargain_id'];
  554. if ($orderInfo['virtual_type'] == 1) {
  555. /** @var StoreOrderServices $orderService */
  556. $orderService = app()->make(StoreOrderServices::class);
  557. $sku = $orderInfo['cart_info'][$orderInfo['cart_id'][0]]['cart_info']['productInfo']['attrInfo']['suk'];
  558. if ($activityStatus) {
  559. $product_id = $orderInfo['cart_info'][$orderInfo['cart_id'][0]]['cart_info']['productInfo']['product_id'];
  560. /** @var StoreProductAttrValueServices $attrValue */
  561. $attrValue = app()->make(StoreProductAttrValueServices::class);
  562. $disk_info = $attrValue->value(['product_id' => $product_id, 'suk' => $sku, 'type' => 0, 'is_virtual' => 1], 'disk_info');
  563. } else {
  564. $disk_info = $orderInfo['cart_info'][$orderInfo['cart_id'][0]]['cart_info']['productInfo']['attrInfo']['disk_info'];
  565. }
  566. if ($disk_info != '') {
  567. $orderService->update(['id' => $orderInfo['id']], ['status' => 1, 'delivery_type' => 'fictitious', 'virtual_info' => $disk_info, 'remark' => '密钥自动发放:' . $disk_info]);
  568. $this->SystemSend($orderInfo['uid'], [
  569. 'mark' => 'virtual_info',
  570. 'title' => '虚拟密钥发放',
  571. 'content' => '您购买的密钥商品已支付成功,支付金额' . $orderInfo['pay_price'] . '元,订单号:' . $orderInfo['order_id'] . ',密钥:' . $disk_info . ',感谢您的光临!'
  572. ]);
  573. } else {
  574. if ($activityStatus) {
  575. $product_id = $orderInfo['cart_info'][$orderInfo['cart_id'][0]]['cart_info']['productInfo']['product_id'];
  576. /** @var StoreProductAttrValueServices $attrValue */
  577. $attrValue = app()->make(StoreProductAttrValueServices::class);
  578. $unique = $attrValue->value(['product_id' => $product_id, 'suk' => $sku, 'type' => 0, 'is_virtual' => 1], 'unique');
  579. } else {
  580. $unique = $orderInfo['cart_info'][$orderInfo['cart_id'][0]]['cart_info']['productInfo']['attrInfo']['unique'];
  581. }
  582. /** @var StoreProductVirtualServices $virtualService */
  583. $virtualService = app()->make(StoreProductVirtualServices::class);
  584. $virtual = $virtualService->get(['attr_unique' => $unique, 'uid' => 0]);
  585. $virtual->order_id = $orderInfo['order_id'];
  586. $virtual->uid = $orderInfo['uid'];
  587. $virtual->save();
  588. $orderService->update(['id' => $orderInfo['id']], ['status' => 1, 'delivery_type' => 'fictitious', 'virtual_info' => $virtual->card_unique, 'remark' => '卡密已自动发放,卡号:' . $virtual->card_no . ';密码:' . $virtual->card_pwd]);
  589. $this->SystemSend($orderInfo['uid'], [
  590. 'mark' => 'virtual_info',
  591. 'title' => '虚拟卡密发放',
  592. 'content' => '您购买的卡密商品已支付成功,支付金额' . $orderInfo['pay_price'] . '元,订单号:' . $orderInfo['order_id'] . ',卡号:' . $virtual->card_no . ';密码:' . $virtual->card_pwd . ',感谢您的光临!'
  593. ]);
  594. }
  595. $statusService->save([
  596. 'oid' => $orderInfo['id'],
  597. 'change_type' => 'delivery_fictitious',
  598. 'change_message' => '卡密自动发货',
  599. 'change_time' => time()
  600. ]);
  601. } elseif ($orderInfo['virtual_type'] == 2) {
  602. if ($activityStatus) {
  603. $sku = $orderInfo['cart_info'][$orderInfo['cart_id'][0]]['cart_info']['productInfo']['attrInfo']['suk'];
  604. $product_id = $orderInfo['cart_info'][$orderInfo['cart_id'][0]]['cart_info']['productInfo']['product_id'];
  605. /** @var StoreProductAttrValueServices $attrValue */
  606. $attrValue = app()->make(StoreProductAttrValueServices::class);
  607. $coupon_id = $attrValue->value(['product_id' => $product_id, 'suk' => $sku, 'type' => 0, 'is_virtual' => 1], 'coupon_id');
  608. } else {
  609. $coupon_id = $orderInfo['cart_info'][$orderInfo['cart_id'][0]]['cart_info']['productInfo']['attrInfo']['coupon_id'];
  610. }
  611. /** @var StoreCouponIssueServices $issueService */
  612. $issueService = app()->make(StoreCouponIssueServices::class);
  613. $coupon = $issueService->get($coupon_id);
  614. if ($issueService->setCoupon($coupon, [$orderInfo['uid']])) {
  615. /** @var StoreOrderServices $orderService */
  616. $orderService = app()->make(StoreOrderServices::class);
  617. $orderService->update(['id' => $orderInfo['id']], ['status' => 1, 'delivery_type' => 'fictitious', 'virtual_info' => $coupon_id, 'remark' => '优惠券已自动发放']);
  618. $this->SystemSend($orderInfo['uid'], [
  619. 'mark' => 'virtual_info',
  620. 'title' => '购买优惠券发放',
  621. 'content' => '您购买的优惠券已支付成功,支付金额' . $orderInfo['pay_price'] . '元,订单号' . $orderInfo['order_id'] . '请在个人中心优惠券中查看,感谢您的光临!'
  622. ]);
  623. } else {
  624. throw new ApiException(410323);
  625. }
  626. $statusService->save([
  627. 'oid' => $orderInfo['id'],
  628. 'change_type' => 'delivery_fictitious',
  629. 'change_message' => '优惠券自动发货',
  630. 'change_time' => time()
  631. ]);
  632. }
  633. }
  634. /**
  635. * 虚拟商品站内信
  636. * @param int $uid
  637. * @param array $noticeInfo
  638. */
  639. public function SystemSend(int $uid, array $noticeInfo)
  640. {
  641. /** @var MessageSystemServices $MessageSystemServices */
  642. $MessageSystemServices = app()->make(MessageSystemServices::class);
  643. $data = [];
  644. $data['mark'] = $noticeInfo['mark'];
  645. $data['uid'] = $uid;
  646. $data['title'] = $noticeInfo['title'];
  647. $data['content'] = $noticeInfo['content'];
  648. $data['type'] = 1;
  649. $data['add_time'] = time();
  650. $MessageSystemServices->save($data);
  651. }
  652. }