| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace addons\Material\common\models;
- use common\enums\StatusEnum;
- use common\models\common\CapitalLog;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "qimall_addons_material_cate".
- *
- * @property int $id
- * @property int $mall_id 商城模块id
- * @property string|null $name 分类名称
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $sort 排序
- * @property int $parent_id 父级ID
- * @property int|null $updated_at 更新时间
- */
- class MaterialCate extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_material_cate}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id'], 'required'],
- [['mall_id', 'status','created_at', 'parent_id','updated_at','sort'], 'integer'],
- [['name'], 'string', 'max' => 80],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城模块id',
- 'name' => '分类名称',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'sort' => '排序,越小越前面',
- 'parent_id' => '父级ID',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- ];
- }
- public function getChildren()
- {
- return $this->hasMany(self::className(), ['parent_id' => 'id'])
- ->where(['!=', 'status', -1])
- ->orderBy('sort ASC');
- }
- public function getParent()
- {
- return $this->hasOne(Cate::className(), ['id' => 'parent_id']);
- }
- /**
- * 保存数据
- * @Author: ltm
- * @DateTime: 2021/12/13 0022 17:13
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return AddonsMaterial|bool
- */
- public static function setData(array $params){
- $t = \Yii::$app->db->beginTransaction();
- try{
- $params = self::exceptNullVal($params);
- $where[] = ['mall_id' => $params['mall_id'],'status'=>[StatusEnum::DISABLED,StatusEnum::ENABLED]];
- if($params['name']) $where[] = ['name' => $params['name']];
- $find_data = self::getOne($where);
- if ($find_data && (!$params['id'] || $find_data->id != $params['id']) && !empty($params['name'])) {
- self::$static_error = "分类名称已经存在";
- return false;
- }
- if($params['id']) {
- $cont = ['id' => $params['id']];
- $model = self::findOne($cont);
- }
- //var_dump($params);die;
- //判断分类是否已经被使用
- if($params['status']=='-1'){
- $findMaterial = Material::getOne([['cate_id' => $params['id'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED, 'status' => StatusEnum::DISABLED]],1);
- if($findMaterial){
- self::$static_error = "该分类已经关联素材,请先移除该分类的下素材";
- return false;
- }
- if ($model && $model->parent_id > 0) {
- //二级分类删除,关联素材归到一级分类
- Material::updateAll([
- 'second_cate_id' => 0,
- 'updated_at' => $params['updated_at'] ?? time()
- ],
- [
- 'mall_id' => $params['mall_id'],
- 'second_cate_id' => $model->id,
- 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED],
- ]);
- }
- }
- if(empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $params['mall_id'];
- $model->parent_id = $params['parent_id'] ?? 0;
- }
- if(!$model->load($params,'') || !$model->save()){
- throw new Exception($model->getErrorMessage());
- }
- $t->commit();
- return $model;
- }catch(\Exception $e){
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 获取分类列表
- * @Author: lun
- * @DateTime: 2023/7/3 0003 14:13
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @return array|\yii\db\ActiveRecord[]
- */
- public static function getList($mall_id)
- {
- $list = self::find()
- ->select('id, id as value, name as label')
- ->where([
- 'mall_id' => $mall_id,
- 'status' => StatusEnum::ENABLED,
- 'parent_id' => 0
- ])
- ->with([
- 'children' => function ($query) {
- $query->select('id, id as value, name as label,parent_id');
- }
- ])
- ->orderBy('sort asc')
- ->asArray()->all();
- return $list;
- }
- }
|