Esempio n. 1
0
 /**
  * 喝倒彩或者赞
  * @param User $user
  * @param Post $model
  * @param $action 动作
  * @return array
  */
 protected static function toggleType(User $user, Post $model, $action)
 {
     $data = ['target_id' => $model->id, 'target_type' => $model::TYPE, 'user_id' => $user->id, 'value' => '1'];
     if (!UserMeta::deleteOne($data + ['type' => $action])) {
         // 删除数据有行数则代表有数据,无行数则添加数据
         $userMeta = new UserMeta();
         $userMeta->setAttributes($data + ['type' => $action]);
         $result = $userMeta->save();
         if ($result) {
             // 如果是新增数据, 删除掉Hate的同类型数据
             $attributeName = $action == 'like' ? 'hate' : 'like';
             $attributes = [$action . '_count' => 1];
             if (UserMeta::deleteOne($data + ['type' => $attributeName])) {
                 // 如果有删除hate数据, hate_count也要-1
                 $attributes[$attributeName . '_count'] = -1;
             }
             //更新版块统计
             $model->updateCounters($attributes);
             // 更新个人总统计
             UserInfo::updateAllCounters($attributes, ['user_id' => $model->user_id]);
         }
         return [$result, $userMeta];
     }
     $model->updateCounters([$action . '_count' => -1]);
     UserInfo::updateAllCounters([$action . '_count' => -1], ['user_id' => $model->user_id]);
     return [true, null];
 }