fix: 修改临近城市上传模版

This commit is contained in:
han0
2024-12-25 10:00:36 +08:00
parent 54345a9d52
commit 236369be4e
5 changed files with 106 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
package mjkf.xinke.main.model.db;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
@@ -170,8 +171,44 @@ public class DataAdjacent extends Model<DataAdjacent> {
}
}
public DataAdjacent (String regionName, Row row, MaterialTask task) {
var policy = Row.MissingCellPolicy.CREATE_NULL_AS_BLANK;
row.getCell(0, policy).setCellType(CellType.STRING);
this.setMaterialId(row.getCell(0, policy).getStringCellValue());
this.setName(row.getCell(1, policy).getStringCellValue().replace(" ", ""));
this.setSpec(row.getCell(2, policy).getStringCellValue().replace(" ", ""));
this.setYear(task.getYear());
this.setMonth(task.getMonth());
if (this.getMaterialId().isEmpty()) {
throw new IllegalArgumentException("材料编码为空");
}
if ("广东".equals(regionName)) {
this.setPriceGuangzhou(BigDecimal.valueOf(row.getCell(3, policy).getNumericCellValue()));
this.setPriceMeizhou(BigDecimal.valueOf(row.getCell(4, policy).getNumericCellValue()));
this.setPriceChaozhou(BigDecimal.valueOf(row.getCell(5, policy).getNumericCellValue()));
} else if ("江西".equals(regionName)) {
this.setPriceNanchang(BigDecimal.valueOf(row.getCell(3, policy).getNumericCellValue()));
this.setPriceFuzhou(BigDecimal.valueOf(row.getCell(4, policy).getNumericCellValue()));
this.setPriceGanzhou(BigDecimal.valueOf(row.getCell(5, policy).getNumericCellValue()));
this.setPriceShangrao(BigDecimal.valueOf(row.getCell(6, policy).getNumericCellValue()));
this.setPriceYingtan(BigDecimal.valueOf(row.getCell(7, policy).getNumericCellValue()));
} else if ("浙江".equals(regionName)) {
this.setPriceHangzhou(BigDecimal.valueOf(row.getCell(3, policy).getNumericCellValue()));
this.setPriceLishui(BigDecimal.valueOf(row.getCell(4, policy).getNumericCellValue()));
this.setPriceQuzhou(BigDecimal.valueOf(row.getCell(5, policy).getNumericCellValue()));
this.setPriceWenzhou(BigDecimal.valueOf(row.getCell(6, policy).getNumericCellValue()));
} else if ("云南".equals(regionName)) {
this.setPriceKunming(BigDecimal.valueOf(row.getCell(3, policy).getNumericCellValue()));
} else {
throw new IllegalArgumentException("无效的地区名称: " + regionName);
}
}
public DataAdjacent update(DataAdjacent item) {
BeanUtil.copyProperties(item, this);
BeanUtil.copyProperties(item, this, CopyOptions.create().setIgnoreNullValue(true));
return this;
}
}

View File

