Import.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace app\api\controller\ps;
  3. use app\common\controller\Api;
  4. use app\common\library\ps\BizExtractor;
  5. use app\common\library\ps\BizIngest;
  6. use app\common\library\ps\FactoryContext;
  7. use app\common\library\ps\FactoryPerm;
  8. /**
  9. * 文件导入产品 / 工单(登录工厂账号)
  10. */
  11. class Import extends Api
  12. {
  13. protected $noNeedRight = '*';
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. if (!FactoryContext::id()) {
  18. $this->error('账号未绑定工厂');
  19. }
  20. $type = $this->request->param('type', 'workorder');
  21. $perm = $type === 'product' ? 'product.import' : 'workorder.import';
  22. if (FactoryPerm::deny($perm)) {
  23. $this->error('没有权限:' . FactoryPerm::label($perm));
  24. }
  25. }
  26. /**
  27. * 上传文件,AI/规则提取后返回预览,不入库
  28. */
  29. public function preview()
  30. {
  31. @set_time_limit(180);
  32. $type = $this->request->post('type', 'workorder');
  33. $type = $type === 'product' ? 'product' : 'workorder';
  34. $file = $this->request->file('file');
  35. if (!$file) {
  36. $this->error('请选择文件');
  37. }
  38. $dir = RUNTIME_PATH . 'import' . DS;
  39. if (!is_dir($dir)) {
  40. @mkdir($dir, 0755, true);
  41. }
  42. $info = $file->validate([
  43. 'size' => 12 * 1024 * 1024,
  44. 'ext' => 'csv,txt,xlsx,xls,pdf,png,jpg,jpeg,webp,bmp',
  45. ])->move($dir);
  46. if (!$info) {
  47. $this->error($file->getError() ?: '上传失败');
  48. }
  49. $path = $info->getPathname();
  50. try {
  51. $data = BizExtractor::extract($path, $type);
  52. } catch (\Exception $e) {
  53. @unlink($path);
  54. $this->error($e->getMessage());
  55. }
  56. @unlink($path);
  57. $this->success('已识别,请核对后确认导入', $data);
  58. }
  59. /**
  60. * 确认写入数据库
  61. */
  62. public function confirm()
  63. {
  64. $type = $this->request->post('type', 'workorder');
  65. $type = $type === 'product' ? 'product' : 'workorder';
  66. $items = $this->request->post('items/a');
  67. if (!$items) {
  68. $raw = $this->request->post('items_json', '', null);
  69. $items = json_decode((string)$raw, true);
  70. }
  71. if (!is_array($items) || !$items) {
  72. $this->error('没有可导入的数据');
  73. }
  74. $items = BizExtractor::normalizeItems($items, $type);
  75. $saved = [];
  76. $errors = [];
  77. foreach ($items as $i => $item) {
  78. try {
  79. if ($type === 'product') {
  80. $row = BizIngest::saveProduct($item, $this->auth->id);
  81. $saved[] = ['id' => (int)$row->id, 'code' => $row->code, 'name' => $row->name];
  82. } else {
  83. $row = BizIngest::saveWorkorder($item, $this->auth->id);
  84. $saved[] = ['id' => (int)$row->id, 'no' => $row->no];
  85. }
  86. } catch (\Exception $e) {
  87. $errors[] = '第' . ($i + 1) . '条:' . $e->getMessage();
  88. }
  89. }
  90. if (!$saved) {
  91. $this->error($errors ? implode(';', $errors) : '导入失败');
  92. }
  93. $this->success('已导入 ' . count($saved) . ' 条', [
  94. 'saved' => $saved,
  95. 'errors' => $errors,
  96. ]);
  97. }
  98. }