diff --git a/app/common/dao/system/merchant/MerchantIntentionDao.php b/app/common/dao/system/merchant/MerchantIntentionDao.php index 759fa0a3..be3e74c3 100644 --- a/app/common/dao/system/merchant/MerchantIntentionDao.php +++ b/app/common/dao/system/merchant/MerchantIntentionDao.php @@ -32,11 +32,7 @@ class MerchantIntentionDao extends BaseDao })->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) { $query->where('status', (int)$where['status']); })->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) { - if(is_array($where['type'])){ - $query->whereOr('type',$where['type'][0])->whereOr('type',$where['type'][1]); - }else{ - $query->where('type', (int)$where['type']); - } + $query->whereIn('type',$where['type']); })->when(isset($where['mer_intention_id']) && $where['mer_intention_id'] !== '', function ($query) use ($where) { $query->where('mer_intention_id', $where['mer_intention_id']); })->when(isset($where['category_id']) && $where['category_id'] !== '', function ($query) use ($where) { diff --git a/app/common/repositories/store/order/StoreOrderCreateRepository.php b/app/common/repositories/store/order/StoreOrderCreateRepository.php index 24f54394..9d0dbe6e 100644 --- a/app/common/repositories/store/order/StoreOrderCreateRepository.php +++ b/app/common/repositories/store/order/StoreOrderCreateRepository.php @@ -131,7 +131,11 @@ class StoreOrderCreateRepository extends StoreOrderRepository $noDeliver = false; $presellType = 0; $fn = []; - $enabledPlatformCoupon = !$order_type; + if (in_array($source, [0, 2, 103])) { + $enabledPlatformCoupon = true; + } else { + $enabledPlatformCoupon = false; + } $order_total_postage = 0; $platformCoupon = []; @@ -479,6 +483,161 @@ class StoreOrderCreateRepository extends StoreOrderRepository $usePlatformCouponId = 0; $total_platform_coupon_price = 0; + //计算平台券优惠金额 + // if ($total_true_price > 0) { + $StoreCouponUser = app()->make(StoreCouponUserRepository::class); + $platformCoupon = $StoreCouponUser->validUserPlatformCoupon($uid); + if ($enabledPlatformCoupon && count($platformCoupon)) { + + $catePriceLst = []; + $storePriceLst = []; + $_cartNum = 0; + + foreach ($merchantCartList as &$merchantCart) { + if ($merchantCart['order']['true_price'] <= 0) continue; + foreach ($merchantCart['list'] as &$cart) { + $_cartNum++; + if ($cart['product']['cate_id']) { + if (!isset($catePriceLst[$cart['product']['cate_id']])) { + $catePriceLst[$cart['product']['cate_id']] = ['price' => 0, 'cart' => []]; + } + $catePriceLst[$cart['product']['cate_id']]['price'] = bcadd($catePriceLst[$cart['product']['cate_id']]['price'], $cart['true_price']); + $catePriceLst[$cart['product']['cate_id']]['cart'][] = &$cart; + } + } + unset($cart); + $storePriceLst[$merchantCart['mer_id']] = [ + 'price' => $merchantCart['order']['true_price'], + 'num' => count($merchantCart['list']) + ]; + } + unset($merchantCart); + $flag = false; + $platformCouponRate = null; + + foreach ($platformCoupon as &$coupon) { + $coupon['checked'] = false; + //通用券 + if ($coupon['coupon']['type'] === StoreCouponRepository::TYPE_PLATFORM_ALL) { + $coupon['disabled'] = $total_true_price <= 0 || $coupon['use_min_price'] > $total_true_price; + if (!$platformCouponRate && !$coupon['disabled'] && !$flag && ((!$usePlatformCouponId && !$usePlatformCouponFlag) || $usePlatformCouponId == $coupon['coupon_user_id'])) { + $platformCouponRate = [ + 'id' => $coupon['coupon_user_id'], + 'type' => $coupon['coupon']['type'], + 'price' => $total_true_price, + 'coupon_price' => $coupon['coupon_price'], + 'use_count' => $_cartNum, + 'check' => function ($cart) { + return true; + } + ]; + $coupon['checked'] = true; + $flag = true; + } + //品类券 + } else if ($coupon['coupon']['type'] === StoreCouponRepository::TYPE_PLATFORM_CATE) { + $_price = 0; + $_use_count = 0; + $cateIds = $coupon['product']->column('product_id'); + $allCateIds = array_unique(array_merge(app()->make(StoreCategoryRepository::class)->allChildren($cateIds), $cateIds)); + $flag2 = true; + foreach ($allCateIds as $cateId) { + if (isset($catePriceLst[$cateId])) { + $_price = bcadd($catePriceLst[$cateId]['price'], $_price, 2); + $_use_count += count($catePriceLst[$cateId]['cart']); + $flag2 = false; + } + } + $coupon['disabled'] = $flag2 || $coupon['use_min_price'] > $_price; + //品类券可用 + if (!$platformCouponRate && !$coupon['disabled'] && !$flag && !$flag2 && ((!$usePlatformCouponId && !$usePlatformCouponFlag) || $usePlatformCouponId == $coupon['coupon_user_id'])) { + $platformCouponRate = [ + 'id' => $coupon['coupon_user_id'], + 'type' => $coupon['coupon']['type'], + 'price' => $_price, + 'use_cate' => $allCateIds, + 'coupon_price' => $coupon['coupon_price'], + 'use_count' => $_use_count, + 'check' => function ($cart) use ($allCateIds) { + return in_array($cart['product']['cate_id'], $allCateIds); + } + ]; + $coupon['checked'] = true; + $flag = true; + } + //跨店券 + } else if ($coupon['coupon']['type'] === StoreCouponRepository::TYPE_PLATFORM_STORE) { + $_price = 0; + $_use_count = 0; + $flag2 = true; + foreach ($coupon['product'] as $item) { + $merId = $item['product_id']; + if (isset($storePriceLst[$merId])) { + $_price = bcadd($storePriceLst[$merId]['price'], $_price, 2); + $_use_count += $storePriceLst[$merId]['num']; + $flag2 = false; + } + } + $coupon['disabled'] = $flag2 || $coupon['use_min_price'] > $_price; + //店铺券可用 + if (!$platformCouponRate && !$coupon['disabled'] && !$flag && !$flag2 && ((!$usePlatformCouponId && !$usePlatformCouponFlag) || $usePlatformCouponId == $coupon['coupon_user_id'])) { + $_merIds = $coupon['product']->column('product_id'); + $platformCouponRate = [ + 'id' => $coupon['coupon_user_id'], + 'type' => $coupon['coupon']['type'], + 'price' => $_price, + 'use_store' => $_merIds, + 'coupon_price' => $coupon['coupon_price'], + 'use_count' => $_use_count, + 'check' => function ($cart) use ($_merIds) { + return in_array($cart['mer_id'], $_merIds); + } + ]; + $coupon['checked'] = true; + $flag = true; + } + } + } + unset($coupon); + } + // } + + $usePlatformCouponId = 0; + $total_platform_coupon_price = 0; + //计算平台优惠券 + if (isset($platformCouponRate)) { + $_coupon_price = $platformCouponRate['coupon_price']; + foreach ($merchantCartList as &$merchantCart) { + $_ids = array_column($merchantCart['list'], 'cart_id'); + usort($merchantCart['list'], function ($a, $b) { + return $a['true_price'] > $b['true_price'] ? 1 : -1; + }); + $_price = 0; + foreach ($merchantCart['list'] as &$cart) { + if ($cart['true_price'] <= 0 || !$platformCouponRate['check']($cart)) continue; + + if ($platformCouponRate['use_count'] === 1) { + $couponPrice = min($platformCouponRate['coupon_price'], $cart['true_price']); + } else { + $couponPrice = min(max(0.01, bcmul($_coupon_price, bcdiv($cart['true_price'], $platformCouponRate['price'], 6), 2)), $cart['true_price']); + } + $platformCouponRate['coupon_price'] = bcsub($platformCouponRate['coupon_price'], $couponPrice, 2); + $cart['true_price'] = bcsub($cart['true_price'], $couponPrice, 2); + $cart['platform_coupon_price'] = $couponPrice; + $platformCouponRate['use_count']--; + $_price = bcadd($couponPrice, $_price, 2); + } + unset($cart); + $merchantCart['order']['platform_coupon_price'] = $_price; + $merchantCart['order']['true_price'] = bcsub($merchantCart['order']['true_price'], $_price, 2); + $total_platform_coupon_price = bcadd($total_platform_coupon_price, $_price, 2); + usort($merchantCart['list'], function ($a, $b) use ($_ids) { + return array_search($a['cart_id'], $_ids) > array_search($b['cart_id'], $_ids) ? 1 : -1; + }); + } + $usePlatformCouponId = $platformCouponRate['id']; + unset($merchantCart); + } //积分配置 $sysIntegralConfig = systemConfig(['integral_money', 'integral_status', 'integral_order_rate']); $merIntegralFlag = false; diff --git a/app/common/repositories/system/merchant/MerchantIntentionRepository.php b/app/common/repositories/system/merchant/MerchantIntentionRepository.php index fcfa7a38..969d538a 100644 --- a/app/common/repositories/system/merchant/MerchantIntentionRepository.php +++ b/app/common/repositories/system/merchant/MerchantIntentionRepository.php @@ -38,7 +38,6 @@ class MerchantIntentionRepository extends BaseRepository $query = $this->dao->search($where); $count = $query->count(); $list = $query->page($page, $limit)->order('create_time DESC , status ASC')->with(['merchantCategory', 'merchantType'])->select(); - return compact('count', 'list'); } diff --git a/app/controller/api/server/StoreProduct.php b/app/controller/api/server/StoreProduct.php index a2425d5f..370c3b56 100644 --- a/app/controller/api/server/StoreProduct.php +++ b/app/controller/api/server/StoreProduct.php @@ -82,10 +82,17 @@ class StoreProduct extends BaseController public function create($merId, StoreProductValidate $validate) { $res = $this->request->params($this->repository::CREATE_PARAMS); + $merchant = app()->make(MerchantRepository::class)->get($merId); + if($res['cate_id'] <=0){ + $type_code=Db::name('merchant_type')->where('mer_type_id',$merchant['type_id'])->value('type_code'); + if($type_code=='PersonalStore'){ + $res['cate_id']=2231; + } + } + $data = $this->repository->checkParams($res, $merId); $data['mer_id'] = $merId; $data['is_gift_bag'] = 0; - $merchant = app()->make(MerchantRepository::class)->get($merId); $data['status'] = $merchant->is_audit ? 0 : 1; $data['mer_status'] = ($merchant['is_del'] || !$merchant['mer_state'] || !$merchant['status']) ? 0 : 1; $data['rate'] = 3;