StoreIntegralOrderServices.php 27 KB

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