Record.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\controller\Base;
  4. use app\hander\HelpHander;
  5. use think\App;
  6. use think\Db;
  7. use think\Exception;
  8. use think\exception\Handle;
  9. class Record extends Base
  10. {
  11. public function updateTypeOrView() {
  12. $userRole = Db::name('user_roles')
  13. ->where('user_id',$this->userId)
  14. ->find();
  15. $roleId=$userRole?$userRole['roles_id']:"";
  16. $view = input('view');
  17. $time = input('time');
  18. $time = $time>0?$time:1;
  19. $articleId = input('articleId');
  20. $type = input('type');
  21. $curDay = date('Y-m-d');
  22. $article = Db::name('article')
  23. ->where('id',$articleId)->find();
  24. // 先获取今天是否已有学习
  25. $info = Db::name('record')
  26. ->where('user_id',$this->userId)
  27. ->where('article_id',$articleId)
  28. ->where('create_time',$curDay)
  29. ->find();
  30. Db::startTrans();
  31. try{
  32. if($info){
  33. if($type == 0){ // 只加浏览次数
  34. $ret = Db::name('article')
  35. ->where('id',$articleId)
  36. ->update(['view' => $article['view'] + $view]);
  37. if(!$ret){
  38. exception('事务失败');
  39. }
  40. $res = Db::name('record')
  41. ->where('id',$info['id'])
  42. ->update(['view' => $info['view'] + $view]);
  43. if(!$res){
  44. exception('事务失败');
  45. }
  46. $res = Db::name('record')
  47. ->where('id',$info['id'])
  48. ->update(['time' => $info['time'] + $time]);
  49. if(!$res){
  50. exception('事务失败');
  51. }
  52. } else {
  53. $res = Db::name('record')
  54. ->where('id',$info['record_id'])
  55. ->update(['time' => $info['time'] + $time]);
  56. if(!$res){
  57. exception('事务失败');
  58. }
  59. }
  60. } else {
  61. if($type == 0){ // 只加浏览次数
  62. $ret = Db::name('article')
  63. ->where('id',$articleId)
  64. ->update(['view' => $article['view'] + $view]);
  65. if(!$ret){
  66. exception('事务失败');
  67. }
  68. $res = Db::name('record')
  69. ->insert([
  70. 'user_id' => $this->userId,
  71. 'org_id' => $this->orgId,
  72. 'create_time' => $curDay,
  73. 'roles_id' => $roleId,
  74. 'article_id' => $articleId,
  75. 'view' => $view,
  76. 'time' => $time,
  77. 'create_yyyy' => date('Y'),
  78. 'create_yyyymm' => date('Ym'),
  79. 'create_yyyymmdd' => date('Ymd'),
  80. ]);
  81. if(!$res){
  82. exception('事务失败');
  83. }
  84. } else {
  85. $res = Db::name('record')
  86. ->insert([
  87. 'user_id' => $this->userId,
  88. 'create_time' => $curDay,
  89. 'org_id' => $this->orgId,
  90. 'roles_id' => $roleId,
  91. 'article_id' => $articleId,
  92. 'view' => $view,
  93. 'time' => $time,
  94. 'create_yyyy' => date('Y'),
  95. 'create_yyyymm' => date('Ym'),
  96. 'create_yyyymmdd' => date('Ymd'),
  97. ]);
  98. if(!$res){
  99. exception('事务失败');
  100. }
  101. }
  102. }
  103. Db::commit();
  104. }catch (Exception $e){
  105. Db::rollback();
  106. HelpHander::error($e->getmessage());
  107. }
  108. HelpHander::success([],'操作成功');
  109. }
  110. }