| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use yii\db\ActiveRecord;
- use yii\behaviors\TimestampBehavior;
- use Yii;
- /**
- * This is the model class for table "{{%md_goods}}".
- *
- * @property integer $id
- * @property integer $user_id
- * @property integer $store_id
- * @property integer $goods_id
- * @property string $name
- * @property string $pic
- * @property string $attr
- * @property integer $price
- * @property integer $platform_price
- * @property integer $virtual_sales
- * @property integer $num
- * @property integer $cloud_inventory_cat_id
- * @property integer $created_at
- * @property integer $updated_at
- */
- class CloudInventoryGoods extends \yii\db\ActiveRecord
- {
- //活动价(临时字段)
- public $activityPrice = 0;
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%cloud_inventory_goods}}';
- }
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class,
- 'attributes' => [
- ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
- ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
- ]
- ]
- ];
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['store_id','user_id', 'goods_id', 'virtual_sales', 'cloud_inventory_cat_id', 'num'], 'integer'],
- [['price','platform_price'], 'number'],
- [['attr','pic','name'], 'string'],
- [['created_at', 'updated_at'], 'safe']
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'user_id' => '用户id',
- 'store_id' => '商城id',
- 'goods_id' => '商品id',
- 'name' => '商品名称',
- 'virtual_sales' => '销量',
- 'cloud_inventory_cat_id' => '云库存商品类别',
- 'attr' => '规格',
- 'price' => '售价',
- 'platform_price' => '平台价',
- 'num' => '库存',
- 'pic' => '商品缩略图',
- 'created_at' => '',
- 'updated_at' => ''
- ];
- }
- public function beforeSave($insert)
- {
- if (parent::beforeSave($insert)) {
- $goods_price = $this->price;
- $goods_num = $this->goods_num;
- $this->getPriceNum($goods_price, $goods_num);
- $this->price = $goods_price;
- $this->goods_num = $goods_num;
- return true;
- }
- }
- public function getPriceNum(&$goods_price, &$goods_num) {
- $attr = $this->attr;
- if (!empty($attr)) {
- $num = 0;
- $attr_rows = json_decode($attr, true);
- foreach ($attr_rows as $attr_row) {
- $num += intval($attr_row['num']);
- }
- $goods_num = $num;
- $price_arr = array_column($attr_rows, 'price');
- $goods_price = min($price_arr);
- }
- }
- /**
- * 获取商品可选的规格列表
- */
- public function getAttrGroupList($use_attr = 1)
- {
- $attr_rows = json_decode($this->attr, true);
- if (empty($attr_rows)) {
- return [];
- }
- $attr_group_list = [];
- foreach ($attr_rows as $attr_row) {
- foreach ($attr_row['attr_list'] as $i => $attr) {
- $attr_id = $attr['attr_id'];
- $attr = Attr::findOne(['id' => $attr_id, 'is_delete' => 0]);
- if (!$attr) {
- continue;
- }
- $in_list = false;
- foreach ($attr_group_list as $j => $attr_group) {
- if ($attr_group->attr_group_id == $attr->attr_group_id) {
- $attr_obj = (object)[
- 'attr_id' => $attr->id,
- 'attr_name' => $attr->attr_name,
- ];
- if (!in_array($attr_obj, $attr_group_list[$j]->attr_list)) {
- $attr_group_list[$j]->attr_list[] = $attr_obj;
- }
- $in_list = true;
- continue;
- }
- }
- if (!$in_list) {
- $attr_group = AttrGroup::findOne(['is_delete' => 0, 'id' => $attr->attr_group_id]);
- if ($attr_group) {
- $attr_group_list[] = (object)[
- 'attr_group_id' => $attr_group->id,
- 'attr_group_name' => $attr_group->attr_group_name,
- 'attr_list' => [
- (object)[
- 'attr_id' => $attr->id,
- 'attr_name' => $attr->attr_name,
- ],
- ],
- ];
- }
- }
- }
- }
- if ((int)$use_attr === 0) {
- $attr_group_list = [
- $attr_group_list[0]
- ];
- }
- return $attr_group_list;
- }
- }
|