| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- namespace app\modules\admin\models;
- use app\models\SaasUser;
- use app\models\User;
- use app\models\UserAuditLog;
- use app\models\UserAuditSetting;
- use app\models\UserLabel;
- use app\utils\Notice\NoticeSend;
- use yii\base\Model;
- use yii\helpers\ArrayHelper;
- class UserLabelForm extends Model
- {
- public $id;
- public $store_id;
- public $status;
- public $sort;
- public $label_name;
- public $start_time;
- public $end_time;
- public $ids;
- public function rules()
- {
- return [
- [['id', 'store_id', 'status', 'sort'], 'integer'],
- [['label_name'], 'trim'],
- [['label_name', 'start_time', 'end_time', 'ids'], 'string'],
- [['sort'], 'integer', 'min' => 1],
- ['status', 'in', 'range' => [UserLabel::STATUS_CLOSE, UserLabel::STATUS_OPEN]],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'store_id' => 'Store ID',
- 'status' => '状态',
- 'sort' => '排序',
- 'label_name' => '标签名称',
- 'start_time' => '开始时间',
- 'end_time' => '结束时间',
- ];
- }
- //用户标签列表
- public function list() {
- try {
- $store_id = $this->store_id;
- $status = $this->status;
- $label_name = $this->label_name;
- $start_time = $this->start_time;
- $end_time = $this->end_time;
- $query = UserLabel::find()->where(['store_id' => $store_id, 'is_delete' => 0]);
- if (isset($status) && in_array($status, [UserLabel::STATUS_CLOSE, UserLabel::STATUS_OPEN])) {
- $query->andWhere(['status' => $status]);
- }
- if (!empty($label_name)) {
- $query->andWhere(['LIKE', 'label_name', $label_name]);
- }
- if (!empty($start_time)) {
- $start_time = strtotime($start_time);
- $query->andWhere(['>=', 'created_at', $start_time]);
- }
- if (!empty($end_time)) {
- $end_time = strtotime($end_time);
- $query->andWhere(['<=', 'created_at', $end_time]);
- }
- $query->select('id, label_name, sort, status, created_at')->orderBy('sort DESC');
- $list = pagination_make($query);
- foreach ($list['list'] as &$item) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- $item['status'] = intval($item['status']);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $list
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //保存标签
- public function save() {
- try {
- if (!$this->validate()) {
- throw new \Exception($this->getErrorSummary(false)[0]);
- }
- $id = $this->id;
- $label_name = $this->label_name;
- $sort = $this->sort;
- $status = intval($this->status);
- if (empty($label_name)) {
- throw new \Exception('标签名称不能为空');
- }
- $userLabel = UserLabel::findOne(['id' => $id, 'is_delete' => 0]) ?: new UserLabel();
- $userLabel->label_name = $label_name;
- $userLabel->sort = $sort;
- $userLabel->status = $status;
- $userLabel->store_id = $this->store_id;
- if (!$userLabel->save()) {
- throw new \Exception(implode(';', array_values($userLabel->firstErrors)));
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //修改状态
- public function setStatus() {
- try {
- if (!$this->validate()) {
- throw new \Exception($this->getErrorSummary(false)[0]);
- }
- $ids = explode(',', $this->ids);
- $status = intval($this->status);
- if (empty($ids)) {
- throw new \Exception('用户标签不存在1');
- }
- foreach ($ids as $id) {
- $userLabel = UserLabel::findOne(['id' => $id, 'is_delete' => 0]);
- if (!$userLabel) {
- throw new \Exception('用户标签不存在2');
- }
- $userLabel->status = $status;
- if (!$userLabel->save()) {
- throw new \Exception(implode(';', array_values($userLabel->firstErrors)));
- }
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //删除标签
- public function delete() {
- try {
- $ids = explode(',', $this->ids);
- if (empty($ids)) {
- throw new \Exception('用户标签不存在');
- }
- foreach ($ids as $id) {
- $userLabel = UserLabel::findOne(['id' => $id, 'is_delete' => 0]);
- if (!$userLabel) {
- throw new \Exception('用户标签不存在');
- }
- $userLabel->is_delete = 1;
- if (!$userLabel->save()) {
- throw new \Exception(implode(';', array_values($userLabel->firstErrors)));
- }
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|