Bursting.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. class Bursting extends Base
  5. {
  6. protected $userId = 0;
  7. protected function initialize()
  8. {
  9. parent::initialize();
  10. if (!is_login()) {
  11. $this->redirect(url('Common/logout'));
  12. }
  13. $this->userId = is_login();
  14. }
  15. public function index()
  16. {
  17. $info = Db::name('burst_record')
  18. ->where('user_id',$this->userId)
  19. ->where('status',1)
  20. ->where('del',0)
  21. ->find();
  22. if(!$info){
  23. $this->redirect(url('Index/index'));
  24. }
  25. $binfo = Db::name('burst')
  26. ->where('id',$info['burst_id'])
  27. ->find();
  28. $info['title'] = $binfo['title'];
  29. $this->changeStatus($info);
  30. $info = Db::name('burst_record')
  31. ->where('id',$info['id'])
  32. ->where('del',0)
  33. ->find();
  34. $info['title'] = $binfo['title'];
  35. $this->assign('info',$info);
  36. return $this->fetch();
  37. }
  38. public function detail()
  39. {
  40. $id = input('id');
  41. $info = Db::name('burst_record')
  42. ->where('id',$id)
  43. ->where('del',0)
  44. ->find();
  45. if(!$info){
  46. $this->error('数据不存在');
  47. exit;
  48. }
  49. $binfo = Db::name('burst')
  50. ->where('id',$info['burst_id'])
  51. ->find();
  52. $info['title'] = $binfo['title'];
  53. $this->assign('info',$info);
  54. return $this->fetch();
  55. }
  56. // 更改步骤状态
  57. private function changeStatus($info){
  58. if($info['status'] == 2 || $info['status'] == 3){
  59. return $info;
  60. }
  61. $curTime = time();
  62. $ctime = ($curTime - strtotime($info['create_time']))/60;
  63. $content = json_decode($info['content'],true);
  64. $t = 0;
  65. // 查状态
  66. foreach ($content as $k=>$v){
  67. $t1 = $t;
  68. $t = $t + $v['time'];
  69. $s = 0; // 都已完成
  70. foreach ($v['options'] as $kk=>$vv){
  71. if($vv['status'] == 0){
  72. $s = 1;
  73. }
  74. }
  75. if($v['status'] == 0||$v['status'] == 1){
  76. if($ctime < $t && $ctime >= $t1){
  77. $content[$k]['status'] = 1;
  78. if($s == 0){
  79. $content[$k]['status'] = 2;
  80. if($k+1 < count($content)){
  81. $content[$k+1]['status'] = 1;
  82. }
  83. }
  84. }else if($ctime >= $t){
  85. $content[$k]['status'] = 2;
  86. }
  87. }
  88. }
  89. $status = $info['status'];
  90. if($ctime < $t){
  91. $flag = 0;
  92. foreach ($content as $k=>$v){
  93. foreach ($v['options'] as $kk=>$vv){
  94. if($vv['status'] == 0){
  95. $flag = 1;
  96. break;
  97. }
  98. }
  99. }
  100. if($flag == 0){
  101. $status = 2;
  102. }
  103. }else{
  104. $status = 3;
  105. }
  106. // if($content[count($content) - 1]['status'] == 2 && $ctime >= $t){
  107. // $status = 3;
  108. // } else if ($content[count($content) - 1]['status'] == 2 && $ctime < $t){
  109. // $status = 2;
  110. // }
  111. $content = json_encode($content);
  112. Db::name('burst_record')->where('id',$info['id'])->update(['status'=>$status,'content' => $content,'update_time'=>date('Y-m-d H:i:s')]);
  113. $info['content'] = $content;
  114. return $info;
  115. }
  116. public function finish(){
  117. $id = input('id/d',0);
  118. $index = input('index/d',0);
  119. $index2 = input('index2/d',0);
  120. $info = Db::name('burst_record')
  121. ->where('id',$id)
  122. ->where('del',0)
  123. ->find();
  124. if(!$info){
  125. $this->error('数据不存在');
  126. }
  127. if($info['status'] != 1){
  128. $this->error('无权限操作');
  129. }
  130. $content = json_decode($info['content'],true);
  131. foreach ($content as $k=>$v){
  132. if($k == $index){
  133. foreach ($v['options'] as $kk => $vv){
  134. if($kk == $index2){
  135. $content[$k]['options'][$kk]['status'] = 1;
  136. }
  137. }
  138. }
  139. }
  140. $content = json_encode($content);
  141. $ret = Db::name('burst_record')->where('id',$id)->update(['content' => $content,'update_time'=>date('Y-m-d H:i:s')]);
  142. if($ret){
  143. $info['content'] = $content;
  144. $this->changeStatus($info);
  145. $this->success('操作成功');
  146. }else{
  147. $this->error('操作失败');
  148. }
  149. }
  150. }