StoreOrderDeliveryServices.php 30 KB

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