| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- namespace addons\OperatingNote\common\models;
- use addons\OperatingNote\common\enums\NoteEnum;
- use addons\Redpack\common\enums\RedpackEnum;
- use addons\Redpack\common\models\RedpackWallet;
- use common\enums\AssetsType;
- use common\enums\UserWalletEnum;
- use services\common\UserWalletService;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_operating_note_share_record}}".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $user_id 用户id
- * @property int $note_id 笔记id
- * @property int $has_gift 是否奖励
- * @property int $status
- * @property int $created_at
- * @property int $updated_at
- */
- class AddonsOperatingNoteShareRecord extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_operating_note_share_record}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'note_id'], 'required'],
- [['mall_id', 'user_id', 'note_id', 'has_gift', 'status', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'user_id' => '用户id',
- 'note_id' => '笔记id',
- 'has_gift' => '是否奖励',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public static function setData($id, $user_id)
- {
- $state = false;
- $num = 0;
- $content = '';
- $transaction = \Yii::$app->db->beginTransaction();
- try {
- $model = new self();
- $ins_data['mall_id'] = 1;
- $ins_data['user_id'] = $user_id;
- $ins_data['note_id'] = $id;
- $model->load($ins_data, '');
- $model->save();
- $note = AddonsOperatingNoteContent::getOne([['id' => $id]], false, [], false);
- if ($note['share_reward'] == 1) {
- $record_count = self::count(['where' => [['=', 'user_id', $user_id], ['=', 'note_id', $id], ['=', 'has_gift', 1]]]);
- if ($record_count < $note->share_reward_max_time) {
- if (in_array($note->share_reward_type, [0, 1])) {
- if ($note->share_reward_type == 0) {
- $content = '积分';
- $wallet_type = UserWalletService::WALLET_TYPE_SCORE;
- }
- if ($note->share_reward_type == 1) {
- $content = '余额';
- $wallet_type = UserWalletService::WALLET_TYPE_BALANCE;
- }
- $handle_wallet = [
- 'message_id' => 0,
- 'mall_id' => $note->mall_id,
- 'user_id' => $user_id,
- 'wallet_type' => $wallet_type,
- 'money' => $note->share_reward_content,
- 'desc' => '分享文章奖励',
- 'source_table' => AssetsType::OPERATING_NOTE,
- 'source_table_id' => $model->id,
- 'from_type' => UserWalletEnum::REWARD,
- 'capital_type' => UserWalletEnum::OPERATING_NOTE_SHARE,
- ];
- $reward_state = UserWalletService::handle(0, $handle_wallet);
- } else {
- // 红包
- $reward_state = RedpackWallet::setData([
- 'mall_id' => $note->mall_id,
- 'user_id' => $user_id,
- 'is_frozen' => false,
- 'money' => $note->share_reward_content,
- 'source_table' => static::tableName(),
- 'source_table_id' => $note->id,
- 'from_type' => RedpackEnum::FROM_RECEIVE,
- 'desc' => "运营笔记ID: {$id}分享奖励",
- ]);
- }
- if ($reward_state) {
- $state = true;
- $num = $note->share_reward_content;
- $model->has_gift = 1;
- $model->save();
- $reward = new AddonsOperatingNoteRewardRecord();
- $reward->mall_id = $note->mall_id;
- $reward->user_id = $user_id;
- $reward->note_content_id = $id;
- $reward->note_cate_id = $note->cate_id;
- $reward->note_type = $note->note_type;
- $reward->reward_type = $note->share_reward_type;
- $reward->reward_source = NoteEnum::STATUS_TWO;
- $reward->reward_content = $note->share_reward_content;
- $reward->save();
- }
- }
- }
- $note->share_num++;
- $note->save();
- $transaction->commit();
- } catch (Exception $e) {
- $transaction->rollBack();
- var_dump($e->getMessage());
- throw new Exception($e->getMessage());
- }
- return ['state' => $state, 'content' => $content, 'num' => $num];
- }
- }
|