123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace app\api\controller\v1;
- use app\api\controller\Base;
- use app\hander\HelpHander;
- use think\App;
- use think\Db;
- use think\Exception;
- use think\exception\Handle;
- class Record extends Base
- {
- public function updateTypeOrView() {
- $userRole = Db::name('user_roles')
- ->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([],'操作成功');
- }
- }
|