[self::SCENE_LIST]], [['id', 'type', 'sort', 'goods_id', 'resource_model', 'material_category_id'], 'integer', 'on' => [self::SCENE_EDIT]], [['ids'], 'required', 'on' => [self::SCENE_DELETE, self::SCENE_STATUS, self::SCENE_TOP]], [['resources'], 'safe'], [['material_visual_level'], 'required', 'on' => [self::SCENE_SETTING]] ]; } /** * 自定义验证规则:确保 ids 是数组且长度大于 0 */ public function validateIds($attribute, $params) { if (!is_array($this->$attribute)) { $this->addError($attribute, 'ids 必须是一个数组。'); return; } if (count($this->$attribute) === 0) { $this->addError($attribute, 'ids 数组的长度必须大于 0。'); } } public function getList(): array { $query = Material::find()->with(['materialResource', 'goods'])->alias('m')->leftJoin(['mc' => MaterialCategory::tableName()], 'm.material_category_id = mc.id')->where(['m.store_id' => $this->store_id])->andWhere(['OR', ['m.delete_time' => 0], ['IS', 'm.delete_time', NULL]])->orderBy('m.create_time DESC'); if (!empty($this->promotion)) { $query->andWhere(['like', 'm.promotion', $this->promotion]); } // 根据素材分类id筛选 if (!empty($this->material_category_id)) { $query->andWhere(['m.material_category_id' => $this->material_category_id]); } if (!empty($this->start_time)) { $query->andWhere(['>=', 'm.create_time', $this->start_time]); } if (!empty($this->end_time)) { $query->andWhere(['<=', 'm.create_time', $this->end_time]); } if ($this->status != -1) { $query->andWhere(['m.status' => $this->status]); } $query->select(['m.*', 'mc.name as material_category_name']); $result = pagination_make($query); return ['code' => 0, 'data' => $result, 'msg' => '获取数据成功']; } public function postEdit(): array { // 开启yii事务 $transaction = \Yii::$app->db->beginTransaction(); try { $model = Material::findOne(['id' => $this->id]); if (!$model) { $model = new Material(); } $model->store_id = $this->store_id; $model->material_category_id = $this->material_category_id; $model->type = $this->type; $model->promotion = $this->promotion; $model->status = $this->status; $model->sort = $this->sort; $model->goods_id = $this->goods_id; $model->resource_model = $this->resource_model; $model->promotion_image = $this->promotion_image; $model->promotion_image_f = $this->promotion_image_f; if (!$model->save()) { $transaction->rollBack(); return ['code' => 1, 'msg' => '素材主信息创建失败']; } if ($this->id > 0) { // 需要先删除 之前的素材 MaterialResource::deleteAll(['material_id' => $this->id]); } if (!empty($this->resources)) { $resourceModel = new MaterialResource(); foreach ($this->resources as $item) { $resourceModelItem = clone $resourceModel; $resourceModelItem->material_id = $model->id; $resourceModelItem->model = $this->resource_model; $resourceModelItem->resource_url = $item; $resourceModelItem->save(); } } // 如果type == 1 商品是必须的 // if ($this->type == Material::TYPE_SYSTEM) { // if (empty($this->goods_id)) { // $transaction->rollBack(); // throw new \Exception('请选择商品'); // } // // 使用系统默认素材 // if ($this->resource_model == MaterialResource::MODEL_IMAGE) { // $goodsPics = GoodsPic::findAll(['goods_id' => $this->goods_id, 'is_delete' => 0]); // // 图片类素材 // // 循环写入 material_resource表 // if (empty($goodsPics)) { // $transaction->rollBack(); // throw new \Exception('商品相册数据为空'); // } // $resourceModel = new MaterialResource(); // foreach ($goodsPics as $item) { // $resourceModelItem = clone $resourceModel; // $resourceModelItem->material_id = $model->id; // $resourceModelItem->model = MaterialResource::MODEL_IMAGE; // $resourceModelItem->resource_url = $item->pic_url; // $resourceModelItem->save(); // } // } // if ($this->resource_model == MaterialResource::MODEL_VOICE) { // $goods = Goods::findOne($this->goods_id); // // 视频类素材 // $resourceModel = new MaterialResource(); // $resourceModel->material_id = $model->id; // $resourceModel->model = MaterialResource::MODEL_VOICE; // $resourceModel->resource_url = $goods->video_url; // $resourceModel->save(); // } // } // if ($this->type == Material::TYPE_CUSTOM) { // // 使用自定义素材 // if (empty($this->resources)) { // $transaction->rollBack(); // throw new \Exception('请上传素材'); // } // $resourceModel = new MaterialResource(); // foreach ($this->resources as $item) { // $resourceModelItem = clone $resourceModel; // $resourceModelItem->material_id = $model->id; // $resourceModelItem->model = $this->resource_model; // $resourceModelItem->resource_url = $item; // $resourceModelItem->save(); // } // } $transaction->commit(); return ['code' => 0, 'msg' => '素材创建成功']; } catch (\Exception $e) { return ['code' => 1, 'msg' => $e->getMessage()]; } } public function postDelete(): array { $transaction = \Yii::$app->db->beginTransaction(); try { $model = Material::find()->where(['id' => $this->ids])->all(); if (empty($model)) { return ['code' => 1, 'msg' => '数据不存在']; } $result = Material::updateAll(['delete_time' => date('Y-m-n H:i:s')], ['id' => $this->ids]); MaterialResource::deleteAll(['material_id' => $this->ids]); if ($result > 0) { $transaction->commit(); return ['code' => 0, 'msg' => '成功删除' . $result . '条数据']; } else { $transaction->rollBack(); return ['code' => 1, 'msg' => '删除失败']; } } catch (\Exception $e) { $transaction->rollBack(); return ['code' => 1, 'msg' => $e->getMessage()]; } } public function postStatus(): array { $transaction = \Yii::$app->db->beginTransaction(); try { $model = Material::find()->where(['id' => $this->ids])->all(); if (empty($model)) { return ['code' => 1, 'msg' => '数据不存在']; } $result = Material::updateAll(['status' => $this->status], ['id' => $this->ids]); if ($result > 0) { $transaction->commit(); return ['code' => 0, 'msg' => '成功修改' . $result . '条数据']; } else { $transaction->rollBack(); return ['code' => 1, 'msg' => '修改失败']; } } catch (\Exception $e) { $transaction->rollBack(); return ['code' => 1, 'msg' => $e->getMessage()]; } } public function postTop(): array { $transaction = \Yii::$app->db->beginTransaction(); try { $model = Material::find()->where(['id' => $this->ids])->all(); if (empty($model)) { return ['code' => 1, 'msg' => '数据不存在']; } $result = Material::updateAll(['is_top' => $this->is_top], ['id' => $this->ids]); if ($result > 0) { $transaction->commit(); return ['code' => 0, 'msg' => '成功修改' . $result . '条数据']; } else { $transaction->rollBack(); return ['code' => 1, 'msg' => '修改失败']; } } catch (\Exception $e) { $transaction->rollBack(); return ['code' => 1, 'msg' => $e->getMessage()]; } } //基础设置 public function materialSetting() { try { $store_id = $this->store_id; if (\Yii::$app->request->isPost) { $material_visual_level_str = $this->material_visual_level; $material_visual_level = explode(',', $material_visual_level_str); if (empty($material_visual_level)) { throw new \Exception('请选择会员等级'); } $level = Level::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'level' => $material_visual_level])->select('level') ->orderBy('level asc')->column(); if (in_array(-1, $material_visual_level)) { $level[] = -1; } Option::set('material_visual_level', json_encode($level, JSON_UNESCAPED_UNICODE), $store_id, 'store'); return ['code' => 0, 'msg' => '修改成功']; } else { $level_data = Level::find()->where(['store_id' => $this->store_id, 'is_delete' => 0])->select('level, name') ->orderBy('level asc')->asArray()->all(); array_unshift($level_data, [ 'level' => -1, 'name' => '全部会员' ]); $materialVisualLevel = Option::get('material_visual_level', $store_id, 'store', json_encode([-1], JSON_UNESCAPED_UNICODE))['value']; return [ 'code' => 0, 'msg' => '', 'data' => [ 'level_list' => $level_data, 'material_visual_level' => json_decode($materialVisualLevel, true) ] ]; } } catch (\Exception $e) { return ['code' => 1, 'msg' => $e->getMessage()]; } } }