|
|
@@ -293,33 +293,32 @@ class GoodsClass extends BaseClass
|
|
|
return $info;
|
|
|
}
|
|
|
|
|
|
- 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(['delStatus' => 0]);
|
|
|
$query->andWhere(['>', 'stock', 0]);
|
|
|
- if (isset($flowerNum) && $flowerNum > 0) {
|
|
|
- $query->andWhere(['flowerNum' => $flowerNum]);
|
|
|
+ if(isset($flowerNum) && $flowerNum > 0){
|
|
|
+ $query->andWhere(['flowerNum' => $flowerNum]);
|
|
|
}
|
|
|
}])
|
|
|
->orderBy(['inTurn' => SORT_DESC]);
|
|
|
-
|
|
|
+
|
|
|
// 获取总数
|
|
|
$total = $query->count();
|
|
|
-
|
|
|
+
|
|
|
// 分页查询
|
|
|
$goodsList = $query->offset(($page - 1) * $pageSize)
|
|
|
->limit($pageSize)
|
|
|
->all();
|
|
|
-
|
|
|
+
|
|
|
// 整理结果,只要有库存的商品
|
|
|
$list = [];
|
|
|
foreach ($goodsList as $goodsCategory) {
|
|
|
@@ -327,7 +326,7 @@ class GoodsClass extends BaseClass
|
|
|
$list[] = $goodsCategory->goods->attributes;
|
|
|
}
|
|
|
}
|
|
|
- if (!empty($list)) {
|
|
|
+ if(!empty($list)){
|
|
|
$list = self::groupGoodsBaseInfo($list);
|
|
|
}
|
|
|
|
|
|
@@ -343,24 +342,44 @@ class GoodsClass extends BaseClass
|
|
|
];
|
|
|
}
|
|
|
|
|
|
- //组装商品基本信息 ssh 2019.12.2
|
|
|
- public static function groupGoodsBaseInfo($list, $getItem = false, $sortIds = [])
|
|
|
+ /**
|
|
|
+ * 组装商品基本信息
|
|
|
+ * @param $list goods商品数组
|
|
|
+ * @param bool $getItem 是否获取"成品的花材"
|
|
|
+ * @param array $sortIds 按商品分类的inTurn 进行排序的商品id数组
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public static function groupGoodsBaseInfo($list, $getItem = false, $sortIds= [])
|
|
|
{
|
|
|
- $newList = [];
|
|
|
+ if (empty($list)) {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
$arrNotEmpty = is_array($sortIds) && !empty($sortIds);
|
|
|
-
|
|
|
+
|
|
|
//注意这里涉及商家节日涨价的问题,不要去取不同商家的商品,确有必要另外起方法!!!!!!!!!!
|
|
|
$currentData = current($list);
|
|
|
- $mainId = $currentData['mainId'] ?? 0;
|
|
|
+ $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);
|
|
|
|
|
|
- foreach ($list as $key => $val) {
|
|
|
+ $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'];
|
|
|
//大中小图片转化
|
|
|
$val = business::formatGoodsImg($val);
|
|
|
@@ -388,6 +407,7 @@ class GoodsClass extends BaseClass
|
|
|
}
|
|
|
}
|
|
|
$val['belongCategory'] = $belongCategory;
|
|
|
+
|
|
|
$shortCover = $val['cover'] ?? '';
|
|
|
// 兼容 cover 保存的是数字(表示第几张图片)
|
|
|
if (intval($shortCover) > 0) {
|
|
|
@@ -401,17 +421,16 @@ class GoodsClass extends BaseClass
|
|
|
$val['cover'] = imgUtil::groupImg($shortCover);
|
|
|
$val['smallCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
|
|
|
$val['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
|
|
|
+
|
|
|
if ($getItem) {
|
|
|
- $itemList = GoodsItemClass::getAllByCondition(['goodsId' => $id], null, '*');
|
|
|
- foreach ($itemList as $itemKey => $itemVal) {
|
|
|
+ $currentItems = isset($groupedItems[$id]) ? $groupedItems[$id] : [];
|
|
|
+ foreach ($currentItems as &$itemVal) {
|
|
|
$shortCover = $itemVal['cover'] ?? '';
|
|
|
- $smallCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
|
|
|
- $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
|
|
|
- $itemList[$itemKey]['shortCover'] = $shortCover;
|
|
|
- $itemList[$itemKey]['smallCover'] = $smallCover;
|
|
|
- $itemList[$itemKey]['bigCover'] = $bigCover;
|
|
|
+ $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'] = $itemList;
|
|
|
+ $val['goodsItem'] = $currentItems;
|
|
|
}
|
|
|
//前端使用字段
|
|
|
$val['bigCount'] = 0;
|
|
|
@@ -420,12 +439,11 @@ class GoodsClass extends BaseClass
|
|
|
// $newList[$sortIndex] = $val;
|
|
|
$val['sortWeight'] = $sortIndex !== false ? $sortIndex : count($sortIds);
|
|
|
}
|
|
|
-
|
|
|
- $list[$key] = $val;
|
|
|
}
|
|
|
+ unset($val); // 销毁引用
|
|
|
|
|
|
if ($arrNotEmpty) {
|
|
|
- usort($list, function ($a, $b) {
|
|
|
+ usort($list, function($a, $b) {
|
|
|
return $a['sortWeight'] - $b['sortWeight'];
|
|
|
});
|
|
|
//return $newList;
|
|
|
@@ -435,6 +453,7 @@ class GoodsClass extends BaseClass
|
|
|
return $list;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
//新建商品 ssh 20220404
|
|
|
public static function addGoods($data, $work = null)
|
|
|
{
|
|
|
@@ -599,14 +618,16 @@ class GoodsClass extends BaseClass
|
|
|
}
|
|
|
//精确处理分类变更:分别处理删除、新增、修改三种情况
|
|
|
$goodsHasCategoryIds = GoodsCategoryService::getGoodsHasCategoryIds($id);
|
|
|
-
|
|
|
+
|
|
|
// 1. 找出需要删除的分类:原有分类中不在新分类列表中的
|
|
|
$toDeleteCategoryIds = array_diff($goodsHasCategoryIds, $categoryIdList);
|
|
|
if (!empty($toDeleteCategoryIds)) {
|
|
|
- GoodsCategoryClass::deleteByCondition(['gId' => $id, 'cId' => ['in', $toDeleteCategoryIds]]);
|
|
|
+ //GoodsCategoryClass::deleteByCondition(['gId' => $id, 'cId' => ['in', $toDeleteCategoryIds]]); //不支持,改用 ActiveRecord 的 deleteAll 方法
|
|
|
+ GoodsCategoryClass::getModel()::deleteAll(['and', ['gId' => $id], ['in', 'cId', $toDeleteCategoryIds]]); //先通过 getModel 获取 AR 实例,再调用 deleteAll 方法
|
|
|
+ //\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)) {
|
|
|
@@ -629,7 +650,7 @@ class GoodsClass extends BaseClass
|
|
|
GoodsCategoryClass::batchAdd($addCategory);
|
|
|
CategoryClass::counters(['goodsNum' => 1], ['id' => $toAddCategoryIds]);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 3. 处理需要修改的分类:既在原有分类中也在新分类列表中的
|
|
|
$toUpdateCategoryIds = array_intersect($goodsHasCategoryIds, $categoryIdList);
|
|
|
if (!empty($toUpdateCategoryIds)) {
|
|
|
@@ -645,10 +666,10 @@ class GoodsClass extends BaseClass
|
|
|
GoodsCategoryClass::updateByCondition(['gId' => $id, 'cId' => $categoryId], $updateData);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ // 字符串的,即认为是 图片URL
|
|
|
+ if (isset($data['cover']) && is_string($data['cover'])) {
|
|
|
|
|
|
- // 兼容 cover 参数是数字
|
|
|
- if (intval($data['cover']) > 0) {
|
|
|
- // 如果是大于0的整数,说明是图片序号
|
|
|
} else {
|
|
|
$data['cover'] = $data['shopImg'][0] ?? '';
|
|
|
}
|
|
|
@@ -805,84 +826,84 @@ class GoodsClass extends BaseClass
|
|
|
//改库存
|
|
|
$newStock = $goodsData['stock'] - $num;
|
|
|
|
|
|
- //不管有没库存,都生成任务
|
|
|
-
|
|
|
- $goodsName = $goodsData['name'] ?? '';
|
|
|
- if ($checkStock) {
|
|
|
- util::fail($goodsName . '库存不足,剩余:' . $goodsData['stock']);
|
|
|
- }
|
|
|
-
|
|
|
- $flower = $goodsData['flower'] ?? 0;
|
|
|
- if ($flower == 0) {
|
|
|
- util::complete('绿植盆栽资材类无法生成制作单');
|
|
|
- }
|
|
|
-
|
|
|
- //缺的库存生成制作单
|
|
|
- $sjId = $goodsData['sjId'] ?? 0;
|
|
|
- $orderSn = $params['orderSn'] ?? '';
|
|
|
- $orderId = $params['orderId'] ?? 0;
|
|
|
- $shopId = $params['shopId'] ?? 0;
|
|
|
- $orderPrice = $params['orderPrice'] ?? 0;
|
|
|
- $fromType = $params['fromType'] ?? 1;
|
|
|
- $remark = $params['remark'] ?? '';
|
|
|
- $orderDebtPrice = $params['orderDebtPrice'] ?? 0;
|
|
|
- $sendType = $params['sendType'] ?? 0;
|
|
|
- $thirdSn = $params['thirdSn'] ?? '';
|
|
|
- $cardInfo = $params['cardInfo'] ?? '';
|
|
|
- $anonymity = $params['anonymity'] ?? 0;
|
|
|
- $manyType = $params['manyType'] ?? 0;
|
|
|
- $sendNum = $params['sendNum'] ?? '';
|
|
|
- $hasReachTime = 0;
|
|
|
- $reachPeriod = 0;
|
|
|
- $reachDate = '0000-00-00';
|
|
|
- $staffName = '';
|
|
|
- $staffId = 0;
|
|
|
- $bookName = '';
|
|
|
- if (!empty($orderId)) {
|
|
|
- $order = OrderClass::getById($orderId, true);
|
|
|
- if (!empty($order)) {
|
|
|
- $reachDate = $order->reachDate ?? '';
|
|
|
- if (!empty($reachDate) && $reachDate != '0000-00-00') {
|
|
|
- $hasReachTime = 1;
|
|
|
+ if ($newStock < 0) {
|
|
|
+
|
|
|
+ $goodsName = $goodsData['name'] ?? '';
|
|
|
+ if ($checkStock) {
|
|
|
+ util::fail($goodsName . '库存不足,剩余:' . $goodsData['stock']);
|
|
|
+ }
|
|
|
+
|
|
|
+ $flower = $goodsData['flower'] ?? 0;
|
|
|
+ if ($flower == 0) {
|
|
|
+ util::complete('绿植盆栽资材类无法生成制作单');
|
|
|
+ }
|
|
|
+
|
|
|
+ //缺的库存生成制作单
|
|
|
+ $sjId = $goodsData['sjId'] ?? 0;
|
|
|
+ $orderSn = $params['orderSn'] ?? '';
|
|
|
+ $orderId = $params['orderId'] ?? 0;
|
|
|
+ $shopId = $params['shopId'] ?? 0;
|
|
|
+ $orderPrice = $params['orderPrice'] ?? 0;
|
|
|
+ $fromType = $params['fromType'] ?? 1;
|
|
|
+ $remark = $params['remark'] ?? '';
|
|
|
+ $orderDebtPrice = $params['orderDebtPrice'] ?? 0;
|
|
|
+ $sendType = $params['sendType'] ?? 0;
|
|
|
+ $thirdSn = $params['thirdSn'] ?? '';
|
|
|
+ $cardInfo = $params['cardInfo'] ?? '';
|
|
|
+ $anonymity = $params['anonymity'] ?? 0;
|
|
|
+ $manyType = $params['manyType'] ?? 0;
|
|
|
+ $sendNum = $params['sendNum'] ?? '';
|
|
|
+ $hasReachTime = 0;
|
|
|
+ $reachPeriod = 0;
|
|
|
+ $reachDate = '0000-00-00';
|
|
|
+ $staffName = '';
|
|
|
+ $staffId = 0;
|
|
|
+ $bookName = '';
|
|
|
+ if (!empty($orderId)) {
|
|
|
+ $order = OrderClass::getById($orderId, true);
|
|
|
+ if (!empty($order)) {
|
|
|
+ $reachDate = $order->reachDate ?? '';
|
|
|
+ if (!empty($reachDate) && $reachDate != '0000-00-00') {
|
|
|
+ $hasReachTime = 1;
|
|
|
+ }
|
|
|
+ $reachPeriod = $order->reachPeriod ?? 0;
|
|
|
+ $staffName = $order->shopAdminName ?? '';
|
|
|
+ $staffId = $order->shopAdminId ?? '';
|
|
|
+ $bookName = $order->bookName ?? '';
|
|
|
}
|
|
|
- $reachPeriod = $order->reachPeriod ?? 0;
|
|
|
- $staffName = $order->shopAdminName ?? '';
|
|
|
- $staffId = $order->shopAdminId ?? '';
|
|
|
- $bookName = $order->bookName ?? '';
|
|
|
}
|
|
|
+ $sh = $goodsData['sh'] ?? 0;
|
|
|
+ $params = [
|
|
|
+ 'goods' => [['productId' => $goodsId, 'num' => abs($num)]],
|
|
|
+ 'sjId' => $sjId,
|
|
|
+ 'mainId' => $mainId,
|
|
|
+ 'orderSn' => $orderSn,
|
|
|
+ 'orderId' => $orderId,
|
|
|
+ 'hasReachTime' => $hasReachTime,
|
|
|
+ 'reachPeriod' => $reachPeriod,
|
|
|
+ 'reachDate' => $reachDate,
|
|
|
+ 'shopId' => $shopId,
|
|
|
+ 'staffName' => $staffName,
|
|
|
+ 'staffId' => $staffId,
|
|
|
+ 'bookName' => $bookName,
|
|
|
+ 'orderPrice' => $orderPrice,
|
|
|
+ 'orderDebtPrice' => $orderDebtPrice,
|
|
|
+ 'fromType' => $fromType,
|
|
|
+ 'sendType' => $sendType,
|
|
|
+ 'remark' => $remark,
|
|
|
+ 'thirdSn' => $thirdSn,
|
|
|
+ 'cardInfo' => $cardInfo,
|
|
|
+ 'anonymity' => $anonymity,
|
|
|
+ 'sh' => $sh,
|
|
|
+ 'manyType' => $manyType,
|
|
|
+ 'sendNum' => $sendNum,
|
|
|
+ ];
|
|
|
+ $main = MainClass::getById($mainId, true);
|
|
|
+ if (empty($main)) {
|
|
|
+ util::fail('没有main信息37');
|
|
|
+ }
|
|
|
+ WorkClass::goodsCreateFinish($params, $main);
|
|
|
}
|
|
|
- $sh = $goodsData['sh'] ?? 0;
|
|
|
- $params = [
|
|
|
- 'goods' => [['productId' => $goodsId, 'num' => abs($num)]],
|
|
|
- 'sjId' => $sjId,
|
|
|
- 'mainId' => $mainId,
|
|
|
- 'orderSn' => $orderSn,
|
|
|
- 'orderId' => $orderId,
|
|
|
- 'hasReachTime' => $hasReachTime,
|
|
|
- 'reachPeriod' => $reachPeriod,
|
|
|
- 'reachDate' => $reachDate,
|
|
|
- 'shopId' => $shopId,
|
|
|
- 'staffName' => $staffName,
|
|
|
- 'staffId' => $staffId,
|
|
|
- 'bookName' => $bookName,
|
|
|
- 'orderPrice' => $orderPrice,
|
|
|
- 'orderDebtPrice' => $orderDebtPrice,
|
|
|
- 'fromType' => $fromType,
|
|
|
- 'sendType' => $sendType,
|
|
|
- 'remark' => $remark,
|
|
|
- 'thirdSn' => $thirdSn,
|
|
|
- 'cardInfo' => $cardInfo,
|
|
|
- 'anonymity' => $anonymity,
|
|
|
- 'sh' => $sh,
|
|
|
- 'manyType' => $manyType,
|
|
|
- 'sendNum' => $sendNum,
|
|
|
- ];
|
|
|
- $main = MainClass::getById($mainId, true);
|
|
|
- if (empty($main)) {
|
|
|
- util::fail('没有main信息37');
|
|
|
- }
|
|
|
- WorkClass::goodsCreateFinish($params, $main);
|
|
|
-
|
|
|
|
|
|
$data['stock'] = $newStock;
|
|
|
self::updateById($goodsId, $data);
|