PtActivityGoodsCatForm.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\pt;
  8. use app\models\Cat;
  9. use app\models\DeliveryRules;
  10. use app\models\Goods;
  11. use app\models\GoodsCat;
  12. use app\models\PtActivity;
  13. use app\models\PtActivityGoodsCat;
  14. use app\models\SeckillActivity;
  15. use app\models\SeckillActivityGoods;
  16. use app\models\SeckillActivityOrderLog;
  17. use yii\base\Model;
  18. class PtActivityGoodsCatForm extends Model
  19. {
  20. public $id;
  21. public $ids;
  22. public $name;
  23. public $is_use_coupon;
  24. public $start_time;
  25. public $end_time;
  26. public $goods;
  27. public $status;
  28. public $self_limit_num;
  29. public $order_limit_num;
  30. public $cat_id;
  31. public function rules()
  32. {
  33. return [
  34. [['status', 'is_use_coupon', 'id', 'goods_id', 'cat_id'], 'integer'],
  35. [['start_time', 'end_time', 'ids', 'name'], 'string'],
  36. [['goods'], 'safe']
  37. ];
  38. }
  39. public function search ()
  40. {
  41. try {
  42. $query = PtActivityGoodsCat::find()->where(['store_id' => get_store_id(), 'is_delete' => 0]);
  43. if ($this->name) {
  44. $query->andWhere(['LIKE', 'name', $this->name]);
  45. }
  46. if ((int)$this->status !== -1 && $this->status !== null) {
  47. $query->andWhere(['status' => $this->status]);
  48. }
  49. if ($this->start_time) {
  50. $start_time = strtotime($this->start_time);
  51. $query->andWhere(['>=', 'created_at', $start_time]);
  52. }
  53. if ($this->end_time) {
  54. $end_time = strtotime($this->end_time);
  55. $query->andWhere(['<=', 'created_at', $end_time]);
  56. }
  57. $query->select('id, status, created_at, updated_at, name')->orderBy('created_at desc');
  58. $pagination = pagination_make($query);
  59. foreach ($pagination['list'] as &$item) {
  60. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  61. $item['updated_at'] = $item['updated_at'] ? date("Y-m-d H:i:s", $item['updated_at']) : '';
  62. $item['status'] *= 1;
  63. }
  64. return [
  65. 'code' => 0,
  66. 'msg' => 'success',
  67. 'data' => [
  68. 'data' => $pagination['list'],
  69. 'pageNo' => $pagination['pageNo'],
  70. 'totalCount' => $pagination['totalCount'],
  71. ]
  72. ];
  73. } catch (\Exception $e) {
  74. return [
  75. 'code' => 1,
  76. 'msg' => $e->getMessage()
  77. ];
  78. }
  79. }
  80. public function save ()
  81. {
  82. $t = \Yii::$app->db->beginTransaction();
  83. try {
  84. if (empty($this->name)) {
  85. throw new \Exception('分类名称不可为空');
  86. }
  87. $pt_goods_cat = new PtActivityGoodsCat();
  88. if ($this->id) {
  89. $pt_goods_cat = PtActivityGoodsCat::findOne($this->id);
  90. if (empty($pt_goods_cat)) {
  91. throw new \Exception('查找失败');
  92. }
  93. }
  94. $pt_goods_cat->name = $this->name;
  95. $pt_goods_cat->status = $this->status ?: 1;
  96. $pt_goods_cat->store_id = get_store_id();
  97. if (!$pt_goods_cat->save()) {
  98. throw new \Exception(json_encode($pt_goods_cat->errors));
  99. }
  100. $t->commit();
  101. return [
  102. 'code' => 0,
  103. 'msg' => '操作成功!'
  104. ];
  105. } catch (\Exception $e) {
  106. $t->rollBack();
  107. return [
  108. 'code' => 1,
  109. 'msg' => $e->getMessage()
  110. ];
  111. }
  112. }
  113. public function setStatus ()
  114. {
  115. try {
  116. if ($this->ids) {
  117. $ids = explode(',', $this->ids);
  118. if (in_array($this->status, [0, 1])) {
  119. PtActivityGoodsCat::updateAll(['status' => $this->status], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]);
  120. }
  121. if ((int)$this->status === 2) {
  122. PtActivityGoodsCat::updateAll(['is_delete' => 1], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]);
  123. }
  124. } else {
  125. $rules = PtActivityGoodsCat::findOne(['id' => $this->id, 'is_delete' => 0, 'store_id' => get_store_id()]);
  126. if (empty($rules)) {
  127. throw new \Exception("分类未找到");
  128. }
  129. if (in_array($this->status, [0, 1])) {
  130. $rules->status = $this->status;
  131. }
  132. if ((int)$this->status === 2) {
  133. $rules->is_delete = 1;
  134. }
  135. if (!$rules->save()) {
  136. throw new \Exception(json_encode($rules->errors));
  137. }
  138. }
  139. return [
  140. 'code' => 0,
  141. 'msg' => '操作成功!'
  142. ];
  143. } catch (\Exception $e) {
  144. return [
  145. 'code' => 1,
  146. 'msg' => $e->getMessage()
  147. ];
  148. }
  149. }
  150. }