123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- namespace app\services\order;
- use app\dao\order\StoreOrderPromotionsDao;
- use app\services\BaseServices;
- use crmeb\traits\ServicesTrait;
- use think\exception\ValidateException;
- class StoreOrderPromotionsServices extends BaseServices
- {
- use ServicesTrait;
-
- public function __construct(StoreOrderPromotionsDao $dao)
- {
- $this->dao = $dao;
- }
-
- public function setPromotionsDetail(int $uid, int $oid, array $cartList, array $promotionsList)
- {
- $group = [];
- if ($promotionsList) {
- $time = time();
- foreach ($cartList as $key => $cart) {
- foreach ($promotionsList as $promotions) {
- $details = $promotions['details'] ?? [];
- $unique = $cart['product_attr_unique'] ?? '';
- if ($details && isset($details[$unique]['promotions_true_price'])) {
- $group[] = [
- 'oid' => $oid,
- 'uid' => $uid,
- 'product_id' => $cart['productInfo']['id'],
- 'promotions_id' => $promotions['id'],
- 'promotions_price' => bcmul((string)($details[$unique]['promotions_true_price'] ?? 0), (string)$cart['cart_num'], 2),
- 'add_time' => $time
- ];
- }
- }
- }
- }
- if ($group) {
- return $this->dao->saveAll($group);
- }
- return true;
- }
-
-
- public function getOrderPromotionsDetail(int $oid)
- {
- $result = $this->dao->getPromotionsDetailList(['oid' => $oid], '*,sum(`promotions_price`) as promotions_price', ['promotions' => function($query) {
- $query->field('id,name,title,desc')->bind([
- 'promotions_type' => 'promotions_type',
- 'name' => 'name',
- 'title' => 'title',
- 'desc' => 'desc'
- ]);
- }], 'promotions_id');
- if ($result) {
- $typeArr = array_column($result, 'promotions_type');
- array_multisort($typeArr, SORT_ASC, $result);
- }
- return $result;
- }
-
- public function splitOrderPromotions(int $oid)
- {
-
- $storeOrderServices = app()->make(StoreOrderServices::class);
- $orderInfo = $storeOrderServices->getOne(['id' => $oid, 'is_del' => 0]);
- if (!$orderInfo) {
- throw new ValidateException('订单不存在');
- }
- $promotions_give = [];
- $promotions = [];
- if (isset($orderInfo['promotions_give']) && $orderInfo['promotions_give']) {
- $promotions_give = is_string($orderInfo['promotions_give']) ? json_decode($promotions_give, true) : $orderInfo['promotions_give'];
- }
- $promotions = $promotions_give['promotions'] ?? [];
- $pid = $orderInfo['pid'] > 0 ? $orderInfo['pid'] : $orderInfo['id'];
-
- $orderPromotions = $this->dao->getPromotionsDetailList(['order_id' => $oid]);
-
- $spliteOrder = $storeOrderServices->getColumn(['pid' => $pid, 'is_system_del' => 0], 'id,order_id');
- if ($spliteOrder && $orderPromotions && $promotions) {
-
- $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
- $data_all = $data = [];
- $data['uid'] = $orderInfo['uid'];
- $data['add_time'] = time();
- foreach ($spliteOrder as $order) {
- $cartInfo = $storeOrderCartInfoServices->getColumn(['oid' => $order['id']], 'id,product_id,cart_num,cart_info');
- if (!$cartInfo) {
- continue;
- }
- $data['oid'] = $order['id'];
- foreach ($cartInfo as $key => $cart) {
- $data['product_id'] = $cart['product_id'];
- $_info = is_string($cart['cart_info']) ? json_decode($cart['cart_info'], true) : $cart['cart_info'];
- $unique = $_info['product_attr_unique'] ?? '';
- foreach ($promotions as $key => $info) {
- if (isset($info['details'][$unique])) {
- $data['promotions_id'] = $info['id'];
- $data['promotions_price'] = floatval(bcmul((string)($info['details'][$unique]['promotions_true_price'] ?? 0), (string)$cart['cart_num'], 2));
- $data_all[] = $data;
- }
- }
- }
- }
- if ($data_all) {
- $this->transaction(function () use ($data_all, $spliteOrder) {
- $this->dao->delete(['oid' => array_column($spliteOrder, 'id')]);
- $this->dao->saveAll($data_all);
-
- });
- }
- }
- return true;
- }
-
-
- public function applyRefundOrderPromotions(int $oid, array $cartInfo = [])
- {
-
- $storeOrderServices = app()->make(StoreOrderServices::class);
- $orderInfo = $storeOrderServices->getOne(['id' => $oid, 'is_del' => 0]);
- if (!$orderInfo) {
- return [];
- }
- $promotions_give = [];
- $promotions = [];
- if (isset($orderInfo['promotions_give']) && $orderInfo['promotions_give']) {
- $promotions_give = is_string($orderInfo['promotions_give']) ? json_decode($promotions_give, true) : $orderInfo['promotions_give'];
- }
- $promotions = $promotions_give['promotions'] ?? [];
- if (!$cartInfo || !$promotions) {
- return [];
- }
- $data_all = [];
- foreach ($promotions as $key => $info) {
- $data = [];
- foreach ($cartInfo as $key => $cart) {
- if (isset($info['details'][$cart['product_id']])) {
- $data['promotions_id'] = $info['id'];
- $data['promotions_type'] = $info['promotions_type'];
- $data['name'] = $info['name'];
- $data['title'] = $info['title'];
- $data['desc'] = $info['desc'];
- $data['promotions_price'] = bcadd((string)($data['promotions_price'] ?? 0), (string)bcmul((string)($info['details'][$cart['product_id']]['promotions_true_price'] ?? 0), (string)$cart['cart_num'], 2), 2);
- }
- }
- if ($data) {
- $data_all[] = $data;
- }
- }
- if ($data_all) {
- $typeArr = array_column($data_all, 'promotions_type');
- array_multisort($typeArr, SORT_ASC, $data_all);
- }
- return $data_all;
- }
- }
|