PondUserNum.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\models;
  3. use yii\behaviors\TimestampBehavior;
  4. use yii\db\ActiveRecord;
  5. /**
  6. * This is the model class for table "{{%pond_user_num}}".
  7. *
  8. * @property integer $id
  9. * @property integer $store_id
  10. * @property integer $user_id
  11. * @property integer $status
  12. * @property string $remark
  13. * @property integer $create_time
  14. * @property integer $update_time
  15. */
  16. class PondUserNum extends \yii\db\ActiveRecord
  17. {
  18. /**
  19. * @inheritdoc
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%pond_user_num}}';
  24. }
  25. /**
  26. * @inheritdoc
  27. */
  28. public function rules()
  29. {
  30. return [
  31. [['store_id', 'user_id', 'status', 'create_time', 'update_time'], 'integer'],
  32. [['remark'], 'safe'],
  33. ];
  34. }
  35. public function behaviors()
  36. {
  37. return [
  38. [
  39. 'class' => TimestampBehavior::class,
  40. 'attributes' => [
  41. ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],
  42. ActiveRecord::EVENT_BEFORE_UPDATE => ['update_time']
  43. ]
  44. ]
  45. ];
  46. }
  47. /**
  48. * @inheritdoc
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'store_id' => 'Store ID',
  55. 'user_id' => 'User ID',
  56. 'status' => ' 0未领取1 已领取',
  57. 'remark' => 'remark',
  58. 'create_time' => 'Create Time',
  59. 'update_time' => '领取时间',
  60. ];
  61. }
  62. public function sendNum($user_id)
  63. {
  64. $t = \Yii::$app->db->beginTransaction();
  65. try {
  66. $time = time();
  67. $user = User::findOne($user_id);
  68. if ($user && $user->parent_id) {
  69. $setting = PondSetting::find()
  70. ->where(['store_id' => $user->store_id])
  71. ->andWhere(['<=', 'start_time', $time])
  72. ->andWhere(['>', 'end_time', $time])
  73. ->asArray()->one();
  74. if ($setting) {
  75. $obj = new self();
  76. $obj->store_id = $user->store_id;
  77. $obj->user_id = $user->parent_id;
  78. $obj->status = 0;
  79. $obj->remark = '下级用户下单赠送';
  80. $obj->save();
  81. }
  82. }
  83. $t->commit();
  84. } catch (\Exception $e) {
  85. $t->rollBack();
  86. \Yii::error($e->getMessage());
  87. }
  88. }
  89. }