StoreIntegralOrderServices.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. /** @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 ValidateException('可用积分不足!');
  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 ValidateException('订单生成失败!');
  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 ValidateException('使用积分抵扣失败!');
  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 ValidateException('库存不足!');
  242. }
  243. } catch (\Throwable $e) {
  244. throw new ValidateException('库存不足!');
  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 ValidateException('请先配置小票打印开发者');
  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 ValidateException($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 ValidateException('该商品已下架');
  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 ValidateException('该订单无法删除!');
  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 ValidateException('订单删除失败!');
  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 ValidateException('订单未能查到,不能发货!');
  370. }
  371. if ($orderInfo->is_del) {
  372. throw new ValidateException('订单已删除,不能发货!');
  373. }
  374. if ($orderInfo->status != 1) {
  375. throw new ValidateException('订单已发货请勿重复操作!');
  376. }
  377. $type = (int)$data['type'];
  378. unset($data['type']);
  379. switch ($type) {
  380. case 1:
  381. //发货
  382. $this->orderDeliverGoods($id, $data, $orderInfo);
  383. break;
  384. case 2:
  385. $this->orderDelivery($id, $data, $orderInfo);
  386. break;
  387. case 3:
  388. $this->orderVirtualDelivery($id, $data, $orderInfo);
  389. break;
  390. default:
  391. throw new ValidateException('暂时不支持其他发货类型');
  392. }
  393. return true;
  394. }
  395. /**
  396. * 虚拟发货
  397. * @param int $id
  398. * @param array $data
  399. */
  400. public function orderVirtualDelivery(int $id, array $data)
  401. {
  402. $data['delivery_type'] = 'fictitious';
  403. $data['status'] = 2;
  404. unset($data['sh_delivery_name'], $data['sh_delivery_id'], $data['delivery_name'], $data['delivery_id']);
  405. //保存信息
  406. /** @var StoreIntegralOrderStatusServices $services */
  407. $services = app()->make(StoreIntegralOrderStatusServices::class);
  408. $this->transaction(function () use ($id, $data, $services) {
  409. $this->dao->update($id, $data);
  410. $services->save([
  411. 'oid' => $id,
  412. 'change_type' => 'delivery_fictitious',
  413. 'change_message' => '已虚拟发货',
  414. 'change_time' => time()
  415. ]);
  416. });
  417. }
  418. /**
  419. * 订单配送
  420. * @param int $id
  421. * @param array $data
  422. */
  423. public function orderDelivery(int $id, array $data, $orderInfo)
  424. {
  425. $data['delivery_type'] = 'send';
  426. $data['delivery_name'] = $data['sh_delivery_name'];
  427. $data['delivery_id'] = $data['sh_delivery_id'];
  428. $data['delivery_uid'] = $data['sh_delivery_uid'];
  429. // 获取核销码
  430. $data['verify_code'] = $this->getStoreCode();
  431. unset($data['sh_delivery_name'], $data['sh_delivery_id'], $data['sh_delivery_uid']);
  432. if (!$data['delivery_name']) {
  433. throw new ValidateException('请输入送货人姓名');
  434. }
  435. if (!$data['delivery_id']) {
  436. throw new ValidateException('请输入送货人电话号码');
  437. }
  438. if (!$data['delivery_uid']) {
  439. throw new ValidateException('请输入送货人信息');
  440. }
  441. if (!check_phone($data['delivery_id'])) {
  442. throw new ValidateException('请输入正确的送货人电话号码');
  443. }
  444. $data['status'] = 2;
  445. $orderInfo->delivery_type = $data['delivery_type'];
  446. $orderInfo->delivery_name = $data['delivery_name'];
  447. $orderInfo->delivery_id = $data['delivery_id'];
  448. $orderInfo->status = $data['status'];
  449. /** @var StoreIntegralOrderStatusServices $services */
  450. $services = app()->make(StoreIntegralOrderStatusServices::class);
  451. $this->transaction(function () use ($id, $data, $services) {
  452. $this->dao->update($id, $data);
  453. //记录订单状态
  454. $services->save([
  455. 'oid' => $id,
  456. 'change_type' => 'delivery',
  457. 'change_time' => time(),
  458. 'change_message' => '已配送 发货人:' . $data['delivery_name'] . ' 发货人电话:' . $data['delivery_id']
  459. ]);
  460. });
  461. return true;
  462. }
  463. /**
  464. * 订单快递发货
  465. * @param int $id
  466. * @param array $data
  467. */
  468. public function orderDeliverGoods(int $id, array $data, $orderInfo)
  469. {
  470. if (!$data['delivery_name']) {
  471. throw new ValidateException('请选择快递公司');
  472. }
  473. $data['delivery_type'] = 'express';
  474. if ($data['express_record_type'] == 2) {//电子面单
  475. if (!$data['delivery_code']) {
  476. throw new ValidateException('快递公司编缺失');
  477. }
  478. if (!$data['express_temp_id']) {
  479. throw new ValidateException('请选择电子面单模板');
  480. }
  481. if (!$data['to_name']) {
  482. throw new ValidateException('请填写寄件人姓名');
  483. }
  484. if (!$data['to_tel']) {
  485. throw new ValidateException('请填写寄件人电话');
  486. }
  487. if (!$data['to_addr']) {
  488. throw new ValidateException('请填写寄件人地址');
  489. }
  490. /** @var ServeServices $ServeServices */
  491. $ServeServices = app()->make(ServeServices::class);
  492. $expData['com'] = $data['delivery_code'];
  493. $expData['to_name'] = $orderInfo->real_name;
  494. $expData['to_tel'] = $orderInfo->user_phone;
  495. $expData['to_addr'] = $orderInfo->user_address;
  496. $expData['from_name'] = $data['to_name'];
  497. $expData['from_tel'] = $data['to_tel'];
  498. $expData['from_addr'] = $data['to_addr'];
  499. $expData['siid'] = sys_config('config_export_siid');
  500. $expData['temp_id'] = $data['express_temp_id'];
  501. $expData['count'] = $orderInfo->total_num;
  502. $expData['cargo'] = $orderInfo->store_name . '(' . $orderInfo->suk . ')*' . $orderInfo->total_num;
  503. $expData['order_id'] = $orderInfo->order_id;
  504. if (!sys_config('config_export_open', 0)) {
  505. throw new ValidateException('系统通知:电子面单已关闭,请选择其他发货方式!');
  506. }
  507. $dump = $ServeServices->express()->dump($expData);
  508. $orderInfo->delivery_id = $dump['kuaidinum'];
  509. $data['express_dump'] = json_encode([
  510. 'com' => $expData['com'],
  511. 'from_name' => $expData['from_name'],
  512. 'from_tel' => $expData['from_tel'],
  513. 'from_addr' => $expData['from_addr'],
  514. 'temp_id' => $expData['temp_id'],
  515. 'cargo' => $expData['cargo'],
  516. ]);
  517. $data['delivery_id'] = $dump['kuaidinum'];
  518. } else {
  519. if (!$data['delivery_id']) {
  520. throw new ValidateException('请输入快递单号');
  521. }
  522. $orderInfo->delivery_id = $data['delivery_id'];
  523. }
  524. $data['status'] = 2;
  525. $orderInfo->delivery_type = $data['delivery_type'];
  526. $orderInfo->delivery_name = $data['delivery_name'];
  527. $orderInfo->status = $data['status'];
  528. /** @var StoreIntegralOrderStatusServices $services */
  529. $services = app()->make(StoreIntegralOrderStatusServices::class);
  530. $this->transaction(function () use ($id, $data, $services) {
  531. $res = $this->dao->update($id, $data);
  532. $res = $res && $services->save([
  533. 'oid' => $id,
  534. 'change_time' => time(),
  535. 'change_type' => 'delivery_goods',
  536. 'change_message' => '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']
  537. ]);
  538. if (!$res) {
  539. throw new ValidateException('发货失败:数据保存不成功');
  540. }
  541. });
  542. return true;
  543. }
  544. /**
  545. * 核销订单生成核销码
  546. * @return false|string
  547. */
  548. public function getStoreCode()
  549. {
  550. mt_srand();
  551. list($msec, $sec) = explode(' ', microtime());
  552. $num = time() + mt_rand(10, 999999) . '' . substr($msec, 2, 3);//生成随机数
  553. if (strlen($num) < 12)
  554. $num = str_pad((string)$num, 12, 0, STR_PAD_RIGHT);
  555. else
  556. $num = substr($num, 0, 12);
  557. if ($this->dao->count(['verify_code' => $num])) {
  558. return $this->getStoreCode();
  559. }
  560. return $num;
  561. }
  562. /**
  563. * 获取修改配送信息表单结构
  564. * @param int $id
  565. * @return array
  566. * @throws \FormBuilder\Exception\FormBuilderException
  567. */
  568. public function distributionForm(int $id)
  569. {
  570. if (!$orderInfo = $this->dao->get($id))
  571. throw new ValidateException('订单不存在');
  572. $f[] = Form::input('order_id', '订单号', $orderInfo->getData('order_id'))->disabled(1);
  573. switch ($orderInfo['delivery_type']) {
  574. case 'send':
  575. $f[] = Form::input('delivery_name', '送货人姓名', $orderInfo->getData('delivery_name'))->required('请输入送货人姓名');
  576. $f[] = Form::input('delivery_id', '送货人电话', $orderInfo->getData('delivery_id'))->required('请输入送货人电话');
  577. break;
  578. case 'express':
  579. /** @var ExpressServices $expressServices */
  580. $expressServices = app()->make(ExpressServices::class);
  581. $f[] = Form::select('delivery_name', '快递公司', (string)$orderInfo->getData('delivery_name'))->setOptions(array_map(function ($item) {
  582. $item['value'] = $item['label'];
  583. return $item;
  584. }, $expressServices->expressSelectForm(['is_show' => 1])))->required('请选择快递公司');
  585. $f[] = Form::input('delivery_id', '快递单号', $orderInfo->getData('delivery_id'))->required('请填写快递单号');
  586. break;
  587. }
  588. return create_form('配送信息', $f, $this->url('/marketing/integral/order/distribution/' . $id), 'PUT');
  589. }
  590. /**
  591. * 用户订单收货
  592. * @param $uni
  593. * @param $uid
  594. * @return bool
  595. */
  596. public function takeOrder(string $order_id, int $uid)
  597. {
  598. $order = $this->dao->getUserOrderDetail($order_id, $uid);
  599. if (!$order) {
  600. throw new ValidateException('订单不存在!');
  601. }
  602. if ($order['status'] != 2) {
  603. throw new ValidateException('订单状态错误!');
  604. }
  605. $order->status = 3;
  606. /** @var StoreIntegralOrderStatusServices $statusService */
  607. $statusService = app()->make(StoreIntegralOrderStatusServices::class);
  608. $res = $order->save() && $statusService->save([
  609. 'oid' => $order['id'],
  610. 'change_type' => 'user_take_delivery',
  611. 'change_message' => '用户已收货',
  612. 'change_time' => time()
  613. ]);
  614. if (!$res) {
  615. throw new ValidateException('收货失败');
  616. }
  617. return $order;
  618. }
  619. /**
  620. * 修改配送信息
  621. * @param int $id 订单id
  622. * @return mixed
  623. */
  624. public function updateDistribution(int $id, array $data)
  625. {
  626. $order = $this->dao->get($id);
  627. if (!$order) {
  628. throw new ValidateException('数据不存在!');
  629. }
  630. switch ($order['delivery_type']) {
  631. case 'send':
  632. if (!$data['delivery_name']) {
  633. throw new ValidateException('请输入送货人姓名');
  634. }
  635. if (!$data['delivery_id']) {
  636. throw new ValidateException('请输入送货人电话号码');
  637. }
  638. if (!check_phone($data['delivery_id'])) {
  639. throw new ValidateException('请输入正确的送货人电话号码');
  640. }
  641. break;
  642. case 'express':
  643. if (!$data['delivery_name']) {
  644. throw new ValidateException('请选择快递公司');
  645. }
  646. if (!$data['delivery_id']) {
  647. throw new ValidateException('请输入快递单号');
  648. }
  649. break;
  650. default:
  651. throw new ValidateException('未发货,请先发货再修改配送信息');
  652. }
  653. /** @var StoreIntegralOrderStatusServices $statusService */
  654. $statusService = app()->make(StoreIntegralOrderStatusServices::class);
  655. $statusService->save([
  656. 'oid' => $id,
  657. 'change_type' => 'distribution',
  658. 'change_message' => '修改发货信息为' . $data['delivery_name'] . '号' . $data['delivery_id'],
  659. 'change_time' => time()
  660. ]);
  661. return $this->dao->update($id, $data);
  662. }
  663. }