This commit is contained in:
佟明轩 2025-05-16 16:09:18 +08:00
parent a68ee2ee85
commit f04ef2689f
1 changed files with 133 additions and 1 deletions

View File

@ -20,7 +20,6 @@ import com.syjiaer.clinic.server.mapper.inventory.*;
import com.syjiaer.clinic.server.mapper.manager.ManagerUserMapper; import com.syjiaer.clinic.server.mapper.manager.ManagerUserMapper;
import com.syjiaer.clinic.server.service.BaseService; import com.syjiaer.clinic.server.service.BaseService;
import com.syjiaer.clinic.server.service.goods.GoodsService; import com.syjiaer.clinic.server.service.goods.GoodsService;
import lombok.val;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -30,6 +29,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.io.*; import java.io.*;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.*; import java.util.*;
@ -536,4 +536,136 @@ public class InventoryPurchaseService extends BaseService {
cell.setCellValue(value.toString()); cell.setCellValue(value.toString());
} }
} }
public Map<String,Object> fromExcel(String excelPath) {
Map<String,Object> map=new HashMap<>();
List list=new ArrayList();
//表格中头部单独的4行
Map<String,Object> tagMap=new HashMap<>();
try {
FileInputStream fis = new FileInputStream(excelPath);
Workbook workbook = WorkbookFactory.create(fis);
Sheet sheet = workbook.getSheetAt(0);
//采购日期
tagMap.put("purchase_date",getCellValueAsString(sheet.getRow(1).getCell(1)));
//货单号
tagMap.put("shipping_code",getCellValueAsString(sheet.getRow(2).getCell(1)));
//发票号
tagMap.put("invoice_code",getCellValueAsString(sheet.getRow(3).getCell(1)));
//备注
tagMap.put("remark",getCellValueAsString(sheet.getRow(4).getCell(1)));
map.put("tag",tagMap);
int i=6;
while (true){
Row row = sheet.getRow(i);
double id_double;
try{
id_double= row.getCell(0).getNumericCellValue();
}catch (Exception e)
{
break;
}
//四舍五入为int
int id = (int) Math.round(id_double);
Map<String,Object> rowMap=doRow(id,row);
if(rowMap!=null){
list.add(rowMap);
}
i++;
}
map.put("list",list);
return map;
}catch (Exception e) {
logger.warning("解析文件异常:"+e.getMessage());
e.printStackTrace();
throw new MessageException("解析文件异常");
}
}
private Map<String,Object> doRow(int id,Row row) throws ParseException {
Map<String,Object> map=new HashMap<>();
Goods goods = goodsMapper.selectById(id);
if(goods==null){
return null;
}
map.put("id",id);
map.put("goods",goods);
Map<String,Object> infoMap=new HashMap<>();
map.put("info",infoMap);
double whole_number_double=0;
try {
whole_number_double=row.getCell(7).getNumericCellValue();
}catch (Exception e){
return null;
}
int whole_number = (int) Math.round(whole_number_double);
infoMap.put("whole_number",whole_number);
infoMap.put("purchase_unit_price",getCellValueAsString(row.getCell(8)));
infoMap.put("production_batch_code",getCellValueAsString(row.getCell(9)));
infoMap.put("production_date",transformDate(getCellValueAsString(row.getCell(10))));
infoMap.put("expiry_date",transformDate(getCellValueAsString(row.getCell(11))));
return map;
}
public String transformDate(String inputDate) throws ParseException {
if (Objects.equals(inputDate, ""))
{
return "";
}
// 定义输入格式注意 CST 时区和 Locale.US
SimpleDateFormat inputFormat = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss zzz yyyy", Locale.US
);
// 解析输入日期
Date date = inputFormat.parse(inputDate);
// 定义输出格式
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
return outputFormat.format(date);
}
private static String getCellValueAsString(Cell cell) {
if (cell == null) {
return "";
}
switch (cell.getCellType()) {
case STRING:
return cell.getStringCellValue().trim();
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
// 如果是日期格式格式化为字符串
return cell.getDateCellValue().toString();
} else {
// 转换为整数或保留小数
double numericValue = cell.getNumericCellValue();
if (numericValue == (long) numericValue) {
return String.format("%d", (long) numericValue);
} else {
return String.valueOf(numericValue);
}
}
case BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
case FORMULA:
return cell.getCellFormula();
case BLANK:
return "";
default:
return "";
}
}
} }