OutStoreOrderServices.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\order;
  12. use app\dao\order\StoreOrderDao;
  13. use app\services\activity\combination\StorePinkServices;
  14. use app\services\BaseServices;
  15. use app\services\pay\PayServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\exceptions\ApiException;
  18. /**
  19. * Class OutStoreOrderServices
  20. * @package app\services\order
  21. * @method getOrderIdsCount(array $ids) 获取订单id下没有删除的订单数量
  22. * @method StoreOrderDao getUserOrderDetail(string $key, int $uid, array $with) 获取订单详情
  23. * @method chartTimePrice($start, $stop) 获取当前时间到指定时间的支付金额 管理员
  24. * @method chartTimeNumber($start, $stop) 获取当前时间到指定时间的支付订单数 管理员
  25. * @method together(array $where, string $field, string $together = 'sum') 聚合查询
  26. * @method getBuyCount($uid, $type, $typeId) 获取用户已购买此活动商品的个数
  27. * @method getDistinctCount(array $where, $field, ?bool $search = true)
  28. * @method getTrendData($time, $type, $timeType, $str) 用户趋势
  29. * @method getRegion($time, $channelType) 地域统计
  30. * @method getProductTrend($time, $timeType, $field, $str) 商品趋势
  31. */
  32. class OutStoreOrderServices extends BaseServices
  33. {
  34. /**
  35. * 发货类型
  36. * @var string[]
  37. */
  38. public $deliveryType = ['send' => '商家配送', 'express' => '快递配送', 'fictitious' => '虚拟发货', 'delivery_part_split' => '拆分部分发货', 'delivery_split' => '拆分发货完成'];
  39. /**
  40. * StoreOrderProductServices constructor.
  41. * @param StoreOrderDao $dao
  42. */
  43. public function __construct(StoreOrderDao $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)
  56. {
  57. $where['order_status'] = $where['status'];
  58. unset($where['status']);
  59. if (!is_numeric($where['paid'])) {
  60. $where['paid'] = -1;
  61. }
  62. [$page, $limit] = $this->getPageValue();
  63. $field = ['id', 'pid', 'order_id', 'trade_no', 'uid', 'freight_price', 'real_name', 'user_phone', 'user_address', 'total_num',
  64. 'total_price', 'total_postage', 'pay_price', 'coupon_price', 'deduction_price', 'paid', 'pay_time', 'pay_type', 'add_time',
  65. 'shipping_type', 'status', 'refund_status', 'delivery_name', 'delivery_code', 'delivery_id'];
  66. $data = $this->dao->getOutOrderList($where, $field, $page, $limit);
  67. $count = $this->dao->count($where);
  68. $list = $this->tidyOrderList($data);
  69. return compact('list', 'count');
  70. }
  71. /**
  72. * 数据转换
  73. * @param array $data
  74. * @return array
  75. */
  76. public function tidyOrderList(array $data)
  77. {
  78. /** @var StoreOrderCartInfoServices $services */
  79. $services = app()->make(StoreOrderCartInfoServices::class);
  80. foreach ($data as &$item) {
  81. $list = [];
  82. $carts = $services->getOrderCartInfo((int)$item['id']);
  83. foreach ($carts as $key => $cart) {
  84. $list = $this->tidyCartList($cart['cart_info'], $list, $key);
  85. }
  86. $item['pay_type_name'] = PayServices::PAY_TYPE[$item['pay_type']] ?? '其他方式';
  87. $item['items'] = $list;
  88. unset($item['refund_status'], $item['shipping_type']);
  89. }
  90. return $data;
  91. }
  92. /**
  93. * 订单详情
  94. * @param string $orderId 订单号
  95. * @param int $id 订单ID
  96. * @return mixed
  97. */
  98. public function getInfo(string $orderId = '', int $id = 0)
  99. {
  100. $field = ['id', 'pid', 'order_id', 'trade_no', 'uid', 'freight_price', 'real_name', 'user_phone', 'user_address', 'total_num',
  101. 'total_price', 'total_postage', 'pay_price', 'coupon_price', 'deduction_price', 'paid', 'pay_time', 'pay_type', 'add_time',
  102. 'shipping_type', 'status', 'refund_status', 'delivery_name', 'delivery_code', 'delivery_id', 'refund_type', 'delivery_type', 'pink_id', 'use_integral', 'back_integral'];
  103. if ($id > 0) {
  104. $where = $id;
  105. } else {
  106. $where = ['order_id' => $orderId];
  107. }
  108. if (!$orderInfo = $this->dao->get($where, $field, ['invoice'])) {
  109. throw new ApiException(400118);
  110. }
  111. if (!$orderInfo['invoice']) {
  112. $orderInfo['invoice'] = new \StdClass();
  113. } else {
  114. $orderInfo['invoice']->hidden(['uid', 'category', 'id', 'order_id', 'add_time']);
  115. }
  116. $orderInfo = $this->tidyOrder($orderInfo->toArray(), true);
  117. //核算优惠金额
  118. $vipTruePrice = array_column($orderInfo['items'], 'vip_sum_truePrice');
  119. $vipTruePrice = round(array_sum($vipTruePrice), 2);
  120. $orderInfo['vip_true_price'] = sprintf("%.2f", $vipTruePrice ?: '0.00');
  121. $orderInfo['total_price'] = bcadd($orderInfo['total_price'], $orderInfo['vip_true_price'], 2);
  122. return $orderInfo;
  123. }
  124. /**
  125. * 订单详情数据格式化
  126. * @param $order
  127. * @param bool $detail 是否需要订单商品详情
  128. * @return mixed
  129. */
  130. public function tidyOrder($order, bool $detail = false)
  131. {
  132. if ($detail == true && isset($order['id'])) {
  133. /** @var StoreOrderCartInfoServices $cartServices */
  134. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  135. $carts = $cartServices->getOrderCartInfo((int)$order['id']);
  136. $list = [];
  137. foreach ($carts as $key => $cart) {
  138. $list = $this->tidyCartList($cart['cart_info'], $list, $key);
  139. }
  140. $order['items'] = $list;
  141. }
  142. $order['pay_type_name'] = PayServices::PAY_TYPE[$order['pay_type']] ?? '其他方式';
  143. // if (!$order['paid'] && $order['pay_type'] == 'offline' && !$order['status'] >= 2) {
  144. // $order['status_name'] = '线下付款,未支付';
  145. // } else if (!$order['paid']) {
  146. // $order['status_name'] = '未支付';
  147. // } else if ($order['status'] == 4) {
  148. // if ($order['delivery_type'] == 'send') {
  149. // $order['status_name'] = '待收货';
  150. // } elseif ($order['delivery_type'] == 'express') {
  151. // $order['status_name'] = '待收货';
  152. // } elseif ($order['delivery_type'] == 'split') {//拆分发货
  153. // $order['status_name'] = '待收货';
  154. // } else {
  155. // $order['status_name'] = '待收货';
  156. // }
  157. // } else if ($order['refund_status'] == 1) {
  158. // if (in_array($order['refund_type'], [0, 1, 2])) {
  159. // $order['status_name'] = '申请退款中';
  160. // } elseif ($order['refund_type'] == 4) {
  161. // $order['status_name'] = '申请退款中';
  162. // } elseif ($order['refund_type'] == 5) {
  163. // $order['status_name'] = '申请退款中';
  164. // }
  165. // } else if ($order['refund_status'] == 2 || $order['refund_type'] == 6) {
  166. // $order['status_name'] = '已退款';
  167. // } else if ($order['refund_status'] == 3) {
  168. // $order['status_name'] = '部分退款(子订单)';
  169. // } else if ($order['refund_status'] == 4) {
  170. // $order['status_name'] = '子订单已全部申请退款中';
  171. // } else if (!$order['status']) {
  172. // if ($order['pink_id']) {
  173. // /** @var StorePinkServices $pinkServices */
  174. // $pinkServices = app()->make(StorePinkServices::class);
  175. // if ($pinkServices->getCount(['id' => $order['pink_id'], 'status' => 1])) {
  176. // $order['status_name'] = '拼团中';
  177. // } else {
  178. // $order['status_name'] = '未发货';
  179. // }
  180. // } else {
  181. // if ($order['shipping_type'] === 1) {
  182. // $order['status_name'] = '未发货';
  183. // } else {
  184. // $order['status_name'] = '待核销';
  185. // }
  186. // }
  187. // } else if ($order['status'] == 1) {
  188. // if ($order['delivery_type'] == 'send') {//TODO 送货
  189. // $order['status_name'] = '待收货';
  190. // } elseif ($order['delivery_type'] == 'express') {//TODO 发货
  191. // $order['status_name'] = '待收货';
  192. // } elseif ($order['delivery_type'] == 'split') {//拆分发货
  193. // $order['status_name'] = '待收货';
  194. // } else {
  195. // $order['status_name'] = '待收货';
  196. // }
  197. // } else if ($order['status'] == 2) {
  198. // $order['status_name'] = '待评价';
  199. // } else if ($order['status'] == 3) {
  200. // $order['status_name'] = '交易完成';
  201. // }
  202. // 处理未支付状态
  203. if (!$order['paid']) {
  204. if ($order['pay_type'] == 'offline') {
  205. $order['status_name'] = '线下付款,未支付';
  206. } else {
  207. $order['status_name'] = '未支付';
  208. }
  209. } elseif ($order['status'] == 4 || $order['status'] == 1) { // 合并待收货逻辑
  210. $order['status_name'] = '待收货';
  211. } elseif ($order['refund_status'] == 1) {
  212. if (in_array($order['refund_type'], [0, 1, 2, 4, 5])) {
  213. $order['status_name'] = '申请退款中';
  214. }
  215. } elseif ($order['refund_status'] == 2 || $order['refund_type'] == 6) {
  216. $order['status_name'] = '已退款';
  217. } elseif ($order['refund_status'] == 3) {
  218. $order['status_name'] = '部分退款(子订单)';
  219. } elseif ($order['refund_status'] == 4) {
  220. $order['status_name'] = '子订单全部申请退款中';
  221. } elseif (!$order['status']) {
  222. if ($order['pink_id']) {
  223. /** @var StorePinkServices $pinkServices */
  224. $pinkServices = app()->make(StorePinkServices::class);
  225. if ($pinkServices->getCount(['id' => $order['pink_id'], 'status' => 1])) {
  226. $order['status_name'] = '拼团中';
  227. } else {
  228. $order['status_name'] = '未发货';
  229. }
  230. } else {
  231. if ($order['shipping_type'] === 1) {
  232. $order['status_name'] = '未发货';
  233. } else {
  234. $order['status_name'] = '待核销';
  235. }
  236. }
  237. } elseif ($order['status'] == 2) {
  238. $order['status_name'] = '待评价';
  239. } elseif ($order['status'] == 3) {
  240. $order['status_name'] = '交易完成';
  241. } else {
  242. // 处理未知状态
  243. $order['status_name'] = '未知状态';
  244. }
  245. unset($order['pink_id'], $order['refund_type']);
  246. return $order;
  247. }
  248. /**
  249. * 格式化订单商品
  250. * @param array $cartInfo
  251. * @param array $list
  252. * @return array
  253. */
  254. public function tidyCartList(array $cartInfo, array $list, $cartId = 0): array
  255. {
  256. $list[] = [
  257. 'cart_id' => $cartId,
  258. 'store_name' => $cartInfo['productInfo']['store_name'] ?? '',
  259. 'suk' => $cartInfo['productInfo']['attrInfo']['suk'] ?? '',
  260. 'image' => $cartInfo['productInfo']['attrInfo']['image'] ?: $cartInfo['productInfo']['image'],
  261. 'price' => sprintf("%.2f", $cartInfo['truePrice'] ?? '0.00'),
  262. 'cart_num' => $cartInfo['cart_num'] ?? 0,
  263. 'surplus_num' => $cartInfo['surplus_num'] ?? 0,
  264. 'refund_num' => $cartInfo['refund_num'] ?? 0
  265. ];
  266. return $list;
  267. }
  268. /**
  269. * 获取订单可以拆分商品信息
  270. * @param string $orderId 订单号
  271. * @return array
  272. */
  273. public function getCartList(string $orderId): array
  274. {
  275. $order = $this->dao->get(['order_id' => $orderId]);
  276. if (!$order) {
  277. throw new ApiException(400118);
  278. }
  279. $list = [];
  280. /** @var StoreOrderCartInfoServices $services */
  281. $services = app()->make(StoreOrderCartInfoServices::class);
  282. $carts = $services->getSplitCartList((int)$order['id']);
  283. foreach ($carts as $key => $cart) {
  284. $list = $this->tidyCartList($cart['cart_info'], $list, $key);
  285. }
  286. return $list;
  287. }
  288. /**
  289. * 订单收货
  290. * @param string $orderId 订单号
  291. * @return bool
  292. * @throws \think\db\exception\DataNotFoundException
  293. * @throws \think\db\exception\DbException
  294. * @throws \think\db\exception\ModelNotFoundException
  295. */
  296. public function receive(string $orderId): bool
  297. {
  298. $order = $this->dao->get(['order_id' => $orderId]);
  299. if (!$order) {
  300. throw new ApiException(400118);
  301. }
  302. if ($order['status'] == 2) {
  303. throw new ApiException(400114);
  304. }
  305. if (($order['paid'] == 1 && $order['status'] == 1) || $order['pay_type'] == 'offline') {
  306. $data['status'] = 2;
  307. } else {
  308. throw new ApiException(400115);
  309. }
  310. if (!$this->dao->update($order['id'], $data)) {
  311. throw new ApiException(400116);
  312. }
  313. /** @var StoreOrderTakeServices $takeServices */
  314. $takeServices = app()->make(StoreOrderTakeServices::class);
  315. if (!$takeServices->storeProductOrderUserTakeDelivery($order)) {
  316. throw new ApiException(400116);
  317. }
  318. return true;
  319. }
  320. /**
  321. * 发货
  322. * @param string $orderId 订单号
  323. * @param array $data
  324. * @return array
  325. * @throws \think\db\exception\DataNotFoundException
  326. * @throws \think\db\exception\DbException
  327. * @throws \think\db\exception\ModelNotFoundException
  328. */
  329. public function delivery(string $orderId, array $data)
  330. {
  331. $orderInfo = $this->dao->get(['order_id' => $orderId]);
  332. if (!$orderInfo) {
  333. throw new ApiException(400470);
  334. }
  335. /** @var StoreOrderDeliveryServices $deliveryServices */
  336. $deliveryServices = app()->make(StoreOrderDeliveryServices::class);
  337. return $deliveryServices->delivery((int)$orderInfo['id'], $data);
  338. }
  339. /**
  340. * 订单拆单发送货
  341. * @param string $orderId 订单号
  342. * @param array $data
  343. * @return bool
  344. * @throws \think\db\exception\DataNotFoundException
  345. * @throws \think\db\exception\DbException
  346. * @throws \think\db\exception\ModelNotFoundException
  347. */
  348. public function splitDelivery(string $orderId, array $data): bool
  349. {
  350. $orderInfo = $this->dao->get(['order_id' => $orderId]);
  351. if (!$orderInfo) {
  352. throw new ApiException(400470);
  353. }
  354. /** @var StoreOrderDeliveryServices $deliveryServices */
  355. $deliveryServices = app()->make(StoreOrderDeliveryServices::class);
  356. return $deliveryServices->splitDelivery((int)$orderInfo['id'], $data);
  357. }
  358. /**
  359. * 设置发票
  360. * @param string $orderId 订单号
  361. * @param array $data
  362. * @return bool
  363. * @throws \think\db\exception\DataNotFoundException
  364. * @throws \think\db\exception\DbException
  365. * @throws \think\db\exception\ModelNotFoundException
  366. */
  367. public function setInvoice(string $orderId, array $data): bool
  368. {
  369. $orderInfo = $this->dao->get(['order_id' => $orderId], ['id'], ['invoice']);
  370. if (!$orderInfo) {
  371. throw new AdminException(400118);
  372. }
  373. if (!$orderInfo->invoice || !$invoiceId = $orderInfo->invoice->id) {
  374. throw new ApiException(100026);
  375. }
  376. /** @var StoreOrderInvoiceServices $invoiceServices */
  377. $invoiceServices = app()->make(StoreOrderInvoiceServices::class);
  378. return $invoiceServices->setInvoice($invoiceId, $data);
  379. }
  380. /**
  381. * 修改配送信息
  382. * @param string $orderId 订单号
  383. * @param array $data
  384. * @return mixed
  385. */
  386. public function updateDistribution(string $orderId, array $data)
  387. {
  388. $orderInfo = $this->dao->get(['order_id' => $orderId]);
  389. if (!$orderInfo) {
  390. throw new AdminException(400118);
  391. }
  392. /** @var StoreOrderDeliveryServices $deliveryServices */
  393. $deliveryServices = app()->make(StoreOrderDeliveryServices::class);
  394. return $deliveryServices->updateDistribution($orderInfo['id'], $data);
  395. }
  396. /**
  397. * 订单推送
  398. * @param int $id
  399. * @param string $pushUrl
  400. * @return bool
  401. */
  402. public function orderCreatePush(int $id, string $pushUrl): bool
  403. {
  404. $orderInfo = $this->getInfo('', $id);
  405. return out_push($pushUrl, $orderInfo, '订单');
  406. }
  407. /**
  408. * 支付推送
  409. * @param int $id
  410. * @param string $pushUrl
  411. * @return bool
  412. */
  413. public function paySuccessPush(int $id, string $pushUrl): bool
  414. {
  415. $orderInfo = $this->getInfo('', $id);
  416. return out_push($pushUrl, $orderInfo, '订单支付');
  417. }
  418. }