StoreIntegralOrderServices.php 27 KB

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