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', 'parent_id' => '父级ID', 'name' => '分类名称', 'pic' => 'Pic', 'big_pic' => 'Big Pic', 'advert_pic' => '广告图片', 'advert_url' => '广告链接', 'advert_open_type' => '打开方式', 'advert_params' => '导航参数', 'is_show' => '是否显示', 'status' => '状态[0禁用 1启用 -1删除]', 'sort' => '排序', 'created_at' => '创建时间', 'updated_at' => '修改时间', ]; } /** * @notes:关联父级分类 * @return \yii\db\ActiveQuery * @author: lun * @Time: 2022/10/22 11:15 */ public function getParent() { return $this->hasOne(self::class, ['id' => 'parent_id'])->andWhere(['<>', 'status', StatusEnum::DELETE]); } /** * @notes:关联子分类 * @return \yii\db\ActiveQuery * @author: lun * @Time: 2022/10/22 11:15 */ public function getChild() { return $this->hasMany(self::class, ['parent_id' => 'id'])->andWhere(['<>', 'status', StatusEnum::DELETE])->orderBy(['sort' => SORT_ASC, 'created_at' => SORT_DESC]); } /** * @notes:关联商品 * @return \yii\db\ActiveQuery * @author: lun * @Time: 2022/10/22 11:17 */ public function getPurchaseGoodsCateRelate() { return $this->hasMany(PurchaseGoodsCateRelate::class, ['cate_id' => 'id']); } /** * 保存数据 * @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->parent_id = $params['parent_id'] ?? 0; } isset($params['advert_params']) && $params['advert_params'] = json_encode($params['advert_params']); if (!$model->load($params, '') || !$model->save()) { throw new Exception($model->getErrorMessage()); } return $model; } catch (\Exception $e) { self::$static_error = $e->getMessage(); return false; } } /** * @notes:获取分类列表 * @param array $cond * @param array $with * @param bool $is_array * @param string $select * @return array|\yii\db\ActiveRecord[] * @author: lun * @Time: 2022/10/22 11:18 */ public static function getCates(array $cond = [], array $with = [], bool $is_array = true, string $select = '*') { $default_cond = [['<>', 'status', StatusEnum::DELETE]]; $cond = array_merge($default_cond, $cond); $query = self::find()->select($select); self::paramPack(['where' => $cond, 'with' => $with], $query); $cates = $query->asArray($is_array)->all(); return $cates; } /** * @notes:获取树状分类 * @param array $cates * @param int $parent_id * @return array * @author: lun * @Time: 2022/10/22 11:19 */ 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 bool */ 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; } } /** * @notes:修改分类排序 * @param $sort_cates * @throws \Exception * @author: lun * @Time: 2022/10/22 11:19 */ 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()); } } }