MembersCard.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace addons\MembersCard\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%addons_members_card}}".
  7. *
  8. * @property int $id
  9. * @property int $mall_id 商城ID
  10. * @property string $name 会员卡名称
  11. * @property int $is_enable 是否启用 0 否 1 是
  12. * @property float $discount 折扣
  13. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  14. * @property int $created_at 创建时间
  15. * @property int $updated_at 修改时间
  16. * @property float $child_discount 子卡折扣
  17. * @property int $child_num 子卡数量
  18. * @property int $child_expired_days 子卡有效期
  19. * @property string $background_url 会员卡背景图
  20. * @property string $member_limit 会员权限
  21. */
  22. class MembersCard extends \common\models\base\BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%addons_members_card}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'is_enable', 'status', 'created_at', 'updated_at', 'child_num', 'child_expired_days','is_enable_virtual'], 'integer'],
  38. [['discount', 'child_discount'], 'number'],
  39. [['name'], 'string', 'max' => 100],
  40. [['background_url', 'member_limit','child_upgrade_links'], 'string', 'max' => 255],
  41. ];
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'id' => 'ID',
  50. 'mall_id' => 'Mall ID',
  51. 'name' => 'Name',
  52. 'is_enable' => 'Is Enable',
  53. 'discount' => 'Discount',
  54. 'status' => 'Status',
  55. 'created_at' => 'Created At',
  56. 'updated_at' => 'Updated At',
  57. 'child_discount' => 'Child Discount',
  58. 'child_num' => 'Child Num',
  59. 'child_expired_days' => 'Child Expired Days',
  60. 'background_url' => 'Background Url',
  61. 'member_limit' => 'Member Limit',
  62. 'child_upgrade_links' => 'Child Upgrade Links',
  63. 'is_enable_virtual' => 'Is Enable Virtual',
  64. ];
  65. }
  66. public static function setData(array $params)
  67. {
  68. try {
  69. if (!empty($params['id'])) {
  70. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  71. if (empty($model)) throw new \Exception('数据不存在或已删除');
  72. } else {
  73. $model = new self();
  74. $model->loadDefaultValues();
  75. }
  76. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  77. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  78. return $model;
  79. } catch (\Exception $e) {
  80. self::$static_error = $e->getMessage();
  81. return false;
  82. }
  83. }
  84. public static function search($params)
  85. {
  86. $query = self::find()
  87. ->where(['status' => StatusEnum::ENABLED, 'mall_id' => $params['mall_id'],'is_enable'=>1]);
  88. if (isset($params['keyword']) && !empty($params['keyword'])) {
  89. $query->andWhere(['or', ['like', 'name', $params['keyword']]]);
  90. }
  91. $list = $query->limit(20)->select('id,name')->asArray()->all();
  92. return $list;
  93. }
  94. }