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.

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