StoreIntegralOrderServices.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\services\FormBuilder as Form;
  21. use crmeb\services\printer\Printer;
  22. use crmeb\traits\ServicesTrait;
  23. use think\exception\ValidateException;
  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. use ServicesTrait;
  34. /**
  35. * 发货类型
  36. * @var string[]
  37. */
  38. public $deliveryType = ['send' => '商家配送', 'express' => '快递配送', 'fictitious' => '虚拟发货'];
  39. /**
  40. * StoreIntegralOrderServices constructor.
  41. * @param StoreIntegralOrderDao $dao
  42. */
  43. public function __construct(StoreIntegralOrderDao $dao)
  44. {
  45. $this->dao = $dao;
  46. }
  47. /**
  48. * 获取列表
  49. * @param array $where
  50. * @return array
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function getOrderList(array $where, array $field = ['*'], array $with = [])
  56. {
  57. [$page, $limit] = $this->getPageValue();
  58. $data = $this->dao->getOrderList($where, $field, $page, $limit, $with);
  59. $count = $this->dao->count($where);
  60. $data = $this->tidyOrderList($data);
  61. $batch_url = "file/upload/1";
  62. return compact('data', 'count', 'batch_url');
  63. }
  64. /**
  65. * 获取导出数据
  66. * @param array $where
  67. * @param int $limit
  68. * @return array
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public function getExportList(array $where, int $limit = 0)
  74. {
  75. if ($limit) {
  76. [$page] = $this->getPageValue();
  77. } else {
  78. [$page, $limit] = $this->getPageValue();
  79. }
  80. $data = $this->dao->getOrderList($where, ['*'], $page, $limit);
  81. $data = $this->tidyOrderList($data);
  82. return $data;
  83. }
  84. /**
  85. * 前端订单列表
  86. * @param array $where
  87. * @param array|string[] $field
  88. * @param array $with
  89. * @return array
  90. * @throws \think\db\exception\DataNotFoundException
  91. * @throws \think\db\exception\DbException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. */
  94. public function getOrderApiList(array $where, array $field = ['*'], array $with = [])
  95. {
  96. [$page, $limit] = $this->getPageValue();
  97. $data = $this->dao->getOrderList($where, $field, $page, $limit, $with);
  98. $data = $this->tidyOrderList($data);
  99. return $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 ValidateException('请选择收货地址!');
  153. }
  154. if (!$addressInfo = $addressServices->getOne(['uid' => $uid, 'id' => $addressId, 'is_del' => 0]))
  155. throw new ValidateException('地址选择有误!');
  156. $addressInfo = $addressInfo->toArray();
  157. $total_price = bcmul($productInfo['price'], $num, 2);
  158. if ($total_price > $userInfo['integral']) throw new ValidateException('积分不足!');
  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 ValidateException('订单生成失败!');
  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'], $orderId);
  217. $res2 = $res2 && false != $res3;
  218. }
  219. if (!$res2) {
  220. throw new ValidateException('使用积分抵扣失败!');
  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 ValidateException('库存不足!');
  239. }
  240. } catch (\Throwable $e) {
  241. throw new ValidateException('库存不足!');
  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 ValidateException('请先配置小票打印开发者');
  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 ValidateException($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 ValidateException('该商品已下架');
  320. }
  321. $data = [];
  322. $attrValue = is_object($attrValue) ? $attrValue->toArray() : $attrValue;
  323. $data['integral'] = $user['integral'];
  324. $data['num'] = $num;
  325. $data['total_price'] = bcmul($num, $attrValue['price'], 2);
  326. $data['productInfo'] = $attrValue;
  327. return $data;
  328. }
  329. /**
  330. * 删除订单
  331. * @param $uni
  332. * @param $uid
  333. * @return bool
  334. */
  335. public function removeOrder(string $order_id, int $uid)
  336. {
  337. $order = $this->getUserOrderDetail($order_id, $uid);
  338. if ($order['status'] != 3)
  339. throw new ValidateException('该订单无法删除!');
  340. $order->is_del = 1;
  341. /** @var StoreIntegralOrderStatusServices $statusService */
  342. $statusService = app()->make(StoreIntegralOrderStatusServices::class);
  343. $res = $statusService->save([
  344. 'oid' => $order['id'],
  345. 'change_type' => 'remove_order',
  346. 'change_message' => '删除订单',
  347. 'change_time' => time()
  348. ]);
  349. if ($order->save() && $res) {
  350. return true;
  351. } else
  352. throw new ValidateException('订单删除失败!');
  353. }
  354. /**
  355. * 订单发货
  356. * @param int $id
  357. * @param array $data
  358. * @return bool
  359. */
  360. public function delivery(int $id, array $data)
  361. {
  362. $orderInfo = $this->dao->get($id);
  363. if (!$orderInfo) {
  364. throw new ValidateException('订单未能查到,不能发货!');
  365. }
  366. if ($orderInfo->is_del) {
  367. throw new ValidateException('订单已删除,不能发货!');
  368. }
  369. if ($orderInfo->status != 1) {
  370. throw new ValidateException('订单已发货请勿重复操作!');
  371. }
  372. $type = (int)$data['type'];
  373. unset($data['type']);
  374. switch ($type) {
  375. case 1:
  376. //发货
  377. $this->orderDeliverGoods($id, $data, $orderInfo);
  378. break;
  379. case 2:
  380. $this->orderDelivery($id, $data, $orderInfo);
  381. break;
  382. case 3:
  383. $this->orderVirtualDelivery($id, $data, $orderInfo);
  384. break;
  385. default:
  386. throw new ValidateException('暂时不支持其他发货类型');
  387. }
  388. return true;
  389. }
  390. /**
  391. * 虚拟发货
  392. * @param int $id
  393. * @param array $data
  394. */
  395. public function orderVirtualDelivery(int $id, array $data)
  396. {
  397. $data['delivery_type'] = 'fictitious';
  398. $data['status'] = 2;
  399. unset($data['sh_delivery_name'], $data['sh_delivery_id'], $data['delivery_name'], $data['delivery_id']);
  400. //保存信息
  401. /** @var StoreIntegralOrderStatusServices $services */
  402. $services = app()->make(StoreIntegralOrderStatusServices::class);
  403. $this->transaction(function () use ($id, $data, $services) {
  404. $this->dao->update($id, $data);
  405. $services->save([
  406. 'oid' => $id,
  407. 'change_type' => 'delivery_fictitious',
  408. 'change_message' => '已虚拟发货',
  409. 'change_time' => time()
  410. ]);
  411. });
  412. }
  413. /**
  414. * 订单配送
  415. * @param int $id
  416. * @param array $data
  417. */
  418. public function orderDelivery(int $id, array $data, $orderInfo)
  419. {
  420. $data['delivery_type'] = 'send';
  421. $data['delivery_name'] = $data['sh_delivery_name'];
  422. $data['delivery_id'] = $data['sh_delivery_id'];
  423. $data['delivery_uid'] = $data['sh_delivery_uid'];
  424. // 获取核销码
  425. $data['verify_code'] = $this->getStoreCode();
  426. unset($data['sh_delivery_name'], $data['sh_delivery_id'], $data['sh_delivery_uid']);
  427. if (!$data['delivery_name']) {
  428. throw new ValidateException('请输入送货人姓名');
  429. }
  430. if (!$data['delivery_id']) {
  431. throw new ValidateException('请输入送货人电话号码');
  432. }
  433. if (!$data['delivery_uid']) {
  434. throw new ValidateException('请输入送货人信息');
  435. }
  436. if (!check_phone($data['delivery_id'])) {
  437. throw new ValidateException('请输入正确的送货人电话号码');
  438. }
  439. $data['status'] = 2;
  440. $orderInfo->delivery_type = $data['delivery_type'];
  441. $orderInfo->delivery_name = $data['delivery_name'];
  442. $orderInfo->delivery_id = $data['delivery_id'];
  443. $orderInfo->status = $data['status'];
  444. /** @var StoreIntegralOrderStatusServices $services */
  445. $services = app()->make(StoreIntegralOrderStatusServices::class);
  446. $this->transaction(function () use ($id, $data, $services) {
  447. $this->dao->update($id, $data);
  448. //记录订单状态
  449. $services->save([
  450. 'oid' => $id,
  451. 'change_type' => 'delivery',
  452. 'change_time' => time(),
  453. 'change_message' => '已配送 发货人:' . $data['delivery_name'] . ' 发货人电话:' . $data['delivery_id']
  454. ]);
  455. });
  456. return true;
  457. }
  458. /**
  459. * 订单快递发货
  460. * @param int $id
  461. * @param array $data
  462. */
  463. public function orderDeliverGoods(int $id, array $data, $orderInfo)
  464. {
  465. if (!$data['delivery_name']) {
  466. throw new ValidateException('请选择快递公司');
  467. }
  468. $data['delivery_type'] = 'express';
  469. if ($data['express_record_type'] == 2) {//电子面单
  470. if (!$data['delivery_code']) {
  471. throw new ValidateException('快递公司编缺失');
  472. }
  473. if (!$data['express_temp_id']) {
  474. throw new ValidateException('请选择电子面单模板');
  475. }
  476. if (!$data['to_name']) {
  477. throw new ValidateException('请填写寄件人姓名');
  478. }
  479. if (!$data['to_tel']) {
  480. throw new ValidateException('请填写寄件人电话');
  481. }
  482. if (!$data['to_addr']) {
  483. throw new ValidateException('请填写寄件人地址');
  484. }
  485. /** @var ServeServices $ServeServices */
  486. $ServeServices = app()->make(ServeServices::class);
  487. $expData['com'] = $data['delivery_code'];
  488. $expData['to_name'] = $orderInfo->real_name;
  489. $expData['to_tel'] = $orderInfo->user_phone;
  490. $expData['to_addr'] = $orderInfo->user_address;
  491. $expData['from_name'] = $data['to_name'];
  492. $expData['from_tel'] = $data['to_tel'];
  493. $expData['from_addr'] = $data['to_addr'];
  494. $expData['siid'] = sys_config('config_export_siid');
  495. $expData['temp_id'] = $data['express_temp_id'];
  496. $expData['count'] = $orderInfo->total_num;
  497. $expData['cargo'] = $orderInfo->store_name . '(' . $orderInfo->suk . ')*' . $orderInfo->total_num;
  498. $expData['order_id'] = $orderInfo->order_id;
  499. if (!sys_config('config_export_open', 0)) {
  500. throw new ValidateException('系统通知:电子面单已关闭,请选择其他发货方式!');
  501. }
  502. $dump = $ServeServices->express()->dump($expData);
  503. $orderInfo->delivery_id = $dump['kuaidinum'];
  504. $data['express_dump'] = json_encode([
  505. 'com' => $expData['com'],
  506. 'from_name' => $expData['from_name'],
  507. 'from_tel' => $expData['from_tel'],
  508. 'from_addr' => $expData['from_addr'],
  509. 'temp_id' => $expData['temp_id'],
  510. 'cargo' => $expData['cargo'],
  511. ]);
  512. $data['delivery_id'] = $dump['kuaidinum'];
  513. } else {
  514. if (!$data['delivery_id']) {
  515. throw new ValidateException('请输入快递单号');
  516. }
  517. $orderInfo->delivery_id = $data['delivery_id'];
  518. }
  519. $data['status'] = 2;
  520. $orderInfo->delivery_type = $data['delivery_type'];
  521. $orderInfo->delivery_name = $data['delivery_name'];
  522. $orderInfo->status = $data['status'];
  523. /** @var StoreIntegralOrderStatusServices $services */
  524. $services = app()->make(StoreIntegralOrderStatusServices::class);
  525. $this->transaction(function () use ($id, $data, $services) {
  526. $res = $this->dao->update($id, $data);
  527. $res = $res && $services->save([
  528. 'oid' => $id,
  529. 'change_time' => time(),
  530. 'change_type' => 'delivery_goods',
  531. 'change_message' => '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']
  532. ]);
  533. if (!$res) {
  534. throw new ValidateException('发货失败:数据保存不成功');
  535. }
  536. });
  537. return true;
  538. }
  539. /**
  540. * 核销订单生成核销码
  541. * @return false|string
  542. */
  543. public function getStoreCode()
  544. {
  545. mt_srand();
  546. list($msec, $sec) = explode(' ', microtime());
  547. $num = time() + mt_rand(10, 999999) . '' . substr($msec, 2, 3);//生成随机数
  548. if (strlen($num) < 12)
  549. $num = str_pad((string)$num, 12, 0, STR_PAD_RIGHT);
  550. else
  551. $num = substr($num, 0, 12);
  552. if ($this->dao->count(['verify_code' => $num])) {
  553. return $this->getStoreCode();
  554. }
  555. return $num;
  556. }
  557. /**
  558. * 获取修改配送信息表单结构
  559. * @param int $id
  560. * @return array
  561. * @throws \FormBuilder\Exception\FormBuilderException
  562. */
  563. public function distributionForm(int $id)
  564. {
  565. if (!$orderInfo = $this->dao->get($id))
  566. throw new ValidateException('订单不存在');
  567. $f[] = Form::input('order_id', '订单号', $orderInfo->getData('order_id'))->disabled(1);
  568. switch ($orderInfo['delivery_type']) {
  569. case 'send':
  570. $f[] = Form::input('delivery_name', '送货人姓名', $orderInfo->getData('delivery_name'))->required('请输入送货人姓名');
  571. $f[] = Form::input('delivery_id', '送货人电话', $orderInfo->getData('delivery_id'))->required('请输入送货人电话');
  572. break;
  573. case 'express':
  574. /** @var ExpressServices $expressServices */
  575. $expressServices = app()->make(ExpressServices::class);
  576. $f[] = Form::select('delivery_name', '快递公司', (string)$orderInfo->getData('delivery_name'))->setOptions(array_map(function ($item) {
  577. $item['value'] = $item['label'];
  578. return $item;
  579. }, $expressServices->expressSelectForm(['is_show' => 1])))->required('请选择快递公司');
  580. $f[] = Form::input('delivery_id', '快递单号', $orderInfo->getData('delivery_id'))->required('请填写快递单号');
  581. break;
  582. }
  583. return create_form('配送信息', $f, $this->url('/marketing/integral/order/distribution/' . $id), 'PUT');
  584. }
  585. /**
  586. * 用户订单收货
  587. * @param $uni
  588. * @param $uid
  589. * @return bool
  590. */
  591. public function takeOrder(string $order_id, int $uid)
  592. {
  593. $order = $this->dao->getUserOrderDetail($order_id, $uid);
  594. if (!$order) {
  595. throw new ValidateException('订单不存在!');
  596. }
  597. if ($order['status'] != 2) {
  598. throw new ValidateException('订单状态错误!');
  599. }
  600. $order->status = 3;
  601. /** @var StoreIntegralOrderStatusServices $statusService */
  602. $statusService = app()->make(StoreIntegralOrderStatusServices::class);
  603. $res = $order->save() && $statusService->save([
  604. 'oid' => $order['id'],
  605. 'change_type' => 'user_take_delivery',
  606. 'change_message' => '用户已收货',
  607. 'change_time' => time()
  608. ]);
  609. if (!$res) {
  610. throw new ValidateException('收货失败');
  611. }
  612. return $order;
  613. }
  614. /**
  615. * 修改配送信息
  616. * @param int $id 订单id
  617. * @return mixed
  618. */
  619. public function updateDistribution(int $id, array $data)
  620. {
  621. $order = $this->dao->get($id);
  622. if (!$order) {
  623. throw new ValidateException('数据不存在!');
  624. }
  625. switch ($order['delivery_type']) {
  626. case 'send':
  627. if (!$data['delivery_name']) {
  628. throw new ValidateException('请输入送货人姓名');
  629. }
  630. if (!$data['delivery_id']) {
  631. throw new ValidateException('请输入送货人电话号码');
  632. }
  633. if (!check_phone($data['delivery_id'])) {
  634. throw new ValidateException('请输入正确的送货人电话号码');
  635. }
  636. break;
  637. case 'express':
  638. if (!$data['delivery_name']) {
  639. throw new ValidateException('请选择快递公司');
  640. }
  641. if (!$data['delivery_id']) {
  642. throw new ValidateException('请输入快递单号');
  643. }
  644. break;
  645. default:
  646. throw new ValidateException('未发货,请先发货再修改配送信息');
  647. break;
  648. }
  649. /** @var StoreIntegralOrderStatusServices $statusService */
  650. $statusService = app()->make(StoreIntegralOrderStatusServices::class);
  651. $statusService->save([
  652. 'oid' => $id,
  653. 'change_type' => 'distribution',
  654. 'change_message' => '修改发货信息为' . $data['delivery_name'] . '号' . $data['delivery_id'],
  655. 'change_time' => time()
  656. ]);
  657. return $this->dao->update($id, $data);
  658. }
  659. }