| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- namespace addons\Mch\common\models;
- use common\enums\StatusEnum;
- use Yii;
- /**
- * This is the model class for table "{{%addons_mch_cate}}".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $parent_id 父级ID
- * @property string $name 分类名称
- * @property string $pic
- * @property string $big_pic
- * @property string|null $advert_pic 广告图片
- * @property string|null $advert_url 广告链接
- * @property string $advert_open_type 打开方式
- * @property string|null $advert_params 导航参数
- * @property int $is_show 是否显示
- * @property int $status 状态[0禁用 1启用 -1删除]
- * @property int $sort 排序
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class MchCate extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_mch_cate}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'parent_id', 'is_show', 'status', 'sort', 'created_at', 'updated_at'], 'integer'],
- [['advert_pic', 'advert_url', 'advert_params'], 'string'],
- [['name'], 'string', 'max' => 45],
- [['pic', 'big_pic'], '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' => '修改时间',
- ];
- }
- 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 ewgof
- * @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;
- }
- // 关联模型
- 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]);
- }
- /**
- * 排序
- * @Author ewgof
- * @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 ewgof
- * @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());
- }
- }
- /**
- * 保存数据
- * @Author ewgof
- * @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;
- }
- }
- /**
- * 获取树状菜单
- * @Author bing
- * @DateTime 2021-08-10 18:22:16
- * @param array $menu 菜单
- * @param integer $parentId
- * @param string $app_id APP_ID
- * @return array
- * @copyright: Copyright (c) 2020 广东七件事集团
- */
- public static function getTreeMenu(array $menu, int $pid = 0)
- {
- $tree = [];
- foreach ($menu as $k => $v) {
- $v['value'] = $v['id'];
- if ($v['parent_id'] == $pid) {
- $tmp = $v;
- $children = self::getTreeMenu($menu, $v['id']);
- $children && $tmp['children'] = $children;
- $tree[] = $tmp;
- }
- }
- return $tree;
- }
- public static function getCateListInDiy(int $mall_id, array $params)
- {
- $wheres[] = ['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED];
- if (!empty($params['keyword'])) {
- $wheres[] = ['like', 'name', $params['keyword']];
- }
- $params = [
- 'where' => $wheres,
- 'order' => 'sort asc,created_at desc',
- 'page' => $params['page'] ?? 1,
- 'limit' => $params['limit'] ?? 15,
- ];
- $list = self::listPage($params);
- if (false === $list) {
- return self::$static_error;
- }
- foreach ($list['list'] as &$lv) {
- $lv['title'] = $lv['name'];
- }
- return $list;
- }
- }
|