UserCoupon.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use Yii;
  9. use yii\db\ActiveRecord;
  10. use yii\behaviors\TimestampBehavior;
  11. /**
  12. * This is the model class for table "{{%user_coupon}}".
  13. *
  14. * @property int $id
  15. * @property int $store_id
  16. * @property int $user_id 用户id
  17. * @property int $coupon_id 优惠券id
  18. * @property int $coupon_auto_send_id 自动发放id
  19. * @property int $begin_time 有效期开始时间
  20. * @property int $end_time 有效期结束时间
  21. * @property int $is_expire 是否已过期:0=未过期,1=已过期
  22. * @property int $is_use 是否已使用:0=未使用,1=已使用
  23. * @property int $is_delete 是否删除
  24. * @property int $created_at 添加时间
  25. * @property int|null $type 领取类型 0--平台发放 1--自动发放 2--领券中心领取
  26. * @property int $integral 兑换支付积分数量
  27. * @property float $price 兑换支付价格
  28. * @property int|null $updated_at 更新时间
  29. * @property int $give_user_id
  30. * @property int $voucher_code_id 支付宝赠送卡券id
  31. */
  32. class UserCoupon extends \yii\db\ActiveRecord
  33. {
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function tableName()
  38. {
  39. return '{{%user_coupon}}';
  40. }
  41. const IS_DELETE_YES = 1;//已删除
  42. const IS_DELETE_NO = 0;//未删除
  43. const IS_USE_NO = 0;//未使用
  44. const IS_USE_YES = 1;//已使用
  45. const IS_EXPIRE_NO = 0;//未过期
  46. const IS_EXPIRE_YES = 1;//已过期
  47. const TYPE_STORE = 0;//平台发放
  48. const TYPE_AUTO = 1;//自动发放
  49. const TYPE_GET = 2;//领券中心领取
  50. public function behaviors()
  51. {
  52. return [
  53. [
  54. 'class' => TimestampBehavior::class,
  55. 'attributes' => [
  56. ActiveRecord::EVENT_BEFORE_INSERT => ['updated_at', 'created_at'],
  57. ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at'
  58. ]
  59. ]
  60. ];
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function rules()
  66. {
  67. return [
  68. [['store_id', 'user_id', 'coupon_id'], 'required'],
  69. [['store_id', 'user_id', 'coupon_id', 'coupon_auto_send_id', 'begin_time', 'end_time', 'is_expire', 'is_use', 'is_delete', 'created_at', 'type', 'integral', 'updated_at', 'give_user_id', 'voucher_code_id'], 'integer'],
  70. [['price'], 'number'],
  71. [['alipay_open_id'], 'safe'],
  72. ];
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function attributeLabels()
  78. {
  79. return [
  80. 'id' => 'ID',
  81. 'store_id' => 'Store ID',
  82. 'user_id' => '用户id',
  83. 'alipay_open_id' => 'alipay_open_id',
  84. 'coupon_id' => '优惠券id',
  85. 'coupon_auto_send_id' => '自动发放id',
  86. 'begin_time' => '有效期开始时间',
  87. 'end_time' => '有效期结束时间',
  88. 'is_expire' => '是否已过期:0=未过期,1=已过期',
  89. 'is_use' => '是否已使用:0=未使用,1=已使用',
  90. 'is_delete' => '是否删除',
  91. 'created_at' => '添加时间',
  92. 'type' => '领取类型 0--平台发放 1--自动发放 2--领券中心领取 3--saas福利中心 4--转赠',
  93. 'integral' => '兑换支付积分数量',
  94. 'price' => '兑换支付价格',
  95. 'updated_at' => '更新时间',
  96. 'voucher_code_id' => "支付宝赠送卡券id"
  97. ];
  98. }
  99. public function afterSave($insert, $changedAttributes)
  100. {
  101. parent::afterSave($insert, $changedAttributes);
  102. if($insert){
  103. (new \app\utils\OrderUtil())->couponTimeout($this->id);
  104. }
  105. }
  106. public function getCoupon()
  107. {
  108. return $this->hasOne(Coupon::className(), ['id' => 'coupon_id']);
  109. }
  110. public function getUser()
  111. {
  112. return $this->hasOne(User::className(), ['id'=>'user_id']);
  113. }
  114. }