UpgradeGiftGoods.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace addons\UpgradeGift\common\models;
  3. use Exception;
  4. use RuntimeException;
  5. use Yii;
  6. /**
  7. * This is the model class for table "qimall_addons_upgrade_gift_goods".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城id
  11. * @property int $user_id 用户id
  12. * @property int $level 触发领取的等级
  13. * @property int $status -1已失效(目前只有配置取消才会已失效) 0未领取 1已领取 2放弃领取
  14. * @property int $receive_time 领取时间
  15. * @property int $order_id 下单的订单id
  16. * @property int $goods_id 领取的商品id
  17. * @property int $created_at
  18. * @property int $updated_at
  19. */
  20. class UpgradeGiftGoods extends \common\models\base\BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return 'qimall_addons_upgrade_gift_goods';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['mall_id', 'user_id', 'level', 'status', 'receive_time', 'order_id', 'goods_id', 'attr_id', 'created_at', 'updated_at'], 'integer'],
  36. ];
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'id' => 'ID',
  45. 'mall_id' => 'Mall ID',
  46. 'user_id' => 'User ID',
  47. 'level' => 'Level',
  48. 'status' => 'Status',
  49. 'receive_time' => 'Receive Time',
  50. 'order_id' => 'Order ID',
  51. 'goods_id' => 'Goods ID',
  52. 'created_at' => 'Created At',
  53. 'updated_at' => 'Updated At',
  54. ];
  55. }
  56. /**
  57. * @param array $params
  58. *
  59. * @return bool|self
  60. */
  61. public static function setData(array $params)
  62. {
  63. try {
  64. if (!empty($params['id'])) {
  65. $model = self::findOne(['id' => $params['id']]);
  66. if (!$model) {
  67. throw new RuntimeException('数据不存在');
  68. }
  69. if (isset($params['mall_id']) && $model->mall_id != $params['mall_id']) {
  70. throw new RuntimeException('不允许操作此数据');
  71. }
  72. } else {
  73. $model = new self();
  74. $model->loadDefaultValues();
  75. }
  76. if (!$model->load($params, '')) {
  77. throw new RuntimeException($model->getErrorMessage());
  78. }
  79. if (!$model->save()) {
  80. throw new RuntimeException($model->getErrorMessage());
  81. }
  82. return $model;
  83. } catch (Exception $e) {
  84. self::$static_error = $e->getMessage();
  85. return false;
  86. }
  87. }
  88. /**
  89. * 获取未领取的礼品记录
  90. *
  91. * @param int $userId
  92. * @param string $field
  93. *
  94. * @return array
  95. */
  96. public static function getUnclaimedGift(int $userId, string $field = 'level'): array
  97. {
  98. return self::find()
  99. ->where([
  100. 'user_id' => $userId,
  101. 'status' => 0
  102. ])
  103. ->select($field)
  104. ->asArray()
  105. ->all() ?: [];
  106. }
  107. /**
  108. * 返回用户某个等级的礼品是否还未领取
  109. *
  110. * @param int $userId
  111. * @param int $level
  112. *
  113. * @return int
  114. */
  115. public static function getUnclaimedGiftIdByLevel(int $userId, int $level): int
  116. {
  117. return self::find()
  118. ->where([
  119. 'user_id' => $userId,
  120. 'level' => $level,
  121. 'status' => 0
  122. ])
  123. ->select('id')
  124. ->scalar() ?: 0;
  125. }
  126. }