AdoptAnswer.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: sdf_sky
  5. * Date: 2017/4/24
  6. * Time: 下午7:01
  7. */
  8. namespace App\Console\Commands;
  9. use App\Models\Answer;
  10. use App\Models\Question;
  11. use App\Services\QuestionService;
  12. use Carbon\Carbon;
  13. use Illuminate\Console\Command;
  14. class AdoptAnswer extends Command
  15. {
  16. /**
  17. * The name and signature of the console command.
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'adoptAnswer';
  22. /**
  23. * The console command description.
  24. *
  25. * @var string
  26. */
  27. protected $description = 'auto adopt answer';
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return mixed
  32. */
  33. public function handle()
  34. {
  35. $this->comment('start to adopt answer');
  36. $answerAdoptPeriod = Setting()->get('answer_adopt_period',0);
  37. if( $answerAdoptPeriod > 0 ){
  38. $startTime = Carbon::createFromTimestamp(Carbon::today()->timestamp - $answerAdoptPeriod * 24*3600);
  39. $questions = Question::where("price",">",0)->where("created_at","<",$startTime)->where("status","=",1)->where("answers",'>',0)->orderBy('created_at','asc')->take(30)->get();
  40. $this->comment('total question:'.$questions->count());
  41. foreach( $questions as $question ){
  42. $this->comment('doing question id: '.$question->id);
  43. $answers = $question->answers()->whereNull('adopted_at')->where("status",">",0)->orderBy("supports",'desc')->orderBy("created_at","asc")->take(30)->get();
  44. $this->comment('question['.$question->id.']total answers:'.$answers->count());
  45. if( $answers->count() == 0 ){
  46. continue;
  47. }
  48. $bestAnswerId = null;
  49. /*优先采纳专家答案*/
  50. foreach( $answers as $answer ){
  51. if($answer->userData && $answer->userData->authentication_status == 1){
  52. $bestAnswerId = $answer->id;
  53. }
  54. }
  55. if(!$bestAnswerId){
  56. $bestAnswerId = $answers[0]->id;
  57. }
  58. if($bestAnswerId){
  59. $this->comment('best answer id: '.$bestAnswerId);
  60. QuestionService::adoptAnswer($bestAnswerId);
  61. }
  62. }
  63. }
  64. $this->comment('finished!');
  65. }
  66. }