ImportFollowRecord.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace app\common\command;
  3. use app\admin\model\Record;
  4. use app\admin\model\User;
  5. use app\crm\model\Customer;
  6. use think\Config;
  7. use think\console\Command;
  8. use think\console\Input;
  9. use think\console\input\Argument;
  10. use think\console\Output;
  11. use think\Request;
  12. class ImportFollowRecord extends Command
  13. {
  14. protected function configure()
  15. {
  16. $this->setName('import:record')
  17. ->addArgument('file_path', null, '导入文件路径')
  18. ->setDescription('导入跟进记录');
  19. }
  20. protected function execute(Input $input, Output $output)
  21. {
  22. /**
  23. * 第三行开始、共六列
  24. *
  25. * 客户名称* 客户号码* 跟进方式* 跟进内容* 跟进人* 跟进时间
  26. */
  27. set_time_limit(0);
  28. Request::instance()->module('crm');
  29. Config::load(APP_PATH . '../config/database.php', 'database');
  30. $file_path = $input->getArgument('file_path');
  31. $user_list = User::field(['id', 'realname'])->select();
  32. $user_map = array_column($user_list, 'id', 'realname');
  33. if (file_exists($file_path)) {
  34. // 加载导入数据文件
  35. $objRender = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
  36. $objRender->setReadDataOnly(true);
  37. $ExcelObj = $objRender->load($file_path);
  38. // 指定工作表
  39. $sheet = $ExcelObj->getSheet(0);
  40. // 总行数
  41. $max_row = $sheet->getHighestRow();
  42. $max_column = $sheet->getHighestColumn();
  43. $log_file = RUNTIME_PATH . 'import_record/' . date('Y_m_d_H_i') . '.log';
  44. $data_file = RUNTIME_PATH . 'import_record/' . date('Y_m_d_H_i') . '.data';
  45. if (!file_exists(RUNTIME_PATH . 'import_record')) {
  46. $res = mkdir(RUNTIME_PATH . 'import_record', '0777', true);
  47. if (!$res) {
  48. $output->writeln('Runtime 目录无权限');
  49. return;
  50. }
  51. }
  52. $ask = $output->ask($input, '共 ' . ($max_row - 2) . ' 条数据,开始导入(yes OR no)?', 'no');
  53. if ($ask != 'yes') {
  54. $output->writeln('已取消');
  55. return;
  56. }
  57. for ($i = 3; $i <= $max_row; $i++) {
  58. $res = $sheet->rangeToArray("A{$i}:F{$i}")[0];
  59. $user_id = $user_map[$res[4]] ?? 1;
  60. $time = strtotime($res[5]);
  61. $mobile = preg_replace('/[^\d]/', '', $res[1]);
  62. $customer_id = Customer::where(['name' => $res[0], 'mobile' => $mobile])->value('customer_id');
  63. if (!$customer_id) {
  64. $info = "{$res[0]}@{$res[1]} 未找到客户". PHP_EOL;
  65. $output->writeln($info);
  66. file_put_contents($log_file, $info . PHP_EOL, FILE_APPEND);
  67. file_put_contents($data_file, json_encode($res) . PHP_EOL, FILE_APPEND);
  68. continue;
  69. }
  70. $data = [
  71. 'types' => 'crm_customer',
  72. 'types_id' => $customer_id,
  73. 'content' => $res[3],
  74. 'category' => $res[2],
  75. 'next_time' => 0,
  76. 'business_ids' => '',
  77. 'contacts_ids' => '',
  78. 'create_time' => $time,
  79. 'update_time' => $time,
  80. 'create_user_id' => $user_id,
  81. ];
  82. if (!Record::insert($data)) {
  83. $info = "{$res[0]}@{$res[1]} 写入数据库失败" . PHP_EOL;
  84. $output->writeln($info);
  85. file_put_contents($log_file, $info . PHP_EOL, FILE_APPEND);
  86. file_put_contents($data_file, json_encode($res) . PHP_EOL, FILE_APPEND);
  87. }
  88. }
  89. } else {
  90. $output->writeln('请输入正确的文件路径');
  91. }
  92. }
  93. }