| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- namespace addons\StarChain\common\models;
- use Exception;
- use Yii;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%addons_star_chain_goods}}".
- *
- * @property int $id
- * @property string|null $spu_id SPU
- * @property string|null $name
- * @property string|null $carousel_img_list
- * @property string|null $category_id_1 一级分类ID
- * @property string|null $category_id_2 二级分类ID
- * @property string|null $category_id_3 三级分类ID
- * @property int|null $limit_area_type 限制类型 (0 : 可销售地区,1: 不可销售地区)
- * @property int $is_limit_area 限制销售地区 ( 1: 是, 0:否)
- * @property string|null $limit_area 可下单地址
- * @property string|null $propertys 类目属性/属性值
- * @property int|null $status 0、已上架1、已下架
- * @property int|null $updated_at
- * @property int|null $created_at
- */
- class Goods extends BaseModel
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_star_chain_goods}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['carousel_img_list', 'limit_area', 'propertys'], 'string'],
- [['limit_area_type', 'is_limit_area', 'status', 'updated_at', 'created_at'], 'integer'],
- [['spu_id', 'category_id_1', 'category_id_2', 'category_id_3'], 'string', 'max' => 64],
- [['name'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'spu_id' => 'Spu ID',
- 'name' => 'Name',
- 'carousel_img_list' => 'Carousel Img List',
- 'category_id_1' => 'Category Id 1',
- 'category_id_2' => 'Category Id 2',
- 'category_id_3' => 'Category Id 3',
- 'limit_area_type' => 'Limit Area Type',
- 'is_limit_area' => 'Is Limit Area',
- 'limit_area' => 'Limit Area',
- 'propertys' => 'Propertys',
- 'status' => 'Status',
- 'updated_at' => 'Updated At',
- 'created_at' => 'Created At',
- ];
- }
- /**
- * 一级分类
- *
- * @return ActiveQueryInterface
- */
- public function getCategory1()
- {
- return $this->hasOne(Category::class, ['category_id' => 'category_id_1'])->select('id, category_id, name');
- }
- /**
- * 二级分类
- *
- * @return ActiveQueryInterface
- */
- public function getCategory2()
- {
- return $this->hasOne(Category::class, ['category_id' => 'category_id_2'])->select('id, category_id, name');
- }
- /**
- * 三级分类
- *
- * @return ActiveQueryInterface
- */
- public function getCategory3()
- {
- return $this->hasOne(Category::class, ['category_id' => 'category_id_3'])->select('id, category_id, name');
- }
- /**
- * 批量更新商品
- *
- * @param array $list
- * @return void
- */
- public static function saveList($list, $insert = true)
- {
- $list = self::getGoodsData($list);
- if (empty($list)) return false;
- $spu_ids = array_column($list, 'spu_id');
- // 查询已存储
- $goods_list = self::find()->where(['spu_id' => $spu_ids])->indexBy('spu_id')->all();
- $has_spu_ids = array_column($goods_list, 'spu_id');
- // 更新已存在
- foreach ($list as $k => $v) {
- if (in_array($v['spu_id'], $has_spu_ids)) {
- if (!(
- $goods_list[$v['spu_id']]->load($v, '') &&
- $goods_list[$v['spu_id']]->save()
- )) {
- // $goods_list[$v['spu_id']]->getErrors();
- throw new Exception('SPUID:' . $v['spu_id'] . ',更新失败');
- }
- unset($list[$k]);
- }
- }
- // 剩余使用批量插入
- if ($list && $insert) {
- self::batchInsert($list);
- }
- }
- /**
- * 获取goods数据
- *
- * @param array $spu_list
- * @return array
- */
- protected static function getGoodsData($spu_list)
- {
- $data = [];
- foreach ($spu_list as $v) {
- $data[] = [
- 'spu_id' => strval($v['spuId']),
- 'name' => $v['name'],
- 'carousel_img_list' => Json::encode($v['carouselImgList']),
- 'category_id_1' => strval($v['categoryId1']),
- 'category_id_2' => strval($v['categoryId2']),
- 'category_id_3' => strval($v['categoryId3']),
- 'propertys' => Json::encode($v['propertys']),
- 'limit_area_type' => $v['limitAreaType'],
- 'is_limit_area' => $v['isLimitArea'] == 'Y' ? 1 : 0,
- 'limit_area' => Json::encode($v['limitArea']),
- 'status' => $v['status'],
- ];
- }
- return $data;
- }
- /**
- * 查询商品是否可售
- *
- * @param string $spu_id 商品SPUID
- * @param integer $region_code 地址CODE
- * @return boolean
- */
- public static function checkGoodsLimit($spu_id, $region_code)
- {
- $goods = self::find()
- ->where(['spu_id' => $spu_id])
- ->select('is_limit_area, limit_area_type, limit_area')
- ->cache(5)
- ->one();
- $limit_area = Json::decode($goods->limit_area);
- // 未开启限制
- if ($goods->is_limit_area == 0) {
- return true;
- }
- // 限制类型
- if ($goods->limit_area_type == 0) {
- return in_array($region_code, $limit_area);
- }
- return !in_array($region_code, $limit_area);
- }
- /**
- * 通过SPU_ID获取商品信息
- *
- * @param array $spu_ids
- * @return array
- */
- public static function getInfoBySpuIds(array $spu_ids, string $field = '')
- {
- $model = static::find();
- $model->where(['spu_id' => $spu_ids,]);
- $field && $model->select($field);
- $model->cache(5);
- return $model->all();
- }
- /**
- * @return void
- */
- public function goodsPulled()
- {
- $this->status = 1;
- $this->save();
- }
- }
|