IntelligentSceneForm.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\IntelligentMatchCat;
  9. use app\models\IntelligentScene;
  10. use yii\base\Model;
  11. class IntelligentSceneForm extends Model
  12. {
  13. public $store_id = 1;
  14. public $name;
  15. public $pic_url;
  16. public $status;
  17. public $model;
  18. public $keyword = '';
  19. public $cat_id = 0;
  20. /**
  21. * @return array
  22. */
  23. public function rules()
  24. {
  25. return [
  26. [[ 'store_id', 'sort'], 'integer'],
  27. [['name','pic_url','keyword'], 'string', 'max' => 255],
  28. [['status'], 'default', 'value' => 1],
  29. [['is_delete','cat_id'], 'default', 'value' => 0]
  30. ];
  31. }
  32. public function list(){
  33. $query = IntelligentScene::find()->where(['store_id'=>get_store_id(),'is_delete'=>0]);
  34. if($this->keyword){
  35. $query->andWhere(['like','name',$this->keyword]);
  36. }
  37. if($this->cat_id > 0){
  38. $query->andWhere(['cat_id'=>$this->cat_id]);
  39. }
  40. $data = pagination_make($query);
  41. foreach($data['list'] as &$item){
  42. $item['cat_id_arr'] = $this->getCat($item['cat_id']);
  43. }
  44. return [
  45. 'code' => 0,
  46. 'msg' => 'success',
  47. 'data' => [
  48. 'data' => $data['list'],
  49. 'pageNo' => $data['pageNo'],
  50. 'totalCount' => $data['totalCount']
  51. ]
  52. ];
  53. }
  54. private function getCat($catId){
  55. $item = IntelligentMatchCat::findOne($catId);
  56. if($item['parent_id'] == 0){
  57. return [(string)$item['id']];
  58. }
  59. $item2 = IntelligentMatchCat::findOne($item['parent_id']);
  60. if($item2['parent_id'] == 0){
  61. return [(string)$item2['id'],(string)$item['id']];
  62. }
  63. $item3 = IntelligentMatchCat::findOne($item2['parent_id']);
  64. return [(string)$item3['id'],(string)$item2['id'],(string)$item['id']];
  65. }
  66. /**
  67. * 保存场景
  68. * @return array
  69. */
  70. public function save()
  71. {
  72. $this->model->status = $this->status;
  73. $this->model->name = $this->name;
  74. $this->model->pic_url = $this->pic_url;
  75. $this->model->store_id = get_store_id();
  76. $this->model->cat_id = $this->cat_id;
  77. if ($this->model->save()) {
  78. return [
  79. 'code' => 0,
  80. 'msg' => '提交成功'
  81. ];
  82. } else {
  83. return [
  84. 'code' => 0,
  85. 'msg' => '保存失败'
  86. ];
  87. }
  88. }
  89. }