GoodsBrandForm.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace app\modules\admin\models;
  3. use app\models\Goods;
  4. use app\models\GoodsBrand;
  5. use app\models\GoodsBrandCat;
  6. class GoodsBrandForm extends \yii\base\Model
  7. {
  8. public $brand_name;
  9. public $brand_logo;
  10. public $id;
  11. public $store_id;
  12. public $brand_cat_id;
  13. public $brand_bg;
  14. public $brand_desc;
  15. public $sort;
  16. public $is_show;
  17. public $brand_id;
  18. public $type;//0隐藏 1显示 2删除
  19. public $start_time;
  20. public $end_time;
  21. public function rules()
  22. {
  23. return [
  24. [['brand_name', 'brand_logo', 'brand_id', 'start_time', 'end_time', 'brand_bg', 'brand_desc'], 'string'],
  25. [['id', 'store_id', 'sort', 'is_show', 'type', 'brand_cat_id'], 'integer']
  26. ];
  27. }
  28. public function getList() {
  29. $store_id = $this->store_id;
  30. $brand_name = $this->brand_name;
  31. $brand_cat_id = $this->brand_cat_id;
  32. $is_show = $this->is_show;
  33. $start_time = $this->start_time;
  34. $end_time = $this->end_time;
  35. $query = GoodsBrand::find()->where(['store_id' => $store_id, 'is_delete' => 0]);//->orderBy('sort DESC');
  36. if (!empty($brand_name)) {
  37. $query->andWhere(['LIKE', 'brand_name', $brand_name]);
  38. }
  39. if (!empty($brand_cat_id)) {
  40. $query->andWhere(['brand_cat_id' => $brand_cat_id]);
  41. }
  42. if (isset($is_show) && $is_show > -1) {
  43. $query->andWhere(['is_show' => $is_show]);
  44. }
  45. if (!empty($start_time)) {
  46. $start_time = strtotime($start_time);
  47. $query->andWhere(['>=', 'created_at', $start_time]);
  48. }
  49. if (!empty($end_time)) {
  50. $end_time = strtotime($end_time);
  51. $query->andWhere(['<=', 'created_at', $end_time]);
  52. }
  53. $query->select('id, brand_name, brand_logo, is_show, sort, created_at, updated_at, brand_cat_id, brand_bg ,brand_desc')->orderBy('sort DESC, id DESC');
  54. $list = pagination_make($query);
  55. foreach ($list['list'] as &$item) {
  56. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  57. $item['updated_at'] = date('Y-m-d H:i:s', $item['updated_at']);
  58. $item['is_show'] = (int)$item['is_show'];
  59. $item['goods_num'] = Goods::find()->where(['brand_id' => $item['id'], 'is_delete' => 0])->count() ?: '0';
  60. $goodsBrandCat = GoodsBrandCat::getBrandCat($item['brand_cat_id']);
  61. $item['brand_cat_name'] = $goodsBrandCat->brand_cat_name ?: '';
  62. $item['brand_bg'] = $item['brand_bg'] ?: '';
  63. $item['brand_desc'] = $item['brand_desc'] ?: '';
  64. }
  65. return [
  66. 'code' => 0,
  67. 'msg' => 'success',
  68. 'data' => [
  69. 'data' => $list['list'],
  70. 'pageNo' => $list['pageNo'],
  71. 'totalCount' => $list['totalCount']
  72. ]
  73. ];
  74. }
  75. public function saveBrand() {
  76. try {
  77. $id = $this->id;
  78. $store_id = $this->store_id;
  79. $brand_cat_id = $this->brand_cat_id;
  80. $brand_name = trim($this->brand_name);
  81. $brand_logo = trim($this->brand_logo);
  82. $brand_bg = $this->brand_bg;
  83. $brand_desc = $this->brand_desc;
  84. $sort = (int)$this->sort;
  85. $is_show = (int)$this->is_show;
  86. if (empty($brand_name)) {
  87. throw new \Exception('品牌名称不能为空');
  88. }
  89. if (empty($brand_logo)) {
  90. throw new \Exception('品牌Logo不能为空');
  91. }
  92. if ($brand_cat_id) {
  93. if (!GoodsBrandCat::getBrandCat($brand_cat_id)) {
  94. throw new \Exception('品牌分类查询失败');
  95. }
  96. }
  97. $goods_brand = GoodsBrand::findOne($id) ?: new GoodsBrand();
  98. $goods_brand->store_id = $store_id;
  99. $goods_brand->brand_cat_id = $brand_cat_id;
  100. $goods_brand->brand_name = $brand_name ?: '';
  101. $goods_brand->brand_logo = $brand_logo ?: '';
  102. $goods_brand->brand_bg = $brand_bg ?: '';
  103. $goods_brand->brand_desc = $brand_desc ?: '';
  104. $goods_brand->sort = $sort ?: 0;
  105. $goods_brand->is_show = $is_show ?? 1;
  106. if (!$goods_brand->save()) {
  107. throw new \Exception(json_encode($goods_brand->errors, JSON_UNESCAPED_UNICODE));
  108. }
  109. return [
  110. 'code' => 0,
  111. 'msg' => '操作成功'
  112. ];
  113. } catch (\Exception $e) {
  114. return [
  115. 'code' => 1,
  116. 'msg' => $e->getMessage()
  117. ];
  118. }
  119. }
  120. public function setStatus() {
  121. try {
  122. $brand_id_str = $this->brand_id;
  123. $type = (int)$this->type;
  124. $brand_id_arr = explode(',', $brand_id_str);
  125. foreach ($brand_id_arr as $brand_id) {
  126. $key = "is_show";
  127. $value = 1;
  128. switch ($type) {
  129. case 1:
  130. $value = 1;
  131. break;
  132. case 2:
  133. $key = "is_delete";
  134. break;
  135. default:
  136. $value = 0;
  137. }
  138. $goodsBrand = GoodsBrand::findOne($brand_id);
  139. if ($goodsBrand) {
  140. $goodsBrand->$key = $value;
  141. if (!$goodsBrand->save()) {
  142. throw new \Exception(json_encode($goodsBrand->errors, JSON_UNESCAPED_UNICODE));
  143. }
  144. }
  145. }
  146. return [
  147. 'code' => 0,
  148. 'msg' => '操作成功'
  149. ];
  150. } catch (\Exception $e) {
  151. return [
  152. 'code' => 1,
  153. 'msg' => $e->getMessage()
  154. ];
  155. }
  156. }
  157. }