ImportService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. <?php
  2. namespace App\Service;
  3. use App\Exports\TableHeadExport;
  4. use App\Import\ImportAll;
  5. use App\Model\Device;
  6. use App\Model\Employee;
  7. use App\Model\Item;
  8. use App\Model\ItemDetails;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. use Maatwebsite\Excel\Facades\Excel;
  12. use PhpOffice\PhpSpreadsheet\IOFactory;
  13. use PhpOffice\PhpSpreadsheet\Shared\Date;
  14. class ImportService extends Service
  15. {
  16. public static $type = [
  17. 'device', // 设备
  18. 'item', // 项目
  19. ];
  20. public function getTableTitleXls($data,$user){
  21. if(empty($data['type'])) return [false,'缺少类型'];
  22. if(! in_array($data['type'],self::$type)) return [false,'类型不存在'];
  23. //获取配置文件
  24. $fuc = $data['type'];
  25. list($status,$return) = $this->$fuc($data,$user);
  26. list($msg,$filename) = $return;
  27. if(! $status) return [false, $msg];
  28. $headers = array_column($msg,'value');
  29. $comments = $enums = [];
  30. foreach ($msg as $value){
  31. if(! empty($value['comments'])) $comments[$value['value']] = $value['comments'];
  32. if(! empty($value['enums'])) $enums[$value['value']] = $value['enums'];
  33. }
  34. Excel::store(new TableHeadExport([], $headers, $comments, $enums),"/public/export/{$filename}", null, 'Xlsx', []);
  35. return [true, ['file' => $filename]];
  36. }
  37. private function getTableConfig($type = ""){
  38. if(empty($type)) return [];
  39. //获取配置文件
  40. $config = "excel." . $type;
  41. return config($config) ?? [];
  42. }
  43. private function device($data,$user){
  44. $config_array = $this->getTableConfig($data['type']);
  45. if(empty($config_array)) return [false, ['导入配置表头文件不存在','']];
  46. //生成下载文件
  47. $filename = "设备导入模板_" . time() . '.' . 'xlsx';
  48. return [true, [$config_array, $filename]];
  49. }
  50. private function item($data,$user){
  51. $config_array = $this->getTableConfig($data['type']);
  52. if(empty($config_array)) return [false, ['导入配置表头文件不存在','']];
  53. //生成下载文件
  54. $filename = "项目导入模板_" . time() . '.' . 'xlsx';
  55. return [true, [$config_array, $filename]];
  56. }
  57. //导入入口
  58. public function importAll($data,$user){
  59. // //不超时
  60. // ini_set('max_execution_time', 0);
  61. // //内存设置
  62. // ini_set('memory_limit', -1);
  63. // $reader = IOFactory::createReader('Xlsx');
  64. // $reader->setReadDataOnly(true); // 只读取有数据的单元格
  65. // $spreadsheet = $reader->load($data['file']);
  66. // dd($spreadsheet);
  67. // // 创建一个Reader对象
  68. // $reader = IOFactory::createReader('Xlsx'); // 根据你的文件格式选择合适的reader
  69. //
  70. //// 加载Excel文件
  71. // $spreadsheet = $reader->load($data['file']);
  72. //
  73. //// 获取第一个工作表
  74. // $worksheet = $spreadsheet->getActiveSheet();
  75. //
  76. //// 获取总行数
  77. // $totalRows = $worksheet->getHighestRow();dd($totalRows);
  78. if(empty($data['type'])) return [false,'缺少导入类型,导入失败'];
  79. if(! in_array($data['type'],self::$type)) return [false,'导入类型不存在,导入失败'];
  80. if(empty($data['file'])) return [false,'导入文件不能为空'];
  81. try {
  82. $import = new ImportAll();
  83. //设置导入人id
  84. $import->setCrt($user['id']);
  85. $import->setUser($user);
  86. $import->setType($data['type']);
  87. $other = $data;
  88. unset($other['file']);
  89. $import->setOtherParam($other);
  90. //导入
  91. \Maatwebsite\Excel\Facades\Excel::import($import,$data['file']);
  92. if($import->getMsg()) {
  93. $bool = $import->getIsLongText();
  94. if($bool) {
  95. return [0, $import->getMsg()];
  96. }else{
  97. return [false, $import->getMsg()];
  98. }
  99. }
  100. }catch (\Throwable $exception) {
  101. return [false, $exception->getMessage() . ' (Code: ' . $exception->getCode() . ', Line: ' . $exception->getLine() . ')'];
  102. }
  103. return [true, ''];
  104. }
  105. // 设备 ------------------------------------
  106. public function deviceImport($array, $user, $other_param){
  107. $upload = $array[0];
  108. list($status, $msg) = $this->compareTableAndReturn($upload, $other_param);
  109. if(!$status) return [false, $msg];
  110. $table_config = $msg;
  111. unset($array[0]);
  112. if(empty($array)) return [false, '导入数据不能为空'];
  113. // 公共校验
  114. list($array, $error) = $this->checkCommon($array, $table_config);
  115. if(!empty($error)) return [0, $error];
  116. // 详细校验 (这里 $array 传引用,内部会转换日期)
  117. list($error, $update_map) = $this->deviceCheck($array, $user, $table_config);
  118. if(!empty($error)) return [0, $error];
  119. $time = time();
  120. $insert = [];
  121. $update = [];
  122. foreach ($array as $key => $value){
  123. $main_tmp = [];
  124. foreach ($value as $k => $val){
  125. if(!empty($table_config[$k]['is_main'])){
  126. $main_tmp[$table_config[$k]['key']] = $val;
  127. }
  128. }
  129. if(isset($update_map[$key])){
  130. // 存入待更新数组
  131. $update[] = array_merge($main_tmp, ['id' => $update_map[$key]]);
  132. } else {
  133. $main_tmp['top_depart_id'] = $user['top_depart_id'];
  134. $main_tmp['crt_id'] = $user['id'];
  135. $main_tmp['crt_time'] = $time;
  136. $insert[] = $main_tmp;
  137. }
  138. }
  139. DB::beginTransaction();
  140. try {
  141. // 1. 批量新增
  142. if(!empty($insert)){
  143. foreach(array_chunk($insert, 500) as $chunkInsert){
  144. Device::insert($chunkInsert);
  145. }
  146. }
  147. // 批量更新
  148. foreach (array_chunk($update, 100) as $chunk) {
  149. foreach ($chunk as $item) {
  150. $id = $item['id'];
  151. unset($item['id']);
  152. Device::where('id', $id)->update($item);
  153. }
  154. }
  155. DB::commit();
  156. } catch (\Exception $e) {
  157. DB::rollBack();
  158. return [false, "错误:" . $e->getMessage() . " 行:" . $e->getLine()];
  159. }
  160. return [true, ''];
  161. }
  162. private function deviceCheck(&$array, $user, $table_config)
  163. {
  164. // 动态获取关键列的索引
  165. $codeIdx = array_search('code', array_column($table_config, 'key'));
  166. $dateIdx = array_search('in_time', array_column($table_config, 'key'));
  167. $typeIdx = array_search('type', array_column($table_config, 'key'));
  168. $type2Idx = array_search('is_use', array_column($table_config, 'key'));
  169. $code_map = $this->getDeviceList($array, $user, $codeIdx);
  170. $errors = [];
  171. $update = [];
  172. $map_type = array_flip(Device::$type);
  173. $map_type_2 = array_flip(Device::Use);
  174. foreach ($array as $rowIndex => $value) {
  175. $valCode = $value[$codeIdx] ?? '';
  176. // 记录更新 ID
  177. if(isset($code_map[$valCode])){
  178. $update[$rowIndex] = $code_map[$valCode];
  179. }
  180. if(empty($map_type[$value[$typeIdx]])){
  181. $errors[] = "第{$rowIndex}行固定资产类型错误";
  182. }else{
  183. $array[$rowIndex][$typeIdx] = $map_type[$value[$typeIdx]];
  184. }
  185. if(empty($map_type_2[$value[$type2Idx]])){
  186. $errors[] = "第{$rowIndex}行是否启用错误";
  187. }else{
  188. $array[$rowIndex][$type2Idx] = $map_type_2[$value[$type2Idx]];
  189. }
  190. // 日期处理
  191. if($dateIdx !== false && !empty($value[$dateIdx])){
  192. list($status, $msg) = $this->convertExcelCellToDate($value[$dateIdx]);
  193. if(!$status) {
  194. $errors[] = "第{$rowIndex}行日期格式错误";
  195. } else {
  196. $array[$rowIndex][$dateIdx] = $msg;
  197. }
  198. }
  199. }
  200. $error_string = "";
  201. if(! empty($errors)) $error_string = implode('|', $errors);
  202. return [$error_string, $update];
  203. }
  204. private function getDeviceList($array, $user, $index){
  205. //查找设备
  206. $codes = array_unique(array_filter(array_column($array,$index)));
  207. return Device::where('del_time', 0)
  208. ->where('top_depart_id', $user['top_depart_id'])
  209. ->whereIn('code', $codes)
  210. ->pluck('id','code')
  211. ->toArray();
  212. }
  213. // 项目 -----------------------------------
  214. public function itemImport($array, $user, $other_param)
  215. {
  216. $upload = $array[0];
  217. list($status, $msg) = $this->compareTableAndReturn($upload, $other_param);
  218. if (!$status) return [false, $msg];
  219. $table_config = $msg;
  220. unset($array[0]);
  221. if (empty($array)) return [false, '导入数据不能为空'];
  222. // 1. 公共校验 (必填、唯一性等)
  223. list($array, $error) = $this->checkCommon($array, $table_config);
  224. if (!empty($error)) return [0, $error];
  225. // 2. 业务详细校验 (获取更新映射及明细数据)
  226. list($error, $update_map, $detail_data_map) = $this->itemCheck($array, $user, $table_config);
  227. if (!empty($error)) return [0, $error];
  228. $time = time();
  229. $insert_data = [];
  230. $update_data = [];
  231. $all_detail_insert = [];
  232. $update_main_ids = [];
  233. // 3. 数据分拣
  234. foreach ($array as $key => $value) {
  235. $main_tmp = [];
  236. foreach ($value as $k => $val) {
  237. if (!empty($table_config[$k]['is_main'])) {
  238. $main_tmp[$table_config[$k]['key']] = $val;
  239. }
  240. }
  241. if (isset($update_map[$key])) {
  242. // 更新逻辑
  243. $itemId = $update_map[$key];
  244. $update_data[] = array_merge($main_tmp, ['id' => $itemId]);
  245. $update_main_ids[] = $itemId;
  246. // 收集明细 (后续统一插入)
  247. if (isset($detail_data_map[$key])) {
  248. foreach ($detail_data_map[$key] as $d) {
  249. $all_detail_insert[] = array_merge($d, ['item_id' => $itemId, 'crt_time' => $time]);
  250. }
  251. }
  252. } else {
  253. // 新增逻辑
  254. $main_tmp['top_depart_id'] = $user['top_depart_id'];
  255. $main_tmp['crt_id'] = $user['id'];
  256. $main_tmp['crt_time'] = $time;
  257. // 以 code 为键,方便后续回填 ID
  258. $insert_data[$main_tmp['code']] = $main_tmp;
  259. if (isset($detail_data_map[$key])) {
  260. foreach ($detail_data_map[$key] as $d) {
  261. $all_detail_insert[] = array_merge($d, ['_code' => $main_tmp['code'], 'crt_time' => $time]);
  262. }
  263. }
  264. }
  265. }
  266. DB::beginTransaction();
  267. try {
  268. // 4. 执行新增主表
  269. if (!empty($insert_data)) {
  270. foreach (array_chunk($insert_data, 500) as $chunk) {
  271. Item::insert($chunk);
  272. }
  273. // 获取新插入数据的 ID 映射
  274. $new_item_maps = Item::whereIn('code', array_keys($insert_data))
  275. ->where('del_time', 0)
  276. ->where('top_depart_id', $user['top_depart_id'])
  277. ->pluck('id', 'code')->toArray();
  278. }
  279. // 5. 执行更新主表 (分批更新)
  280. if (!empty($update_data)) {
  281. foreach (array_chunk($update_data, 100) as $chunk) {
  282. foreach ($chunk as $uItem) {
  283. $id = $uItem['id'];
  284. unset($uItem['id']);
  285. Item::where('id', $id)->update($uItem);
  286. }
  287. }
  288. }
  289. // 6. 处理明细表 (先删后插策略)
  290. // 删除旧明细 (逻辑删除)
  291. if (!empty($update_main_ids)) {
  292. ItemDetails::whereIn('item_id', $update_main_ids)
  293. ->where('del_time', 0)
  294. ->update(['del_time' => $time]);
  295. }
  296. // 回填新增主表的 ID 到明细数组
  297. foreach ($all_detail_insert as &$di) {
  298. if (isset($di['_code'])) {
  299. $di['item_id'] = $new_item_maps[$di['_code']] ?? 0;
  300. unset($di['_code']);
  301. }
  302. }
  303. unset($di);
  304. // 批量插入所有明细
  305. if (!empty($all_detail_insert)) {
  306. foreach (array_chunk($all_detail_insert, 500) as $chunk) {
  307. ItemDetails::insert($chunk);
  308. }
  309. }
  310. DB::commit();
  311. } catch (\Exception $e) {
  312. DB::rollBack();
  313. return [false, "入库失败:" . $e->getMessage() . " (行号:" . $e->getLine() . ")"];
  314. }
  315. return [true, '导入成功'];
  316. }
  317. private function getItemList($array, $user, $index){
  318. //查找设备
  319. $codes = array_unique(array_filter(array_column($array,$index)));
  320. return Item::where('del_time', 0)
  321. ->where('top_depart_id', $user['top_depart_id'])
  322. ->whereIn('code', $codes)
  323. ->pluck('id','code')
  324. ->toArray();
  325. }
  326. private function itemCheck(&$array, $user, $table_config)
  327. {
  328. $keys = array_column($table_config, 'key');
  329. $codeIdx = array_search('code', $keys);
  330. $stateIdx = array_search('state', $keys);
  331. $manIdx = array_search('man_list', $keys);
  332. $deviceIdx = array_search('device_list', $keys);
  333. // 日期索引
  334. $dateIdx = array_search('start_time', $keys);
  335. $date2Idx = array_search('end_time', $keys);
  336. $code_map = $this->getItemList($array, $user, $codeIdx);
  337. list($man_map, $device_map) = $this->getDataList($array, $user, $manIdx, $deviceIdx);
  338. $errors = [];
  339. $update_mapping = [];
  340. $detail_storage = [];
  341. $state_type_map = array_flip(Item::State_Type);
  342. foreach ($array as $rowIndex => $rowValue) {
  343. $valCode = $rowValue[$codeIdx] ?? '';
  344. // 1. 判定更新还是新增
  345. if (isset($code_map[$valCode])) {
  346. $update_mapping[$rowIndex] = $code_map[$valCode];
  347. }
  348. // 2. 状态校验
  349. $state_text = $rowValue[$stateIdx] ?? '';
  350. if (!isset($state_type_map[$state_text])) {
  351. $errors[] = "第{$rowIndex}行:状态[{$state_text}]无效";
  352. } else {
  353. $array[$rowIndex][$stateIdx] = $state_type_map[$state_text];
  354. }
  355. // 3. 日期转换
  356. foreach ([$dateIdx, $date2Idx] as $dIdx) {
  357. if ($dIdx !== false && !empty($rowValue[$dIdx])) {
  358. list($s, $m) = $this->convertExcelCellToDate($rowValue[$dIdx]);
  359. if (!$s) $errors[] = "第{$rowIndex}行:日期格式非法";
  360. else $array[$rowIndex][$dIdx] = $m;
  361. }
  362. }
  363. // 4. 解析人员 (明细类型1)
  364. if ($manIdx !== false && !empty($rowValue[$manIdx])) {
  365. foreach (explode(',', $rowValue[$manIdx]) as $mNum) {
  366. $mNum = trim($mNum);
  367. if (!isset($man_map[$mNum])) {
  368. $errors[] = "第{$rowIndex}行:人员编码[{$mNum}]不存在";
  369. } else {
  370. $detail_storage[$rowIndex][] = [
  371. 'type' => ItemDetails::type_one,
  372. 'data_id' => $man_map[$mNum],
  373. ];
  374. }
  375. }
  376. }
  377. // 5. 解析设备 (明细类型2)
  378. if ($deviceIdx !== false && !empty($rowValue[$deviceIdx])) {
  379. foreach (explode(',', $rowValue[$deviceIdx]) as $dCode) {
  380. $dCode = trim($dCode);
  381. if (!isset($device_map[$dCode])) {
  382. $errors[] = "第{$rowIndex}行:资产编码[{$dCode}]不存在";
  383. } else {
  384. $detail_storage[$rowIndex][] = [
  385. 'type' => ItemDetails::type_two,
  386. 'data_id' => $device_map[$dCode],
  387. ];
  388. }
  389. }
  390. }
  391. }
  392. $error_str = !empty($errors) ? implode('|', $errors) : "";
  393. return [$error_str, $update_mapping, $detail_storage];
  394. }
  395. private function getDataList($array, $user, $index1, $index2)
  396. {
  397. $manNums = [];
  398. $devCodes = [];
  399. // 去重收集
  400. foreach ($array as $row) {
  401. if (!empty($row[$index1])) {
  402. foreach (explode(',', $row[$index1]) as $v) $manNums[trim($v)] = true;
  403. }
  404. if (!empty($row[$index2])) {
  405. foreach (explode(',', $row[$index2]) as $v) $devCodes[trim($v)] = true;
  406. }
  407. }
  408. $manMap = Employee::where('del_time', 0)
  409. ->where('top_depart_id', $user['top_depart_id'])
  410. ->whereIn('number', array_keys($manNums))
  411. ->pluck('id', 'number')->toArray();
  412. $devMap = Device::where('del_time', 0)
  413. ->where('top_depart_id', $user['top_depart_id'])
  414. ->whereIn('code', array_keys($devCodes))
  415. ->pluck('id', 'code')->toArray();
  416. return [$manMap, $devMap];
  417. }
  418. //公共校验 -----------------------------------------
  419. private function checkCommon($array, $table_config) {
  420. $error = [];
  421. $uniqueCheck = []; // 格式:[$column_index => [$value => $first_line]]
  422. foreach ($array as $line => $row) {
  423. $rowData = array_filter($row);
  424. if (empty($rowData)) {
  425. unset($array[$line]);
  426. continue;
  427. }
  428. foreach ($row as $colIndex => $value) {
  429. $value = trim($value);
  430. $config = $table_config[$colIndex] ?? null;
  431. // 1. 基础存在性检查
  432. if (!$config) {
  433. $error[] = "第{$line}行第{$colIndex}列配置不存在";
  434. continue;
  435. }
  436. $fieldName = $config['value'];
  437. // 2. 必填校验
  438. if (!empty($config['required']) && ($value === '' || !isset($value))) {
  439. $error[] = "第{$line}行:[{$fieldName}] 必填";
  440. }
  441. // 3. 默认值填充
  442. if ($value === '' && isset($config['default'])) {
  443. $value = $config['default'];
  444. }
  445. // 4. 唯一性校验(重点:一次遍历解决)
  446. if (!empty($config['unique']) && $value !== '') {
  447. if (isset($uniqueCheck[$colIndex][$value])) {
  448. $prevLine = $uniqueCheck[$colIndex][$value];
  449. $error[] = "第{$line}行:[{$fieldName}] 与第{$prevLine}行重复,重复值:{$value}";
  450. } else {
  451. // 记录该值第一次出现的位置
  452. $uniqueCheck[$colIndex][$value] = $line;
  453. }
  454. }
  455. $row[$colIndex] = $value;
  456. }
  457. $array[$line] = $row;
  458. }
  459. $error_string = !empty($error) ? implode('|', $error) : "";
  460. return [$array, $error_string];
  461. }
  462. //模板校验 -----------------------------------------
  463. private function compareTableAndReturn($upload, $param){
  464. if(empty($upload)) return [false, '表头不能为空'];
  465. $config_array = $this->getTableConfig($param['type']);
  466. if(empty($config_array)) return [false, '导入配置表头文件不存在'];
  467. foreach ($config_array as $key => $value){
  468. $key_position = $key + 1;
  469. if(! isset($upload[$key])) return [false, "第" . $key_position . "列表头缺失"];
  470. $tmp_v = trim($upload[$key]);
  471. if($tmp_v != $value['value']) return [false, "第" . $key_position . "列表头与模板不符合,请重新下载模板"];
  472. }
  473. return [true, $config_array];
  474. }
  475. //转换日期 ------------------------------------------
  476. function convertExcelCellToDate($cellValue) {
  477. // 尝试将单元格值转换为浮点数(Excel 日期序列号)
  478. $excelTimestamp = filter_var($cellValue, FILTER_VALIDATE_FLOAT);
  479. if ($excelTimestamp !== false && $excelTimestamp > 0) {
  480. // 如果成功转换并且值大于0,则认为是Excel日期序列号
  481. try {
  482. $dateTimeObject = Date::excelToDateTimeObject($cellValue);
  483. // if ($dateTimeObject->format('H:i:s') === '00:00:00') {
  484. // // 如果是,则将时间设置为 '23:59:00'
  485. // $dateTimeObject->setTime(23, 59);
  486. // }
  487. // 现在你可以格式化这个日期了
  488. $formattedDate = $dateTimeObject->format('Y-m-d');
  489. if(! strtotime($formattedDate)) return [false, ''];
  490. return [true, strtotime($formattedDate)];
  491. } catch (\Exception $e) {
  492. // 处理转换失败的情况
  493. return [false, '单元格日期格式转换时间戳失败'];
  494. }
  495. }
  496. // 如果不是有效的浮点数,则尝试按照多种日期格式解析
  497. if(! strtotime($cellValue)) return [false, '单元格文本格式转换时间戳失败'];
  498. return [true, strtotime($cellValue)];
  499. }
  500. }