Browse Source

商品增加自动生成商品唯一货号方法

wechat_public_accounts
linyaostalker 5 years ago
parent
commit
e7c91f1b08
  1. 2
      vendor/antgoods/goods/src/models/ars/Brand.php
  2. 6
      vendor/antgoods/goods/src/models/ars/Category.php
  3. 42
      vendor/antgoods/goods/src/models/ars/Goods.php
  4. 9
      vendor/antgoods/goods/src/models/ars/ShopCategory.php
  5. 5
      vendor/antgoods/goods/src/views/goods/_form.php

2
vendor/antgoods/goods/src/models/ars/Brand.php

@ -77,6 +77,6 @@ class Brand extends \yii\db\ActiveRecord
public static function modelColumn() public static function modelColumn()
{ {
return $column = Brand::find()->select(['name'])->where(['is_delete' => self::IS_DELETE_NO])->indexBy('id')->column();
return $column = self::find()->select(['name'])->where(['is_delete' => self::IS_DELETE_NO])->indexBy('id')->column();
} }
} }

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

@ -97,8 +97,12 @@ class Category extends \yii\db\ActiveRecord
]; ];
} }
/**
* @return array
* 数据键值对
*/
public static function modelColumn() public static function modelColumn()
{ {
return $column = Category::find()->select(['name'])->where(['is_delete' => self::IS_DELETE_NO])->indexBy('id')->column();
return $column = self::find()->select(['name'])->where(['is_delete' => self::IS_DELETE_NO])->indexBy('id')->column();
} }
} }

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

@ -47,6 +47,13 @@ class Goods extends \yii\db\ActiveRecord
//是否删除is_delete //是否删除is_delete
const IS_DELETE_NO = 0;//未删除 const IS_DELETE_NO = 0;//未删除
const IS_DELETE_YES = 1;//已删除 const IS_DELETE_YES = 1;//已删除
//该商品是否开放销售is_sale
const IS_SALE_NO = 0;//否
const IS_SALE_YES = 1;//是
public static $isSale = [
self::IS_SALE_NO => '否',
self::IS_SALE_YES => '是'
];
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -63,6 +70,7 @@ class Goods extends \yii\db\ActiveRecord
return [ 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'], 'integer'],
[['cat_id', 'brand_id', 'shop_cat_id', 'name', 'sn'], 'required'], [['cat_id', 'brand_id', 'shop_cat_id', 'name', 'sn'], 'required'],
[['sn'], 'checkExist'],
[['description'], 'string'], [['description'], 'string'],
[['name'], 'string', 'max' => 120], [['name'], 'string', 'max' => 120],
[['sn'], 'string', 'max' => 60], [['sn'], 'string', 'max' => 60],
@ -72,6 +80,25 @@ class Goods extends \yii\db\ActiveRecord
]; ];
} }
/**
* @param $attribute
* @param $params
* 验证商品编号唯一
*/
public function checkExist($attribute, $params)
{
$goods = self::find()->where([$attribute => $this->$attribute, 'is_delete' => 0])->one();
if ($this->isNewRecord) {
if ($goods) {
$this->addError($attribute, "该商品编号已经存在");
}
} else {
if ($goods && $goods->id != $this->id) {
$this->addError($attribute, "该商品编号已经存在");
}
}
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -103,7 +130,7 @@ class Goods extends \yii\db\ActiveRecord
'description' => '详细介绍', 'description' => '详细介绍',
'image' => '图片id', 'image' => '图片id',
'model_id' => '模型id', 'model_id' => '模型id',
'is_sale' => '该商品是否开放销售,1为是,0为否',
'is_sale' => '该商品是否开放销售',
'sort_order' => '排序', 'sort_order' => '排序',
'bouns_points' => '奖励积分', 'bouns_points' => '奖励积分',
'experience_points' => '经验值', 'experience_points' => '经验值',
@ -135,4 +162,17 @@ class Goods extends \yii\db\ActiveRecord
], ],
]; ];
} }
/**
* @param bool $insert
* @return bool
* 自动填入参数
*/
public function beforeSave($insert)
{
if (!$this->sn) {
$this->sn = time() . rand(1111, 9999);
}
return parent::beforeSave($insert); // TODO: Change the autogenerated stub
}
} }

9
vendor/antgoods/goods/src/models/ars/ShopCategory.php

@ -105,4 +105,13 @@ class ShopCategory extends \yii\db\ActiveRecord
], ],
]; ];
} }
/**
* @return array
* 数据键值对
*/
public static function modelColumn()
{
return $column = self::find()->select(['name'])->where(['is_delete' => self::IS_DELETE_NO])->indexBy('id')->column();
}
} }

5
vendor/antgoods/goods/src/views/goods/_form.php

@ -4,6 +4,7 @@ use yii\helpers\Html;
use yii\widgets\ActiveForm; use yii\widgets\ActiveForm;
use antgoods\goods\models\ars\Category; use antgoods\goods\models\ars\Category;
use antgoods\goods\models\ars\Brand; use antgoods\goods\models\ars\Brand;
use antgoods\goods\models\ars\ShopCategory;
/* @var $this yii\web\View */ /* @var $this yii\web\View */
/* @var $model antgoods\goods\models\ars\Goods */ /* @var $model antgoods\goods\models\ars\Goods */
@ -18,7 +19,7 @@ use antgoods\goods\models\ars\Brand;
<?= $form->field($model, 'brand_id')->dropDownList(Brand::modelColumn(), ['prompt' => '请选择']) ?> <?= $form->field($model, 'brand_id')->dropDownList(Brand::modelColumn(), ['prompt' => '请选择']) ?>
<?= $form->field($model, 'shop_cat_id')->textInput() ?>
<?= $form->field($model, 'shop_cat_id')->dropDownList(ShopCategory::modelColumn(), ['prompt' => '请选择']) ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?> <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
@ -60,7 +61,7 @@ use antgoods\goods\models\ars\Brand;
<?= $form->field($model, 'model_id')->textInput() ?> <?= $form->field($model, 'model_id')->textInput() ?>
<?= $form->field($model, 'is_sale')->textInput() ?>
<?= $form->field($model, 'is_sale')->radioList($model::$isSale) ?>
<?= $form->field($model, 'sort_order')->textInput() ?> <?= $form->field($model, 'sort_order')->textInput() ?>

Loading…
Cancel
Save