ExcelServer.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /*
  3. * @Descripttion:
  4. * @version: 1.0.0
  5. * @Author: likang
  6. * @Date: 2022-08-05 10:48:47
  7. * @LastEditors: likang
  8. * @LastEditTime: 2022-08-08 16:17:18
  9. */
  10. namespace catchAdmin\hydraulic\server;
  11. use catcher\base\CatchRequest;
  12. use catcher\CatchResponse;
  13. use catcher\library\excel\ExcelContract;
  14. use PhpOffice\PhpSpreadsheet\IOFactory;
  15. use think\facade\Db;
  16. class ExcelServer implements ExcelContract
  17. {
  18. //私有对象
  19. private function __construct()
  20. {
  21. }
  22. public static function getInterface()
  23. {
  24. return new ExcelServer();
  25. }
  26. /**
  27. * @Descripttion:
  28. * @name: likang
  29. * @param {CatchRequest} $request
  30. * @param {*} $map [[key=>'e','value'=>'属性']]
  31. * @param $model 数据库模型
  32. * @return {*}
  33. */
  34. function importExcel(CatchRequest $request, $map, $func, $install)
  35. {
  36. $url = $request->post('url');
  37. if (!$url) {
  38. return CatchResponse::fail('请上传文件');
  39. }
  40. $creator_id = $request->post('creator_id');
  41. //解析地址
  42. $parse_url = parse_url($url)['path'];
  43. //载入excel表格
  44. $objPHPExcel = IOFactory::load(public_path() . $parse_url);
  45. $sheetCount = $objPHPExcel->getSheetCount();
  46. $fail = 0; //失败条数
  47. $success = 0; //成功条数
  48. $total = 0; //总导入数
  49. $excel_data = [];
  50. //循环读取每一张表
  51. for ($index = 0; $index < $sheetCount; $index++) {
  52. //设置当前要读取的表
  53. $sheet = $objPHPExcel->getSheet($index); //excel中的第一张sheet
  54. // var_dump($sheet);exit;
  55. $highestRow = $sheet->getHighestRow(); // 取得总行数
  56. // var_dump($highestRow);
  57. if ($highestRow <= 2) {
  58. continue;
  59. }
  60. $total += $highestRow - 3;
  61. for ($j = 4; $j <= $highestRow - 1; $j++) {
  62. $data = array(); //每条门店信息
  63. foreach ($map as $item) {
  64. $data[$item['value']] = trim($sheet->getCell($item['key'] . $j)->getFormattedValue());
  65. }
  66. $data = $func($data);
  67. //校验数据是否重复
  68. if (!$data) {
  69. continue;
  70. }
  71. array_push($excel_data, $data);
  72. }
  73. }
  74. // var_dump($excel_data);
  75. // return CatchResponse::success();
  76. //防止Excel有重复,去重
  77. array_unique($excel_data, SORT_REGULAR);
  78. $count = 0;
  79. Db::startTrans();
  80. try {
  81. foreach ($excel_data as $item) {
  82. $install($item);
  83. $count++;
  84. }
  85. } catch (\Exception $e) {
  86. Db::rollback();
  87. return CatchResponse::success(['error' => true, 'msg' => $e->getMessage()]);
  88. }
  89. //提交事务
  90. Db::commit();
  91. return CatchResponse::success('共' . $total . '条数据,成功' . $count . '条,失败' . $fail . '条');
  92. }
  93. //导出
  94. public function sheets()
  95. {
  96. // TODO: Implement sheets() method.
  97. return Db::name('user')->field(['username', 'email'])->limit(100)->cursor();
  98. }
  99. //设置头部
  100. public function headers(): array
  101. {
  102. // TODO: Implement headers() method.
  103. return [
  104. '物料号', '液压工具', '设备类别', '工具名称', '设备型号(智能)', '发放单位', '固定资产编号', '类固资产编号', '序列号', '出厂编号', '上次检验日期', '下次检验日期', '状态
  105. (已校验、待校验、已报废)', '状态'
  106. ];
  107. }
  108. }