Browse Source

商品表增加sku类型字段,增加sku添加页面及样式,开发sku的逻辑方法,完成sku的添加修改删除逻辑

wechat_public_accounts
linyaostalker 5 years ago
parent
commit
3bc42973c9
  1. 64
      vendor/antgoods/goods/src/assets/custom/sku_item.6649e882bedbeb22ea3d.js
  2. 55
      vendor/antgoods/goods/src/controllers/GoodsController.php
  3. 167
      vendor/antgoods/goods/src/logic/goods/GoodsManager.php
  4. 20
      vendor/antgoods/goods/src/migrations/m191129_010349_add_column_sku_mode_in_antgoods_goods.php
  5. 6
      vendor/antgoods/goods/src/models/ars/Goods.php
  6. 15
      vendor/antgoods/goods/src/views/goods/sku_edit.php
  7. 21
      vendor/antgoods/goods/src/web/GoodsSkuEditAsset.php

64
vendor/antgoods/goods/src/assets/custom/sku_item.6649e882bedbeb22ea3d.js
File diff suppressed because it is too large
View File

55
vendor/antgoods/goods/src/controllers/GoodsController.php

@ -268,4 +268,59 @@ class GoodsController extends Controller
}
return json_encode($res);
}
/**
* @param $id
* @return string
* 商品编辑sku
*/
public function actionEditSku($id)
{
$sku = GoodsManager::getCreatedSku($id);
$attributes = GoodsManager::getAttrs($id);
return $this->render('sku_edit', [
'attributes' => $attributes,
'sku' => $sku,]);
}
/**
* @return array
* 添加sku
*/
public function actionAddSku()
{
$data = [];
Yii::$app->response->format = 'json';
$res = Yii::$app->request->post('sku');
$goodsId = Yii::$app->request->post('goodsId');
$tra = Yii::$app->db->beginTransaction();
try {
$data['originalIds'] = GoodsManager::getOriginalIds($res['type'], $goodsId);
$data['acceptIds'] = [];
foreach ($res['data'] as $sku) {
GoodsManager::AddOrUpdateData($sku, $res['type'], $goodsId);
if ($sku['id'] > 0) {
$data['acceptIds'][] = $sku['id'];
}
}
GoodsManager::deleteSku($res['type'], $data, $goodsId);
$tra->commit();
return ['status' => true];
} catch (\Exception $e) {
$tra->rollBack();
return ['status' => false, 'info' => $e->getMessage()];
}
}
public function actionSwitch()
{
Yii::$app->response->format = 'json';
$data = [];
$type = Yii::$app->request->get('type');
$id = Yii::$app->request->get('goodsId');
$data['sku'] = GoodsManager::getCreatedSku($id, $type);
$data['attributes'] = GoodsManager::getAttrs($id, $type);
return $data;
}
}

167
vendor/antgoods/goods/src/logic/goods/GoodsManager.php

@ -7,6 +7,7 @@ use yii\base\Exception;
use antgoods\goods\models\ars\GoodsAttr;
use antgoods\goods\models\ars\Attribute;
use antgoods\goods\models\ars\GoodsSku;
use antgoods\goods\models\ars\Goods;
class GoodsManager
{
@ -239,4 +240,170 @@ class GoodsManager
}
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->goods_weight;
return $data;
}
/**
* @param $id
* @return array
* 获取商品属性
*/
public static function getAttrs($id)
{
$attrId = GoodsAttr::find()->where(['goods_id' => $id])
->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;
}
public static function getAttrValue($attrId, $goodsId)
{
return GoodsAttr::find()
->select(['id', 'attr_value'])
->where(['goods_id' => $goodsId])
->andWhere(['attr_id' => $attrId])
->asArray()
->all();
}
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)
{
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) {
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'];
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类型修改失败');
}
}
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();
}
}
}

20
vendor/antgoods/goods/src/migrations/m191129_010349_add_column_sku_mode_in_antgoods_goods.php

@ -0,0 +1,20 @@
<?php
use yii\db\Migration;
/**
* Class m191129_010349_add_column_sku_mode_in_antgoods_goods
*/
class m191129_010349_add_column_sku_mode_in_antgoods_goods extends Migration
{
public function up()
{
$this->addColumn('antgoods_goods', 'sku_mode', $this->tinyInteger(1)->notNull()->defaultValue(1)->comment('sku类型'));
}
public function down()
{
$this->dropColumn('antgoods_goods', 'sku_mode');
return true;
}
}

6
vendor/antgoods/goods/src/models/ars/Goods.php

@ -41,6 +41,7 @@ use yii\behaviors\TimestampBehavior;
* @property int $express_template 配送详情id
* @property int $created_at 创建时间
* @property int $updated_at 更新时间
* @property int $sku_mode sku类型
*/
class Goods extends \yii\db\ActiveRecord
{
@ -56,6 +57,9 @@ class Goods extends \yii\db\ActiveRecord
//该商品是否开放销售is_sale
const IS_SALE_NO = 0;//否
const IS_SALE_YES = 1;//是
//sku类型
const SKU_MODE_ATTR = 1;//SKU类型属性
const SKU_MODE_MANUAL = 2;//SKU类型手写
public static $isSale = [
self::IS_SALE_NO => '否',
self::IS_SALE_YES => '是'
@ -75,7 +79,7 @@ class Goods extends \yii\db\ActiveRecord
public function rules()
{
return [
[['pid', 'cat_id', 'brand_id', 'shop_cat_id', 'supplier_id', 'weight', 'length', 'width', 'height', 'diameter', 'sold_count', 'limit_count', 'stock', 'stock_warn', 'market_price', 'price', 'image', 'model_id', 'is_sale', 'sort_order', 'bouns_points', 'experience_points', 'is_delete', 'express_template'], 'integer'],
[['pid', 'cat_id', 'brand_id', 'shop_cat_id', 'supplier_id', 'weight', 'length', 'width', 'height', 'diameter', 'sold_count', 'limit_count', 'stock', 'stock_warn', 'market_price', 'price', 'image', 'model_id', 'is_sale', 'sort_order', 'bouns_points', 'experience_points', 'is_delete', 'express_template', 'sku_mode'], 'integer'],
[['cat_id', 'brand_id', 'shop_cat_id', 'name'], 'required'],
[['sn'], 'checkExist'],
[['description', 'coverImageId', 'detailImageId'], 'string'],

15
vendor/antgoods/goods/src/views/goods/sku_edit.php

@ -0,0 +1,15 @@
<?php
use antgoods\goods\web\GoodsSkuEditAsset;
$this->title = '添加SKU';
$this->params['breadcrumbs'][] = ['label' => '商品列表', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
GoodsSkuEditAsset::register($this);
?>
<div id="app"></div>
<script>
var sku = <?=json_encode($sku)?>;
var attributes = <?=json_encode($attributes)?>;
var csrfToken = "<?= Yii::$app->request->csrfToken ?>";
</script>

21
vendor/antgoods/goods/src/web/GoodsSkuEditAsset.php

@ -0,0 +1,21 @@
<?php
namespace antgoods\goods\web;
use yii\web\AssetBundle;
/**
* Main backend application asset bundle.
*/
class GoodsSkuEditAsset extends AssetBundle
{
public $sourcePath = "@antgoods/goods/src/assets";
public $css = [
];
public $js = [
'custom/sku_item.6649e882bedbeb22ea3d.js'
];
public $depends = [
'yii\web\YiiAsset',
];
}
Loading…
Cancel
Save