CategoryForm.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * CategoryForm.php
  4. * todo 文件描述
  5. * Created on 2025/1/11 上午10:49
  6. * @author: hankaige
  7. */
  8. namespace app\modules\admin\models\material;
  9. use app\models\material\Material;
  10. use app\models\material\MaterialCategory;
  11. use yii\base\Model;
  12. class CategoryForm extends Model
  13. {
  14. const SCENE_LIST = 'list';
  15. const SCENE_EDIT = 'edit';
  16. const SCENE_DELETE = 'delete';
  17. const SCENE_STATUS = 'status';
  18. public int $store_id;
  19. // 列表搜索
  20. public string $name;
  21. public string $start_time;
  22. public string $end_time;
  23. public array $ids;
  24. public int $id;
  25. public int $sort;
  26. public int $status;
  27. // 新分类创建
  28. public function rules():array
  29. {
  30. return [
  31. [['name','start_time','end_time'],'string'],
  32. ['ids','validateIds','on' => [self::SCENE_DELETE,self::SCENE_STATUS]],
  33. [['sort','status','store_id','id'],'integer', 'on' => [self::SCENE_LIST,self::SCENE_EDIT,self::SCENE_DELETE]],
  34. [['name','sort','status'],'required','on' => self::SCENE_EDIT],
  35. [['ids'],'required','on' => [self::SCENE_DELETE,self::SCENE_STATUS]],
  36. ['status','required','on' => self::SCENE_STATUS]
  37. ];
  38. }
  39. /**
  40. * 自定义验证规则:确保 ids 是数组且长度大于 0
  41. */
  42. public function validateIds($attribute, $params)
  43. {
  44. if (!is_array($this->$attribute)) {
  45. $this->addError($attribute, 'ids 必须是一个数组。');
  46. return;
  47. }
  48. if (count($this->$attribute) === 0) {
  49. $this->addError($attribute, 'ids 数组的长度必须大于 0。');
  50. }
  51. }
  52. public function getList():array{
  53. $query = MaterialCategory::find()->where(['store_id'=>$this->store_id])
  54. ->andWhere(['OR', ['delete_time' => 0], ['IS', 'delete_time', NULL]])->orderBy('sort ASC');
  55. if($this->status != -1){
  56. $query->andWhere(['status'=>$this->status]);
  57. }
  58. if(!empty($this->name)){
  59. $query->andWhere(['like','name',$this->name]);
  60. }
  61. return [
  62. 'code' => 0,
  63. 'data' => pagination_make($query),
  64. 'msg' => '数据获取成功'
  65. ];
  66. }
  67. public function getListNoPage():array{
  68. $query = MaterialCategory::find()->where(['store_id'=>$this->store_id])
  69. ->andWhere(['OR', ['delete_time' => 0], ['IS', 'delete_time', NULL]])->orderBy('sort ASC');
  70. return [
  71. 'code' => 0,
  72. 'data' => $query->select('id,name,sort,status')->asArray()->all(),
  73. 'msg' => '数据获取成功'
  74. ];
  75. }
  76. public function postEdit():array
  77. {
  78. try {
  79. $model = MaterialCategory::findOne($this->id);
  80. if(empty($model)){
  81. $model = new MaterialCategory();
  82. }
  83. $model->store_id = $this->store_id;
  84. $model->name = $this->name;
  85. $model->sort = $this->sort;
  86. $model->status = $this->status;
  87. if($model->save()){
  88. return ['code' => 0,'msg' => '保存成功'];
  89. }else{
  90. return ['code' => 1,'msg' => '保存失败','data' => $model->getErrors()];
  91. }
  92. }catch (\Exception $e){
  93. return ['code' => 1, 'msg' => $e->getMessage()];
  94. }
  95. }
  96. public function postDelete():array
  97. {
  98. try {
  99. $model = MaterialCategory::find()->where(['id' => $this->ids])->all();
  100. if(empty($model)){
  101. return ['code' => 1,'msg' => '数据不存在'];
  102. }
  103. // 验证分类下是否存在对应的素材
  104. $materialCount = Material::find()->where(['store_id' => $this->store_id, 'material_category_id' => $this->ids])->andWhere(['OR', ['delete_time' => 0], ['IS', 'delete_time', NULL]])->count();
  105. if($materialCount > 0){
  106. return ['code' => 1,'msg' => '分类下存在'.$materialCount.'条素材,请先删除素材'];
  107. }
  108. $result = MaterialCategory::updateAll(['delete_time' => date('Y-m-n H:i:s')],['id' => $this->ids]);
  109. if($result > 0){
  110. return ['code' => 0,'msg' => '成功删除'.$result.'条数据'];
  111. }else{
  112. return ['code' => 1,'msg' => '删除失败'];
  113. }
  114. }catch (\Exception $e){
  115. return ['code' => 1, 'msg' => $e->getMessage()];
  116. }
  117. }
  118. public function postStatus():array
  119. {
  120. try {
  121. $model = MaterialCategory::find()->where(['id' => $this->ids])->all();
  122. if(empty($model)){
  123. return ['code' => 1,'msg' => '数据不存在'];
  124. }
  125. MaterialCategory::updateAll(['status' => $this->status],['id' => $this->ids]);
  126. return ['code' => 0,'msg' => '保存成功'];
  127. }catch (\Exception $e){
  128. return ['code' => 1, 'msg' => $e->getMessage()];
  129. }
  130. }
  131. }