PtActivityBannerForm.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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\PtActivityBanner;
  14. use app\models\PtActivityGoodsCat;
  15. use app\models\SeckillActivity;
  16. use app\models\SeckillActivityGoods;
  17. use app\models\SeckillActivityOrderLog;
  18. use yii\base\Model;
  19. class PtActivityBannerForm extends Model
  20. {
  21. public $id;
  22. public $ids;
  23. public $name;
  24. public $start_time;
  25. public $end_time;
  26. public $status;
  27. public $url;
  28. public $sort;
  29. public $cover_pic;
  30. public function rules()
  31. {
  32. return [
  33. [['status', 'id', 'goods_id', 'sort'], 'integer'],
  34. [['start_time', 'ids', 'name', 'cover_pic'], 'string'],
  35. [['url'], 'safe']
  36. ];
  37. }
  38. public function search ()
  39. {
  40. try {
  41. $query = PtActivityBanner::find()->where(['store_id' => get_store_id(), 'is_delete' => 0]);
  42. if ($this->name) {
  43. $query->andWhere(['LIKE', 'name', $this->name]);
  44. }
  45. if ((int)$this->status !== -1 && $this->status !== null) {
  46. $query->andWhere(['status' => $this->status]);
  47. }
  48. if ($this->start_time) {
  49. $start_time = strtotime($this->start_time);
  50. $query->andWhere(['>=', 'created_at', $start_time]);
  51. }
  52. if ($this->end_time) {
  53. $end_time = strtotime($this->end_time);
  54. $query->andWhere(['<=', 'created_at', $end_time]);
  55. }
  56. $query->select('id, created_at, updated_at, name, cover_pic, url, status, sort')->orderBy('created_at desc');
  57. $pagination = pagination_make($query);
  58. foreach ($pagination['list'] as &$item) {
  59. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  60. $item['updated_at'] = $item['updated_at'] ? date("Y-m-d H:i:s", $item['updated_at']) : '';
  61. $item['status'] *= 1;
  62. $item['url'] = !empty($item['url']) ? json_decode($item['url'], true) : [
  63. 'name' => '',
  64. 'link' => '',
  65. 'open_type' => ''
  66. ];
  67. }
  68. return [
  69. 'code' => 0,
  70. 'msg' => 'success',
  71. 'data' => [
  72. 'data' => $pagination['list'],
  73. 'pageNo' => $pagination['pageNo'],
  74. 'totalCount' => $pagination['totalCount'],
  75. ]
  76. ];
  77. } catch (\Exception $e) {
  78. return [
  79. 'code' => 1,
  80. 'msg' => $e->getMessage()
  81. ];
  82. }
  83. }
  84. public function save ()
  85. {
  86. $t = \Yii::$app->db->beginTransaction();
  87. try {
  88. if (empty($this->cover_pic)) {
  89. throw new \Exception('图片地址不可为空');
  90. }
  91. $banner_form = new PtActivityBanner();
  92. if ($this->id) {
  93. $banner_form = PtActivityBanner::findOne($this->id);
  94. if (empty($banner_form)) {
  95. throw new \Exception('查找失败');
  96. }
  97. }
  98. if (!empty($this->url)) {
  99. $url = $this->url;
  100. } else {
  101. $url = [
  102. 'name' => '',
  103. 'link' => '',
  104. 'open_type' => ''
  105. ];
  106. }
  107. $banner_form->name = $this->name;
  108. $banner_form->cover_pic = $this->cover_pic;
  109. $banner_form->url = json_encode($url);
  110. $banner_form->sort = $this->sort;
  111. $banner_form->status = $this->status ?: 0;
  112. $banner_form->store_id = get_store_id();
  113. if (!$banner_form->save()) {
  114. throw new \Exception(json_encode($banner_form->errors));
  115. }
  116. $t->commit();
  117. return [
  118. 'code' => 0,
  119. 'msg' => '操作成功!'
  120. ];
  121. } catch (\Exception $e) {
  122. $t->rollBack();
  123. return [
  124. 'code' => 1,
  125. 'msg' => $e->getMessage()
  126. ];
  127. }
  128. }
  129. public function setStatus ()
  130. {
  131. try {
  132. if ($this->ids) {
  133. $ids = explode(',', $this->ids);
  134. if (in_array($this->status, [0, 1])) {
  135. PtActivityBanner::updateAll(['status' => $this->status], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]);
  136. }
  137. if ((int)$this->status === 2) {
  138. PtActivityBanner::updateAll(['is_delete' => 1], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]);
  139. }
  140. } else {
  141. $rules = PtActivityBanner::findOne(['id' => $this->id, 'is_delete' => 0, 'store_id' => get_store_id()]);
  142. if (empty($rules)) {
  143. throw new \Exception("分类未找到");
  144. }
  145. if (in_array($this->status, [0, 1])) {
  146. $rules->status = $this->status;
  147. }
  148. if ((int)$this->status === 2) {
  149. $rules->is_delete = 1;
  150. }
  151. if (!$rules->save()) {
  152. throw new \Exception(json_encode($rules->errors));
  153. }
  154. }
  155. return [
  156. 'code' => 0,
  157. 'msg' => '操作成功!'
  158. ];
  159. } catch (\Exception $e) {
  160. return [
  161. 'code' => 1,
  162. 'msg' => $e->getMessage()
  163. ];
  164. }
  165. }
  166. }