CatForm.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\Cat;
  9. use app\models\Goods;
  10. use app\models\GoodsCat;
  11. use yii\base\Model;
  12. class CatForm extends Model
  13. {
  14. public $store_id = 1;
  15. public $parent_id;
  16. public $sort;
  17. public $name;
  18. public $pic_url;
  19. public $is_show;
  20. public $model;
  21. public $shop_count;
  22. /**
  23. * @return array
  24. */
  25. public function rules()
  26. {
  27. return [
  28. [['parent_id', 'store_id', 'sort'], 'integer'],
  29. [['name'], 'string', 'max' => 60],
  30. [['pic_url'], 'string'],
  31. [['sort'], 'default', 'value' => 0],
  32. [['is_show'], 'default', 'value' => 1],
  33. [['is_delete'], 'default', 'value' => 0],
  34. [['shop_count'], 'integer']
  35. ];
  36. }
  37. /**
  38. * 保存分类
  39. * @return array
  40. */
  41. public function save()
  42. {
  43. $this->model->parent_id = $this->parent_id;
  44. $this->model->is_show = $this->is_show;
  45. if ($this->is_show == 0) {
  46. // 产品分类隐藏同时下架该分类下所有商品
  47. $goods_ids = GoodsCat::find()->where(['is_delete' => 0, 'store_id' => get_store_id(),
  48. 'cat_id' => post_params('id')])->select('goods_id')->asArray()->all();
  49. Goods::updateAll(['status' => Goods::STATUS_DISABLE],['in', 'id', array_column($goods_ids, 'goods_id')]);
  50. }
  51. $this->model->name = $this->name;
  52. $this->model->pic_url = $this->pic_url;
  53. $this->model->sort = $this->sort;
  54. $this->model->store_id = $this->store_id;
  55. $this->model->shop_count = $this->shop_count;
  56. if ($this->model->save()) {
  57. return [
  58. 'code' => 0,
  59. 'msg' => '提交成功'
  60. ];
  61. } else {
  62. return [
  63. 'code' => 0,
  64. 'msg' => '保存失败'
  65. ];
  66. }
  67. }
  68. /**
  69. * Undocumented function
  70. *
  71. * @Author LGL 24963@qq.com
  72. * @DateTime 2021-01-18
  73. * @desc: 根据id获取分类列表
  74. * @param array $id
  75. * @return void
  76. */
  77. public static function getCatListById ($id = [])
  78. {
  79. if (empty($id) && !is_array($id)) {
  80. return [];
  81. }
  82. return Cat::find()->where(['in', 'id', $id])->select('id, name, pic_url')->asArray()->all();
  83. }
  84. }