OutStoreOrderServices.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. unset($order['pink_id'], $order['refund_type']);
  203. return $order;
  204. }
  205. /**
  206. * 格式化订单商品
  207. * @param array $cartInfo
  208. * @param array $list
  209. * @return array
  210. */
  211. public function tidyCartList(array $cartInfo, array $list, $cartId = 0): array
  212. {
  213. $list[] = [
  214. 'cart_id' => $cartId,
  215. 'store_name' => $cartInfo['productInfo']['store_name'] ?? '',
  216. 'suk' => $cartInfo['productInfo']['attrInfo']['suk'] ?? '',
  217. 'image' => $cartInfo['productInfo']['attrInfo']['image'] ?: $cartInfo['productInfo']['image'],
  218. 'price' => sprintf("%.2f", $cartInfo['truePrice'] ?? '0.00'),
  219. 'cart_num' => $cartInfo['cart_num'] ?? 0,
  220. 'surplus_num' => $cartInfo['surplus_num'] ?? 0,
  221. 'refund_num' => $cartInfo['refund_num'] ?? 0
  222. ];
  223. return $list;
  224. }
  225. /**
  226. * 获取订单可以拆分商品信息
  227. * @param string $orderId 订单号
  228. * @return array
  229. */
  230. public function getCartList(string $orderId): array
  231. {
  232. $order = $this->dao->get(['order_id' => $orderId]);
  233. if (!$order) {
  234. throw new ApiException(400118);
  235. }
  236. $list = [];
  237. /** @var StoreOrderCartInfoServices $services */
  238. $services = app()->make(StoreOrderCartInfoServices::class);
  239. $carts = $services->getSplitCartList((int)$order['id']);
  240. foreach ($carts as $key => $cart) {
  241. $list = $this->tidyCartList($cart['cart_info'], $list, $key);
  242. }
  243. return $list;
  244. }
  245. /**
  246. * 订单收货
  247. * @param string $orderId 订单号
  248. * @return bool
  249. * @throws \think\db\exception\DataNotFoundException
  250. * @throws \think\db\exception\DbException
  251. * @throws \think\db\exception\ModelNotFoundException
  252. */
  253. public function receive(string $orderId): bool
  254. {
  255. $order = $this->dao->get(['order_id' => $orderId]);
  256. if (!$order) {
  257. throw new ApiException(400118);
  258. }
  259. if ($order['status'] == 2) {
  260. throw new ApiException(400114);
  261. }
  262. if (($order['paid'] == 1 && $order['status'] == 1) || $order['pay_type'] == 'offline') {
  263. $data['status'] = 2;
  264. } else {
  265. throw new ApiException(400115);
  266. }
  267. if (!$this->dao->update($order['id'], $data)) {
  268. throw new ApiException(400116);
  269. }
  270. /** @var StoreOrderTakeServices $takeServices */
  271. $takeServices = app()->make(StoreOrderTakeServices::class);
  272. if (!$takeServices->storeProductOrderUserTakeDelivery($order)) {
  273. throw new ApiException(400116);
  274. }
  275. return true;
  276. }
  277. /**
  278. * 发货
  279. * @param string $orderId 订单号
  280. * @param array $data
  281. * @return array
  282. * @throws \think\db\exception\DataNotFoundException
  283. * @throws \think\db\exception\DbException
  284. * @throws \think\db\exception\ModelNotFoundException
  285. */
  286. public function delivery(string $orderId, array $data)
  287. {
  288. $orderInfo = $this->dao->get(['order_id' => $orderId]);
  289. if (!$orderInfo) {
  290. throw new ApiException(400470);
  291. }
  292. /** @var StoreOrderDeliveryServices $deliveryServices */
  293. $deliveryServices = app()->make(StoreOrderDeliveryServices::class);
  294. return $deliveryServices->delivery((int)$orderInfo['id'], $data);
  295. }
  296. /**
  297. * 订单拆单发送货
  298. * @param string $orderId 订单号
  299. * @param array $data
  300. * @return bool
  301. * @throws \think\db\exception\DataNotFoundException
  302. * @throws \think\db\exception\DbException
  303. * @throws \think\db\exception\ModelNotFoundException
  304. */
  305. public function splitDelivery(string $orderId, array $data): bool
  306. {
  307. $orderInfo = $this->dao->get(['order_id' => $orderId]);
  308. if (!$orderInfo) {
  309. throw new ApiException(400470);
  310. }
  311. /** @var StoreOrderDeliveryServices $deliveryServices */
  312. $deliveryServices = app()->make(StoreOrderDeliveryServices::class);
  313. return $deliveryServices->splitDelivery((int)$orderInfo['id'], $data);
  314. }
  315. /**
  316. * 设置发票
  317. * @param string $orderId 订单号
  318. * @param array $data
  319. * @return bool
  320. * @throws \think\db\exception\DataNotFoundException
  321. * @throws \think\db\exception\DbException
  322. * @throws \think\db\exception\ModelNotFoundException
  323. */
  324. public function setInvoice(string $orderId, array $data): bool
  325. {
  326. $orderInfo = $this->dao->get(['order_id' => $orderId], ['id'], ['invoice']);
  327. if (!$orderInfo) {
  328. throw new AdminException(400118);
  329. }
  330. if (!$orderInfo->invoice || !$invoiceId = $orderInfo->invoice->id) {
  331. throw new ApiException(100026);
  332. }
  333. /** @var StoreOrderInvoiceServices $invoiceServices */
  334. $invoiceServices = app()->make(StoreOrderInvoiceServices::class);
  335. return $invoiceServices->setInvoice($invoiceId, $data);
  336. }
  337. /**
  338. * 修改配送信息
  339. * @param string $orderId 订单号
  340. * @param array $data
  341. * @return mixed
  342. */
  343. public function updateDistribution(string $orderId, array $data)
  344. {
  345. $orderInfo = $this->dao->get(['order_id' => $orderId]);
  346. if (!$orderInfo) {
  347. throw new AdminException(400118);
  348. }
  349. /** @var StoreOrderDeliveryServices $deliveryServices */
  350. $deliveryServices = app()->make(StoreOrderDeliveryServices::class);
  351. return $deliveryServices->updateDistribution($orderInfo['id'], $data);
  352. }
  353. /**
  354. * 订单推送
  355. * @param int $id
  356. * @param string $pushUrl
  357. * @return bool
  358. */
  359. public function orderCreatePush(int $id, string $pushUrl): bool
  360. {
  361. $orderInfo = $this->getInfo('', $id);
  362. return out_push($pushUrl, $orderInfo, '订单');
  363. }
  364. /**
  365. * 支付推送
  366. * @param int $id
  367. * @param string $pushUrl
  368. * @return bool
  369. */
  370. public function paySuccessPush(int $id, string $pushUrl): bool
  371. {
  372. $orderInfo = $this->getInfo('', $id);
  373. return out_push($pushUrl, $orderInfo, '订单支付');
  374. }
  375. }