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.

232 lines
7.1 KiB

  1. <?php
  2. namespace backend\modules\goods\models\ars;
  3. use Yii;
  4. use yii\behaviors\TimestampBehavior;
  5. use backend\modules\goods\models\ars\Category;
  6. use backend\modules\goods\models\ars\ShopCategory;
  7. use backend\modules\file\models\ars\File;
  8. use backend\modules\goods\models\ars\Brand;
  9. use backend\modules\goods\models\ars\Supplier;
  10. /**
  11. * This is the model class for table "atg_goods".
  12. *
  13. * @property int $id
  14. * @property int $pid 父级id
  15. * @property int $cat_id 后台商品类别id
  16. * @property int $brand_id 品牌id
  17. * @property int $shop_cat_id 前端商品类别id
  18. * @property string $name 商品名称
  19. * @property string $sn 商品唯一货号
  20. * @property string $code 商品货码
  21. * @property int $supplier_id 供应商id
  22. * @property int $weight 重量
  23. * @property int $length 长度
  24. * @property int $width 宽度
  25. * @property int $height 高度
  26. * @property int $diameter 直径
  27. * @property string $unit 单位
  28. * @property int $sold_count 已售数量
  29. * @property int $limit_count 限购数量
  30. * @property int $stock 库存
  31. * @property int $stock_warn 库存警告
  32. * @property int $market_price 市场价
  33. * @property int $price 销售价
  34. * @property string $brief 简介
  35. * @property string $description 详细介绍
  36. * @property int $image 图片id
  37. * @property int $model_id 模型id
  38. * @property int $is_sale 该商品是否开放销售,1为是,0为否
  39. * @property int $sort_order 排序
  40. * @property int $bouns_points 奖励积分
  41. * @property int $experience_points 经验值
  42. * @property int $is_delete 是否删除,1为已删除
  43. * @property int $express_template 配送详情id
  44. * @property int $created_at 创建时间
  45. * @property int $updated_at 更新时间
  46. * @property int $sku_mode sku类型
  47. */
  48. class Goods extends \yii\db\ActiveRecord
  49. {
  50. //商品封面图
  51. public $coverImagePath;
  52. public $coverImageId;
  53. //商品详情图
  54. public $detailImagePath;
  55. public $detailImageId;
  56. //是否删除is_delete
  57. const IS_DELETE_NO = 0;//未删除
  58. const IS_DELETE_YES = 1;//已删除
  59. //该商品是否开放销售is_sale
  60. const IS_SALE_NO = 0;//否
  61. const IS_SALE_YES = 1;//是
  62. //sku类型
  63. const SKU_MODE_ATTR = 1;//SKU类型属性
  64. const SKU_MODE_MANUAL = 2;//SKU类型手写
  65. public static $isSale = [
  66. self::IS_SALE_NO => '不在售',
  67. self::IS_SALE_YES => '在售'
  68. ];
  69. public $ruleVerify = 0;//规则验证是否通过
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public static function tableName()
  74. {
  75. return 'atg_goods';
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function rules()
  81. {
  82. return [
  83. [['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'],
  84. [['cat_id', 'brand_id', 'shop_cat_id', 'name'], 'required'],
  85. [['sn'], 'checkExist'],
  86. [['description', 'coverImageId', 'detailImageId'], 'string'],
  87. [['name'], 'string', 'max' => 120],
  88. [['sn'], 'string', 'max' => 60],
  89. [['code'], 'string', 'max' => 50],
  90. [['unit'], 'string', 'max' => 16],
  91. [['brief'], 'string', 'max' => 255],
  92. [['weight', 'length', 'width', 'height', 'diameter', 'sold_count', 'market_price', 'price'], 'checkNegative'],
  93. ];
  94. }
  95. /**
  96. * @param $attribute
  97. * @param $params
  98. * 验证是否为负数
  99. */
  100. public function checkNegative($attribute, $params)
  101. {
  102. if ($this->$attribute < 0) {
  103. $this->addError($attribute, "不得为负数");
  104. }
  105. }
  106. /**
  107. * @param $attribute
  108. * @param $params
  109. * 验证商品编号唯一
  110. */
  111. public function checkExist($attribute, $params)
  112. {
  113. $goods = self::find()->where([$attribute => $this->$attribute, 'is_delete' => 0])->one();
  114. if ($this->isNewRecord) {
  115. if ($goods) {
  116. $this->addError($attribute, "该商品编号已经存在");
  117. }
  118. } else {
  119. if ($goods && $goods->id != $this->id) {
  120. $this->addError($attribute, "该商品编号已经存在");
  121. }
  122. }
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function attributeLabels()
  128. {
  129. return [
  130. 'id' => 'id',
  131. 'pid' => '父级id',
  132. 'cat_id' => '后台商品类别',
  133. 'brand_id' => '品牌',
  134. 'shop_cat_id' => '前端商品类别',
  135. 'name' => '商品名称',
  136. 'sn' => '商品唯一货号',
  137. 'code' => '商品货码',
  138. 'supplier_id' => '供应商',
  139. 'weight' => '重量',
  140. 'length' => '长度',
  141. 'width' => '宽度',
  142. 'height' => '高度',
  143. 'diameter' => '直径',
  144. 'unit' => '单位',
  145. 'sold_count' => '已售数量',
  146. 'limit_count' => '限购数量',
  147. 'stock' => '库存',
  148. 'stock_warn' => '库存警告',
  149. 'market_price' => '市场价',
  150. 'price' => '销售价',
  151. 'brief' => '简介',
  152. 'description' => '详细介绍',
  153. 'image' => '首页图片',
  154. 'model_id' => '模型id',
  155. 'is_sale' => '销售状态',
  156. 'sort_order' => '排序',
  157. 'bouns_points' => '奖励积分',
  158. 'experience_points' => '经验值',
  159. 'is_delete' => '是否删除,1为已删除',
  160. 'express_template' => '配送详情id',
  161. 'created_at' => '创建时间',
  162. 'updated_at' => '更新时间',
  163. ];
  164. }
  165. /**
  166. * @author linyao
  167. * @email 602604991@qq.com
  168. * @created Nov 8, 2019
  169. *
  170. * 行为存储创建时间和更新时间
  171. */
  172. public function behaviors()
  173. {
  174. return [
  175. [
  176. 'class' => TimestampBehavior::className(),
  177. 'createdAtAttribute' => 'created_at',
  178. 'updatedAtAttribute' => 'updated_at',
  179. 'value' => function() {
  180. return time();
  181. },
  182. ],
  183. ];
  184. }
  185. /**
  186. * @param bool $insert
  187. * @return bool
  188. * 自动填入参数
  189. */
  190. public function beforeSave($insert)
  191. {
  192. if (!$this->sn) {
  193. $this->sn = time() . rand(1111, 9999);
  194. }
  195. return parent::beforeSave($insert); // TODO: Change the autogenerated stub
  196. }
  197. public function getCategory()
  198. {
  199. return $this->hasOne(Category::className(), ['id' => 'cat_id']);
  200. }
  201. public function getShopCategory()
  202. {
  203. return $this->hasOne(ShopCategory::className(), ['id' => 'shop_cat_id']);
  204. }
  205. public function getImageFile()
  206. {
  207. return $this->hasOne(File::className(), ['id' => 'image']);
  208. }
  209. public function getBrand()
  210. {
  211. return $this->hasOne(Brand::className(), ['id' => 'brand_id']);
  212. }
  213. public function getSupplier()
  214. {
  215. return $this->hasOne(Supplier::className(), ['id' => 'supplier_id']);
  216. }
  217. }