45], [['pic', 'big_pic', 'advert_pic', 'advert_url'], 'string', 'max' => 255], [['advert_open_type'], 'string', 'max' => 65], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => 'Mall ID', 'mch_id' => 'Mch ID', 'parent_id' => '父级ID', 'name' => '分类名称', 'pic' => '分类图片', 'big_pic' => '分类大图', 'advert_pic' => '广告图片', 'advert_url' => '广告链接', 'advert_open_type' => '打开方式', 'advert_params' => '导航参数', 'is_show' => '是否显示', 'status' => '状态[0禁用 1启用 -1删除]', 'sort' => '排序', 'created_at' => '创建时间', 'updated_at' => '修改时间', ]; } // 关联模型 public function getParent() { return $this->hasOne(self::class, ['id' => 'parent_id'])->andWhere(['<>', 'status', StatusEnum::DELETE]); } public function getChild() { return $this->hasMany(self::class, ['parent_id' => 'id'])->andWhere(['<>', 'status', StatusEnum::DELETE])->orderBy(['sort' => SORT_ASC, 'created_at' => SORT_DESC]); } // 分类关系 public function getGoodsCateRelate() { return $this->hasMany(GoodsCateRelate::class, ['cate_id' => 'id']); } // 分类 public function getGoods() { return $this->hasMany(Goods::class, ['id' => 'goods_id']) ->select('id,goods_name,price,original_price,cover_pic,virtual_sales,sales_num') ->where(['mch_id' => 0, 'status' => StatusEnum::ENABLED]) ->via('goodsCateRelate'); } /** * 保存数据 * @Author bing * @DateTime 2021-08-26 14:16:24 * @copyright: Copyright (c) 2020 广东七件事集团 * @param array $params * @return object|boolean */ public static function setData(array $params) { try { if (!empty($params['id'])) { $model = self::findOne(['and', ['id' => $params['id'], 'mall_id' => $params['mall_id']], ['<>', 'status', StatusEnum::DELETE]]); if (empty($model)) throw new \Exception('分类不存在或已删除'); } else { $model = new self(); $model->loadDefaultValues(); $model->mall_id = $params['mall_id']; $model->mch_id = $params['mch_id']; $model->parent_id = $params['parent_id'] ?? 0; } isset($params['advert_params']) && $params['advert_params'] = json_encode($params['advert_params']); if ($model->parent_id == 0) { //上级分类不存在则为层级1 $model->level = 1; } else { //查找上级分类数据 $goods_cate_data = self::findOne(['and', ['id' => $model->parent_id, 'mall_id' => $params['mall_id']], ['<>', 'status', StatusEnum::DELETE]]); if (!empty($goods_cate_data)) $model->level = $goods_cate_data['level'] + 1; } if (!$model->load($params, '') || !$model->save()) { throw new Exception($model->getErrorMessage()); } return $model; } catch (\Exception $e) { self::$static_error = $e->getMessage(); return false; } } /** * 获取分类列表 * @Author bing * @DateTime 2021-08-26 13:00:11 * @copyright: Copyright (c) 2020 广东七件事集团 * @param array $cond * @param array $with * @param integer $is_array * @return array|null */ public static function getCates(array $cond = [], array $with = [], bool $is_array = true, string $select = '*',$order = 'sort asc,created_at desc') { $default_cond = [['<>', 'status', StatusEnum::DELETE]]; $cond = array_merge($default_cond, $cond); $query = self::find()->orderBy($order)->select($select); self::paramPack(['where' => $cond, 'with' => $with], $query); $cates = $query->asArray($is_array)->limit(10000)->all(); return $cates; } /** * 获取树状分类 * @Author bing * @DateTime 2021-08-26 13:00:40 * @copyright: Copyright (c) 2020 广东七件事集团 * @param array $cates * @param integer $parent_id * @return void */ public static function getTreeCate(array $cates, int $parent_id = 0) { $tree = []; foreach ($cates as $k => $v) { $v['value'] = $v['id']; $v['label'] = $v['name']; if ($v['parent_id'] == $parent_id) { $tmp = $v; // 当前路由的菜单被选中 $children = self::getTreeCate($cates, $v['id']); $children && $tmp['children'] = $children; $tree[] = $tmp; } } return $tree; } /** * 排序 * @Author bing * @DateTime 2021-11-12 10:16:01 * @copyright: Copyright (c) 2020 广东七件事集团 * @param array $post * @return void */ public static function sort(array $post) { try { $first_list = json_decode($post['first_list'], true); $second_list = json_decode($post['second_list'], true); $third_list = json_decode($post['third_list'], true); $t = \Yii::$app->db->beginTransaction(); self::_saveSort($first_list); self::_saveSort($second_list); self::_saveSort($third_list); $t->commit(); return true; } catch (Exception $e) { self::setStaticError($e->getMessage()); return false; } } /** * 保存分类 * @Author bing * @DateTime 2021-11-12 10:18:43 * @copyright: Copyright (c) 2020 广东七件事集团 * @param [type] $sort_cates * @return void */ private static function _saveSort($sort_cates) { foreach ($sort_cates as $index => $cate) { $model = self::findOne($cate['id']); if (!$model) throw new \Exception('分类不存在'); $model->sort = $index; $res = $model->save(); if (!$res) throw new \Exception($model->getErrorMessage()); } } /** * 删除分类 * * @param array $params * @return boolean */ public static function del($params) { try { $del_ids = [$params['id']]; $condition = [ 'mall_id' => $params['mall_id'], 'parent_id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED], ]; while ($ids = static::find()->where($condition)->select('id')->column()) { $del_ids = array_merge($del_ids, $ids); $condition['parent_id'] = $ids; } $is_check = GoodsCateRelate::checkCateHasGoods($del_ids); if ($is_check) { self::$static_error = '该分类或其子级分类有商品,请先删除商品'; return false; } GoodsCate::updateAll( ['status' => StatusEnum::DELETE], ['id' => $del_ids, 'mall_id' => $params['mall_id'],'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]] ); return true; } catch (\yii\db\Exception $e) { self::setStaticError($e->getMessage()); return false; } } /** * 根据一级分类获取所属的所有下级分类(包括自己) * @Author: lun * @DateTime: 2023/5/17 0017 11:08 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $mallId * @param array $cateIds * @param array $cate_ids * @return false|string[] */ public static function getAllCate($mallId, $cateIds = [], &$cate_ids = []) { // 获取分类 $cate_ids[] = implode(',', $cateIds); $child_cates = self::find()->select('id,')->where(['mall_id' => $mallId, 'parent_id' => $cateIds, 'is_show' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED])->column(); if ($child_cates) { self::getAllCate($mallId, $child_cates, $cate_ids); } return explode(',', implode(',', $cate_ids)); } /** * 获取该站点隐藏的分类IDs * @param integer $mall_id * @return array */ public static function getHiddenIds(int $mall_id) { return static::find() ->where(['mall_id' => $mall_id, 'is_show' => false, 'status' => StatusEnum::ENABLED]) ->select(['id']) ->column(); } /** * 获取指定分类及其所有子分类的ID(仅包含启用状态) * @param array $ids * @return array */ public static function getAllEnabledSubCateIds(array $ids) { $res_ids = $ids; while (count($ids)) { $ids = static::find() ->where(['parent_id' => $ids, 'status' => StatusEnum::ENABLED]) ->select(['id']) ->column(); // $res_ids = [...$res_ids, ...$ids]; $res_ids = array_merge($res_ids, $ids); } return $res_ids; } //数据迁移-新增分类层级,旧数据维护 public static function migrateCateLevel() { try { //获取一级分类数据 $first_cate_data = self::lists([ 'where' => [ ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]], ['parent_id' => 0] ], ]); $first_cate_ids = array_column($first_cate_data, 'id'); //修改分类层级为1 self::updateAll(['level' => 1], ['id' => $first_cate_ids]); //修改上级id为一级分类的数据层级为2 self::updateAll(['level' => 2], ['parent_id' => $first_cate_ids]); //修改剩余上级id不为0,且不存在层级的分类数据层级为3 self::updateAll(['level' => 3], ['and', ['>', 'parent_id', 0], ['level' => 0]]); } catch (\Exception $e) { throw new Exception($e->getMessage()); } } }