| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- <?php
- /**
- * MaterialForm.php
- * todo 文件描述
- * Created on 2025/1/11 下午2:07
- * @author: hankaige
- */
- namespace app\modules\admin\models\material;
- use app\models\Goods;
- use app\models\GoodsPic;
- use app\models\Level;
- use app\models\material\Material;
- use app\models\material\MaterialCategory;
- use app\models\material\MaterialResource;
- use app\models\Option;
- use yii\base\Model;
- class MaterialForm extends Model
- {
- const SCENE_LIST = 'list';
- const SCENE_EDIT = 'edit';
- const SCENE_DELETE = 'delete';
- const SCENE_STATUS = 'status';
- const SCENE_TOP = 'top';
- const SCENE_SETTING = 'setting';
- public int $store_id;
- public string $promotion;
- public int $material_category_id;
- public string $start_time;
- public string $end_time;
- public int $status = -1;
- // 编辑
- public int $id;
- public int $type;
- public int $sort = 1000;
- public int $goods_id = 0;
- public int $resource_model;
- public array $resources = [];
- public array $ids;
- public int $is_top;
- public string $promotion_image;
- public string $promotion_image_f;
- public string $material_visual_level;
- public function rules(): array
- {
- return [
- [['store_id', 'material_category_id', 'status', 'is_top'], 'integer'],
- [['promotion', 'start_time', 'end_time','promotion_image','promotion_image_f', 'material_visual_level'], 'string'],
- [['status', 'start_time', 'end_time', 'material_category_id', 'promotion'], 'safe', 'on' => [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()];
- }
- }
- }
|