From a5b76b25df212d143b6d911fca23e3f2c287ab85 Mon Sep 17 00:00:00 2001 From: shanao Date: Fri, 6 Sep 2024 19:38:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=89=B9=E9=87=8F=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../accident/AccidentRecordsController.java | 18 +++++++++--------- .../entity/accident/AccidentRecords.java | 10 +++++----- .../accident/AccidentRecordsMapper.java | 2 +- .../accident/AccidentRecordsService.java | 2 +- .../impl/AccidentRecordsServiceImpl.java | 16 ++++++++++------ .../accident/AccidentRecordsMapper.xml | 10 +++++----- .../service/AccidentRecordsServiceTest.java | 4 ++-- 7 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/zcloud/controller/accident/AccidentRecordsController.java b/src/main/java/com/zcloud/controller/accident/AccidentRecordsController.java index d1a6e97c..39644d59 100644 --- a/src/main/java/com/zcloud/controller/accident/AccidentRecordsController.java +++ b/src/main/java/com/zcloud/controller/accident/AccidentRecordsController.java @@ -2,6 +2,7 @@ package com.zcloud.controller.accident; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.convert.Convert; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.zcloud.controller.base.BaseController; import com.zcloud.entity.Page; @@ -17,14 +18,12 @@ import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.stream.Collectors; +@RestController @RequiredArgsConstructor -@RestController("/accident") +@RequestMapping("/accident") public class AccidentRecordsController extends BaseController { private final AccidentRecordsService accidentRecordsService; @@ -69,7 +68,6 @@ public class AccidentRecordsController extends BaseController { @RequestMapping("/save") public Map save(HttpServletRequest request) { AccidentRecords accidentRecords = BeanUtil.mapToBean(new PageData(request), AccidentRecords.class, true); - accidentRecords.setCorpinfoId(Optional.of(accidentRecords.getCorpinfoId()).orElseThrow(() -> new RuntimeException("所属公司不能为空"))); accidentRecordsService.save(accidentRecords); Map result = new HashMap<>(); result.put("result", "success"); @@ -86,7 +84,6 @@ public class AccidentRecordsController extends BaseController { public Map update(HttpServletRequest request) { AccidentRecords accidentRecords = BeanUtil.mapToBean(new PageData(request), AccidentRecords.class, true); accidentRecords.setId(Optional.of(accidentRecords.getId()).orElseThrow(() -> new RuntimeException("id不能为空"))); - accidentRecords.setCorpinfoId(Optional.of(accidentRecords.getCorpinfoId()).orElseThrow(() -> new RuntimeException("所属公司不能为空"))); accidentRecordsService.update(accidentRecords); Map result = new HashMap<>(); result.put("result", "success"); @@ -101,8 +98,11 @@ public class AccidentRecordsController extends BaseController { */ @RequestMapping("/delete/{ids}") public Map delete(@PathVariable("ids") String values) { - String[] split = Optional.of(values).orElseThrow(() -> new RuntimeException("ids不能为空")).split(","); - Long[] ids = Arrays.stream(split).map(Convert::toLong).collect(Collectors.toList()).toArray(new Long[]{}); + if (StrUtil.isEmpty(values)) { + throw new RuntimeException("id不能为空"); + } + List ids = Arrays.stream(values.split(",")).map(Convert::toLong).filter(ObjectUtil::isNotEmpty) + .collect(Collectors.toList()); accidentRecordsService.delete(ids); Map result = new HashMap<>(); result.put("result", "success"); diff --git a/src/main/java/com/zcloud/entity/accident/AccidentRecords.java b/src/main/java/com/zcloud/entity/accident/AccidentRecords.java index 58987287..76e069a8 100644 --- a/src/main/java/com/zcloud/entity/accident/AccidentRecords.java +++ b/src/main/java/com/zcloud/entity/accident/AccidentRecords.java @@ -47,7 +47,7 @@ public class AccidentRecords implements Serializable { /** * 事故类型 */ - private Integer incidentType; + private String incidentType; /** * 所属公司 @@ -59,7 +59,7 @@ public class AccidentRecords implements Serializable { * 事故级别 */ @Size(max = 50, message = "事故级别最大长度要小于 50") - private Integer incidentLevel; + private String incidentLevel; /** * 事故性质 @@ -171,7 +171,7 @@ public class AccidentRecords implements Serializable { private static final long serialVersionUID = 1L; - public AccidentRecords(AccidentRecordsExcel reader, DictionariesService service) { + public AccidentRecords(AccidentRecordsExcel reader, DictionariesService service) throws RuntimeException { this.id = IdUtil.getSnowflake(1, 1).nextId(); this.corpinfoId = reader.getCompanyName(); this.incidentNumber = reader.getIncidentNumber(); @@ -196,7 +196,7 @@ public class AccidentRecords implements Serializable { this.createdTime = new Date(); } - private Integer findByName(String name, DictionariesService service) { + private String findByName(String name, DictionariesService service) { if (StrUtil.isEmpty(name)) { return null; } @@ -204,7 +204,7 @@ public class AccidentRecords implements Serializable { pageData.put("NAME", name); try { PageData data = service.findByName(pageData); - return Optional.ofNullable(data).map(pd -> (Integer) pd.get("BIANMA")).orElseThrow(() -> new RuntimeException("未找到字典数据:" + name)); + return Optional.ofNullable(data).map(pd -> pd.getString("BIANMA")).orElseThrow(() -> new RuntimeException("未找到字典数据:" + name)); } catch (RuntimeException e) { log.error("转换字典失败", e); throw e; diff --git a/src/main/java/com/zcloud/mapper/datasource/accident/AccidentRecordsMapper.java b/src/main/java/com/zcloud/mapper/datasource/accident/AccidentRecordsMapper.java index bb753499..44e73793 100644 --- a/src/main/java/com/zcloud/mapper/datasource/accident/AccidentRecordsMapper.java +++ b/src/main/java/com/zcloud/mapper/datasource/accident/AccidentRecordsMapper.java @@ -49,7 +49,7 @@ public interface AccidentRecordsMapper { * @param ids 主键数组 * @return 受影响的行数 */ - int delete(@Param("ids") Long[] ids); + int delete(@Param("ids") List ids); /** * 导出excel diff --git a/src/main/java/com/zcloud/service/accident/AccidentRecordsService.java b/src/main/java/com/zcloud/service/accident/AccidentRecordsService.java index b70186e3..a87167a9 100644 --- a/src/main/java/com/zcloud/service/accident/AccidentRecordsService.java +++ b/src/main/java/com/zcloud/service/accident/AccidentRecordsService.java @@ -45,7 +45,7 @@ public interface AccidentRecordsService { * * @param ids 主键数组 */ - void delete(Long[] ids); + void delete(List ids); /** * 导出Excel diff --git a/src/main/java/com/zcloud/service/accident/impl/AccidentRecordsServiceImpl.java b/src/main/java/com/zcloud/service/accident/impl/AccidentRecordsServiceImpl.java index 40639f60..a3bfe6b0 100644 --- a/src/main/java/com/zcloud/service/accident/impl/AccidentRecordsServiceImpl.java +++ b/src/main/java/com/zcloud/service/accident/impl/AccidentRecordsServiceImpl.java @@ -2,6 +2,7 @@ package com.zcloud.service.accident.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.IoUtil; import cn.hutool.core.lang.Assert; import cn.hutool.core.util.*; import cn.hutool.extra.servlet.ServletUtil; @@ -56,7 +57,8 @@ public class AccidentRecordsServiceImpl implements AccidentRecordsService { @Override public List queryPage(Page page) { - page.getPd().put("corpInfoId", Jurisdiction.getCORPINFO_ID()); + PageData data = page.getPd(); + data.put("corpInfoId", Jurisdiction.getCORPINFO_ID()); return accidentRecordsMapper.listPage(page); } @@ -72,6 +74,8 @@ public class AccidentRecordsServiceImpl implements AccidentRecordsService { accidentRecords.setCreatedBy(Jurisdiction.getUsername()); accidentRecords.setCreatedTime(new Date()); accidentRecords.setIsDeleted(0); + String corpinfoId = StrUtil.isEmpty(accidentRecords.getCorpinfoId()) ? Jurisdiction.getCORPINFO_ID() : accidentRecords.getCorpinfoId(); + accidentRecords.setCorpinfoId(corpinfoId); Assert.isTrue(accidentRecordsMapper.save(accidentRecords) == 1, "新增事故记录失败"); } @@ -80,13 +84,15 @@ public class AccidentRecordsServiceImpl implements AccidentRecordsService { public void update(AccidentRecords accidentRecords) { accidentRecords.setUpdatedBy(Jurisdiction.getUsername()); accidentRecords.setUpdatedTime(new Date()); + String corpinfoId = StrUtil.isEmpty(accidentRecords.getCorpinfoId()) ? Jurisdiction.getCORPINFO_ID() : accidentRecords.getCorpinfoId(); + accidentRecords.setCorpinfoId(corpinfoId); Assert.isTrue(accidentRecordsMapper.updateById(accidentRecords) == 1, "更新事故记录失败"); } @Override @Transactional(rollbackFor = Exception.class) - public void delete(Long[] ids) { - Assert.isTrue(accidentRecordsMapper.delete(ids) == ids.length, "删除事故记录失败"); + public void delete(List ids) { + Assert.isTrue(accidentRecordsMapper.delete(ids) == ids.size(), "删除事故记录失败"); } @Override @@ -149,9 +155,7 @@ public class AccidentRecordsServiceImpl implements AccidentRecordsService { log.error("导入excel失败", e); throw new RuntimeException("导入excel失败"); } finally { - if (sqlSession != null) { - sqlSession.close(); - } + IoUtil.close(sqlSession); } } diff --git a/src/main/resources/mybatis/datasource/accident/AccidentRecordsMapper.xml b/src/main/resources/mybatis/datasource/accident/AccidentRecordsMapper.xml index 0a36edb8..ac15f802 100644 --- a/src/main/resources/mybatis/datasource/accident/AccidentRecordsMapper.xml +++ b/src/main/resources/mybatis/datasource/accident/AccidentRecordsMapper.xml @@ -3,7 +3,7 @@