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.

282 lines
8.8 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. * @property int $is_taking 是否自提
  48. * @property int $is_express 是否快递发货
  49. * @property int $express_type 快递运费方式
  50. * @property int $uniform_postage 统一邮费
  51. */
  52. class Goods extends \yii\db\ActiveRecord
  53. {
  54. //商品封面图
  55. public $coverImagePath;
  56. public $coverImageId;
  57. //商品详情图
  58. public $detailImagePath;
  59. public $detailImageId;
  60. //是否删除is_delete
  61. const IS_DELETE_NO = 0;//未删除
  62. const IS_DELETE_YES = 1;//已删除
  63. //该商品是否开放销售is_sale
  64. const IS_SALE_NO = 0;//否
  65. const IS_SALE_YES = 1;//是
  66. //类型sku
  67. const SKU_MODE_ATTR = 1;//SKU类型属性
  68. const SKU_MODE_MANUAL = 2;//SKU类型手写
  69. //是否自提is_taking
  70. const IS_TAKING_NO = 0; //否
  71. const IS_TAKING_YES = 1; //是
  72. //是否快递发货is_express
  73. const IS_EXPRESS_NO = 0; //否
  74. const IS_EXPRESS_YES = 1; //是
  75. //快递运费计算方式express_type
  76. const EXPRESS_TYPE_UNIFORM_POSTAGE = 1; //统一邮费
  77. const EXPRESS_TYPE_EXPRESS_TEMPLATE = 2; //运费模板
  78. public static $isTaking = [
  79. self::IS_TAKING_NO => '否',
  80. self::IS_TAKING_YES => '是'
  81. ];
  82. public static $isExpress = [
  83. self::IS_EXPRESS_NO => '否',
  84. self::IS_EXPRESS_YES => '是'
  85. ];
  86. public static $expressType = [
  87. self::EXPRESS_TYPE_UNIFORM_POSTAGE => '统一邮费',
  88. self::EXPRESS_TYPE_EXPRESS_TEMPLATE => '运费模板'
  89. ];
  90. public static $isSale = [
  91. self::IS_SALE_NO => '不在售',
  92. self::IS_SALE_YES => '在售'
  93. ];
  94. //无限库存
  95. const UNLIMITED_STOCK = -1;
  96. public $ruleVerify = 0;//规则验证是否通过
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public static function tableName()
  101. {
  102. return 'atg_goods';
  103. }
  104. public function fields()
  105. {
  106. $fields = parent::fields();
  107. unset($fields['is_sale']);
  108. unset($fields['sort_order']);
  109. unset($fields['pid']);
  110. unset($fields['cat_id']);
  111. unset($fields['brand_id']);
  112. unset($fields['shop_cat_id']);
  113. unset($fields['is_delete']);
  114. unset($fields['express_template']);
  115. unset($fields['model_id']);
  116. return $fields;
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function rules()
  122. {
  123. return [
  124. [['pid', 'cat_id', 'brand_id', 'shop_cat_id', 'supplier_id', 'weight', 'length', 'width', 'height', 'diameter', 'sold_count', 'limit_count', 'stock', 'stock_warn', 'image', 'model_id', 'is_sale', 'sort_order', 'bouns_points', 'experience_points', 'is_delete', 'express_template', 'sku_mode', 'is_taking', 'is_express', 'express_type'], 'integer'],
  125. [['cat_id', 'brand_id', 'shop_cat_id', 'name'], 'required'],
  126. [['sn'], 'checkExist'],
  127. [['description', 'coverImageId', 'detailImageId'], 'string'],
  128. [['name'], 'string', 'max' => 120],
  129. [['sn'], 'string', 'max' => 60],
  130. [['code'], 'string', 'max' => 50],
  131. [['unit'], 'string', 'max' => 16],
  132. [['brief'], 'string', 'max' => 255],
  133. [['weight', 'length', 'width', 'height', 'diameter', 'sold_count', 'market_price', 'price', 'uniform_postage'], 'checkNegative'],
  134. [['uniform_postage', 'market_price', 'price'], 'safe']
  135. ];
  136. }
  137. /**
  138. * @param $attribute
  139. * @param $params
  140. * 验证是否为负数
  141. */
  142. public function checkNegative($attribute, $params)
  143. {
  144. if ($this->$attribute < 0) {
  145. $this->addError($attribute, "不得为负数");
  146. }
  147. }
  148. /**
  149. * @param $attribute
  150. * @param $params
  151. * 验证商品编号唯一
  152. */
  153. public function checkExist($attribute, $params)
  154. {
  155. $goods = self::find()->where([$attribute => $this->$attribute, 'is_delete' => 0])->one();
  156. if ($this->isNewRecord) {
  157. if ($goods) {
  158. $this->addError($attribute, "该商品编号已经存在");
  159. }
  160. } else {
  161. if ($goods && $goods->id != $this->id) {
  162. $this->addError($attribute, "该商品编号已经存在");
  163. }
  164. }
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function attributeLabels()
  170. {
  171. return [
  172. 'id' => 'id',
  173. 'pid' => '父级id',
  174. 'cat_id' => '后台商品类别',
  175. 'brand_id' => '品牌',
  176. 'shop_cat_id' => '前端商品类别',
  177. 'name' => '商品名称',
  178. 'sn' => '商品唯一货号',
  179. 'code' => '商品货码',
  180. 'supplier_id' => '供应商',
  181. 'weight' => '重量',
  182. 'length' => '长度',
  183. 'width' => '宽度',
  184. 'height' => '高度',
  185. 'diameter' => '直径',
  186. 'unit' => '单位',
  187. 'sold_count' => '已售数量',
  188. 'limit_count' => '限购数量',
  189. 'stock' => '库存',
  190. 'stock_warn' => '库存警告',
  191. 'market_price' => '市场价',
  192. 'price' => '销售价',
  193. 'brief' => '简介',
  194. 'description' => '详细介绍',
  195. 'image' => '首页图片',
  196. 'model_id' => '模型id',
  197. 'is_sale' => '销售状态',
  198. 'sort_order' => '排序',
  199. 'bouns_points' => '奖励积分',
  200. 'experience_points' => '经验值',
  201. 'is_delete' => '是否删除,1为已删除',
  202. 'express_template' => '配送详情',
  203. 'created_at' => '创建时间',
  204. 'updated_at' => '更新时间',
  205. 'sku_mode' => 'sku类型',
  206. 'is_taking' => '是否自提',
  207. 'is_express' => '是否快递发货',
  208. 'express_type' => '快递运费方式',
  209. 'uniform_postage' => '统一邮费',
  210. ];
  211. }
  212. /**
  213. * @author linyao
  214. * @email 602604991@qq.com
  215. * @created Nov 8, 2019
  216. *
  217. * 行为存储创建时间和更新时间
  218. */
  219. public function behaviors()
  220. {
  221. return [
  222. [
  223. 'class' => TimestampBehavior::className(),
  224. 'createdAtAttribute' => 'created_at',
  225. 'updatedAtAttribute' => 'updated_at',
  226. 'value' => function() {
  227. return time();
  228. },
  229. ],
  230. ];
  231. }
  232. /**
  233. * @param bool $insert
  234. * @return bool
  235. * 自动填入参数
  236. */
  237. public function beforeSave($insert)
  238. {
  239. if (!$this->sn) {
  240. $this->sn = time() . rand(1111, 9999);
  241. }
  242. return parent::beforeSave($insert); // TODO: Change the autogenerated stub
  243. }
  244. public function getCategory()
  245. {
  246. return $this->hasOne(Category::className(), ['id' => 'cat_id']);
  247. }
  248. public function getShopCategory()
  249. {
  250. return $this->hasOne(ShopCategory::className(), ['id' => 'shop_cat_id']);
  251. }
  252. public function getImageFile()
  253. {
  254. return $this->hasOne(File::className(), ['id' => 'image']);
  255. }
  256. public function getBrand()
  257. {
  258. return $this->hasOne(Brand::className(), ['id' => 'brand_id']);
  259. }
  260. public function getSupplier()
  261. {
  262. return $this->hasOne(Supplier::className(), ['id' => 'supplier_id']);
  263. }
  264. }