Tag.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace common\models\user;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use PHPUnit\Util\Exception;
  6. use Yii;
  7. /**
  8. * This is the model class for table "qimall_tag".
  9. *
  10. *
  11. * @property int $id
  12. * @property int $mall_id
  13. * @property string $name 标签名称
  14. * @property int $type 标签类型
  15. * @property string $condition 条件
  16. * @property int $status 状态[-1:删除;0:禁用;1启用]
  17. * @property int $created_at
  18. * @property int $updated_at
  19. */
  20. class Tag extends BaseActiveRecord
  21. {
  22. public static $type_arr = [
  23. 1 => "价值分层",
  24. 2 => "生命周期",
  25. 3 => "营销偏好",
  26. 4 => "行为偏好",
  27. ];
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%tag}}';
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['mall_id'], 'required'],
  42. [['mall_id', 'type', 'status', 'created_at', 'updated_at'], 'integer'],
  43. [['name'], 'string', 'max' => 45],
  44. [['condition'], 'string', 'max' => 3000],
  45. ];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'mall_id' => 'Mall ID',
  55. 'name' => '标签名称',
  56. 'type' => '标签类型',
  57. 'condition' => '条件',
  58. 'status' => '状态[-1:删除;0:禁用;1启用]',
  59. 'created_at' => 'Created At',
  60. 'updated_at' => 'Updated At',
  61. ];
  62. }
  63. /**
  64. * @param array $where
  65. * @param string $select
  66. * @param array $with
  67. * @param int $is_array
  68. * @return array|Tag[]|\yii\db\ActiveRecord[]
  69. */
  70. public static function getTagList(array $where, $select = '*', array $with = [], int $is_array = 0)
  71. {
  72. $query = self::find();
  73. self::paramPack(['where' => $where, 'with' => $with], $query);
  74. return $query->select($select)->asArray($is_array)->all();
  75. }
  76. /**
  77. * 保存数据
  78. * @param array $params
  79. * @return bool|Tag
  80. */
  81. public static function setData(array $params)
  82. {
  83. try {
  84. if (!empty($params['id'])) {
  85. $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  86. if (empty($model)) throw new Exception('服务不存在或已删除');
  87. } else {
  88. $model = new self();
  89. $model->loadDefaultValues();
  90. }
  91. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  92. return $model;
  93. } catch (Exception $e) {
  94. self::$static_error = $e->getMessage();
  95. return false;
  96. }
  97. }
  98. /**
  99. * 删除
  100. * @param $params
  101. * @param $status
  102. * @return bool
  103. */
  104. public static function del($params, $status)
  105. {
  106. try {
  107. $query = self::find()->where(['and',
  108. ['id' => $params['id']],
  109. ['<>', 'status', StatusEnum::DELETE]
  110. ]);
  111. $model = $query->one();
  112. $model->status = $status;
  113. $res = $model->save();
  114. if ($res === false) throw new Exception('删除失败');
  115. return true;
  116. } catch (\Exception $e) {
  117. return false;
  118. }
  119. }
  120. }