20230108080835_add_endmaketime_to_exam_result.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. use app\model\ExamEmpResult;
  3. use app\model\ExamPaper;
  4. use think\migration\Migrator;
  5. use think\migration\db\Column;
  6. class AddEndmaketimeToExamResult extends Migrator
  7. {
  8. /**
  9. * Change Method.
  10. *
  11. * Write your reversible migrations using this method.
  12. *
  13. * More information on writing migrations is available here:
  14. * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
  15. *
  16. * The following commands can be used in this method and Phinx will
  17. * automatically reverse them when rolling back:
  18. *
  19. * createTable
  20. * renameTable
  21. * addColumn
  22. * renameColumn
  23. * addIndex
  24. * addForeignKey
  25. *
  26. * Remember to call "create()" or "update()" and NOT "save()" when working
  27. * with the Table class.
  28. */
  29. public function change()
  30. {
  31. $this->table('exam_emp_result')
  32. ->addColumn('endmaketime', 'integer', ['comment' => '考试结束时间', 'default' => 0])
  33. ->update();
  34. $paper = ExamPaper::column('duringtime*60 as duringtime, endtime', 'id');
  35. $result = ExamEmpResult::select();
  36. // 更新时间
  37. foreach ($result as $r) {
  38. $r->startmaketime = date('Y-m-d H:i:s', strtotime($r->addtime) - $r->time_spend * 60);
  39. $endTime = strtotime($r->startmaketime) + $paper[$r->paper_id]['duringtime'];
  40. $paperEndtime = strtotime($paper[$r->paper_id]['endtime']);
  41. if ($endTime > $paperEndtime) $endTime = $paperEndtime;
  42. $r->endmaketime = $endTime;
  43. $r->save();
  44. }
  45. }
  46. }