Controller层
/**
* 导入值班信息
* @author LJC
*/
@ResponseBody
@RequestMapping("/importExcel")
public ResponseData importExcel(@RequestParam("dutyTaskId") Long dutyTaskId,@RequestParam("file") MultipartFile file){
InputStream inputStream = null;
try {
inputStream = file.getInputStream();
List<DutyInfoItem> dutyInfoItemList = null;
//声明导入参数封装对象
ImportParams importParams = new ImportParams();
dutyInfoItemList = ExcelImportUtil.importExcel(inputStream, DutyInfoItem.class,importParams);
dutyInfoService.getimportExcel(dutyInfoItemList,dutyTaskId);
return ResponseData.success();
}catch (Exception e){
e.printStackTrace();
return ResponseData.error(e.getMessage());
}
}
Service层 为自己的逻辑代码
@Override
public void getimportExcel(List<DutyInfoItem> dutyInfoItemList, Long dutyTaskId) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//遍历excel中取到的数据
AtomicInteger i = new AtomicInteger(1);
dutyInfoItemList.forEach(node->{
DutyInfoParam dutyInfoParam=new DutyInfoParam();
BeanUtil.copyProperties(node,dutyInfoParam);
//查找用户信息
if (ToolUtil.isEmpty(node.getDate()) && ToolUtil.isEmpty(node.getPerName()) &&ToolUtil.isEmpty(node.getPostType()) && ToolUtil.isEmpty(node.getDeptName())){
i.addAndGet(1);
return;
}
User user = userMapper.getbyId(dutyInfoParam.getPerName());
i.addAndGet(1);
//判断用户是否存在
if (ToolUtil.isEmpty(user)){
throw new ServiceException(500,"表格第"+" "+ i +" "+"列存在错误,请修改!" );
}
String format = sdf.format(node.getDate());
//判断用户是否已经添加过
DutyInfoParam dip=dutyInfoMapper.getHisStroy(user.getUserId(), format);
if (ToolUtil.isEmpty(dip)){
//赋值用户Id
dutyInfoParam.setPerId(user.getUserId());
//赋值部门Id
dutyInfoParam.setDeptId(user.getDeptId());
//查找用户部门名称
dutyInfoParam.setDeptName(deptService.getById(user.getDeptId()).getFullName());
//赋值值班任务ID
dutyInfoParam.setDutyTaskId(dutyTaskId);
//添加用户
// this.add(dutyInfoParam);
}
});
}