diff --git a/ant-design-vue-jeecg/src/views/modulex/ModulexList.vue b/ant-design-vue-jeecg/src/views/modulex/ModulexList.vue index c6c1e0f..282a86c 100644 --- a/ant-design-vue-jeecg/src/views/modulex/ModulexList.vue +++ b/ant-design-vue-jeecg/src/views/modulex/ModulexList.vue @@ -71,7 +71,9 @@
新增 导入模块 - 导入功能 + + 导入功能 + 导入规则 @@ -397,6 +399,7 @@ export default { deleteBatch: "/modulex/modulex/deleteBatch", exportXlsUrl: "/modulex/modulex/exportXls", importExcelUrl: "modulex/modulex/importExcel", + functionImportExcelUrl: "/functionx/functionx/importExcel", tablexImportExcelUrl: "tablex/tablex/importExcel", ruleImportExcelUrl: "rulex/rulex/importExcel", }, @@ -424,6 +427,9 @@ export default { checkColumn: function () { return this.columns.map(item => item.title) }, + functionImportExcelUrl: function(){ + return `${window._CONFIG['domianURL']}/${this.url.functionImportExcelUrl}`; + }, tablexImportExcelUrl: function(){ return `${window._CONFIG['domianURL']}/${this.url.tablexImportExcelUrl}`; }, diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functiontemplate/controller/FunctionTemplateController.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functiontemplate/controller/FunctionTemplateController.java index 45e4357..230143a 100644 --- a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functiontemplate/controller/FunctionTemplateController.java +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functiontemplate/controller/FunctionTemplateController.java @@ -11,6 +11,7 @@ import org.jeecg.common.api.vo.Result; import org.jeecg.common.system.vo.LoginUser; import org.jeecg.common.util.oConvertUtils; import org.jeecg.modules.demo.functiontemplate.entity.FunctionTemplate; +import org.jeecg.modules.demo.functiontemplate.service.FunctionTemplateTestService; import org.jeecg.modules.demo.functiontemplate.service.IFunctionTemplateService; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; @@ -23,6 +24,7 @@ import org.jeecg.common.system.base.controller.JeecgController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -41,6 +43,8 @@ import org.jeecg.common.aspect.annotation.AutoLog; public class FunctionTemplateController extends JeecgController { @Autowired private IFunctionTemplateService functionTemplateService; + @Autowired + private FunctionTemplateTestService templateTestService; /** * 分页列表查询 @@ -191,14 +195,14 @@ public class FunctionTemplateController extends JeecgController importExcel(HttpServletRequest request, HttpServletResponse response) { - return super.importExcel(request, response, FunctionTemplate.class); + public Result importExcel(MultipartFile file) { + + return templateTestService.importExcel(file); +// return super.importExcel(request, response, FunctionTemplate.class); // 原始代码 } } diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functiontemplate/entity/FunctionTemplateTest.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functiontemplate/entity/FunctionTemplateTest.java new file mode 100644 index 0000000..9b14f8a --- /dev/null +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functiontemplate/entity/FunctionTemplateTest.java @@ -0,0 +1,98 @@ +package org.jeecg.modules.demo.functiontemplate.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * + * @TableName function_template_test + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class FunctionTemplateTest implements Serializable { + /** + * + */ + private Long id; + + /** + * 模块模板类型 + */ + private String modulextypename; + + + /** + * 中文名称 + */ + private String functiontemplatename; + + /** + * 英文名称 + */ + private String functiontemplateenname; + + /** + * 功能编码 + */ + private String functiontemplatecode; + + /** + * 功能类型 + */ + private String functiontemplatetype; + + /** + * 功能描述 + */ + private String functiontemplatedescribe; + + /** + * 分析图 + */ + private String diagrams; + + /** + * 用户角色 + */ + private String userrole; + + /** + * 责任人 + */ + private String managerusers; + + /** + * 任务等级 + */ + private String worklevel; + + /** + * 任务状态 + */ + private String workstatus; + + /** + * 发布时间 + */ + private String publishtime; + + /** + * 开始时间 + */ + private String starttime; + + /** + * 任务时长 + */ + private String duration; + + /** + * 创建人 + */ + private String createname; + +} diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functiontemplate/mapper/FunctionTemplateTestMapper.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functiontemplate/mapper/FunctionTemplateTestMapper.java new file mode 100644 index 0000000..d4b292b --- /dev/null +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functiontemplate/mapper/FunctionTemplateTestMapper.java @@ -0,0 +1,9 @@ +package org.jeecg.modules.demo.functiontemplate.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; +import org.jeecg.modules.demo.functiontemplate.entity.FunctionTemplateTest; + +@Mapper +public interface FunctionTemplateTestMapper extends BaseMapper { +} diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functiontemplate/service/FunctionTemplateTestService.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functiontemplate/service/FunctionTemplateTestService.java new file mode 100644 index 0000000..4321288 --- /dev/null +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functiontemplate/service/FunctionTemplateTestService.java @@ -0,0 +1,10 @@ +package org.jeecg.modules.demo.functiontemplate.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import org.jeecg.common.api.vo.Result; +import org.jeecg.modules.demo.functiontemplate.entity.FunctionTemplateTest; +import org.springframework.web.multipart.MultipartFile; + +public interface FunctionTemplateTestService extends IService { + Result importExcel(MultipartFile file); +} diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functiontemplate/service/impl/FunctionTemplateTestServiceImpl.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functiontemplate/service/impl/FunctionTemplateTestServiceImpl.java new file mode 100644 index 0000000..a2c92d8 --- /dev/null +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functiontemplate/service/impl/FunctionTemplateTestServiceImpl.java @@ -0,0 +1,323 @@ +package org.jeecg.modules.demo.functiontemplate.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.StringUtils; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import lombok.extern.slf4j.Slf4j; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.apache.shiro.SecurityUtils; +import org.jeecg.common.api.vo.Result; +import org.jeecg.common.system.vo.LoginUser; +import org.jeecg.modules.demo.functiontemplate.entity.FunctionTemplate; +import org.jeecg.modules.demo.functiontemplate.entity.FunctionTemplateTest; +import org.jeecg.modules.demo.functiontemplate.mapper.FunctionTemplateTestMapper; +import org.jeecg.modules.demo.functiontemplate.service.FunctionTemplateTestService; +import org.jeecg.modules.demo.functiontemplate.service.IFunctionTemplateService; +import org.jeecg.modules.demo.moduletype.entity.ModuleType; +import org.jeecg.modules.demo.moduletype.service.IModuleTypeService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.*; + +@Service +@Slf4j +public class FunctionTemplateTestServiceImpl extends ServiceImpl implements FunctionTemplateTestService { + + @Autowired + private IFunctionTemplateService functionTemplateService; + @Autowired + private IModuleTypeService moduleTypeService; + + /** + * 导入excel + * + * @param file 文件 + * @return {@link Result}<{@link ?}> + */ + @Override + @Transactional + public Result importExcel(MultipartFile file) { + List listVo = new ArrayList<>(); + // 获取当前操作用户 + LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); + String username = loginUser.getUsername(); + + Map> fileMap = new HashMap<>(); + //创建输出流对象 + Workbook wb=null; + /*判断文件是xlsx结尾还是xls结尾 声明XSSF或HSSF对象*/ + String[] split = file.getOriginalFilename().split("\\."); + + try { + if(split[1].equals("xlsx")){ + wb= new XSSFWorkbook(file.getInputStream()); + }else if(split[1].equals("xls")){ + wb= new HSSFWorkbook(file.getInputStream()); + } else { + return Result.error("文件格式错误,请上传Excel文件"); + } + } catch (IOException e) { + throw new RuntimeException("请上传正确格式文件"); + } + + + //获取工作表页数据 + //读取第1页的数据 + Sheet sheet=wb.getSheetAt(0); + //获取工作表页中行数据 + //读取的总的行数 + int lastRowIndex=sheet.getLastRowNum(); + if(lastRowIndex < 1) { + return Result.error("文件无内容或格式错误"); + } + + for (int i=1;i<=lastRowIndex;i++) { + Row row = sheet.getRow(i); + if(row!=null){ + short lastCellNum = row.getLastCellNum(); + List list = new ArrayList<>(); + for (int j=0;j queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(username!=null,FunctionTemplateTest::getCreatename,username); + List list = this.list(queryWrapper); + + boolean remove = this.remove(queryWrapper); + if (!remove){ + log.error("临时表删除错误!"); + } + + List listInsert = new ArrayList<>(); // 正式插入 + + String pattern = "yyyy-MM-dd HH:mm:ss"; + SimpleDateFormat dateFormat = new SimpleDateFormat(pattern); + + for (FunctionTemplateTest function :list){ + FunctionTemplate functionTemplate = new FunctionTemplate(); + String modulextypename = function.getModulextypename(); // 模块模板类型 + String functiontemplatename = function.getFunctiontemplatename(); // 中文名称 + String functiontemplateenname = function.getFunctiontemplateenname(); // 英文名称 + String functiontemplatecode = function.getFunctiontemplatecode(); // 功能编码 + String functiontemplatetype = function.getFunctiontemplatetype(); // 功能类型 + String functiontemplatedescribe = function.getFunctiontemplatedescribe(); // 任务描述 + String diagrams = function.getDiagrams();// 分析图 + String userrole = function.getUserrole(); // 用户角色 + String managerUsers = function.getManagerusers(); // 责任人 + String worklevel = function.getWorklevel(); // 任务等级 + String workstatus = function.getWorkstatus(); // 任务状态 + String publishtime = function.getPublishtime(); // 发布时间 + String starttime = function.getStarttime(); // 开始时间 + String duration = function.getDuration(); // 任务时长 + + if(StringUtils.isBlank(modulextypename)){ // 空和null + return Result.error("模块模板类型不能为空"); + } else{ + LambdaQueryWrapper queryWrapper1 = new LambdaQueryWrapper<>(); + queryWrapper1.eq(ModuleType::getModulextypeName,modulextypename); + ModuleType one = moduleTypeService.getOne(queryWrapper1); + if(one==null) + return Result.error("请输入正确的模块模板类型"); + else + functionTemplate.setModulextypeId(one.getId()); + } + if(StringUtils.isBlank(functiontemplatename)){ // 空和null + return Result.error("中文名称不能为空"); + } else{ + LambdaQueryWrapper queryWrapperCh = new LambdaQueryWrapper<>(); +// queryWrapperCh.eq() + functionTemplate.setFunctionTemplateName(functiontemplatename); + } + if(StringUtils.isBlank(functiontemplateenname)){ + return Result.error("英文名称不能为空"); + } else { + functionTemplate.setFunctionTemplateEnName(functiontemplateenname); + } + + Integer type=100; + if(StringUtils.isBlank(functiontemplatetype)){ + // 模块编码+功能缩写;自动填充+手动修改 + return Result.error("功能类型不能为空"); + } else { + // 值:列表0、新增1、删除2、修改3、查看4、导入5、导出6、其它99;默认99 + switch (functiontemplatetype){ + case "列表": type=0; break; + case "新增": type=1; break; + case "删除": type=2; break; + case "修改": type=3; break; + case "查看": type=4; break; + case "导入": type=5; break; + case "导出": type=6; break; + case "其它": type=99; break; + default: return Result.error("功能类型错误"); + } + functionTemplate.setFunctionTemplateType(type); + } + if(StringUtils.isBlank(functiontemplatecode)){ + return Result.error("功能编码不能为空"); + } else { + if (type!=100){ + String str = "后期替换"+functiontemplatetype; + if(!str.equals(functiontemplatecode)) + return Result.error("功能编码错误"); + } + functionTemplate.setFunctionTemplateCode(functiontemplatecode); + } + if(StringUtils.isNotBlank(functiontemplatedescribe)){ + functionTemplate.setFunctionTemplateDescribe(functiontemplatedescribe); + } + if(StringUtils.isNotBlank(diagrams)){ + functionTemplate.setDiagrams(diagrams); + } + if(StringUtils.isNotBlank(userrole)){ + functionTemplate.setUserRole(userrole); + } + if(StringUtils.isNotBlank(managerUsers)){ + functionTemplate.setManagerUsers(managerUsers); + } + if(StringUtils.isBlank(worklevel)){ // 空和null + return Result.error("任务等级不能为空"); + } else{ + Integer level = Integer.valueOf(worklevel); + if(level<0 || level>6){ + return Result.error("任务等级不符范围"); + } + functionTemplate.setWorkLevel(level); + } + if(StringUtils.isBlank(workstatus)){ + return Result.error("任务状态不能为空"); + } else { + // 预制值:未发布0、已发布1、开发中2、已完成3、已审核4、DEBUG9;默认0 + type=10; + switch (workstatus){ + case "未发布": type=0; break; + case "已发布": type=1; break; + case "开发中": type=2; break; + case "已完成": type=3; break; + case "已审核": type=4; break; + case "DEBUG": type=9; break; + default: return Result.error("任务状态错误"); + } + functionTemplate.setWorkStatus(type); + } + + if(StringUtils.isNotBlank(publishtime)){ + Date date; + Boolean isTime = checkTime(publishtime); + if (!isTime){ + return Result.error("发布日期错误,应为"+pattern+"类型"); + } + try { + date = dateFormat.parse(publishtime); + } catch (Exception e) { + return Result.error("发布日期错误,应为"+pattern+"类型"); + } + functionTemplate.setPublishTime(date); + } + if(StringUtils.isNotBlank(starttime)){ + Date date; + Boolean isTime = checkTime(starttime); + if (!isTime){ + return Result.error("开始日期错误,应为"+pattern+"类型"); + } + try { + date = dateFormat.parse(starttime); + } catch (Exception e) { + return Result.error("开始日期错误,应为"+pattern+"类型"); + } + functionTemplate.setPublishTime(date); + } + if(StringUtils.isNotBlank(duration)){ + Double dura = Double.valueOf(duration); + functionTemplate.setDuration(dura); + } + + functionTemplate.setCreateBy(username); + functionTemplate.setCreateTime(new Date()); + + listInsert.add(functionTemplate); + } + + // -------------------------------校验结束-------------------------------------- + boolean isSuccess = functionTemplateService.saveBatch(listInsert); + if(!isSuccess){ + return Result.OK("插入失败"); + } + + return Result.OK("插入成功"); + } + + /** + * 检查时间格式,是否有效 + */ + private Boolean checkTime(String date){ + // 使用日期解析方式校验日期逻辑有效性 + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + try { + Date parsedDate = dateFormat.parse(date); + // 检查年份是否合法(例如:不能小于1900) + Calendar calendar = Calendar.getInstance(); + calendar.setTime(parsedDate); + int year = calendar.get(Calendar.YEAR); + if (year < 1900) { + return false; + } + + // 检查日期的逻辑有效性(例如:2月30日是无效的) + int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); + int month = calendar.get(Calendar.MONTH) + 1; + + int maxDayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); + if (dayOfMonth > maxDayOfMonth) { + return false; + } + } catch (Exception e) { + return false; + } + return true; + } + + + +} diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functionx/controller/FunctionxController.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functionx/controller/FunctionxController.java index bbde1a4..5c6c0f4 100644 --- a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functionx/controller/FunctionxController.java +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functionx/controller/FunctionxController.java @@ -17,6 +17,7 @@ import org.jeecg.common.system.vo.LoginUser; import org.jeecg.common.util.oConvertUtils; import org.jeecg.modules.demo.functionx.entity.Functionx; import org.jeecg.modules.demo.functionx.entity.RoleConstant; +import org.jeecg.modules.demo.functionx.service.FunctionXTestService; import org.jeecg.modules.demo.functionx.service.IFunctionxService; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; @@ -70,6 +71,9 @@ public class FunctionxController extends JeecgController importExcel(HttpServletRequest request, HttpServletResponse response) { - return super.importExcel(request, response, Functionx.class); + @PostMapping("/importExcel") + public Result importExcel(MultipartFile file) { + return functionXTestService.importExcel(file); } //获得模块管理的编码,填充给功能编码 diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functionx/entity/FunctionxTest.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functionx/entity/FunctionxTest.java new file mode 100644 index 0000000..7ac0734 --- /dev/null +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functionx/entity/FunctionxTest.java @@ -0,0 +1,127 @@ +package org.jeecg.modules.demo.functionx.entity; + +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.io.Serializable; +import java.util.Date; + +/** + * + * @TableName functionx_test + */ +@TableName(value ="functionx_test") +@Data +public class FunctionxTest implements Serializable { + /** + * + */ + @TableId + private String id; + + + /** + * 对应模块 + */ + private String moduleName; + + /** + * 中文名称 + */ + private String functionName; + + /** + * 英文名称 + */ + private String functionEnName; + + /** + * 功能编码 + */ + private String functionCode; + + /** + * 功能类型 + */ + private String functionType; + + /** + * 功能描述 + */ + private String pmDescribe; + + /** + * 分析图 + */ + private String diagrams; + + /** + * 用户角色 + */ + private String userRole; + + /** + * 责任人 + */ + private String managerUsers; + + /** + * 任务等级 + */ + private String workLevel; + + /** + * 任务状态 + */ + private String workStatus; + + /** + * 发布时间 + */ + private String publishTime; + + /** + * 开始时间 + */ + private String startTime; + + /** + * 任务时长 + */ + private String duration; + + /** + * 提交时间 + */ + private String submitTime; + + /** + * 实际时长 + */ + private String realDuration; + + /** + * 功能状态 + */ + private String status; + + /** + * 版本状态 + */ + private String verisonStatus; + + /** + * 版本号 + */ + private String verison; + + /** + * 创建人 + */ + private String creatName; + + @TableField(exist = false) + private static final long serialVersionUID = 1L; +} diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functionx/mapper/FunctionxTestMapper.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functionx/mapper/FunctionxTestMapper.java new file mode 100644 index 0000000..4f4fb3e --- /dev/null +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functionx/mapper/FunctionxTestMapper.java @@ -0,0 +1,8 @@ +package org.jeecg.modules.demo.functionx.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; +import org.jeecg.modules.demo.functionx.entity.FunctionxTest; +@Mapper +public interface FunctionxTestMapper extends BaseMapper { +} diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functionx/service/FunctionXTestService.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functionx/service/FunctionXTestService.java new file mode 100644 index 0000000..32f8667 --- /dev/null +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functionx/service/FunctionXTestService.java @@ -0,0 +1,10 @@ +package org.jeecg.modules.demo.functionx.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import org.jeecg.common.api.vo.Result; +import org.jeecg.modules.demo.functionx.entity.FunctionxTest; +import org.springframework.web.multipart.MultipartFile; + +public interface FunctionXTestService extends IService { + Result importExcel(MultipartFile file); +} diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functionx/service/impl/FunctionXTestServiceImpl.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functionx/service/impl/FunctionXTestServiceImpl.java new file mode 100644 index 0000000..3d0031b --- /dev/null +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/demo/functionx/service/impl/FunctionXTestServiceImpl.java @@ -0,0 +1,411 @@ +package org.jeecg.modules.demo.functionx.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.StringUtils; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.apache.shiro.SecurityUtils; +import org.jeecg.common.api.vo.Result; +import org.jeecg.common.system.vo.LoginUser; +import org.jeecg.modules.demo.functionx.entity.Functionx; +import org.jeecg.modules.demo.functionx.entity.FunctionxTest; +import org.jeecg.modules.demo.functionx.mapper.FunctionxTestMapper; +import org.jeecg.modules.demo.functionx.service.FunctionXTestService; +import org.jeecg.modules.demo.functionx.service.IFunctionxService; +import org.jeecg.modules.modulex.entity.Modulex; +import org.jeecg.modules.modulex.service.IModulexService; +import org.jeecg.modules.projectUserRole.entity.ProjectUserRole; +import org.jeecg.modules.projectUserRole.service.IProjectUserRoleService; +import org.jeecg.modules.system.entity.SysUser; +import org.jeecg.modules.system.service.ISysUserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.List; + +@Service +public class FunctionXTestServiceImpl extends ServiceImpl implements FunctionXTestService { + + @Autowired + private IModulexService modulexService; + @Autowired + private IFunctionxService functionxService; + @Autowired + private IProjectUserRoleService projectUserRoleService; + @Autowired + private ISysUserService sysUserService; + + @Override + public Result importExcel(MultipartFile file) { + + // 获取当前操作用户 + LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); + String username = loginUser.getUsername(); + + //创建输出流对象 + Workbook wb; + /*判断文件是xlsx结尾还是xls结尾 声明XSSF或HSSF对象*/ + + String[] split = file.getOriginalFilename().split("\\."); + + try { + if(split[1].equals("xlsx")){ + wb= new XSSFWorkbook(file.getInputStream()); + }else if(split[1].equals("xls")){ + wb= new HSSFWorkbook(file.getInputStream()); + } else { + return Result.error("文件格式错误,请上传Excel文件"); + } + } catch (IOException e) { + return Result.error("请上传正确格式文件"); + } + + + //获取工作表页数据 + //读取第1页的数据 + Sheet sheet=wb.getSheetAt(0); + //获取工作表页中行数据 + //读取的总的行数 + int lastRowIndex=sheet.getLastRowNum(); + if(lastRowIndex < 1) { + return Result.error("文件无内容或格式错误"); + } + + List listVo = new ArrayList<>(); + for (int i=1;i<=lastRowIndex;i++) { + Row row = sheet.getRow(i); // 行 + if(row!=null){ + short lastCellNum = row.getLastCellNum(); + List list = new ArrayList<>(); + for (int j=0;j queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(username!=null, FunctionxTest::getCreatName,username); + List list = this.list(queryWrapper); + + + boolean remove = this.remove(queryWrapper); + if (!remove){ + log.error("临时表删除错误!"); + } + + // 对应模块校验 + List modulexList = modulexService.list(); + + List listInsert = new ArrayList<>(); // 正式插入 + String pattern = "yyyy-MM-dd HH:mm:ss"; + SimpleDateFormat dateFormat = new SimpleDateFormat(pattern); + + for(FunctionxTest function : list){ + Functionx functionx = new Functionx(); + String moduleId = function.getModuleName(); + String functionName = function.getFunctionName(); + String functionEnName = function.getFunctionEnName(); + String functionCode = function.getFunctionCode(); + String functionType = function.getFunctionType(); + String pmDescribe = function.getPmDescribe(); + String diagrams = function.getDiagrams(); + String userRole = function.getUserRole(); + String managerUsers = function.getManagerUsers(); + String workLevel = function.getWorkLevel(); + String workStatus = function.getWorkStatus(); + String publishTime = function.getPublishTime(); + String startTime = function.getStartTime(); + String duration = function.getDuration(); + String submitTime = function.getSubmitTime(); + String realDuration = function.getRealDuration(); + String status = function.getStatus(); + String verisonStatus = function.getVerisonStatus(); + String verison = function.getVerison(); + + + String moduleName=""; // 模块编码 + functionx.setCreateBy(username); //创建人 + functionx.setCreateTime(new Date()); //创建日期 + if(StringUtils.isBlank(moduleId)){ // 空和null + return Result.error("对应模块不能为空"); + } else{ + for(Modulex modulex : modulexList){ + if (modulex.getModuleName().equals(moduleId)){ + functionx.setModuleId(modulex.getId()); + moduleId = modulex.getId(); + moduleName=modulex.getModuleName(); + break; + } + } + if(functionx.getModuleId() == null){ + return Result.error("请输入正确的对应模块"); + } + } + + if(StringUtils.isBlank(functionName)){ // 空和null + return Result.error("中文名称不能为空"); + } else{ + LambdaQueryWrapper queryWrapper1 = new LambdaQueryWrapper<>(); + queryWrapper1.eq(moduleId!=null,Functionx::getModuleId,moduleId); + queryWrapper1.eq(Functionx::getFunctionName,functionName); + int count = functionxService.count(queryWrapper1); + if(count>0) + return Result.error("中文名称在模块下重复"); + functionx.setFunctionName(functionName); + } + if(StringUtils.isBlank(functionEnName)){ // 空和null + return Result.error("英文名称不能为空"); + } else{ + LambdaQueryWrapper queryWrapper2 = new LambdaQueryWrapper<>(); + queryWrapper2.eq(moduleId!=null,Functionx::getModuleId,moduleId); + queryWrapper2.eq(Functionx::getFunctionEnName,functionEnName); + int count = functionxService.count(queryWrapper2); + if(count>0) + return Result.error("英文名称在模块下重复"); + functionx.setFunctionEnName(functionEnName); + } + // 列表0、增加1、删除2、修改3、查看4、导入5、导出6、其它99;默认99 + int type; + if(StringUtils.isBlank(functionType)){ // 空和null + return Result.error("功能类型不能为空"); + } else{ + switch (functionType){ + case "列表": type=0; break; + case "新增": type=1; break; + case "删除": type=2; break; + case "修改": type=3; break; + case "查看": type=4; break; + case "导入": type=5; break; + case "导出": type=6; break; + case "其它": type=99; break; + default: return Result.error("功能类型错误"); + } + functionx.setFunctionType(type); + } + // 模块编码+功能缩写;自动填充+手动修改 + if(StringUtils.isBlank(functionCode)){ // 空和null + return Result.error("功能编码不能为空"); + } else{ + String str = moduleName+functionType; + if(!str.equals(functionCode)) + return Result.error("功能编码错误"); + functionx.setFunctionCode(functionCode); + } + + if(StringUtils.isNotBlank(pmDescribe)){ // 功能描述 + functionx.setPmDescribe(pmDescribe); + } + if(StringUtils.isNotBlank(diagrams)){ // 分析图 + functionx.setDiagrams(diagrams); + } + if(StringUtils.isNotBlank(userRole)){ // 用户角色 + LambdaQueryWrapper queryWrapperUser = new LambdaQueryWrapper<>(); + queryWrapperUser.eq(ProjectUserRole::getUserRole,userRole); // 是否匹配项目id? + int count = projectUserRoleService.count(queryWrapperUser); + if(count>0) + functionx.setUserRole(userRole); + else + return Result.error("请输入正确的用户角色"); + } + if(StringUtils.isNotBlank(managerUsers)){ // 责任人 + LambdaQueryWrapper queryWrapperUserName = new LambdaQueryWrapper<>(); + queryWrapperUserName.eq(SysUser::getUsername,managerUsers); + int count = sysUserService.count(queryWrapperUserName); + if(count>0) + functionx.setManagerUsers(managerUsers); + else + return Result.error("请输入正确的责任人"); + } + // 值:1、2、3、4、5,默认2 + if(StringUtils.isBlank(workLevel)){ // 任务等级 + return Result.error("任务等级不能为空"); + } else{ + int integer = Integer.parseInt(workLevel); + if (integer<1 || integer>5){ + return Result.error("任务等级错误"); + } + functionx.setWorkLevel(integer); + } + + if(StringUtils.isBlank(workStatus)){ + return Result.error("任务状态不能为空"); + } else { + // 值:未发布0、已发布1、开发中2、已完成3、已撤回4、已取消9;默认0 + switch (workStatus){ + case "未发布": type=0; break; + case "已发布": type=1; break; + case "开发中": type=2; break; + case "已完成": type=3; break; + case "已撤回": type=4; break; + case "已取消": type=9; break; + default: return Result.error("任务状态错误"); + } + functionx.setWorkStatus(type); + } + + if(StringUtils.isNotBlank(publishTime)){ + Date date; + Boolean isTime = checkTime(publishTime); + if (!isTime){ + return Result.error("发布时间错误,应为"+pattern+"类型"); + } + try { + date = dateFormat.parse(publishTime); + } catch (Exception e) { + return Result.error("发布时间错误,应为"+pattern+"类型"); + } + functionx.setPublishTime(date); + } + + if(StringUtils.isNotBlank(startTime)){ + Date date; + Boolean isTime = checkTime(startTime); + if (!isTime){ + return Result.error("开始时间错误,应为"+pattern+"类型"); + } + try { + date = dateFormat.parse(startTime); + } catch (Exception e) { + return Result.error("开始时间错误,应为"+pattern+"类型"); + } + functionx.setStartTime(date); + } + + if(StringUtils.isNotBlank(duration)){ + Double aDouble = Double.valueOf(duration); + functionx.setDuration(aDouble); + } + + if(StringUtils.isNotBlank(submitTime)){ + Date date; + Boolean isTime = checkTime(submitTime); + if (!isTime){ + return Result.error("提交时间时间错误,应为"+pattern+"类型"); + } + try { + date = dateFormat.parse(submitTime); + } catch (Exception e) { + return Result.error("提交时间时间错误,应为"+pattern+"类型"); + } + functionx.setSubmitTime(date); + } + if(StringUtils.isNotBlank(realDuration)){ + Double aDouble = Double.valueOf(realDuration); + if (aDouble<0.1) + return Result.error("实际时长错误"); + functionx.setDuration(aDouble); + } + if(StringUtils.isBlank(status)){ // 空和null + return Result.error("功能状态不能为空"); + } else{ + // 值:正常1、停用0、废弃9;默认1 + switch (status){ + case "正常": type=1; break; + case "停用": type=0; break; + case "废弃": type=9; break; + default: return Result.error("任务状态错误"); + } + functionx.setStatus(type); + } + if(StringUtils.isBlank(verisonStatus)){ // 空和null + return Result.error("版本状态不能为空"); + } else{ + // 值:当前1、历史0;默认1 + switch (verisonStatus){ + case "当前": type=1; break; + case "历史": type=0; break; + default: return Result.error("版本状态错误"); + } + functionx.setStatus(type); + } + if(StringUtils.isBlank(verison)){ // 空和null + return Result.error("版本号不能为空"); + } else{ + Integer integer = Integer.valueOf(verison); + functionx.setStatus(integer); + } + + listInsert.add(functionx); + } + + //**************************校验结束****************************** + boolean isSuccessInsert = functionxService.saveBatch(listInsert); + if(!isSuccessInsert){ + return Result.OK("插入失败"); + } + + return Result.OK("插入成功"); + } + + /** + * 检查时间格式,是否有效 + */ + private Boolean checkTime(String date){ + // 使用日期解析方式校验日期逻辑有效性 + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + try { + Date parsedDate = dateFormat.parse(date); + // 检查年份是否合法(例如:不能小于1900) + Calendar calendar = Calendar.getInstance(); + calendar.setTime(parsedDate); + int year = calendar.get(Calendar.YEAR); + if (year < 1900) { + return false; + } + + // 检查日期的逻辑有效性(例如:2月30日是无效的) + int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); + + int maxDayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); + if (dayOfMonth > maxDayOfMonth) { + return false; + } + } catch (Exception e) { + return false; + } + return true; + } + +}