| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\Cat;
- use app\models\Goods;
- use app\models\GoodsCat;
- use yii\base\Model;
- class CatForm extends Model
- {
- public $store_id = 1;
- public $parent_id;
- public $sort;
- public $name;
- public $pic_url;
- public $is_show;
- public $model;
- public $shop_count;
- /**
- * @return array
- */
- public function rules()
- {
- return [
- [['parent_id', 'store_id', 'sort'], 'integer'],
- [['name'], 'string', 'max' => 60],
- [['pic_url'], 'string'],
- [['sort'], 'default', 'value' => 0],
- [['is_show'], 'default', 'value' => 1],
- [['is_delete'], 'default', 'value' => 0],
- [['shop_count'], 'integer']
- ];
- }
- /**
- * 保存分类
- * @return array
- */
- public function save()
- {
- $this->model->parent_id = $this->parent_id;
- $this->model->is_show = $this->is_show;
- if ($this->is_show == 0) {
- // 产品分类隐藏同时下架该分类下所有商品
- $goods_ids = GoodsCat::find()->where(['is_delete' => 0, 'store_id' => get_store_id(),
- 'cat_id' => post_params('id')])->select('goods_id')->asArray()->all();
- Goods::updateAll(['status' => Goods::STATUS_DISABLE],['in', 'id', array_column($goods_ids, 'goods_id')]);
- }
- $this->model->name = $this->name;
- $this->model->pic_url = $this->pic_url;
- $this->model->sort = $this->sort;
- $this->model->store_id = $this->store_id;
- $this->model->shop_count = $this->shop_count;
- if ($this->model->save()) {
- return [
- 'code' => 0,
- 'msg' => '提交成功'
- ];
- } else {
- return [
- 'code' => 0,
- 'msg' => '保存失败'
- ];
- }
- }
- /**
- * Undocumented function
- *
- * @Author LGL 24963@qq.com
- * @DateTime 2021-01-18
- * @desc: 根据id获取分类列表
- * @param array $id
- * @return void
- */
- public static function getCatListById ($id = [])
- {
- if (empty($id) && !is_array($id)) {
- return [];
- }
- return Cat::find()->where(['in', 'id', $id])->select('id, name, pic_url')->asArray()->all();
- }
- }
|