You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

578 lines
20 KiB

<?php
namespace antgoods\logic\goods;
use common\models\ars\File;
use Yii;
use yii\base\Exception;
use antgoods\models\ars\GoodsAttr;
use antgoods\models\ars\Attribute;
use antgoods\models\ars\GoodsSku;
use antgoods\models\ars\Goods;
use antgoods\models\ars\FilterAttr;
use antgoods\models\ars\Category;
class GoodsManager
{
/**
* @param $newFileIdArr
* @param $goodsModel
* @param array $oldFileIdArr
* @param int $fileType
* @return array
* 保存新文件,删除不需要的文件操作
*/
public static function saveFile($newFileIdArr, $goodsModel, $oldFileIdArr = [], $fileType = 1)
{
//判断类名是否存在
if(!class_exists('\backend\logic\file\FileManager') || !class_exists('\common\models\ars\File')) {
return ['status' => false, 'info' => '操作失败'];
}
//需要新建的文件id
$createFileIdArr = array_diff($newFileIdArr, $oldFileIdArr);
//创建文件
$class = new \backend\logic\file\FileManager();
$createFileRes = $class->saveTemFileToFile($createFileIdArr, $goodsModel->id, $fileType);
//需要删除的文件id
$delFileIdArr = array_diff($oldFileIdArr, $newFileIdArr);
//删除文件
$class->deleteFile($delFileIdArr);
//记录第一张图片id
$firstFileId = 0;
//查看修改数组是否为空
if (!$newFileIdArr[0]) {
$firstFileId = null;
}else {
if ($createFileRes['status']) {
$firstFileId = $createFileRes['first_file_id'];
}
}
return ['status' => true, 'info' => '操作成功', 'first_file_id' => $firstFileId];
}
/**
* @param $data
* @param $model
* @param null $coverImageOldIdStr
* @param null $detailImageOldIdStr
* @return array
* @throws \Throwable
* 创建修改商品操作
*/
public static function updateGoods($data, $model, $coverImageOldIdStr = null, $detailImageOldIdStr = null)
{
$attribute = $data['attribute'];
$filterAttribute = $data['filterattribute'];
$tra = Yii::$app->db->beginTransaction();
try {
if (!$model->save()) {
throw new Exception('');
}
$saveCoverImageRes = self::saveFile(explode(',', $model->coverImageId), $model, explode(',', $coverImageOldIdStr));
$saveDetailImageRes = self::saveFile(explode(',', $model->detailImageId), $model, explode(',', $detailImageOldIdStr), File::OWN_TYPE_GOODS_DETAILS);
if ($saveCoverImageRes['status'] && $saveDetailImageRes['status']) {
if($saveCoverImageRes['first_file_id'] !== 0) {
$model->image = $saveCoverImageRes['first_file_id'];
if (!$model->save()) {
throw new Exception('图片保存失败');
}
}
} else {
throw new Exception('图片保存失败');
}
self::addAttributeOperating(['id' => $model->id, 'attribute' => $attribute]);
self::addFilterAttributeOperating(['id' => $model->id, 'filterAttribute' => $filterAttribute]);
$tra->commit();
return ['status' => true];
} catch (\yii\base\Exception $e) {
$tra->rollBack();
return ['status' => false, 'info' => $e->getMessage()];
}
}
/**
* @param $data
* @return bool
* @throws Exception
* 创建修改商品属性操作
*/
public static function addAttributeOperating($data)
{
if (!$data['attribute']) {
return true;
}
$data['attribute'] = json_decode($data['attribute'], true);
$oldAttr = [];
$goodsAttr = GoodsAttr::find()->where(['goods_id' => $data['id'], 'is_delete' => GoodsAttr::IS_DELETE_NO])->all();
if ($goodsAttr) { //如果商品有旧的属性
if(count($data['attribute']) == 0 && is_array($data['attribute'])) { //如果传上来的是空数组,删除该商品下的全部属性
self::delAttribute($goodsAttr);
return true;
}
foreach ($goodsAttr as $key => $value) { //把旧的商品属性保存到一个数组
$oldAttr[$value->id] = $value->attr_value;
}
}
$newAttr = self::addAttribute($data['attribute'], $data['id']); //添加新的商品属性
$delAttr = array_diff(array_keys($oldAttr), array_keys($newAttr)); //找出需要删除的goodsAttrId
if (!$delAttr) {
return true;
}
foreach ($delAttr as $value) {
$model = GoodsAttr::find()->where(['id' => $value, 'is_delete' => GoodsAttr::IS_DELETE_NO])->One();
if ($model) {
$model->is_delete = GoodsAttr::IS_DELETE_YES;
if (!$model->save()) {
throw new Exception('goodsAttribute delete false');
}
}
}
}
/**
* @param $goodsAttr
* @throws Exception
* 删除商品属性
*/
public static function delAttribute($goodsAttr)
{
foreach ($goodsAttr as $key => $value) {
$value->is_delete = GoodsAttr::IS_DELETE_YES;
if (!$value->save()) {
throw new Exception('goods attribute delete false');
}
}
}
/**
* @param $attribute
* @param $goodsId
* @return array
* @throws Exception
* 保存商品属性
*/
public static function addAttribute($attribute, $goodsId)
{
$newAttr = [];
if (!$attribute) {
return [];
}
foreach ($attribute as $value) {
foreach ($value['value'] as $k => $v) {
$goodsAttrModel = new GoodsAttr();
$goodsAttrModel->attr_id = $value['id'];
$attr = GoodsAttr::find()->where(['goods_id' => $goodsId, 'attr_id' => $value['id'], 'attr_value' => $v, 'is_delete' => GoodsAttr::IS_DELETE_NO])->one();
if ($attr) {
$newAttr[$attr->id] = $attr->attr_value; //原有的数据
} else {
$goodsAttrModel->goods_id = $goodsId;
$goodsAttrModel->attr_value = $v;
if (!$goodsAttrModel->save()) {
throw new Exception('goodsAttribute save false');
}
$newAttr[$goodsAttrModel->id] = $goodsAttrModel->attr_value; //新增的数据
}
}
}
return $newAttr;
}
/**
* @param $id
* @return Attribute|array|null
* 获取属性信息
*/
public static function getAttribute($id)
{
$goodsAttributes = GoodsAttr::find()->where(['goods_id' => $id, 'is_delete' => GoodsAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all();
$filter = [];
$goodsAttributeModel = [];
if (!$goodsAttributes) {
return $goodsAttributeModel;
}
foreach ($goodsAttributes as $key => $value) {
$attribute = Attribute::findOne($value->attr_id);
if (!in_array($attribute->name, $filter)) {
$filter[] = $attribute->name;
$attribute = ['name' => $attribute->name, 'id' => $attribute->id, 'value' => [$value->attr_value]];
$goodsAttributeModel[] = $attribute;
} else {
foreach ($goodsAttributeModel as $k => $v) {
if ($v['name'] == $attribute->name) {
$goodsAttributeModel[$k]['value'][] = $value->attr_value;
}
}
}
}
return $goodsAttributeModel;
}
/**
* @param $id
* @return array
* 获取sku信息
*/
public static function getSkuInfo($id)
{
$skus = GoodsSku::find()->where(['goods_id' => $id, 'is_delete' => GoodsSku::IS_DELETE_NO])->all();
$attrId = [];
foreach ($skus as $sku) {
$attrId = array_merge(explode(',', $sku->goods_attr), $attrId);
}
$attrs = GoodsAttr::find()->where(['id' => $attrId, 'is_delete' => GoodsAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all();
$checkAttr = [];
$filterAttr = [];
foreach ($attrs as $value) {
$attr = Attribute::findOne(['id' => $value->attr_id, 'is_delete' => Attribute::IS_DELETE_NO]);
if (!in_array($attr->name, $filterAttr)) {
$filterAttr[] = $attr->name;
$newAttr = ['id' => $attr->id, 'name' => $attr->name, 'value' => [$value->attr_value]];
$checkAttr[] = $newAttr;
} else {
foreach ($checkAttr as $key => $item) {
if ($item['name'] == $attr->name) { //如果attr_id与$filter的attr_id相符,入栈
$checkAttr[$key]['value'][] = $value->attr_value;
}
}
}
}
return $checkAttr;
}
/**
* @param $id
* @return mixed
* 已创建sku信息
*/
public static function getCreatedSku($id, $type = 0)
{
$goods = Goods::findOne($id);
$data['type'] = $type ?: $goods->sku_mode;
$data['data'] = [];
if ($data['type'] == Goods::SKU_MODE_ATTR) {
$sku = GoodsSku::find()
->where(['goods_id' => $id, 'is_manaul' => 0])
->all();
} else {
$sku = GoodsSku::find()
->where(['goods_id' => $id, 'is_manaul' => 1])
->all();
}
foreach ($sku as $value) {
$data['data'][] = self::getAttrInfo($data['type'], $value);
}
return $data;
}
/**
* @param $type
* @param $sku
* @return array
* 获取商品详情
*/
public static function getAttrInfo($type, $sku)
{
$data = [];
if ($type == Goods::SKU_MODE_ATTR) {
$data['value'] = array_filter(explode(',', $sku->goods_attr));
} else {
$attr = explode(',', $sku->goods_attr);
$goodsAttr = GoodsAttr::findOne($attr[0]);
$data['value'] = $goodsAttr->attr_value;
}
$data['id'] = $sku->id;
$data['price'] = $sku->price;
$data['stock'] = $sku->stock;
$data['weight'] = $sku->weight;
return $data;
}
/**
* @param $id
* @return array
* 获取商品属性
*/
public static function getAttrs($id)
{
$attrId = GoodsAttr::find()->where(['goods_id' => $id, 'is_delete' => GoodsAttr::IS_DELETE_NO])
->andWhere(['>', 'attr_id', 0])
->select('attr_id')
->distinct()
->all();
$attributes = [];
foreach ($attrId as $v) {
$attribute = Attribute::findOne($v);
if ($attribute && $attribute->type == Attribute::TYPE_ATTR) {
$ret['name'] = $attribute->name;
$ret['id'] = $attribute->id;
$ret['attrValue'] = self::getAttrValue($attribute->id, $id);
$attributes[] = $ret;
}
}
return $attributes;
}
/**
* @param $attrId
* @param $goodsId
* @return GoodsAttr[]|GoodsSku[]|array|File[]|\common\models\ars\TemFile[]|\yii\db\ActiveRecord[]
* 获取属性值
*/
public static function getAttrValue($attrId, $goodsId)
{
return GoodsAttr::find()
->select(['id', 'attr_value'])
->where(['goods_id' => $goodsId])
->andWhere(['attr_id' => $attrId])
->asArray()
->all();
}
/**
* @param $type
* @param $goodsId
* @return array
* 获取已存储的商品sku的id
*/
public static function getOriginalIds($type, $goodsId)
{
$ids = [];
if ($type == Goods::SKU_MODE_MANUAL) {
$query = GoodsSku::find()
->where(['is_manaul' => 1]);
} else {
$query = GoodsSku::find()
->where(['is_manaul' => 0]);
}
$sku = $query
->andWhere(['goods_id' => $goodsId])
->all();
foreach ($sku as $value) {
$ids[] = $value->id;
}
return $ids;
}
/**
* @param $sku
* @throws \yii\db\Exception
* 添加或更新sku数据
*/
public static function AddOrUpdateData($sku, $type, $goodsId)
{
$goodsModel = Goods::findOne($goodsId);
if ($sku['id'] > 0) {
$goodsSku = GoodsSku::findOne($sku['id']);
$attrId = array_filter(explode(',', $goodsSku->goods_attr));
$attr = GoodsAttr::findOne($attrId[0]);
} else {
$goodsSku = new GoodsSku();
$attr = new GoodsAttr();
}
if (!$attr || !$goodsSku || !$goodsModel) {
throw new \yii\db\Exception('系统异常');
}
if ($type == Goods::SKU_MODE_MANUAL) {
$attr->attr_value = $sku['value'];
if (!$attr->save()) {
throw new \yii\db\Exception('手动属性修改失败');
}
$goodsSku->goods_attr = (string)$attr->id;
$goodsSku->is_manaul = 1;
} else {
$goodsSku->goods_attr = implode(',', array_filter($sku['value']));
}
$goodsSku->goods_id = $goodsId;
$goodsSku->price = $sku['price'];
$goodsSku->stock = $sku['stock'];
$goodsSku->weight = $sku['weight'];
$goodsSku->goods_sn = $goodsModel->sn;
if (!$goodsSku->save()) {
throw new \yii\db\Exception('保存失败,请检查是否有重复规格');
}
$goods = Goods::findOne($goodsId);
$goods->sku_mode = $type;
if (!$goods->save()) {
throw new \yii\db\Exception('商品sku类型修改失败');
}
}
/**
* @param $type
* @param $data
* @param $goodsId
* @return bool
* @throws \Throwable
* @throws \yii\db\StaleObjectException
* 删除sku
*/
public static function deleteSku($type, $data, $goodsId)
{
if (!$data['originalIds']) {
return true;
}
if ($type == Goods::SKU_MODE_MANUAL) {
$query = GoodsSku::find()
->where(['is_manaul' => 1]);
} else {
$query = GoodsSku::find()
->where(['is_manaul' => 0]);
}
$sku = $query
->andWhere(['goods_id' => $goodsId])
->andWhere(['in', 'id', $data['originalIds']])
->andWhere(['not in', 'id', $data['acceptIds']])
->all();
foreach ($sku as $value) {
$value->delete();
}
}
/**
* @param $data
* @return bool
* @throws Exception
* 创建修改商品筛选属性操作
*/
public static function addFilterAttributeOperating($data)
{
if (!$data['filterAttribute']) {
return true;
}
$data['filterAttribute'] = json_decode($data['filterAttribute'], true);
$oldFilterAttr = [];
$goodsFilterAttr = FilterAttr::find()->where(['goods_id' => $data['id'], 'is_delete' => FilterAttr::IS_DELETE_NO])->all();
if ($goodsFilterAttr) { //如果商品有旧的属性
if(count($data['filterAttribute']) == 0 && is_array($data['filterAttribute'])) { //如果传上来的是空数组,删除该商品下的全部属性
self::delFilterAttribute($goodsFilterAttr);
return true;
}
foreach ($goodsFilterAttr as $key => $value) { //把旧的商品属性保存到一个数组
$oldFilterAttr[$value->id] = $value->attr_value;
}
}
$newAttr = self::addFilterAttribute($data['filterAttribute'], $data['id']); //添加新的商品属性
$delAttr = array_diff(array_keys($oldFilterAttr), array_keys($newAttr)); //找出需要删除的goodsAttrId
if (!$delAttr) {
return true;
}
foreach ($delAttr as $value) {
$model = FilterAttr::find()->where(['id' => $value, 'is_delete' => FilterAttr::IS_DELETE_NO])->One();
if ($model) {
$model->is_delete = FilterAttr::IS_DELETE_YES;
if (!$model->save()) {
throw new Exception('goodsAttribute delete false');
}
}
}
}
/**
* @param $goodsFilterAttr
* @throws Exception
* 删除商品筛选属性
*/
public static function delFilterAttribute($goodsFilterAttr)
{
foreach ($goodsFilterAttr as $key => $value) {
$value->is_delete = FilterAttr::IS_DELETE_YES;
if (!$value->save()) {
throw new Exception('goods attribute delete false');
}
}
}
/**
* @param $attribute
* @param $goodsId
* @return array
* @throws Exception
* 保存商品筛选属性
*/
public static function addFilterAttribute($attribute, $goodsId)
{
$newAttr = [];
if (!$attribute) {
return [];
}
foreach ($attribute as $value) {
foreach ($value['value'] as $k => $v) {
$goodsFilterAttrModel = new FilterAttr();
$goodsFilterAttrModel->attr_id = $value['id'];
$attr = FilterAttr::find()->where(['goods_id' => $goodsId, 'attr_id' => $value['id'], 'attr_value' => $v, 'is_delete' => FilterAttr::IS_DELETE_NO])->one();
if ($attr) {
$newAttr[$attr->id] = $attr->attr_value; //原有的数据
} else {
$goodsFilterAttrModel->goods_id = $goodsId;
$goodsFilterAttrModel->attr_value = $v;
if (!$goodsFilterAttrModel->save()) {
throw new Exception('goodsAttribute save false');
}
$newAttr[$goodsFilterAttrModel->id] = $goodsFilterAttrModel->attr_value; //新增的数据
}
}
}
return $newAttr;
}
/**
* @param $id
* @return Attribute|array|null
* 获取筛选属性信息
*/
public static function getFilterAttribute($id)
{
$goodsFilterAttributes = FilterAttr::find()->where(['goods_id' => $id, 'is_delete' => FilterAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all();
$filter = [];
$goodsFilterAttributeModel = [];
if (!$goodsFilterAttributes) {
return $goodsFilterAttributeModel;
}
foreach ($goodsFilterAttributes as $key => $value) {
$attribute = Attribute::findOne($value->attr_id);
if (!in_array($attribute->name, $filter)) {
$filter[] = $attribute->name;
$attribute = ['name' => $attribute->name, 'id' => $attribute->id, 'value' => [$value->attr_value]];
$goodsFilterAttributeModel[] = $attribute;
} else {
foreach ($goodsFilterAttributeModel as $k => $v) {
if ($v['name'] == $attribute->name) {
$goodsFilterAttributeModel[$k]['value'][] = $value->attr_value;
}
}
}
}
return $goodsFilterAttributeModel;
}
/**
* @param $goodsModel
* @return bool
*
*/
public static function judgeGoodsCategory($goodsModel)
{
$skus = GoodsSku::find()->where(['goods_id' => $goodsModel->id, 'is_delete' => GoodsSku::IS_DELETE_NO])->all();
$attrId = [];
foreach ($skus as $sku) {
$attrId = array_merge(explode(',', $sku->goods_attr), $attrId);
}
$goodsAttr = array_unique(GoodsAttr::find()->select(['attr_id'])->where(['id' => $attrId, 'is_delete' => GoodsAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->column());
$attrArr = array_unique(Attribute::find()->select(['cat_id'])->where(['is_delete' => Attribute::IS_DELETE_NO, 'id' => $goodsAttr])->column());
if (in_array($goodsModel->cat_id, $attrArr)) {
return true; //存在,则返回true,表示后台分类不得修改
}
return false; //否则返回false,表示后台分类可以修改
}
public static function categoryBtreeList()
{
}
}