StoreIntegralOrderServices.php 27 KB

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