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(); } }