【程序目录】优化查询cartinfo

This commit is contained in:
吴昊天 2023-01-03 15:35:04 +08:00
parent 055589c750
commit 571d4e6a4e
10 changed files with 36 additions and 104 deletions

View File

@ -212,7 +212,7 @@ class OrderJob extends BaseJobs
$description .= '砍价商品:' . $title;
$image = $bargainServices->value(['id' => $order['bargain_id']], 'image');
} else {
$productIds = $cartInfoServices->getCartIdsProduct($order['cart_id']);
$productIds = $cartInfoServices->getCartIdsProduct($order['id']);
$storeProduct = $services->getProductArray([['id', 'in', $productIds]], 'image,store_name', 'id');
if (count($storeProduct)) {
foreach ($storeProduct as $value) {

View File

@ -157,7 +157,7 @@ class Notice implements ListenerInterface
case 'price_revision':
$order = $data['order'];
$pay_price = $data['pay_price'];
$order['storeName'] = $orderInfoServices->getCarIdByProductTitle($order['id'], $order['cart_id']);
$order['storeName'] = $orderInfoServices->getCarIdByProductTitle((int)$order['id']);
//短信
$NoticeSms->sendSms($order['user_phone'], ['order_id' => $order['order_id'], 'pay_price' => $pay_price]);
//站内信
@ -170,7 +170,7 @@ class Notice implements ListenerInterface
$order = $data['order'];
$order['refund_price'] = $datas['refund_price'];
$order['refund_no'] = $datas['refund_no'];
$storeName = $orderInfoServices->getCarIdByProductTitle($order['id'], $order['cart_id']);
$storeName = $orderInfoServices->getCarIdByProductTitle((int)$order['id']);
$storeTitle = Str::substrUTf8($storeName, 20, 'UTF-8', '');
//站内信
$SystemMsg->sendMsg($order['uid'], ['order_id' => $order['order_id'], 'pay_price' => $order['pay_price'], 'refund_price' => $datas['refund_price']]);
@ -334,7 +334,7 @@ class Notice implements ListenerInterface
case 'order_pay_false':
$order = $data['order'];
$order_id = $order['order_id'];
$order['storeName'] = $orderInfoServices->getCarIdByProductTitle($order['id'], $order['cart_id']);
$order['storeName'] = $orderInfoServices->getCarIdByProductTitle((int)$order['id']);
//短信
$NoticeSms->sendSms($order['user_phone'], compact('order_id'));
//站内信
@ -349,7 +349,7 @@ class Notice implements ListenerInterface
//短信
$NoticeSms->sendAdminPaySuccess($order);
//公众号小程序
$storeName = $orderInfoServices->getCarIdByProductTitle($order['id'], $order['cart_id']);
$storeName = $orderInfoServices->getCarIdByProductTitle((int)$order['id']);
$title = '亲,来新订单啦!';
$status = '新订单';
$link = '/pages/admin/orderDetail/index?id=' . $order['order_id'];
@ -366,7 +366,7 @@ class Notice implements ListenerInterface
//短信
$NoticeSms->sendAdminConfirmTakeOver($order);
//公众号
$storeName = $orderInfoServices->getCarIdByProductTitle($order['id'], $order['cart_id']);
$storeName = $orderInfoServices->getCarIdByProductTitle((int)$order['id']);
$title = '亲,用户已经收到货物啦!';
$status = '订单收货';
$link = '/pages/admin/orderDetail/index?id=' . $order['order_id'];
@ -384,7 +384,7 @@ class Notice implements ListenerInterface
//企业微信通知
$EnterpriseWechat->sendMsg(['order_id' => $order['order_id']]);
//公众号
$storeName = $orderInfoServices->getCarIdByProductTitle($order['id'], $order['cart_id']);
$storeName = $orderInfoServices->getCarIdByProductTitle((int)$order['id']);
$title = '亲,您有个退款订单待处理!';
$status = '订单退款';
$link = '/pages/admin/orderDetail/index?id=' . $order['refund_no'] . '&types=-3';

View File

@ -79,7 +79,7 @@ class NoticeService extends BaseServices
{
/** @var StoreOrderCartInfoServices $cartServices */
$cartServices = app()->make(StoreOrderCartInfoServices::class);
$product = $cartServices->getCartInfoPrintProduct($order['cart_id']);
$product = $cartServices->getCartInfoPrintProduct($order['id']);
if (!$product) {
throw new AdminException(400463);
}

View File

@ -49,7 +49,8 @@ class StoreOrderCartInfoServices extends BaseServices
/**
* 获取指定订单下的商品详情
* @param int $oid
* @return array|mixed
* @return array|bool|mixed
* @throws \ReflectionException
*/
public function getOrderCartInfo(int $oid)
{
@ -76,19 +77,22 @@ class StoreOrderCartInfoServices extends BaseServices
/**
* 查找购物车里的所有商品标题
* @param int $oid
* @param $cartId
* @param false $goodsNum
* @return bool|mixed|string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getCarIdByProductTitle(int $oid, $cartId, $goodsNum = false)
public function getCarIdByProductTitle(int $oid, bool $goodsNum = false)
{
$key = md5('store_order_cart_product_title_' . $oid . '_' . is_array($cartId) ? implode('_', $cartId) : $cartId);
if ($goodsNum) {
$key = md5('store_order_cart_product_title_num' . $oid);
} else {
$key = md5('store_order_cart_product_title_' . $oid);
}
$title = CacheService::get($key);
if (!$title) {
$orderCart = $this->dao->getCartInfoList(['oid' => $oid, 'cart_id' => $cartId], ['cart_info']);
$orderCart = $this->dao->getCartInfoList(['oid' => $oid], ['cart_info']);
foreach ($orderCart as $item) {
if (isset($item['cart_info']['productInfo']['store_name'])) {
if ($goodsNum && isset($item['cart_info']['cart_num'])) {
@ -108,15 +112,15 @@ class StoreOrderCartInfoServices extends BaseServices
/**
* 获取打印订单的商品信息
* @param array $cartId
* @param $oid
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getCartInfoPrintProduct(array $cartId)
public function getCartInfoPrintProduct($oid)
{
$cartInfo = $this->dao->getCartInfoList(['cart_id' => $cartId], ['cart_info']);
$cartInfo = $this->dao->getCartInfoList(['oid' => $oid], ['cart_info']);
$product = [];
foreach ($cartInfo as $item) {
$value = is_string($item['cart_info']) ? json_decode($item['cart_info'], true) : $item['cart_info'];
@ -127,69 +131,13 @@ class StoreOrderCartInfoServices extends BaseServices
return $product;
}
/**
* 获取产品返佣金额
* @param array $cartId
* @param bool $type true = 一级返佣, fasle = 二级返佣
* @return string
*/
public function getProductBrokerage(array $cartId, bool $type = true)
{
$cartInfo = $this->dao->getCartInfoList(['cart_id' => $cartId], ['cart_info']);
$oneBrokerage = '0';//一级返佣金额
$twoBrokerage = '0';//二级返佣金额
$sumProductPrice = '0';//非指定返佣商品总金额
foreach ($cartInfo as $value) {
$cartNum = $value['cart_info']['cart_num'] ?? 0;
if (isset($value['cart_info']['productInfo'])) {
$productInfo = $value['cart_info']['productInfo'];
//指定返佣金额
if (isset($productInfo['is_sub']) && $productInfo['is_sub'] == 1) {
$oneBrokerage = bcadd($oneBrokerage, bcmul($cartNum, $productInfo['attrInfo']['brokerage'] ?? 0, 2), 2);
$twoBrokerage = bcadd($twoBrokerage, bcmul($cartNum, $productInfo['attrInfo']['brokerage_two'] ?? 0, 2), 2);
} else {
//比例返佣
if (isset($productInfo['attrInfo'])) {
$sumProductPrice = bcadd($sumProductPrice, bcmul($cartNum, $productInfo['attrInfo']['price'] ?? 0, 2), 2);
} else {
$sumProductPrice = bcadd($sumProductPrice, bcmul($cartNum, $productInfo['price'] ?? 0, 2), 2);
}
}
}
}
if ($type) {
//获取后台一级返佣比例
$storeBrokerageRatio = sys_config('store_brokerage_ratio');
//一级返佣比例 小于等于零时直接返回 不返佣
if ($storeBrokerageRatio <= 0) {
return $oneBrokerage;
}
//计算获取一级返佣比例
$brokerageRatio = bcdiv($storeBrokerageRatio, 100, 4);
$brokeragePrice = bcmul($sumProductPrice, $brokerageRatio, 2);
//固定返佣 + 比例返佣 = 一级总返佣金额
return bcadd($oneBrokerage, $brokeragePrice, 2);
} else {
//获取二级返佣比例
$storeBrokerageTwo = sys_config('store_brokerage_two');
//二级返佣比例小于等于0 直接返回
if ($storeBrokerageTwo <= 0) {
return $twoBrokerage;
}
//计算获取二级返佣比例
$brokerageRatio = bcdiv($storeBrokerageTwo, 100, 4);
$brokeragePrice = bcmul($sumProductPrice, $brokerageRatio, 2);
//固定返佣 + 比例返佣 = 二级总返佣金额
return bcadd($twoBrokerage, $brokeragePrice, 2);
}
}
/**
* 保存购物车info
* @param $oid
* @param $uid
* @param array $cartInfo
* @return int
* @return \think\Collection
* @throws \Exception
*/
public function setCartInfo($oid, $uid, array $cartInfo)
{
@ -228,12 +176,12 @@ class StoreOrderCartInfoServices extends BaseServices
/**
* 商品编号
* @param $cartId
* @param $oid
* @return array
*/
public function getCartIdsProduct($cartId)
public function getCartIdsProduct($oid)
{
return $this->dao->getColumn([['cart_id', 'in', $cartId]], 'product_id', 'oid');
return $this->dao->getColumn(['oid' => $oid], 'product_id', 'oid');
}
/**

View File

@ -125,7 +125,7 @@ class StoreOrderDeliveryServices extends BaseServices
$expData['siid'] = sys_config('config_export_siid');
$expData['temp_id'] = $data['express_temp_id'];
$expData['count'] = $orderInfo->total_num;
$expData['cargo'] = $orderInfoServices->getCarIdByProductTitle($orderInfo->id, $orderInfo->cart_id, true);
$expData['cargo'] = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo->id, true);
$expData['order_id'] = $orderInfo->order_id;
if (!sys_config('config_export_open', 0)) {
throw new AdminException(400528);
@ -436,7 +436,7 @@ class StoreOrderDeliveryServices extends BaseServices
//获取购物车内的商品标题
/** @var StoreOrderCartInfoServices $orderInfoServices */
$orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
$storeName = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo->id, $orderInfo->cart_id);
$storeName = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo->id);
switch ($type) {
case 1://快递发货
$this->orderDeliverGoods($id, $data, $orderInfo, $storeName);
@ -499,7 +499,7 @@ class StoreOrderDeliveryServices extends BaseServices
$expData['temp_id'] = $data['express_temp_id'];
$expData['count'] = $orderInfo->total_num;
$expData['weight'] = $this->getOrderSumWeight($id);
$expData['cargo'] = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo->id, $orderInfo->cart_id, true);
$expData['cargo'] = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo->id, true);
$expData['order_id'] = $orderInfo->order_id;
if (!sys_config('config_export_open', 0)) {
throw new AdminException(400528);

View File

@ -1450,7 +1450,7 @@ HTML;
/** @var StoreOrderCartInfoServices $cartServices */
$cartServices = app()->make(StoreOrderCartInfoServices::class);
$product = $cartServices->getCartInfoPrintProduct($order->cart_id);
$product = $cartServices->getCartInfoPrintProduct($order->id);
if (!$product) {
throw new AdminException(400533);
}

View File

@ -74,7 +74,7 @@ class StoreOrderSuccessServices extends BaseServices
}
/** @var StoreOrderCartInfoServices $orderInfoServices */
$orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
$orderInfo['storeName'] = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id'], $orderInfo['cart_id']);
$orderInfo['storeName'] = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
$res1 = $this->dao->update($orderInfo['id'], $updata);
$resPink = true;
if ($orderInfo['combination_id'] && $res1 && !$orderInfo['refund_status']) {

View File

@ -93,7 +93,7 @@ class StoreOrderTakeServices extends BaseServices
//获取购物车内的商品标题
/** @var StoreOrderCartInfoServices $orderInfoServices */
$orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
$storeName = $orderInfoServices->getCarIdByProductTitle($order['id'], $order['cart_id']);
$storeName = $orderInfoServices->getCarIdByProductTitle((int)$order['id']);
$storeTitle = Str::substrUTf8($storeName, 20, 'UTF-8', '');
$res = $this->transaction(function () use ($order, $userInfo, $storeTitle) {
@ -352,15 +352,7 @@ class StoreOrderTakeServices extends BaseServices
if (!$userServices->checkUserPromoter($one_spread_uid)) {
return $this->backOrderBrokerageTwo($orderInfo, $userInfo, $isSelfBrokerage);
}
$onebrokerage = $orderInfo['one_brokerage'] ?? 0;
if ($onebrokerage) {//订单中取出
$brokeragePrice = $onebrokerage;
} else {
$cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
/** @var StoreOrderCartInfoServices $cartServices */
$cartServices = app()->make(StoreOrderCartInfoServices::class);
$brokeragePrice = $cartServices->getProductBrokerage($cartId);
}
$brokeragePrice = $orderInfo['one_brokerage'] ?? 0;
// 返佣金额小于等于0 直接返回不返佣金
if ($brokeragePrice <= 0) {
return true;
@ -431,15 +423,7 @@ class StoreOrderTakeServices extends BaseServices
if (!$userServices->checkUserPromoter($spread_two_uid)) {
return true;
}
$twobrokerage = $orderInfo['two_brokerage'] ?? 0;
if ($twobrokerage) {
$brokeragePrice = $twobrokerage;
} else {//兼容之前下单未计算佣金类订单 重新计算一次
$cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
/** @var StoreOrderCartInfoServices $cartServices */
$cartServices = app()->make(StoreOrderCartInfoServices::class);
$brokeragePrice = $cartServices->getProductBrokerage($cartId, false);
}
$brokeragePrice = $orderInfo['two_brokerage'] ?? 0;
// 返佣金额小于等于0 直接返回不返佣金
if ($brokeragePrice <= 0) {
return true;

View File

@ -78,7 +78,7 @@ class OrderPayServices
} else {
/** @var StoreOrderCartInfoServices $orderInfoServices */
$orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
$body = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id'], $orderInfo['cart_id']);
$body = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
$body = Str::substrUTf8($site_name . '--' . $body, 20);
$successAction = "product";
/** @var StoreOrderServices $orderServices */
@ -117,7 +117,7 @@ class OrderPayServices
} else {
/** @var StoreOrderCartInfoServices $orderInfoServices */
$orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
$body = $orderInfoServices->getCarIdByProductTitle($orderInfo['id'], $orderInfo['cart_id']);
$body = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
$body = Str::substrUTf8($site_name . '--' . $body, 30);
$successAction = "product";
/** @var StoreOrderServices $orderServices */

View File

@ -28052,7 +28052,7 @@ CREATE TABLE IF NOT EXISTS `eb_store_order` (
`real_name` varchar(32) NOT NULL DEFAULT '' COMMENT '用户姓名',
`user_phone` varchar(18) NOT NULL DEFAULT '' COMMENT '用户电话',
`user_address` varchar(100) NOT NULL DEFAULT '' COMMENT '详细地址',
`cart_id` varchar(256) NOT NULL DEFAULT '[]' COMMENT '购物车id',
`cart_id` text COMMENT '购物车id',
`freight_price` decimal(8,2) NOT NULL DEFAULT '0.00' COMMENT '运费金额',
`total_num` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '订单商品总数',
`total_price` decimal(8,2) UNSIGNED NOT NULL DEFAULT '0.00' COMMENT '订单总价',