机具需求计划机具类型里面表格导入

This commit is contained in:
lSun 2025-03-21 18:23:25 +08:00
parent 36eb289e97
commit 36e8aa1188
5 changed files with 131 additions and 0 deletions

View File

@ -30,6 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
/**
@ -211,4 +212,15 @@ public class PlanApplicationController {
PageInfo<PlanDevBean> pageInfo = new PageInfo<>(list);
return pageInfo;
}
/**
* 机具的excel的方法
* @param
* @return
*/
@PostMapping("importExcelTools")
public ServerResponse importExcelTools(HttpServletRequest request, @RequestParam(value = "file[]",required = false) MultipartFile[] files) {
return service.importExcelTools(request,files);
}
}

View File

@ -96,4 +96,11 @@ public interface PlanApplicationMapper {
*/
List<PlanDevBean> getTypeList(PlanDevBean data);
/**
* 查询设备是否存在
* @param planDevBean
* @return
*/
PlanDevBean getExist(@Param("param") PlanDevBean planDevBean);
}

View File

@ -7,7 +7,9 @@ import com.bonus.gzgqj.business.plan.entity.PlanApplyBean;
import com.bonus.gzgqj.business.plan.entity.PlanDataDetailBean;
import com.bonus.gzgqj.business.plan.entity.PlanDevBean;
import com.bonus.gzgqj.manager.webResult.ServerResponse;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
/**
@ -71,4 +73,12 @@ public interface PlanApplicationService {
* @return
*/
List<PlanDataDetailBean> getPlanDetailsList(PlanDataDetailBean data);
/**
* 机具的excel的方法
* @param request
* @param files
* @return
*/
ServerResponse importExcelTools(HttpServletRequest request, MultipartFile[] files);
}

View File

@ -14,8 +14,15 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import org.springframework.web.multipart.MultipartFile;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
@ -189,6 +196,87 @@ public class PlanApplicationServiceImp implements PlanApplicationService {
return new ArrayList<PlanDataDetailBean>();
}
@Override
public ServerResponse importExcelTools(HttpServletRequest request, MultipartFile[] files) {
List<PlanDevBean> dataList = new ArrayList<>();
List<PlanDevBean> nonExistList = new ArrayList<>(); // 用于记录数据库中不存在的数据
try {
for (MultipartFile file : files) {
if (!file.isEmpty()) {
InputStream inputStream = file.getInputStream();
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0);
//先去取数据
for (int i = 2; i <= sheet.getLastRowNum(); i++) { // 假设第一行是标题行
Row row = sheet.getRow(i);
PlanDevBean data = new PlanDevBean();
// 获取全部列的值
data.setPpName(row.getCell(1).getStringCellValue()); //物资类型
data.setpName(row.getCell(2).getStringCellValue()); //物资名称
data.setName(row.getCell(3).getStringCellValue()); //物资型号
data.setUnit(row.getCell(4).getStringCellValue()); //单位
data.setNeedNum((int)row.getCell(5).getNumericCellValue()); //数量
// data.setTimes(row.getCell(6).getStringCellValue()); //时间
Cell cell = row.getCell(6); // 假设时间是在第7列0-based index 6
if (cell != null) {
String timeValue;
switch (cell.getCellType()) {
case STRING:
// 如果单元格内容是字符串直接获取其值
timeValue = cell.getStringCellValue().trim();
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
// 如果单元格是日期格式
Date date = cell.getDateCellValue();
// 格式化日期为字符串
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
timeValue = dateFormat.format(date);
} else {
// 如果不是日期格式但为数值类型可能需要其他处理逻辑
log.warn("单元格不是日期格式,而是数值: {}", cell.getNumericCellValue());
timeValue = String.valueOf(cell.getNumericCellValue());
}
break;
default:
log.warn("不支持的单元格类型: {}", cell.getCellType());
timeValue = "";
}
data.setTimes(timeValue);
} else {
log.warn("单元格为空");
data.setTimes("");
}
data.setRemarks(row.getCell(7).getStringCellValue()); //备注
// 继续设置其他属性...
dataList.add(data);
}
workbook.close();
}
}
if(dataList!=null && !dataList.isEmpty()){
for (PlanDevBean planDevBean : dataList) {
//根据物资类型物资名称物资型号 去数据库查询是否存在
PlanDevBean bean = dao.getExist(planDevBean);
if(bean == null){
// 在数据库中不存在则记录下全部判断完返回到前段提示
nonExistList.add(planDevBean);
}else{
planDevBean.setId(bean.getId());
}
}
// 如果存在数据库中不存在的数据返回提示
if (!nonExistList.isEmpty()) {
return ServerResponse.createError("导入失败,以下数据在数据库中不存在", nonExistList);
}
}
return ServerResponse.createSuccess("导入成功", dataList);
} catch (Exception e) {
log.error(e.toString(), e);
}
return ServerResponse.createError("导入失败,请检查表格数据",dataList);
}
/**
* 获取数据详情

View File

@ -227,4 +227,18 @@
ORDER BY mt.`NAME` desc
</select>
<select id="getExist" resultType="com.bonus.gzgqj.business.plan.entity.PlanDevBean">
SELECT mt.ID id ,mt.PARENT_ID pId,mt.`LEVEL` level,mt.UNIT unit,mt.NUM num ,mt.`NAME` name ,mt1.`NAME` pName ,
mt2.`NAME` ppName,"" needNum,"" times,"" remarks
from mm_type mt
LEFT JOIN mm_type mt1 on mt.PARENT_ID=mt1.id and mt1.IS_ACTIVE=1
LEFT JOIN mm_type mt2 on mt1.PARENT_ID=mt2.id and mt2.IS_ACTIVE=1
WHERE mt.IS_ACTIVE=1
and mt.IS_LABEL=1
and mt2.`NAME` = #{param.ppName}
and mt1.`NAME` = #{param.pName}
and mt.`NAME` = #{param.name}
</select>
</mapper>