MaterialForm.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /**
  3. * MaterialForm.php
  4. * todo 文件描述
  5. * Created on 2025/1/11 下午2:07
  6. * @author: hankaige
  7. */
  8. namespace app\modules\admin\models\material;
  9. use app\models\Goods;
  10. use app\models\GoodsPic;
  11. use app\models\Level;
  12. use app\models\material\Material;
  13. use app\models\material\MaterialCategory;
  14. use app\models\material\MaterialResource;
  15. use app\models\Option;
  16. use yii\base\Model;
  17. class MaterialForm extends Model
  18. {
  19. const SCENE_LIST = 'list';
  20. const SCENE_EDIT = 'edit';
  21. const SCENE_DELETE = 'delete';
  22. const SCENE_STATUS = 'status';
  23. const SCENE_TOP = 'top';
  24. const SCENE_SETTING = 'setting';
  25. public int $store_id;
  26. public string $promotion;
  27. public int $material_category_id;
  28. public string $start_time;
  29. public string $end_time;
  30. public int $status = -1;
  31. // 编辑
  32. public int $id;
  33. public int $type;
  34. public int $sort = 1000;
  35. public int $goods_id = 0;
  36. public int $resource_model;
  37. public array $resources = [];
  38. public array $ids;
  39. public int $is_top;
  40. public string $promotion_image;
  41. public string $promotion_image_f;
  42. public string $material_visual_level;
  43. public function rules(): array
  44. {
  45. return [
  46. [['store_id', 'material_category_id', 'status', 'is_top'], 'integer'],
  47. [['promotion', 'start_time', 'end_time','promotion_image','promotion_image_f', 'material_visual_level'], 'string'],
  48. [['status', 'start_time', 'end_time', 'material_category_id', 'promotion'], 'safe', 'on' => [self::SCENE_LIST]],
  49. [['id', 'type', 'sort', 'goods_id', 'resource_model', 'material_category_id'], 'integer', 'on' => [self::SCENE_EDIT]],
  50. [['ids'], 'required', 'on' => [self::SCENE_DELETE, self::SCENE_STATUS, self::SCENE_TOP]],
  51. [['resources'], 'safe'],
  52. [['material_visual_level'], 'required', 'on' => [self::SCENE_SETTING]]
  53. ];
  54. }
  55. /**
  56. * 自定义验证规则:确保 ids 是数组且长度大于 0
  57. */
  58. public function validateIds($attribute, $params)
  59. {
  60. if (!is_array($this->$attribute)) {
  61. $this->addError($attribute, 'ids 必须是一个数组。');
  62. return;
  63. }
  64. if (count($this->$attribute) === 0) {
  65. $this->addError($attribute, 'ids 数组的长度必须大于 0。');
  66. }
  67. }
  68. public function getList(): array
  69. {
  70. $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');
  71. if (!empty($this->promotion)) {
  72. $query->andWhere(['like', 'm.promotion', $this->promotion]);
  73. }
  74. // 根据素材分类id筛选
  75. if (!empty($this->material_category_id)) {
  76. $query->andWhere(['m.material_category_id' => $this->material_category_id]);
  77. }
  78. if (!empty($this->start_time)) {
  79. $query->andWhere(['>=', 'm.create_time', $this->start_time]);
  80. }
  81. if (!empty($this->end_time)) {
  82. $query->andWhere(['<=', 'm.create_time', $this->end_time]);
  83. }
  84. if ($this->status != -1) {
  85. $query->andWhere(['m.status' => $this->status]);
  86. }
  87. $query->select(['m.*', 'mc.name as material_category_name']);
  88. $result = pagination_make($query);
  89. return ['code' => 0, 'data' => $result, 'msg' => '获取数据成功'];
  90. }
  91. public function postEdit(): array
  92. {
  93. // 开启yii事务
  94. $transaction = \Yii::$app->db->beginTransaction();
  95. try {
  96. $model = Material::findOne(['id' => $this->id]);
  97. if (!$model) {
  98. $model = new Material();
  99. }
  100. $model->store_id = $this->store_id;
  101. $model->material_category_id = $this->material_category_id;
  102. $model->type = $this->type;
  103. $model->promotion = $this->promotion;
  104. $model->status = $this->status;
  105. $model->sort = $this->sort;
  106. $model->goods_id = $this->goods_id;
  107. $model->resource_model = $this->resource_model;
  108. $model->promotion_image = $this->promotion_image;
  109. $model->promotion_image_f = $this->promotion_image_f;
  110. if (!$model->save()) {
  111. $transaction->rollBack();
  112. return ['code' => 1, 'msg' => '素材主信息创建失败'];
  113. }
  114. if ($this->id > 0) {
  115. // 需要先删除 之前的素材
  116. MaterialResource::deleteAll(['material_id' => $this->id]);
  117. }
  118. if (!empty($this->resources)) {
  119. $resourceModel = new MaterialResource();
  120. foreach ($this->resources as $item) {
  121. $resourceModelItem = clone $resourceModel;
  122. $resourceModelItem->material_id = $model->id;
  123. $resourceModelItem->model = $this->resource_model;
  124. $resourceModelItem->resource_url = $item;
  125. $resourceModelItem->save();
  126. }
  127. }
  128. // 如果type == 1 商品是必须的
  129. // if ($this->type == Material::TYPE_SYSTEM) {
  130. // if (empty($this->goods_id)) {
  131. // $transaction->rollBack();
  132. // throw new \Exception('请选择商品');
  133. // }
  134. // // 使用系统默认素材
  135. // if ($this->resource_model == MaterialResource::MODEL_IMAGE) {
  136. // $goodsPics = GoodsPic::findAll(['goods_id' => $this->goods_id, 'is_delete' => 0]);
  137. // // 图片类素材
  138. // // 循环写入 material_resource表
  139. // if (empty($goodsPics)) {
  140. // $transaction->rollBack();
  141. // throw new \Exception('商品相册数据为空');
  142. // }
  143. // $resourceModel = new MaterialResource();
  144. // foreach ($goodsPics as $item) {
  145. // $resourceModelItem = clone $resourceModel;
  146. // $resourceModelItem->material_id = $model->id;
  147. // $resourceModelItem->model = MaterialResource::MODEL_IMAGE;
  148. // $resourceModelItem->resource_url = $item->pic_url;
  149. // $resourceModelItem->save();
  150. // }
  151. // }
  152. // if ($this->resource_model == MaterialResource::MODEL_VOICE) {
  153. // $goods = Goods::findOne($this->goods_id);
  154. // // 视频类素材
  155. // $resourceModel = new MaterialResource();
  156. // $resourceModel->material_id = $model->id;
  157. // $resourceModel->model = MaterialResource::MODEL_VOICE;
  158. // $resourceModel->resource_url = $goods->video_url;
  159. // $resourceModel->save();
  160. // }
  161. // }
  162. // if ($this->type == Material::TYPE_CUSTOM) {
  163. // // 使用自定义素材
  164. // if (empty($this->resources)) {
  165. // $transaction->rollBack();
  166. // throw new \Exception('请上传素材');
  167. // }
  168. // $resourceModel = new MaterialResource();
  169. // foreach ($this->resources as $item) {
  170. // $resourceModelItem = clone $resourceModel;
  171. // $resourceModelItem->material_id = $model->id;
  172. // $resourceModelItem->model = $this->resource_model;
  173. // $resourceModelItem->resource_url = $item;
  174. // $resourceModelItem->save();
  175. // }
  176. // }
  177. $transaction->commit();
  178. return ['code' => 0, 'msg' => '素材创建成功'];
  179. } catch (\Exception $e) {
  180. return ['code' => 1, 'msg' => $e->getMessage()];
  181. }
  182. }
  183. public function postDelete(): array
  184. {
  185. $transaction = \Yii::$app->db->beginTransaction();
  186. try {
  187. $model = Material::find()->where(['id' => $this->ids])->all();
  188. if (empty($model)) {
  189. return ['code' => 1, 'msg' => '数据不存在'];
  190. }
  191. $result = Material::updateAll(['delete_time' => date('Y-m-n H:i:s')], ['id' => $this->ids]);
  192. MaterialResource::deleteAll(['material_id' => $this->ids]);
  193. if ($result > 0) {
  194. $transaction->commit();
  195. return ['code' => 0, 'msg' => '成功删除' . $result . '条数据'];
  196. } else {
  197. $transaction->rollBack();
  198. return ['code' => 1, 'msg' => '删除失败'];
  199. }
  200. } catch (\Exception $e) {
  201. $transaction->rollBack();
  202. return ['code' => 1, 'msg' => $e->getMessage()];
  203. }
  204. }
  205. public function postStatus(): array
  206. {
  207. $transaction = \Yii::$app->db->beginTransaction();
  208. try {
  209. $model = Material::find()->where(['id' => $this->ids])->all();
  210. if (empty($model)) {
  211. return ['code' => 1, 'msg' => '数据不存在'];
  212. }
  213. $result = Material::updateAll(['status' => $this->status], ['id' => $this->ids]);
  214. if ($result > 0) {
  215. $transaction->commit();
  216. return ['code' => 0, 'msg' => '成功修改' . $result . '条数据'];
  217. } else {
  218. $transaction->rollBack();
  219. return ['code' => 1, 'msg' => '修改失败'];
  220. }
  221. } catch (\Exception $e) {
  222. $transaction->rollBack();
  223. return ['code' => 1, 'msg' => $e->getMessage()];
  224. }
  225. }
  226. public function postTop(): array
  227. {
  228. $transaction = \Yii::$app->db->beginTransaction();
  229. try {
  230. $model = Material::find()->where(['id' => $this->ids])->all();
  231. if (empty($model)) {
  232. return ['code' => 1, 'msg' => '数据不存在'];
  233. }
  234. $result = Material::updateAll(['is_top' => $this->is_top], ['id' => $this->ids]);
  235. if ($result > 0) {
  236. $transaction->commit();
  237. return ['code' => 0, 'msg' => '成功修改' . $result . '条数据'];
  238. } else {
  239. $transaction->rollBack();
  240. return ['code' => 1, 'msg' => '修改失败'];
  241. }
  242. } catch (\Exception $e) {
  243. $transaction->rollBack();
  244. return ['code' => 1, 'msg' => $e->getMessage()];
  245. }
  246. }
  247. //基础设置
  248. public function materialSetting() {
  249. try {
  250. $store_id = $this->store_id;
  251. if (\Yii::$app->request->isPost) {
  252. $material_visual_level_str = $this->material_visual_level;
  253. $material_visual_level = explode(',', $material_visual_level_str);
  254. if (empty($material_visual_level)) {
  255. throw new \Exception('请选择会员等级');
  256. }
  257. $level = Level::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'level' => $material_visual_level])->select('level')
  258. ->orderBy('level asc')->column();
  259. if (in_array(-1, $material_visual_level)) {
  260. $level[] = -1;
  261. }
  262. Option::set('material_visual_level', json_encode($level, JSON_UNESCAPED_UNICODE), $store_id, 'store');
  263. return ['code' => 0, 'msg' => '修改成功'];
  264. } else {
  265. $level_data = Level::find()->where(['store_id' => $this->store_id, 'is_delete' => 0])->select('level, name')
  266. ->orderBy('level asc')->asArray()->all();
  267. array_unshift($level_data, [
  268. 'level' => -1,
  269. 'name' => '全部会员'
  270. ]);
  271. $materialVisualLevel = Option::get('material_visual_level', $store_id, 'store', json_encode([-1], JSON_UNESCAPED_UNICODE))['value'];
  272. return [
  273. 'code' => 0,
  274. 'msg' => '',
  275. 'data' => [
  276. 'level_list' => $level_data,
  277. 'material_visual_level' => json_decode($materialVisualLevel, true)
  278. ]
  279. ];
  280. }
  281. } catch (\Exception $e) {
  282. return ['code' => 1, 'msg' => $e->getMessage()];
  283. }
  284. }
  285. }