UserLabelForm.php 6.0 KB

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