'ID', 'cate_id' => '分类id', 'goods_id' => '产品id', 'created_at' => '创建时间', 'updated_at' => '修改时间', ]; } /** * 改变商品分类 * @Author bing * @DateTime 2021-08-23 09:38:52 * @copyright: Copyright (c) 2020 广东七件事集团 * @param integer $before_cate_id * @param integer $after_cate_id * @return void */ public static function changeGoodsCate(int $before_cate_id, int $after_cate_id){ try{ $count = self::updateAll( ['cate_id' => $after_cate_id], ['cate_id' => $before_cate_id] ); return $count; }catch(Exception $e){ self::$static_error = $e->getMessage(); return false; } } /** * 保存商品分类关系 * @Author bing * @DateTime 2021-09-01 15:25:06 * @copyright: Copyright (c) 2020 广东七件事集团 * @param [type] $goods_id * @param [type] $cats * @return void */ public static function setData($goods_id,$cats){ try{ if($goods_id && $cats){ $count = self::deleteAll([ 'and', ['goods_id' => $goods_id], ['not in','cate_id',$cats] ]); $exist_cate_ids = self::find()->select('cate_id')->where(array('goods_id'=>$goods_id))->column(); $insert = []; $now = time(); foreach($cats as $key => $cate_id){ if(in_array($cate_id,$exist_cate_ids)) continue; $insert[] = [$goods_id,$cate_id,$now,$now]; } $field = ['goods_id', 'cate_id','created_at','updated_at']; // 批量插入数据 Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $insert)->execute(); } return true; }catch(Exception $e){ self::$static_error = $e->getMessage(); return false; } } /** * 通过商品ID获取商品详情 * * @param array $goods_ids * @param string $field * @param boolean|int $cache * @return array */ public static function getListByGoodsIds(array $goods_ids, string $field = '*', $cache = false) { $model = self::find()->where(['goods_id' => $goods_ids]); $model->select($field); $cache && $model->cache($cache); return $model->asArray()->all(); } /** * 关联商品 * @return \yii\db\ActiveQuery */ public function getGoods() { return $this->hasOne(Goods::class, ['id' => 'goods_id'])->where(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); } /** * 检查分类下是否有商品 * @param array|null $cate_ids * @return bool */ public static function checkCateHasGoods(?array $cate_ids): bool { $cateGoods = self::find()->where(['cate_id'=> $cate_ids]) ->with('goods') ->asArray() ->all(); $is_check = false; foreach($cateGoods as $cateGood){ if(!empty($cateGood['goods'])){// 有商品则不能删除 $is_check = true; break; } } return $is_check; } /** * 根据分类ID获取商品ID * * @param array $cate_ids * @return array */ public static function getGoodsIdByCateIds(array $cate_ids) { return static::find() ->where(['cate_id' => $cate_ids]) ->select(['goods_id']) ->column(); } }