|
|
@@ -282,43 +282,43 @@ class GoodsClass extends BaseClass
|
|
|
}
|
|
|
|
|
|
//获取商品完整详情,包括商品的分类 ssh 2019.12.3
|
|
|
- public static function getGoodsInfo($id)
|
|
|
+ public static function getGoodsInfo($id, $shop, $custom, $params)
|
|
|
{
|
|
|
$info = self::getById($id);
|
|
|
if (empty($info)) {
|
|
|
return [];
|
|
|
}
|
|
|
- $list = self::groupGoodsBaseInfo([$info], true);
|
|
|
- $info = current($list);
|
|
|
- return $info;
|
|
|
+ $list = self::groupGoodsBaseInfo([$info], $shop, $custom, $params);
|
|
|
+ return current($list);
|
|
|
}
|
|
|
|
|
|
- public static function getGoodsData($where,$params){
|
|
|
+ public static function getGoodsData($where, $params)
|
|
|
+ {
|
|
|
// 获取分页参数
|
|
|
$page = Yii::$app->request->get('page', 1);
|
|
|
$pageSize = Yii::$app->request->get('pageSize', 20);
|
|
|
-
|
|
|
+
|
|
|
$flowerNum = $params['flowerNum'] ?? 0;
|
|
|
// 查询某分类下有库存的商品
|
|
|
$query = GoodsCategory::find()
|
|
|
->where($where)
|
|
|
- ->joinWith(['goods' => function($query) use ($flowerNum) {
|
|
|
+ ->joinWith(['goods' => function ($query) use ($flowerNum) {
|
|
|
$query->andWhere(['>', 'xhGoods.stock', 0]);
|
|
|
- if(isset($flowerNum) && $flowerNum > 0){
|
|
|
- $query->andWhere(['xhGoods.flowerNum' => $flowerNum]);
|
|
|
+ if (isset($flowerNum) && $flowerNum > 0) {
|
|
|
+ $query->andWhere(['xhGoods.flowerNum' => $flowerNum]);
|
|
|
}
|
|
|
}])
|
|
|
->andWhere(['xhGoodsCategory.delStatus' => 0])
|
|
|
->orderBy(['inTurn' => SORT_DESC]);
|
|
|
-
|
|
|
+
|
|
|
// 获取总数
|
|
|
$total = $query->count();
|
|
|
-
|
|
|
+
|
|
|
// 分页查询
|
|
|
$goodsList = $query->offset(($page - 1) * $pageSize)
|
|
|
->limit($pageSize)
|
|
|
->all();
|
|
|
-
|
|
|
+
|
|
|
// 整理结果,只要有库存的商品
|
|
|
$list = [];
|
|
|
foreach ($goodsList as $goodsCategory) {
|
|
|
@@ -326,7 +326,7 @@ class GoodsClass extends BaseClass
|
|
|
$list[] = $goodsCategory->goods->attributes;
|
|
|
}
|
|
|
}
|
|
|
- if(!empty($list)){
|
|
|
+ if (!empty($list)) {
|
|
|
$list = self::groupGoodsBaseInfo($list);
|
|
|
}
|
|
|
|
|
|
@@ -342,43 +342,64 @@ class GoodsClass extends BaseClass
|
|
|
];
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 组装商品基本信息
|
|
|
- * @param $list "goods商品数组"
|
|
|
- * @param bool $getItem 是否获取"成品的花材"
|
|
|
- * @param array $sortIds 按商品分类的inTurn 进行排序的商品id数组
|
|
|
- * @return mixed
|
|
|
- */
|
|
|
- public static function groupGoodsBaseInfo($list, $getItem = false, $sortIds= [])
|
|
|
+ //$data 可以是一维数组,也可以二维数组
|
|
|
+ //花束的价格全部由这边输出 $custom $params 必须预留,为后面做准备
|
|
|
+ //会返回两个参数 price 价格,涨价后的价格 priceBeforeRise 涨价前的价格,如果price==priceBeforeRise说明没有涨价
|
|
|
+ public static function getFinalPrice($data, $shop, $custom, $params)
|
|
|
+ {
|
|
|
+ if (empty($data)) {
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+ $mainId = $shop->mainId;
|
|
|
+ $rise = GoodsSettingClass::getRise($mainId);
|
|
|
+ $riseSwitch = $rise['riseSwitch'] ?? 0;
|
|
|
+ $riseType = $rise['riseType'] ?? 0;
|
|
|
+ $riseAmount = $rise['riseAmount'] ?? 0;
|
|
|
+ reset($data);
|
|
|
+ $current = current($data);
|
|
|
+ if (is_array($current)) {
|
|
|
+ foreach ($data as $key => $item) {
|
|
|
+ $item = self::obeyRiseRule($item, $riseSwitch, $riseType, $riseAmount, $custom, $params);
|
|
|
+ $data[$key] = $item;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $data = self::obeyRiseRule($data, $riseSwitch, $riseType, $riseAmount, $custom, $params);
|
|
|
+ }
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ //遵循涨价规则 ssh 20250828
|
|
|
+ //会返回两个参数 price 价格或涨价后的价格 priceBeforeRise 涨价前的价格,如果price==priceBeforeRise说明没有涨价
|
|
|
+ public static function obeyRiseRule($data, $riseSwitch, $riseType, $riseAmount, $custom, $params)
|
|
|
+ {
|
|
|
+ $price = $data['price'] ?? 0;
|
|
|
+ $priceBeforeRise = $price;
|
|
|
+ //后台修改商品时,取原价,不需要显示涨价金额 getOriginalPrice=1 有涨价也强制取原价
|
|
|
+ $getOriginalPrice = $params['getOriginalPrice'] ?? 0;
|
|
|
+ if ($riseSwitch == 1 && $riseAmount > 0 && $getOriginalPrice == 0) {
|
|
|
+ if ($riseType == 0) {
|
|
|
+ //百分比
|
|
|
+ $add = sprintf("%.1f", ($price * $riseAmount / 100));
|
|
|
+ $price = bcadd($price, $add, 2);
|
|
|
+ } else {
|
|
|
+ //金额
|
|
|
+ $price = bcadd($price, $riseAmount, 2);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $data['price'] = $price;
|
|
|
+ $data['priceBeforeRise'] = $priceBeforeRise;
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function groupGoodsBaseInfo($list, $shop, $custom, $params)
|
|
|
{
|
|
|
if (empty($list)) {
|
|
|
return [];
|
|
|
}
|
|
|
- $arrNotEmpty = is_array($sortIds) && !empty($sortIds);
|
|
|
-
|
|
|
- //注意这里涉及商家节日涨价的问题,不要去取不同商家的商品,确有必要另外起方法!!!!!!!!!!
|
|
|
- $currentData = current($list);
|
|
|
- $mainId = intval($currentData['mainId']);
|
|
|
- if ($mainId <= 0) {
|
|
|
- util::fail('mainId 出错');
|
|
|
- }
|
|
|
- $rise = GoodsSettingClass::getRise($mainId);
|
|
|
- $sjHasCategory = CategoryClass::getCategory($mainId);
|
|
|
-
|
|
|
// 收集所有商品ID
|
|
|
$goodsIds = array_column($list, 'id');
|
|
|
// 一次性获取所有商品的分类关系
|
|
|
$allGoodsCategories = GoodsCategoryClass::getGoodsHasCategoryIdsBatch($goodsIds);
|
|
|
-
|
|
|
- $groupedItems = [];
|
|
|
- if ($getItem && !empty($goodsIds)) {
|
|
|
- $itemList = GoodsItemClass::getAllByCondition(['goodsId' => ['in', $goodsIds]]);
|
|
|
- // 手动按 goodsId 分组
|
|
|
- foreach ($itemList as $item) {
|
|
|
- $groupedItems[$item['goodsId']][] = $item;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
foreach ($list as $key => &$val) {
|
|
|
$id = $val['id'];
|
|
|
//大中小图片转化
|
|
|
@@ -386,66 +407,21 @@ class GoodsClass extends BaseClass
|
|
|
//免费配送范围
|
|
|
$val['freeSendDist'] = 5;
|
|
|
$val['createDate'] = isset($val['createTime']) ? date("Y-m-d", strtotime($val['createTime'])) : '';
|
|
|
- //节日涨价
|
|
|
- if (!empty($rise)) {
|
|
|
- $price = $val['price'];
|
|
|
- if ($rise['riseType'] == 0) {
|
|
|
- //百分比
|
|
|
- $val['price'] = $price + sprintf("%.1f", ($price * $rise['riseAmount'] / 100));
|
|
|
- } else {
|
|
|
- //金额
|
|
|
- $val['price'] = $price + $rise['riseAmount'];
|
|
|
- }
|
|
|
- }
|
|
|
//商品的分类
|
|
|
$categoryIds = $allGoodsCategories[$id] ?? [];
|
|
|
$val['categoryIdList'] = $categoryIds;
|
|
|
- $belongCategory = [];
|
|
|
- if (!empty($categoryIds)) {
|
|
|
- foreach ($categoryIds as $currentId) {
|
|
|
- $belongCategory[] = $sjHasCategory[$currentId]['categoryName'] ?? '';
|
|
|
- }
|
|
|
- }
|
|
|
- $val['belongCategory'] = $belongCategory;
|
|
|
-
|
|
|
$shortCover = $val['cover'] ?? '';
|
|
|
$val['shortCover'] = $shortCover;
|
|
|
- $val['cover'] = imgUtil::groupImg($shortCover);
|
|
|
- $val['smallCover'] = $val['cover'] . "?x-oss-process=image/resize,m_fill,h_130,w_130";
|
|
|
- $val['bigCover'] = $val['cover'] . "?x-oss-process=image/resize,m_fill,h_700,w_700";
|
|
|
-
|
|
|
- if ($getItem) {
|
|
|
- $currentItems = isset($groupedItems[$id]) ? $groupedItems[$id] : [];
|
|
|
- foreach ($currentItems as &$itemVal) {
|
|
|
- $shortCover = $itemVal['cover'] ?? '';
|
|
|
- $itemVal['shortCover'] = $shortCover;
|
|
|
- $itemVal['smallCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
|
|
|
- $itemVal['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
|
|
|
- }
|
|
|
- $val['goodsItem'] = $currentItems;
|
|
|
- }
|
|
|
+ $val['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
|
|
|
+ $val['smallCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
|
|
|
+ $val['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
|
|
|
//前端使用字段
|
|
|
$val['bigCount'] = 0;
|
|
|
- if ($arrNotEmpty) {
|
|
|
- $sortIndex = array_search($id, $sortIds);
|
|
|
- // $newList[$sortIndex] = $val;
|
|
|
- $val['sortWeight'] = $sortIndex !== false ? $sortIndex : count($sortIds);
|
|
|
- }
|
|
|
}
|
|
|
- unset($val); // 销毁引用
|
|
|
-
|
|
|
- if ($arrNotEmpty) {
|
|
|
- usort($list, function($a, $b) {
|
|
|
- return $a['sortWeight'] - $b['sortWeight'];
|
|
|
- });
|
|
|
- //return $newList;
|
|
|
- // return array_values($newList);
|
|
|
- }
|
|
|
-
|
|
|
- return $list;
|
|
|
+ //获取最终需要的价格
|
|
|
+ return self::getFinalPrice($list, $shop, $custom, $params);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
//新建商品 ssh 20220404
|
|
|
public static function addGoods($data, $work = null)
|
|
|
{
|
|
|
@@ -587,7 +563,7 @@ class GoodsClass extends BaseClass
|
|
|
$id = $goods['id'] ?? 0;
|
|
|
$preSetItemNum = $goods['setItemNum'] ?? 0;
|
|
|
|
|
|
- $categoryIdList = isset($data['categoryIdList']) && !empty($data['categoryIdList']) ? $data['categoryIdList'] : [];
|
|
|
+ $categoryIdList = !empty($data['categoryIdList']) ? $data['categoryIdList'] : [];
|
|
|
unset($data['categoryIdList']);
|
|
|
if (empty($categoryIdList)) {
|
|
|
util::fail('没有选择分类');
|
|
|
@@ -608,7 +584,7 @@ class GoodsClass extends BaseClass
|
|
|
}
|
|
|
//精确处理分类变更:分别处理删除、新增、修改三种情况
|
|
|
$goodsHasCategoryIds = GoodsCategoryService::getGoodsHasCategoryIds($id);
|
|
|
-
|
|
|
+
|
|
|
// 1. 找出需要删除的分类:原有分类中不在新分类列表中的
|
|
|
$toDeleteCategoryIds = array_diff($goodsHasCategoryIds, $categoryIdList);
|
|
|
if (!empty($toDeleteCategoryIds)) {
|
|
|
@@ -617,7 +593,7 @@ class GoodsClass extends BaseClass
|
|
|
//\bizHd\goods\models\GoodsCategory::deleteAll(['and', ['gId' => $id], ['in', 'cId', $toDeleteCategoryIds]]); //直接调用 deleteAll 方法
|
|
|
CategoryClass::counters(['goodsNum' => -1], ['id' => $toDeleteCategoryIds]);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 2. 找出需要新增的分类:新分类列表中不在原有分类中的
|
|
|
$toAddCategoryIds = array_diff($categoryIdList, $goodsHasCategoryIds);
|
|
|
if (!empty($toAddCategoryIds)) {
|
|
|
@@ -633,14 +609,13 @@ class GoodsClass extends BaseClass
|
|
|
'sjId' => $sjId,
|
|
|
'flower' => $flower,
|
|
|
'mainId' => $mainId,
|
|
|
- 'status' => $status,
|
|
|
'setItemNum' => $setItemNum,
|
|
|
];
|
|
|
}
|
|
|
GoodsCategoryClass::batchAdd($addCategory);
|
|
|
CategoryClass::counters(['goodsNum' => 1], ['id' => $toAddCategoryIds]);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 3. 处理需要修改的分类:既在原有分类中也在新分类列表中的
|
|
|
$toUpdateCategoryIds = array_intersect($goodsHasCategoryIds, $categoryIdList);
|
|
|
if (!empty($toUpdateCategoryIds)) {
|
|
|
@@ -650,13 +625,12 @@ class GoodsClass extends BaseClass
|
|
|
'name' => $name,
|
|
|
'py' => $py,
|
|
|
'cName' => $cName,
|
|
|
- 'status' => $status,
|
|
|
'setItemNum' => $setItemNum,
|
|
|
];
|
|
|
GoodsCategoryClass::updateByCondition(['gId' => $id, 'cId' => $categoryId], $updateData);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 字符串的,即认为是 图片URL
|
|
|
if (isset($data['cover']) && is_string($data['cover'])) {
|
|
|
|