GoodsBrandCatForm.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace app\modules\admin\models;
  3. use app\models\GoodsBrandCat;
  4. use yii\base\Model;
  5. class GoodsBrandCatForm extends Model
  6. {
  7. public $id;
  8. public $store_id;
  9. public $status;
  10. public $sort;
  11. public $brand_cat_name;
  12. public $start_time;
  13. public $end_time;
  14. public $ids;
  15. public function rules()
  16. {
  17. return [
  18. [['id', 'store_id', 'status', 'sort'], 'integer'],
  19. [['brand_cat_name'], 'trim'],
  20. [['brand_cat_name', 'start_time', 'end_time', 'ids'], 'string'],
  21. [['sort'], 'integer', 'min' => 1],
  22. ['status', 'in', 'range' => [GoodsBrandCat::STATUS_CLOSE, GoodsBrandCat::STATUS_OPEN]],
  23. ];
  24. }
  25. public function attributeLabels()
  26. {
  27. return [
  28. 'id' => 'ID',
  29. 'store_id' => 'Store ID',
  30. 'status' => '状态',
  31. 'sort' => '排序',
  32. 'brand_cat_name' => '品牌分类名称',
  33. 'start_time' => '开始时间',
  34. 'end_time' => '结束时间',
  35. ];
  36. }
  37. //品牌分类列表
  38. public function list() {
  39. try {
  40. $store_id = $this->store_id;
  41. $status = $this->status;
  42. $brand_cat_name = $this->brand_cat_name;
  43. $start_time = $this->start_time;
  44. $end_time = $this->end_time;
  45. $query = GoodsBrandCat::find()->where(['store_id' => $store_id, 'is_delete' => 0]);
  46. if (isset($status) && in_array($status, [GoodsBrandCat::STATUS_CLOSE, GoodsBrandCat::STATUS_OPEN])) {
  47. $query->andWhere(['status' => $status]);
  48. }
  49. if (!empty($brand_cat_name)) {
  50. $query->andWhere(['LIKE', 'brand_cat_name', $brand_cat_name]);
  51. }
  52. if (!empty($start_time)) {
  53. $start_time = strtotime($start_time);
  54. $query->andWhere(['>=', 'created_at', $start_time]);
  55. }
  56. if (!empty($end_time)) {
  57. $end_time = strtotime($end_time);
  58. $query->andWhere(['<=', 'created_at', $end_time]);
  59. }
  60. $query->select('id, brand_cat_name, sort, status, created_at')->orderBy('sort DESC');
  61. $list = pagination_make($query);
  62. foreach ($list['list'] as &$item) {
  63. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  64. $item['status'] = intval($item['status']);
  65. }
  66. return [
  67. 'code' => 0,
  68. 'msg' => 'success',
  69. 'data' => $list
  70. ];
  71. } catch (\Exception $e) {
  72. return [
  73. 'code' => 1,
  74. 'msg' => $e->getMessage()
  75. ];
  76. }
  77. }
  78. //保存品牌分类
  79. public function save() {
  80. try {
  81. if (!$this->validate()) {
  82. throw new \Exception($this->getErrorSummary(false)[0]);
  83. }
  84. $id = $this->id;
  85. $brand_cat_name = $this->brand_cat_name;
  86. $sort = $this->sort;
  87. $status = intval($this->status);
  88. if (empty($brand_cat_name)) {
  89. throw new \Exception('品牌分类名称不能为空');
  90. }
  91. $GoodsBrandCat = GoodsBrandCat::findOne(['id' => $id, 'is_delete' => 0]) ?: new GoodsBrandCat();
  92. $GoodsBrandCat->brand_cat_name = $brand_cat_name;
  93. $GoodsBrandCat->sort = $sort;
  94. $GoodsBrandCat->status = $status;
  95. $GoodsBrandCat->store_id = $this->store_id;
  96. if (!$GoodsBrandCat->save()) {
  97. throw new \Exception(implode(';', array_values($GoodsBrandCat->firstErrors)));
  98. }
  99. return [
  100. 'code' => 0,
  101. 'msg' => '操作成功'
  102. ];
  103. } catch (\Exception $e) {
  104. return [
  105. 'code' => 1,
  106. 'msg' => $e->getMessage()
  107. ];
  108. }
  109. }
  110. //修改状态
  111. public function setStatus() {
  112. try {
  113. if (!$this->validate()) {
  114. throw new \Exception($this->getErrorSummary(false)[0]);
  115. }
  116. $ids = explode(',', $this->ids);
  117. $status = intval($this->status);
  118. if (empty($ids)) {
  119. throw new \Exception('品牌分类不存在');
  120. }
  121. foreach ($ids as $id) {
  122. $GoodsBrandCat = GoodsBrandCat::findOne(['id' => $id, 'is_delete' => 0]);
  123. if (!$GoodsBrandCat) {
  124. throw new \Exception('品牌分类不存在');
  125. }
  126. $GoodsBrandCat->status = $status;
  127. if (!$GoodsBrandCat->save()) {
  128. throw new \Exception(implode(';', array_values($GoodsBrandCat->firstErrors)));
  129. }
  130. }
  131. return [
  132. 'code' => 0,
  133. 'msg' => '操作成功'
  134. ];
  135. } catch (\Exception $e) {
  136. return [
  137. 'code' => 1,
  138. 'msg' => $e->getMessage()
  139. ];
  140. }
  141. }
  142. //删除品牌分类
  143. public function delete() {
  144. try {
  145. $ids = explode(',', $this->ids);
  146. if (empty($ids)) {
  147. throw new \Exception('品牌分类不存在');
  148. }
  149. foreach ($ids as $id) {
  150. $GoodsBrandCat = GoodsBrandCat::findOne(['id' => $id, 'is_delete' => 0]);
  151. if (!$GoodsBrandCat) {
  152. throw new \Exception('品牌分类不存在');
  153. }
  154. $GoodsBrandCat->is_delete = 1;
  155. if (!$GoodsBrandCat->save()) {
  156. throw new \Exception(implode(';', array_values($GoodsBrandCat->firstErrors)));
  157. }
  158. }
  159. return [
  160. 'code' => 0,
  161. 'msg' => '操作成功'
  162. ];
  163. } catch (\Exception $e) {
  164. return [
  165. 'code' => 1,
  166. 'msg' => $e->getMessage()
  167. ];
  168. }
  169. }
  170. }