PmsCommand.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/4/24
  6. * Time: 19:49
  7. */
  8. namespace App\Console\Commands\Transfer;
  9. use Illuminate\Console\Command;
  10. use App\Transfer\Pms;
  11. use App\Transfer\SysPms;
  12. use App\Models\SubsiteSysMessage;
  13. use App\Models\Pms as newPms;
  14. use App\Models\SysMessage;
  15. class PmsCommand extends Command
  16. {
  17. protected $signature = 'aix:transfer-pms';
  18. protected $description = 'add the transfer-pms data';
  19. /**
  20. * PmsCommand constructor.
  21. */
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. }
  26. public function handle()
  27. {
  28. SysMessage::truncate();
  29. SubsiteSysMessage::truncate();
  30. newPms::truncate();
  31. //获取系统消息表数据
  32. $sys = SysPms::orderBy('spmid', 'asc')->get();
  33. $sys_count = 0;
  34. $sys_last_id = 0;
  35. if ($sys->isNotEmpty()) {
  36. $sys_data = [];
  37. foreach ($sys as $k => $v) {
  38. $time = date('Y-m-d H:i:s', $v->dateline);
  39. $sys_data = array(
  40. 'id' => $v->spmid,
  41. 'user_type' => $v->spms_usertype,
  42. 'news_type' => 1,
  43. 'type' => $v->spms_type,
  44. 'content' => $v->message,
  45. 'started_at' => 0,
  46. 'ended_at' => 0,
  47. 'subsite_id' => $v->subsite_id,
  48. 'created_at' => $time,
  49. 'updated_at' => $time
  50. );
  51. if (SysMessage::insert($sys_data)) {
  52. //添加分站对应关系
  53. $subsite_data = array(
  54. 'sys_message_id' => $v->spmid,
  55. 'subsite_id' => $v->subsite_id,
  56. 'created_at' => null,
  57. 'updated_at' => null,
  58. );
  59. SubsiteSysMessage::insert($subsite_data);
  60. $sys_count++;
  61. $sys_last_id = $v->spmid;
  62. } else {
  63. $this->info('导入系统站内信'.$v->spmid.'失败');
  64. }
  65. }
  66. }
  67. $this->info("导入系统站内信:".$sys_count.'条,最后导入的系统站内信id是:'.$sys_last_id);
  68. //添加个人消息
  69. $num = 3000;
  70. $all_count = intval(Pms::count()/$num) +1;
  71. for ($i=0; $i<$all_count; $i++) {
  72. $insert=[];
  73. $list = Pms::with(['memberInfo'])->offset($i * $num)->limit($num)->get();
  74. if ($list) {
  75. foreach ($list as $key => $val) {
  76. $utype = 0;
  77. if ($val->memberInfo) {
  78. $utype = $val->memberInfo->utype;
  79. $insert[$key] = array(
  80. 'id' => $val->pmid,
  81. 'utype' => $utype,
  82. 'msgtype' => $val->msgtype,
  83. 'msgfromuid' => $val->msgfromuid,
  84. 'msgfrom' => $val->msgfrom,
  85. 'msgtoname' => $val->msgtoname,
  86. 'msgtouid' => $val->msgtouid,
  87. 'message' => $val->message,
  88. 'started_at' => 0,
  89. 'ended_at' => 0,
  90. 'new' => $val->new,
  91. 'created_at' => date('Y-m-d H:i:s', $val->dateline),
  92. 'updated_at' => date('Y-m-d H:i:s', $val->dateline)
  93. );
  94. }
  95. }
  96. if (newPms::insert($insert)) {
  97. $has_total = (int)(count($insert) + $i*$num);
  98. $this->info('已导入第'.($i+1).'页数据,共'.$has_total.'条pms数据');
  99. } else {
  100. $this->info('已导入'.$num*$i.'条pms数据');
  101. break;
  102. }
  103. }
  104. }
  105. $this->info("导入系统消息成功");
  106. }
  107. }