AddonsOperatingNoteShareRecord.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace addons\OperatingNote\common\models;
  3. use addons\OperatingNote\common\enums\NoteEnum;
  4. use addons\Redpack\common\enums\RedpackEnum;
  5. use addons\Redpack\common\models\RedpackWallet;
  6. use common\enums\AssetsType;
  7. use common\enums\UserWalletEnum;
  8. use services\common\UserWalletService;
  9. use Yii;
  10. use yii\db\Exception;
  11. /**
  12. * This is the model class for table "{{%addons_operating_note_share_record}}".
  13. *
  14. * @property int $id
  15. * @property int $mall_id 商城id
  16. * @property int $user_id 用户id
  17. * @property int $note_id 笔记id
  18. * @property int $has_gift 是否奖励
  19. * @property int $status
  20. * @property int $created_at
  21. * @property int $updated_at
  22. */
  23. class AddonsOperatingNoteShareRecord extends \common\models\base\BaseActiveRecord
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function tableName()
  29. {
  30. return '{{%addons_operating_note_share_record}}';
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['mall_id', 'user_id', 'note_id'], 'required'],
  39. [['mall_id', 'user_id', 'note_id', 'has_gift', 'status', 'created_at', 'updated_at'], 'integer'],
  40. ];
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function attributeLabels()
  46. {
  47. return [
  48. 'id' => 'ID',
  49. 'mall_id' => '商城id',
  50. 'user_id' => '用户id',
  51. 'note_id' => '笔记id',
  52. 'has_gift' => '是否奖励',
  53. 'status' => 'Status',
  54. 'created_at' => 'Created At',
  55. 'updated_at' => 'Updated At',
  56. ];
  57. }
  58. public static function setData($id, $user_id)
  59. {
  60. $state = false;
  61. $num = 0;
  62. $content = '';
  63. $transaction = \Yii::$app->db->beginTransaction();
  64. try {
  65. $model = new self();
  66. $ins_data['mall_id'] = 1;
  67. $ins_data['user_id'] = $user_id;
  68. $ins_data['note_id'] = $id;
  69. $model->load($ins_data, '');
  70. $model->save();
  71. $note = AddonsOperatingNoteContent::getOne([['id' => $id]], false, [], false);
  72. if ($note['share_reward'] == 1) {
  73. $record_count = self::count(['where' => [['=', 'user_id', $user_id], ['=', 'note_id', $id], ['=', 'has_gift', 1]]]);
  74. if ($record_count < $note->share_reward_max_time) {
  75. if (in_array($note->share_reward_type, [0, 1])) {
  76. if ($note->share_reward_type == 0) {
  77. $content = '积分';
  78. $wallet_type = UserWalletService::WALLET_TYPE_SCORE;
  79. }
  80. if ($note->share_reward_type == 1) {
  81. $content = '余额';
  82. $wallet_type = UserWalletService::WALLET_TYPE_BALANCE;
  83. }
  84. $handle_wallet = [
  85. 'message_id' => 0,
  86. 'mall_id' => $note->mall_id,
  87. 'user_id' => $user_id,
  88. 'wallet_type' => $wallet_type,
  89. 'money' => $note->share_reward_content,
  90. 'desc' => '分享文章奖励',
  91. 'source_table' => AssetsType::OPERATING_NOTE,
  92. 'source_table_id' => $model->id,
  93. 'from_type' => UserWalletEnum::REWARD,
  94. 'capital_type' => UserWalletEnum::OPERATING_NOTE_SHARE,
  95. ];
  96. $reward_state = UserWalletService::handle(0, $handle_wallet);
  97. } else {
  98. // 红包
  99. $reward_state = RedpackWallet::setData([
  100. 'mall_id' => $note->mall_id,
  101. 'user_id' => $user_id,
  102. 'is_frozen' => false,
  103. 'money' => $note->share_reward_content,
  104. 'source_table' => static::tableName(),
  105. 'source_table_id' => $note->id,
  106. 'from_type' => RedpackEnum::FROM_RECEIVE,
  107. 'desc' => "运营笔记ID: {$id}分享奖励",
  108. ]);
  109. }
  110. if ($reward_state) {
  111. $state = true;
  112. $num = $note->share_reward_content;
  113. $model->has_gift = 1;
  114. $model->save();
  115. $reward = new AddonsOperatingNoteRewardRecord();
  116. $reward->mall_id = $note->mall_id;
  117. $reward->user_id = $user_id;
  118. $reward->note_content_id = $id;
  119. $reward->note_cate_id = $note->cate_id;
  120. $reward->note_type = $note->note_type;
  121. $reward->reward_type = $note->share_reward_type;
  122. $reward->reward_source = NoteEnum::STATUS_TWO;
  123. $reward->reward_content = $note->share_reward_content;
  124. $reward->save();
  125. }
  126. }
  127. }
  128. $note->share_num++;
  129. $note->save();
  130. $transaction->commit();
  131. } catch (Exception $e) {
  132. $transaction->rollBack();
  133. var_dump($e->getMessage());
  134. throw new Exception($e->getMessage());
  135. }
  136. return ['state' => $state, 'content' => $content, 'num' => $num];
  137. }
  138. }