where('user_id',$this->userId) ->find(); $roleId=$userRole?$userRole['roles_id']:""; $view = input('view'); $time = input('time'); $time = $time>0?$time:1; $articleId = input('articleId'); $type = input('type'); $curDay = date('Y-m-d'); $article = Db::name('article') ->where('id',$articleId)->find(); // 先获取今天是否已有学习 $info = Db::name('record') ->where('user_id',$this->userId) ->where('article_id',$articleId) ->where('create_time',$curDay) ->find(); Db::startTrans(); try{ if($info){ if($type == 0){ // 只加浏览次数 $ret = Db::name('article') ->where('id',$articleId) ->update(['view' => $article['view'] + $view]); if(!$ret){ exception('事务失败'); } $res = Db::name('record') ->where('id',$info['id']) ->update(['view' => $info['view'] + $view]); if(!$res){ exception('事务失败'); } $res = Db::name('record') ->where('id',$info['id']) ->update(['time' => $info['time'] + $time]); if(!$res){ exception('事务失败'); } } else { $res = Db::name('record') ->where('id',$info['record_id']) ->update(['time' => $info['time'] + $time]); if(!$res){ exception('事务失败'); } } } else { if($type == 0){ // 只加浏览次数 $ret = Db::name('article') ->where('id',$articleId) ->update(['view' => $article['view'] + $view]); if(!$ret){ exception('事务失败'); } $res = Db::name('record') ->insert([ 'user_id' => $this->userId, 'org_id' => $this->orgId, 'create_time' => $curDay, 'roles_id' => $roleId, 'article_id' => $articleId, 'view' => $view, 'time' => $time, 'create_yyyy' => date('Y'), 'create_yyyymm' => date('Ym'), 'create_yyyymmdd' => date('Ymd'), ]); if(!$res){ exception('事务失败'); } } else { $res = Db::name('record') ->insert([ 'user_id' => $this->userId, 'create_time' => $curDay, 'org_id' => $this->orgId, 'roles_id' => $roleId, 'article_id' => $articleId, 'view' => $view, 'time' => $time, 'create_yyyy' => date('Y'), 'create_yyyymm' => date('Ym'), 'create_yyyymmdd' => date('Ymd'), ]); if(!$res){ exception('事务失败'); } } } Db::commit(); }catch (Exception $e){ Db::rollback(); HelpHander::error($e->getmessage()); } HelpHander::success([],'操作成功'); } }