| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- <?php
- namespace addons\Store\common\models;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use addons\Store\common\enums\StoreEnum;
- use Exception;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_store_good_cate".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $store_id
- * @property int $cate_id 主商城分类id
- * @property int $parent_id 父级ID
- * @property string $name 分类名称
- * @property string $pic
- * @property string $big_pic
- * @property string $advert_pic 广告图片
- * @property string|null $advert_url 广告链接
- * @property string $advert_open_type 打开方式
- * @property string|null $advert_params 导航参数
- * @property int $mall_goods_cate_id 关联主商城商品分类ID
- * @property int $is_show 是否显示
- * @property int $status 状态[0禁用 1启用 -1删除]
- * @property int $sort 排序
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class StoreGoodsCate extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_store_goods_cate}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'store_id', 'cate_id', 'parent_id', 'is_show', 'status', 'sort', 'mall_goods_cate_id', 'created_at', 'updated_at', 'level'], 'integer'],
- [['advert_params'], 'string'],
- [['name'], 'string', 'max' => 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',
- 'store_id' => 'Store ID',
- 'cate_id' => '主商城分类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' => '排序',
- 'mall_goods_cate_id' => '关联主商城商品分类ID',
- '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 getStoreGoodsCateRelate()
- {
- return $this->hasMany(StoreGoodsCateRelate::class, ['cate_id' => 'id']);
- }
- // 分类
- public function getGoods()
- {
- return $this->hasMany(StoreGoods::class, ['id' => 'goods_id'])
- ->via('storeGoodsCateRelate');
- }
- // public function getGoodsRecommend()
- // {
- // return $this->hasMany(StoreGoodsRecommend::class, ['id' => 'goods_id'])
- // ->via('storeGoodsCateRelate');
- // }
- 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->store_id = $params['store_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;
- }
- }
- public static function insertDataForName(array $data)
- {
- $model = self::findOne(['mall_id' => $data['mall_id'], 'store_id' => $data['store_id'], 'parent_id' => $data['parent_id'], 'name' => $data['name'], 'status' => StatusEnum::ENABLED]);
- if (!empty($model)) return $model;
- $model = new self();
- $model->loadDefaultValues();
- if (!$model->load($data, '') || !$model->save()) {
- throw new \Exception($model->getErrorMessage());
- }
- return $model;
- }
- public static function insertDataForMallCateID(array $data)
- {
- $model = self::findOne(['mall_id' => $data['mall_id'], 'store_id' => $data['store_id'], 'parent_id' => $data['parent_id'], 'mall_goods_cate_id' => $data['mall_goods_cate_id'], 'status' => StatusEnum::ENABLED]);
- if (!empty($model)) return $model;
- $model = new self();
- $model->loadDefaultValues();
- if (!$model->load($data, '') || !$model->save()) {
- throw new \Exception($model->getErrorMessage());
- }
- return $model;
- }
- /**
- * 获取分类列表
- * @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 = '*')
- {
- $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;
- }
- /**
- * 获取树状分类
- * @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 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;
- }
- }
- /**
- * 保存分类
- * @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());
- }
- }
- public static function getRecommendGoodCateListInDiy(int $mall_id, array $params)
- {
- $wheres[] = [
- 'sc.mall_id' => $mall_id,
- // 'sc.parent_id' => 0,
- 'sc.status' => StatusEnum::ENABLED,
- 'sc.is_show' => StatusEnum::ENABLED,
- 's.status' => StatusEnum::ENABLED,
- 's.check_status' => StoreEnum::CHECK_ADOPT
- ];
- $wheres[] = ['>', 'sc.store_id', 0];
- if (!empty($params['keyword'])) {
- $wheres[] = ['like', 'sc.name', $params['keyword']];
- }
- if (!empty($params['store_id'])) {
- $wheres[] = ['=', 'sc.store_id', $params['store_id']];
- }
- //商品分类层级
- if (!empty($params['level'])) $wheres[] = ['sc.level' => $params['level']];
- $params = [
- 'alias' => 'sc',
- 'select' => 'sc.*,s.store_name ',
- 'join' => [
- ['INNER JOIN', Store::tableName() . ' as s', 'sc.store_id = s.id'],
- ],
- 'where' => $wheres,
- 'order' => 'sc.sort asc,sc.created_at desc',
- 'page' => $params['page'] ?? 1,
- 'limit' => $params['limit'] ?? 15,
- ];
- $list = StoreGoodsCate::listPage($params);
- if (false === $list) {
- return self::$static_error;
- }
- foreach ($list['list'] as &$lv) {
- $lv['title'] = $lv['name'];
- switch ($lv['level']) {
- case 1:
- $lv['level_name'] = '一级分类';
- break;
- case 2:
- $lv['level_name'] = '二级分类';
- break;
- case 3:
- $lv['level_name'] = '三级分类';
- break;
- }
- }
- return $list;
- }
- public static function getAllCate($mallId, $store_id = null, $cateIds = [], &$cate_ids = [])
- {
- // 获取分类
- $cate_ids[] = implode(',', $cateIds);
- $where = ['mall_id' => $mallId, 'parent_id' => $cateIds, 'is_show' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED];
- if ($store_id) {
- $where['store_id'] = $store_id;
- }
- $child_cates = self::find()->select('id,')->where($where)->column();
- if ($child_cates) {
- self::getAllCate($mallId, $store_id, $child_cates, $cate_ids);
- }
- return explode(',', implode(',', $cate_ids));
- }
- /**
- * 根据门店分类获取次分类门店下商品分类
- * @param $mall_id
- * @param $cate_id
- * @return array
- */
- public static function getStoreGoodsAllCate($mall_id, $cate_id): array
- {
- if (empty($mall_id) || empty($cate_id)) return [];
- $cate_id = explode(',', $cate_id);
- $store_id = Store::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'check_status' => StatusEnum::ENABLED])
- ->andWhere(['c_id' => $cate_id])
- ->select('id')
- ->column();
- return self::find()->select('id')
- ->where(['mall_id' => $mall_id, 'is_show' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED, 'store_id' => $store_id])
- ->andWhere(['parent_id' => StatusEnum::DISABLED])
- ->column();
- }
- public static function getChildCateIDByTree($cate = [], $key = 'children')
- {
- $children_cate_id_arr = [];
- if (!empty($cate[$key])){
- $children_cate_id_arr = array_column($cate[$key], 'id');
- foreach ($cate[$key] as $child_row) {
- $children_cate_id_arr = array_merge($children_cate_id_arr, self::getChildCateIDByTree($child_row, $key));
- }
- }
- return $children_cate_id_arr;
- }
- /**
- * 获取子分类,不递归
- * @param array $cates
- * @param int $parent_id
- * @return array
- */
- public static function getChildrenCate(array $cates, int $parent_id = 0)
- {
- $children = [];
- foreach ($cates as $v) {
- if ($v['parent_id'] == $parent_id) {
- $children[] = $v;
- }
- }
- return $children;
- }
- //数据迁移-新增分类层级,旧数据维护
- 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());
- }
- }
- }
|