StoreIntegralOrderServices.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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\activity\integral;
  12. use app\dao\activity\integral\StoreIntegralOrderDao;
  13. use app\services\BaseServices;
  14. use app\services\product\sku\StoreProductAttrValueServices;
  15. use app\services\serve\ServeServices;
  16. use app\services\shipping\ExpressServices;
  17. use app\services\user\UserServices;
  18. use app\services\user\UserAddressServices;
  19. use app\services\user\UserBillServices;
  20. use crmeb\exceptions\AdminException;
  21. use crmeb\exceptions\ApiException;
  22. use crmeb\services\FormBuilder as Form;
  23. use crmeb\services\printer\Printer;
  24. /**
  25. * Class StoreIntegralOrderServices
  26. * @package app\services\order
  27. * @method getOrderIdsCount(array $ids) 获取订单id下没有删除的订单数量
  28. * @method getUserOrderDetail(string $key, int $uid) 获取订单详情
  29. * @method getBuyCount($uid, $type) 获取用户已购买此活动商品的个数
  30. */
  31. class StoreIntegralOrderServices extends BaseServices
  32. {
  33. /**
  34. * 发货类型
  35. * @var string[]
  36. */
  37. public $deliveryType = ['send' => '商家配送', 'express' => '快递配送', 'fictitious' => '虚拟发货'];
  38. /**
  39. * StoreIntegralOrderServices constructor.
  40. * @param StoreIntegralOrderDao $dao
  41. */
  42. public function __construct(StoreIntegralOrderDao $dao)
  43. {
  44. $this->dao = $dao;
  45. }
  46. /**
  47. * 获取列表
  48. * @param array $where
  49. * @return array
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function getOrderList(array $where, array $field = ['*'], array $with = [])
  55. {
  56. [$page, $limit] = $this->getPageValue();
  57. $data = $this->dao->getOrderList($where, $field, $page, $limit, $with);
  58. $count = $this->dao->count($where);
  59. $data = $this->tidyOrderList($data);
  60. $batch_url = "file/upload/1";
  61. return compact('data', 'count', 'batch_url');
  62. }
  63. /**
  64. * 获取导出数据
  65. * @param array $where
  66. * @param int $limit
  67. * @return array
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public function getExportList(array $where, int $limit = 0)
  73. {
  74. if ($limit) {
  75. [$page] = $this->getPageValue();
  76. } else {
  77. [$page, $limit] = $this->getPageValue();
  78. }
  79. $data = $this->dao->getOrderList($where, ['*'], $page, $limit);
  80. $data = $this->tidyOrderList($data);
  81. return $data;
  82. }
  83. /**
  84. * 前端订单列表
  85. * @param array $where
  86. * @param array|string[] $field
  87. * @param array $with
  88. * @return array
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\DbException
  91. * @throws \think\db\exception\ModelNotFoundException
  92. */
  93. public function getOrderApiList(array $where, array $field = ['*'], array $with = [])
  94. {
  95. [$page, $limit] = $this->getPageValue();
  96. $data = $this->dao->getOrderList($where, $field, $page, $limit, $with);
  97. return $this->tidyOrderList($data);
  98. }
  99. /**
  100. * 订单详情数据格式化
  101. * @param $order
  102. * @return mixed
  103. */
  104. public function tidyOrder($order)
  105. {
  106. $order['add_time'] = date('Y-m-d H:i:s', $order['add_time']);
  107. if ($order['status'] == 1) {
  108. $order['status_name'] = '未发货';
  109. } else if ($order['status'] == 2) {
  110. $order['status_name'] = '待收货';
  111. } else if ($order['status'] == 3) {
  112. $order['status_name'] = '已完成';
  113. }
  114. $order['price'] = (int)$order['price'];
  115. $order['total_price'] = (int)$order['total_price'];
  116. return $order;
  117. }
  118. /**
  119. * 数据转换
  120. * @param array $data
  121. * @return array
  122. */
  123. public function tidyOrderList(array $data)
  124. {
  125. foreach ($data as &$item) {
  126. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  127. if ($item['status'] == 1) {
  128. $item['status_name'] = '未发货';
  129. } else if ($item['status'] == 2) {
  130. $item['status_name'] = '待收货';
  131. } else if ($item['status'] == 3) {
  132. $item['status_name'] = '已完成';
  133. }
  134. $item['price'] = (int)$item['price'];
  135. $item['total_price'] = (int)$item['total_price'];
  136. }
  137. return $data;
  138. }
  139. /**
  140. * 创建订单
  141. * @param $uid
  142. * @param $addressId
  143. * @param string $mark
  144. * @param $user
  145. * @param $num
  146. * @param $productInfo
  147. * @throws \Exception
  148. */
  149. public function createOrder($uid, $addressId, $mark = '', $userInfo, $num, $productInfo)
  150. {
  151. /** @var UserAddressServices $addressServices */
  152. $addressServices = app()->make(UserAddressServices::class);
  153. if (!$addressId) {
  154. throw new ApiException(410045);
  155. }
  156. if (!$addressInfo = $addressServices->getOne(['uid' => $uid, 'id' => $addressId, 'is_del' => 0])) throw new ApiException(410046);
  157. $addressInfo = $addressInfo->toArray();
  158. $total_price = bcmul($productInfo['price'], $num, 2);
  159. /** @var UserBillServices $userBillServices */
  160. $userBillServices = app()->make(UserBillServices::class);
  161. $usable_integral = bcsub((string)$userInfo['integral'], (string)$userBillServices->getBillSum(['uid' => $userInfo['uid'], 'is_frozen' => 1]), 0);
  162. if ($total_price > $usable_integral) throw new ApiException(410047);
  163. $orderInfo = [
  164. 'uid' => $uid,
  165. 'order_id' => $this->getNewOrderId(),
  166. 'real_name' => $addressInfo['real_name'],
  167. 'user_phone' => $addressInfo['phone'],
  168. 'user_address' => $addressInfo['province'] . ' ' . $addressInfo['city'] . ' ' . $addressInfo['district'] . ' ' . $addressInfo['detail'],
  169. 'product_id' => $productInfo['product_id'],
  170. 'image' => $productInfo['image'],
  171. 'store_name' => $productInfo['store_name'],
  172. 'suk' => $productInfo['suk'],
  173. 'total_num' => $num,
  174. 'price' => $productInfo['price'],
  175. 'total_price' => $total_price,
  176. 'add_time' => time(),
  177. 'status' => 1,
  178. 'mark' => $mark,
  179. 'channel_type' => $userInfo['user_type']
  180. ];
  181. $order = $this->transaction(function () use ($orderInfo, $userInfo, $productInfo, $uid, $num, $total_price) {
  182. //创建订单
  183. $order = $this->dao->save($orderInfo);
  184. if (!$order) {
  185. throw new ApiException(410200);
  186. }
  187. //扣库存
  188. $this->decGoodsStock($productInfo, $num);
  189. //减积分
  190. $this->deductIntegral($userInfo, $total_price, (int)$userInfo['uid'], $order->id);
  191. return $order;
  192. });
  193. /** @var StoreIntegralOrderStatusServices $statusService */
  194. $statusService = app()->make(StoreIntegralOrderStatusServices::class);
  195. $statusService->save([
  196. 'oid' => $order['id'],
  197. 'change_type' => 'cache_key_create_order',
  198. 'change_message' => '订单生成',
  199. 'change_time' => time()
  200. ]);
  201. return $order;
  202. }
  203. /**
  204. * 抵扣积分
  205. * @param array $userInfo
  206. * @param bool $useIntegral
  207. * @param array $priceData
  208. * @param int $uid
  209. * @param string $key
  210. */
  211. public function deductIntegral(array $userInfo, $priceIntegral, int $uid, string $orderId)
  212. {
  213. $res2 = true;
  214. if ($userInfo['integral'] > 0) {
  215. /** @var UserServices $userServices */
  216. $userServices = app()->make(UserServices::class);
  217. $res2 = false !== $userServices->bcDec($userInfo['uid'], 'integral', $priceIntegral, 'uid');
  218. /** @var UserBillServices $userBillServices */
  219. $userBillServices = app()->make(UserBillServices::class);
  220. $res3 = $userBillServices->income('storeIntegral_use_integral', $uid, $priceIntegral, $userInfo['integral'] - $priceIntegral, $orderId);
  221. $res2 = $res2 && false != $res3;
  222. }
  223. if (!$res2) {
  224. throw new ApiException(410227);
  225. }
  226. }
  227. /**
  228. * 扣库存
  229. * @param array $cartInfo
  230. * @param int $combinationId
  231. * @param int $seckillId
  232. * @param int $bargainId
  233. */
  234. public function decGoodsStock(array $productInfo, int $num)
  235. {
  236. $res5 = true;
  237. /** @var StoreIntegralServices $StoreIntegralServices */
  238. $StoreIntegralServices = app()->make(StoreIntegralServices::class);
  239. try {
  240. $res5 = $res5 && $StoreIntegralServices->decIntegralStock((int)$num, $productInfo['product_id'], $productInfo['unique']);
  241. if (!$res5) {
  242. throw new ApiException(410296);
  243. }
  244. } catch (\Throwable $e) {
  245. throw new ApiException(410296);
  246. }
  247. }
  248. /**
  249. * 使用雪花算法生成订单ID
  250. * @return string
  251. * @throws \Exception
  252. */
  253. public function getNewOrderId(string $prefix = 'wx')
  254. {
  255. $snowflake = new \Godruoyi\Snowflake\Snowflake();
  256. //32位
  257. if (PHP_INT_SIZE == 4) {
  258. $id = abs($snowflake->id());
  259. } else {
  260. $id = $snowflake->setStartTimeStamp(strtotime('2020-06-05') * 1000)->id();
  261. }
  262. return $prefix . $id;
  263. }
  264. /**
  265. *获取订单数量
  266. * @param array $where
  267. * @return mixed
  268. */
  269. public function orderCount(array $where)
  270. {
  271. //全部订单
  272. $data['statusAll'] = (string)$this->dao->count($where + ['is_system_del' => 0]);
  273. //未发货
  274. $data['unshipped'] = (string)$this->dao->count($where + ['status' => 1, 'is_system_del' => 0]);
  275. //待收货
  276. $data['untake'] = (string)$this->dao->count($where + ['status' => 2, 'is_system_del' => 0]);
  277. //待评价
  278. // $data['unevaluate'] = (string)$this->dao->count(['status' => 3, 'time' => $where['time'], 'is_system_del' => 0]);
  279. //交易完成
  280. $data['complete'] = (string)$this->dao->count($where + ['status' => 3, 'is_system_del' => 0]);
  281. return $data;
  282. }
  283. /**
  284. * 打印订单
  285. * @param $order
  286. * @throws \think\db\exception\DataNotFoundException
  287. * @throws \think\db\exception\DbException
  288. * @throws \think\db\exception\ModelNotFoundException
  289. */
  290. public function orderPrint($order)
  291. {
  292. $data = [
  293. 'clientId' => sys_config('printing_client_id', ''),
  294. 'apiKey' => sys_config('printing_api_key', ''),
  295. 'partner' => sys_config('develop_id', ''),
  296. 'terminal' => sys_config('terminal_number', '')
  297. ];
  298. if (!$data['clientId'] || !$data['apiKey'] || !$data['partner'] || !$data['terminal']) {
  299. throw new AdminException(400099);
  300. }
  301. $printer = new Printer('yi_lian_yun', $data);
  302. $res = $printer->setIntegralPrinterContent([
  303. 'name' => sys_config('site_name'),
  304. 'orderInfo' => is_object($order) ? $order->toArray() : $order,
  305. ])->startPrinter();
  306. if (!$res) {
  307. throw new AdminException($printer->getError());
  308. }
  309. return $res;
  310. }
  311. /**
  312. * 获取订单确认数据
  313. * @param array $user
  314. * @param $cartId
  315. * @return mixed
  316. */
  317. public function getOrderConfirmData(array $user, $unique, $num)
  318. {
  319. /** @var StoreProductAttrValueServices $StoreProductAttrValueServices */
  320. $StoreProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  321. $attrValue = $StoreProductAttrValueServices->uniqueByField($unique, 'product_id,suk,price,image,unique');
  322. if (!$attrValue || !isset($attrValue['storeIntegral']) || !$attrValue['storeIntegral']) {
  323. throw new ApiException(410295);
  324. }
  325. $data = [];
  326. $attrValue = is_object($attrValue) ? $attrValue->toArray() : $attrValue;
  327. $attrValue['price'] = (int)$attrValue['price'];
  328. /** @var UserBillServices $userBillServices */
  329. $userBillServices = app()->make(UserBillServices::class);
  330. $data['integral'] = bcsub((string)$user['integral'], (string)$userBillServices->getBillSum(['uid' => $user['uid'], 'is_frozen' => 1]), 0);
  331. $data['num'] = $num;
  332. $data['total_price'] = bcmul($num, $attrValue['price']);
  333. $data['productInfo'] = $attrValue;
  334. return $data;
  335. }
  336. /**
  337. * 删除订单
  338. * @param $uni
  339. * @param $uid
  340. * @return bool
  341. */
  342. public function removeOrder(string $order_id, int $uid)
  343. {
  344. $order = $this->getUserOrderDetail($order_id, $uid);
  345. if ($order['status'] != 3)
  346. throw new ApiException(100008);
  347. $order->is_del = 1;
  348. /** @var StoreIntegralOrderStatusServices $statusService */
  349. $statusService = app()->make(StoreIntegralOrderStatusServices::class);
  350. $res = $statusService->save([
  351. 'oid' => $order['id'],
  352. 'change_type' => 'remove_order',
  353. 'change_message' => '删除订单',
  354. 'change_time' => time()
  355. ]);
  356. if ($order->save() && $res) {
  357. return true;
  358. } else
  359. throw new ApiException(100008);
  360. }
  361. /**
  362. * 订单发货
  363. * @param int $id
  364. * @param array $data
  365. * @return bool
  366. */
  367. public function delivery(int $id, array $data)
  368. {
  369. $orderInfo = $this->dao->get($id);
  370. if (!$orderInfo) {
  371. throw new AdminException(400118);
  372. }
  373. if ($orderInfo->is_del) {
  374. throw new AdminException(400520);
  375. }
  376. if ($orderInfo->status != 1) {
  377. throw new AdminException(400521);
  378. }
  379. $type = (int)$data['type'];
  380. unset($data['type']);
  381. if ($type == 1) {
  382. // 检测快递公司编码
  383. /** @var ExpressServices $expressServices */
  384. $expressServices = app()->make(ExpressServices::class);
  385. if (!$expressServices->be(['code' => $data['delivery_code']])) {
  386. throw new AdminException(410324);
  387. }
  388. }
  389. switch ($type) {
  390. case 1:
  391. //发货
  392. $this->orderDeliverGoods($id, $data, $orderInfo);
  393. break;
  394. case 2:
  395. $this->orderDelivery($id, $data, $orderInfo);
  396. break;
  397. case 3:
  398. $this->orderVirtualDelivery($id, $data, $orderInfo);
  399. break;
  400. default:
  401. throw new AdminException(400522);
  402. }
  403. return true;
  404. }
  405. /**
  406. * 虚拟发货
  407. * @param int $id
  408. * @param array $data
  409. */
  410. public function orderVirtualDelivery(int $id, array $data)
  411. {
  412. $data['delivery_type'] = 'fictitious';
  413. $data['status'] = 2;
  414. unset($data['sh_delivery_name'], $data['sh_delivery_id'], $data['delivery_name'], $data['delivery_id']);
  415. //保存信息
  416. /** @var StoreIntegralOrderStatusServices $services */
  417. $services = app()->make(StoreIntegralOrderStatusServices::class);
  418. $this->transaction(function () use ($id, $data, $services) {
  419. $this->dao->update($id, $data);
  420. $services->save([
  421. 'oid' => $id,
  422. 'change_type' => 'delivery_fictitious',
  423. 'change_message' => '已虚拟发货',
  424. 'change_time' => time()
  425. ]);
  426. });
  427. }
  428. /**
  429. * 订单配送
  430. * @param int $id
  431. * @param array $data
  432. */
  433. public function orderDelivery(int $id, array $data, $orderInfo)
  434. {
  435. $data['delivery_type'] = 'send';
  436. $data['delivery_name'] = $data['sh_delivery_name'];
  437. $data['delivery_id'] = $data['sh_delivery_id'];
  438. $data['delivery_uid'] = $data['sh_delivery_uid'];
  439. // 获取核销码
  440. $data['verify_code'] = $this->getStoreCode();
  441. unset($data['sh_delivery_name'], $data['sh_delivery_id'], $data['sh_delivery_uid']);
  442. if (!$data['delivery_name']) {
  443. throw new AdminException(400523);
  444. }
  445. if (!$data['delivery_id']) {
  446. throw new AdminException(400524);
  447. }
  448. if (!$data['delivery_uid']) {
  449. throw new AdminException(400525);
  450. }
  451. if (!check_phone($data['delivery_id'])) {
  452. throw new AdminException(400526);
  453. }
  454. $data['status'] = 2;
  455. $orderInfo->delivery_type = $data['delivery_type'];
  456. $orderInfo->delivery_name = $data['delivery_name'];
  457. $orderInfo->delivery_id = $data['delivery_id'];
  458. $orderInfo->status = $data['status'];
  459. /** @var StoreIntegralOrderStatusServices $services */
  460. $services = app()->make(StoreIntegralOrderStatusServices::class);
  461. $this->transaction(function () use ($id, $data, $services) {
  462. $this->dao->update($id, $data);
  463. //记录订单状态
  464. $services->save([
  465. 'oid' => $id,
  466. 'change_type' => 'delivery',
  467. 'change_time' => time(),
  468. 'change_message' => '已配送 发货人:' . $data['delivery_name'] . ' 发货人电话:' . $data['delivery_id']
  469. ]);
  470. });
  471. return true;
  472. }
  473. /**
  474. * 订单快递发货
  475. * @param int $id
  476. * @param array $data
  477. */
  478. public function orderDeliverGoods(int $id, array $data, $orderInfo)
  479. {
  480. if (!$data['delivery_name']) {
  481. throw new AdminException(400007);
  482. }
  483. $data['delivery_type'] = 'express';
  484. if ($data['express_record_type'] == 2) {//电子面单
  485. if (!$data['delivery_code']) {
  486. throw new AdminException(400123);
  487. }
  488. if (!$data['express_temp_id']) {
  489. throw new AdminException(400527);
  490. }
  491. if (!$data['to_name']) {
  492. throw new AdminException(400008);
  493. }
  494. if (!$data['to_tel']) {
  495. throw new AdminException(400009);
  496. }
  497. if (!$data['to_addr']) {
  498. throw new AdminException(400011);
  499. }
  500. /** @var ServeServices $ServeServices */
  501. $ServeServices = app()->make(ServeServices::class);
  502. $expData['com'] = $data['delivery_code'];
  503. $expData['to_name'] = $orderInfo->real_name;
  504. $expData['to_tel'] = $orderInfo->user_phone;
  505. $expData['to_addr'] = $orderInfo->user_address;
  506. $expData['from_name'] = $data['to_name'];
  507. $expData['from_tel'] = $data['to_tel'];
  508. $expData['from_addr'] = $data['to_addr'];
  509. $expData['siid'] = sys_config('config_export_siid');
  510. $expData['temp_id'] = $data['express_temp_id'];
  511. $expData['count'] = $orderInfo->total_num;
  512. $expData['cargo'] = $orderInfo->store_name . '(' . $orderInfo->suk . ')*' . $orderInfo->total_num;
  513. $expData['order_id'] = $orderInfo->order_id;
  514. if (!sys_config('config_export_open', 0)) {
  515. throw new AdminException(400528);
  516. }
  517. $dump = $ServeServices->express()->dump($expData);
  518. $orderInfo->delivery_id = $dump['kuaidinum'];
  519. $data['express_dump'] = json_encode([
  520. 'com' => $expData['com'],
  521. 'from_name' => $expData['from_name'],
  522. 'from_tel' => $expData['from_tel'],
  523. 'from_addr' => $expData['from_addr'],
  524. 'temp_id' => $expData['temp_id'],
  525. 'cargo' => $expData['cargo'],
  526. ]);
  527. $data['delivery_id'] = $dump['kuaidinum'];
  528. } else {
  529. if (!$data['delivery_id']) {
  530. throw new AdminException(400120);
  531. }
  532. $orderInfo->delivery_id = $data['delivery_id'];
  533. }
  534. $data['status'] = 2;
  535. $orderInfo->delivery_type = $data['delivery_type'];
  536. $orderInfo->delivery_name = $data['delivery_name'];
  537. $orderInfo->status = $data['status'];
  538. /** @var StoreIntegralOrderStatusServices $services */
  539. $services = app()->make(StoreIntegralOrderStatusServices::class);
  540. $this->transaction(function () use ($id, $data, $services) {
  541. $res = $this->dao->update($id, $data);
  542. $res = $res && $services->save([
  543. 'oid' => $id,
  544. 'change_time' => time(),
  545. 'change_type' => 'delivery_goods',
  546. 'change_message' => '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']
  547. ]);
  548. if (!$res) {
  549. throw new AdminException(400529);
  550. }
  551. });
  552. return true;
  553. }
  554. /**
  555. * 核销订单生成核销码
  556. * @return false|string
  557. */
  558. public function getStoreCode()
  559. {
  560. mt_srand();
  561. list($msec, $sec) = explode(' ', microtime());
  562. $num = time() + mt_rand(10, 999999) . '' . substr($msec, 2, 3);//生成随机数
  563. if (strlen($num) < 12)
  564. $num = str_pad((string)$num, 12, 0, STR_PAD_RIGHT);
  565. else
  566. $num = substr($num, 0, 12);
  567. if ($this->dao->count(['verify_code' => $num])) {
  568. return $this->getStoreCode();
  569. }
  570. return $num;
  571. }
  572. /**
  573. * 获取修改配送信息表单结构
  574. * @param int $id
  575. * @return array
  576. * @throws \FormBuilder\Exception\FormBuilderException
  577. */
  578. public function distributionForm(int $id)
  579. {
  580. if (!$orderInfo = $this->dao->get($id))
  581. throw new AdminException(400118);
  582. $f[] = Form::input('order_id', '订单号', $orderInfo->getData('order_id'))->disabled(1);
  583. switch ($orderInfo['delivery_type']) {
  584. case 'send':
  585. $f[] = Form::input('delivery_name', '送货人姓名', $orderInfo->getData('delivery_name'))->required('请输入送货人姓名');
  586. $f[] = Form::input('delivery_id', '送货人电话', $orderInfo->getData('delivery_id'))->required('请输入送货人电话');
  587. break;
  588. case 'express':
  589. /** @var ExpressServices $expressServices */
  590. $expressServices = app()->make(ExpressServices::class);
  591. $f[] = Form::select('delivery_code', '快递公司', (string)$orderInfo->getData('delivery_code'))->setOptions($expressServices->expressSelectForm(['is_show' => 1]))->required('请选择快递公司');
  592. $f[] = Form::input('delivery_id', '快递单号', $orderInfo->getData('delivery_id'))->required('请填写快递单号');
  593. break;
  594. }
  595. return create_form('配送信息', $f, $this->url('/marketing/integral/order/distribution/' . $id), 'PUT');
  596. }
  597. /**
  598. * 用户订单收货
  599. * @param $uni
  600. * @param $uid
  601. * @return bool
  602. */
  603. public function takeOrder(string $order_id, int $uid)
  604. {
  605. $order = $this->dao->getUserOrderDetail($order_id, $uid);
  606. if (!$order) {
  607. throw new ApiException(400118);
  608. }
  609. if ($order['status'] != 2) {
  610. throw new ApiException(400530);
  611. }
  612. $order->status = 3;
  613. /** @var StoreIntegralOrderStatusServices $statusService */
  614. $statusService = app()->make(StoreIntegralOrderStatusServices::class);
  615. $res = $order->save() && $statusService->save([
  616. 'oid' => $order['id'],
  617. 'change_type' => 'user_take_delivery',
  618. 'change_message' => '用户已收货',
  619. 'change_time' => time()
  620. ]);
  621. if (!$res) {
  622. throw new ApiException(400116);
  623. }
  624. return $order;
  625. }
  626. /**
  627. * 修改配送信息
  628. * @param int $id 订单id
  629. * @return mixed
  630. */
  631. public function updateDistribution(int $id, array $data)
  632. {
  633. $order = $this->dao->get($id);
  634. if (!$order) {
  635. throw new AdminException(400118);
  636. }
  637. switch ($order['delivery_type']) {
  638. case 'send':
  639. if (!$data['delivery_name']) {
  640. throw new AdminException(400523);
  641. }
  642. if (!$data['delivery_id']) {
  643. throw new AdminException(400524);
  644. }
  645. if (!check_phone($data['delivery_id'])) {
  646. throw new AdminException(400526);
  647. }
  648. break;
  649. case 'express':
  650. // 检测快递公司编码
  651. /** @var ExpressServices $expressServices */
  652. $expressServices = app()->make(ExpressServices::class);
  653. if ($name = $expressServices->value(['code' => $data['delivery_code']], 'name')) {
  654. $data['delivery_name'] = $name;
  655. } else {
  656. throw new AdminException(410324);
  657. }
  658. break;
  659. default:
  660. throw new AdminException(400532);
  661. }
  662. /** @var StoreIntegralOrderStatusServices $statusService */
  663. $statusService = app()->make(StoreIntegralOrderStatusServices::class);
  664. $statusService->save([
  665. 'oid' => $id,
  666. 'change_type' => 'distribution',
  667. 'change_message' => '修改发货信息为' . $data['delivery_name'] . '号' . $data['delivery_id'],
  668. 'change_time' => time()
  669. ]);
  670. return $this->dao->update($id, $data);
  671. }
  672. /**
  673. * 批量删除用户已经删除的订单
  674. * @return string|null
  675. */
  676. public function delOrders(array $ids)
  677. {
  678. if (!count($ids)) throw new AdminException(100100);
  679. if ($this->getOrderIdsCount($ids)) throw new AdminException(400118);
  680. return $this->batchUpdate($ids, ['is_system_del' => 1]);
  681. }
  682. /**
  683. * 删除订单
  684. * @param $id
  685. * @return bool
  686. */
  687. public function delOrder(int $id)
  688. {
  689. if (!$id || !($orderInfo = $this->get($id))) throw new AdminException(400118);
  690. if (!$orderInfo->is_del) throw new AdminException(400157);
  691. $orderInfo->is_system_del = 1;
  692. return $orderInfo->save();
  693. }
  694. /**
  695. * 修改备注
  696. * @param int $id
  697. * @param string $remark
  698. * @return mixed
  699. * @throws \think\db\exception\DataNotFoundException
  700. * @throws \think\db\exception\DbException
  701. * @throws \think\db\exception\ModelNotFoundException
  702. */
  703. public function remark(int $id, string $remark)
  704. {
  705. if (!$remark) throw new AdminException(400106);
  706. if (!$id) throw new AdminException(100100);
  707. if (!$order = $this->dao->get($id)) {
  708. throw new AdminException(100025);
  709. }
  710. $order->remark = $remark;
  711. return $order->save();
  712. }
  713. }