@@ -1,10 +1,16 @@
package mjkf.xinke.main.service;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.SneakyThrows;
import mjkf.xinke.main.model.db.DataAdjacent;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import mjkf.xinke.main.model.db.MaterialTask;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
@@ -59,5 +65,32 @@ public class DataAdjacentService extends DataService<BaseMapper<DataAdjacent>, D
query.orderByAsc(DataAdjacent::getYear, DataAdjacent::getMonth);
return query;
}
@SneakyThrows
public List<DataAdjacent> saveOrUpdateByIndexBatch(Map<String, List<Row>> rowsMap, MaterialTask task) {
var result = new ArrayList<DataAdjacent>();
for (var key: rowsMap.keySet()) {
if (!List.of("广东", "江西", "浙江", "云南").contains(key)) {
continue;
}
var rows = rowsMap.get(key);
for (var row: rows) {
try {
var item = new DataAdjacent(key, row, task);
if (ObjectUtil.isNotEmpty(item)) {
result.add(item);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
for (var item : result) {
if (item != null) {
this.saveOrUpdateByIndex(item);
}
}
return null;
}
}

View File

@@ -1,5 +1,6 @@
package mjkf.xinke.main.service;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
@@ -51,7 +52,7 @@ public abstract class DataService<M extends BaseMapper<T>, T> extends ServiceImp
e.printStackTrace();
return null;
}
}).collect(Collectors.toList());
}).filter(i-> ObjectUtil.isNotEmpty(i)).collect(Collectors.toList());
for (T item : list) {
if (item != null) {
this.saveOrUpdateByIndex(item);

View File

@@ -18,9 +18,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.*;
@Service
public class MaterialTaskService extends ServiceImpl<BaseMapper<MaterialTask>, MaterialTask> {
@@ -92,7 +90,8 @@ public class MaterialTaskService extends ServiceImpl<BaseMapper<MaterialTask>, M
} else if (MaterialTaskType.isHighwayBureau(data.getType())) {
list = fuzhouHighwayBureauService.saveOrUpdateByIndexBatch(rows, data);
} else if (data.getType().equals(MaterialTaskType.OTHER)) {
list = dataAdjacentService.saveOrUpdateByIndexBatch(rows, data);
var rowsMap = this.getTableRowsMap(file);
list = dataAdjacentService.saveOrUpdateByIndexBatch(rowsMap, data);
} else {
System.out.println("MaterialTaskService.importData | 未识别的任务类型");
return false;
@@ -111,6 +110,7 @@ public class MaterialTaskService extends ServiceImpl<BaseMapper<MaterialTask>, M
System.out.println("MaterialTaskService.getTableRows | 解析表格 | " + file.getName());
List<Row> result = new ArrayList<>();
try (Workbook workbook = WorkbookFactory.create(file)) {
Sheet sheet = workbook.getSheetAt(0); // 假设文件中只有一个表格
Iterator<Row> rowIterator = sheet.iterator();
rowIterator.next( ); // 跳过标题行
@@ -125,5 +125,31 @@ public class MaterialTaskService extends ServiceImpl<BaseMapper<MaterialTask>, M
return result;
}
private Map<String, List<Row>> getTableRowsMap(File file) throws IOException {
System.out.println("MaterialTaskService.getTableRowsMap | 解析表格 | " + file.getName());
Map<String, List<Row>> result = new HashMap<>();
try (Workbook workbook = WorkbookFactory.create(file)) {
var sheetIterator = workbook.sheetIterator();
while (sheetIterator.hasNext()) {
var name = sheetIterator.next().getSheetName();
var resultList = new ArrayList<Row>();
Sheet sheet = workbook.getSheet(String.valueOf(name));
Iterator<Row> rowIterator = sheet.iterator();
rowIterator.next( ); // 跳过标题行
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
resultList.add(row);
}
result.put(String.valueOf(name), resultList);
}
} catch (IOException e) {
e.printStackTrace();
throw e;
}
return result;
}
}

View File

@@ -27,8 +27,8 @@ public class UploadFileTest {
@Test
public void test() throws Exception {
// var filePath = "C:\\Users\\Administrator\\Desktop\\材料管理系统模版\\历史数据\\地材-202405.xlsx";
// var filePath = "C:\\Users\\Administrator\\Documents\\工作表 在 9月完成功能.xlsx";
var filePath = "C:\\Users\\Administrator\\Documents\\三明钢铁.xlsx";
var filePath = "C:\\Users\\Administrator\\Documents\\对比材料精简9月.xlsx";
// var filePath = "C:\\Users\\Administrator\\Documents\\三明钢铁.xlsx";
var file = new File(filePath);
// var multipartFile = this.mockMultipartFile(file, "相邻城市价格.xlsx", "application/x-zip-compressed");
var multipartFile = this.mockMultipartFile(file);
@@ -43,7 +43,7 @@ public class UploadFileTest {
* 1810482216079302658 交通局.xlsx 40101
* 1810497092512178177 地材.xlsx 1101
* 1810506191866003457 地材-202405.xlsx 1101
* 1838851336371163138 相邻城市.xlsx 600
* 1871732061747179521 相邻城市.xlsx 600
* 1866412571689631746 三明钢铁.xlsx 301
*/