OutStoreOrderServices.php 16 KB

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