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.

250 lines
7.5 KiB

  1. <?php
  2. namespace goods\models\ars;
  3. use Yii;
  4. use yii\behaviors\TimestampBehavior;
  5. use goods\models\ars\Category;
  6. use goods\models\ars\ShopCategory;
  7. use backend\models\ars\File;
  8. use goods\models\ars\Brand;
  9. use 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. //无限库存
  70. const UNLIMITED_STOCK = -1;
  71. public $ruleVerify = 0;//规则验证是否通过
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public static function tableName()
  76. {
  77. return 'atg_goods';
  78. }
  79. public function fields()
  80. {
  81. $fields = parent::fields();
  82. unset($fields['is_sale']);
  83. unset($fields['sort_order']);
  84. unset($fields['pid']);
  85. unset($fields['cat_id']);
  86. unset($fields['brand_id']);
  87. unset($fields['shop_cat_id']);
  88. unset($fields['is_delete']);
  89. unset($fields['express_template']);
  90. unset($fields['model_id']);
  91. return $fields;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function rules()
  97. {
  98. return [
  99. [['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'],
  100. [['cat_id', 'brand_id', 'shop_cat_id', 'name'], 'required'],
  101. [['sn'], 'checkExist'],
  102. [['description', 'coverImageId', 'detailImageId'], 'string'],
  103. [['name'], 'string', 'max' => 120],
  104. [['sn'], 'string', 'max' => 60],
  105. [['code'], 'string', 'max' => 50],
  106. [['unit'], 'string', 'max' => 16],
  107. [['brief'], 'string', 'max' => 255],
  108. [['weight', 'length', 'width', 'height', 'diameter', 'sold_count', 'market_price', 'price'], 'checkNegative'],
  109. ];
  110. }
  111. /**
  112. * @param $attribute
  113. * @param $params
  114. * 验证是否为负数
  115. */
  116. public function checkNegative($attribute, $params)
  117. {
  118. if ($this->$attribute < 0) {
  119. $this->addError($attribute, "不得为负数");
  120. }
  121. }
  122. /**
  123. * @param $attribute
  124. * @param $params
  125. * 验证商品编号唯一
  126. */
  127. public function checkExist($attribute, $params)
  128. {
  129. $goods = self::find()->where([$attribute => $this->$attribute, 'is_delete' => 0])->one();
  130. if ($this->isNewRecord) {
  131. if ($goods) {
  132. $this->addError($attribute, "该商品编号已经存在");
  133. }
  134. } else {
  135. if ($goods && $goods->id != $this->id) {
  136. $this->addError($attribute, "该商品编号已经存在");
  137. }
  138. }
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function attributeLabels()
  144. {
  145. return [
  146. 'id' => 'id',
  147. 'pid' => '父级id',
  148. 'cat_id' => '后台商品类别',
  149. 'brand_id' => '品牌',
  150. 'shop_cat_id' => '前端商品类别',
  151. 'name' => '商品名称',
  152. 'sn' => '商品唯一货号',
  153. 'code' => '商品货码',
  154. 'supplier_id' => '供应商',
  155. 'weight' => '重量',
  156. 'length' => '长度',
  157. 'width' => '宽度',
  158. 'height' => '高度',
  159. 'diameter' => '直径',
  160. 'unit' => '单位',
  161. 'sold_count' => '已售数量',
  162. 'limit_count' => '限购数量',
  163. 'stock' => '库存',
  164. 'stock_warn' => '库存警告',
  165. 'market_price' => '市场价',
  166. 'price' => '销售价',
  167. 'brief' => '简介',
  168. 'description' => '详细介绍',
  169. 'image' => '首页图片',
  170. 'model_id' => '模型id',
  171. 'is_sale' => '销售状态',
  172. 'sort_order' => '排序',
  173. 'bouns_points' => '奖励积分',
  174. 'experience_points' => '经验值',
  175. 'is_delete' => '是否删除,1为已删除',
  176. 'express_template' => '配送详情id',
  177. 'created_at' => '创建时间',
  178. 'updated_at' => '更新时间',
  179. ];
  180. }
  181. /**
  182. * @author linyao
  183. * @email 602604991@qq.com
  184. * @created Nov 8, 2019
  185. *
  186. * 行为存储创建时间和更新时间
  187. */
  188. public function behaviors()
  189. {
  190. return [
  191. [
  192. 'class' => TimestampBehavior::className(),
  193. 'createdAtAttribute' => 'created_at',
  194. 'updatedAtAttribute' => 'updated_at',
  195. 'value' => function() {
  196. return time();
  197. },
  198. ],
  199. ];
  200. }
  201. /**
  202. * @param bool $insert
  203. * @return bool
  204. * 自动填入参数
  205. */
  206. public function beforeSave($insert)
  207. {
  208. if (!$this->sn) {
  209. $this->sn = time() . rand(1111, 9999);
  210. }
  211. return parent::beforeSave($insert); // TODO: Change the autogenerated stub
  212. }
  213. public function getCategory()
  214. {
  215. return $this->hasOne(Category::className(), ['id' => 'cat_id']);
  216. }
  217. public function getShopCategory()
  218. {
  219. return $this->hasOne(ShopCategory::className(), ['id' => 'shop_cat_id']);
  220. }
  221. public function getImageFile()
  222. {
  223. return $this->hasOne(File::className(), ['id' => 'image']);
  224. }
  225. public function getBrand()
  226. {
  227. return $this->hasOne(Brand::className(), ['id' => 'brand_id']);
  228. }
  229. public function getSupplier()
  230. {
  231. return $this->hasOne(Supplier::className(), ['id' => 'supplier_id']);
  232. }
  233. }