diff --git a/pom.xml b/pom.xml index 6576b988..06b1148c 100644 --- a/pom.xml +++ b/pom.xml @@ -215,7 +215,7 @@ aliyun-java-sdk-core 3.3.1 - + com.aliyun aliyun-java-sdk-dysmsapi 1.0.0 diff --git a/src/main/java/com/zcloud/controller/accident/AccidentRecordsController.java b/src/main/java/com/zcloud/controller/accident/AccidentRecordsController.java index cbf62b3e..f2718233 100644 --- a/src/main/java/com/zcloud/controller/accident/AccidentRecordsController.java +++ b/src/main/java/com/zcloud/controller/accident/AccidentRecordsController.java @@ -2,7 +2,6 @@ package com.zcloud.controller.accident; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.convert.Convert; -import cn.hutool.core.lang.Assert; import com.zcloud.controller.base.BaseController; import com.zcloud.entity.Page; import com.zcloud.entity.PageData; @@ -11,12 +10,16 @@ import com.zcloud.service.accident.AccidentRecordsService; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +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.stream.Collectors; @RequiredArgsConstructor @@ -25,7 +28,6 @@ public class AccidentRecordsController extends BaseController { private final AccidentRecordsService accidentRecordsService; - @RequestMapping(value = "/page") public Map queryPage(Page page, HttpServletRequest request) { page.setPd(new PageData(request)); @@ -38,16 +40,16 @@ public class AccidentRecordsController extends BaseController { @RequestMapping("/{id}") public Map getById(@PathVariable("id") Long id) { - Assert.notNull(id, "id不能为空"); Map result = new HashMap<>(); result.put("result", "success"); - result.put("info", accidentRecordsService.getById(id)); + result.put("info", accidentRecordsService.getById(Optional.of(id).orElseThrow(() -> new RuntimeException("id不能为空")))); return result; } @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"); @@ -57,6 +59,8 @@ public class AccidentRecordsController extends BaseController { @RequestMapping("/update") 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"); @@ -65,11 +69,25 @@ public class AccidentRecordsController extends BaseController { @RequestMapping("/delete/{ids}") public Map delete(@PathVariable("ids") String values) { - Assert.notEmpty(values, "ids不能为空"); - Long[] ids = Arrays.stream(values.split(",")).map(Convert::toLong).collect(Collectors.toList()).toArray(new Long[]{}); + String[] split = Optional.of(values).orElseThrow(() -> new RuntimeException("ids不能为空")).split(","); + Long[] ids = Arrays.stream(split).map(Convert::toLong).collect(Collectors.toList()).toArray(new Long[]{}); accidentRecordsService.delete(ids); Map result = new HashMap<>(); result.put("result", "success"); return result; } + + @RequestMapping("/export/excel") + public void exportExcel(HttpServletRequest request ,HttpServletResponse response) { + PageData pd = new PageData(request); + accidentRecordsService.exportExcel(pd,response); + } + + @RequestMapping("/import/excel") + public void importExcel(@RequestParam("file") MultipartFile file) { + if (file == null || file.isEmpty()) { + throw new RuntimeException("文件不能为空"); + } + accidentRecordsService.importExcel(file); + } } diff --git a/src/main/java/com/zcloud/controller/system/UsersController.java b/src/main/java/com/zcloud/controller/system/UsersController.java index 098ad1d2..5cd7b3ae 100644 --- a/src/main/java/com/zcloud/controller/system/UsersController.java +++ b/src/main/java/com/zcloud/controller/system/UsersController.java @@ -7,17 +7,14 @@ import java.util.*; import javax.servlet.http.HttpServletResponse; import com.zcloud.entity.system.Dictionaries; -import com.zcloud.entity.system.User; import com.zcloud.service.bus.*; import com.zcloud.service.system.*; import com.zcloud.util.*; import org.apache.commons.collections.map.ListOrderedMap; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.time.DateUtils; -import org.apache.fop.layoutmgr.PaddingElement; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.crypto.hash.SimpleHash; -import org.hyperic.sigar.pager.PageList; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; @@ -33,8 +30,6 @@ import com.zcloud.entity.PageData; import com.zcloud.entity.system.Role; import net.sf.json.JSONArray; -import sun.security.krb5.internal.PAData; -import sun.util.logging.resources.logging; /** * 说明:系统用户处理类 diff --git a/src/main/java/com/zcloud/entity/accident/AccidentRecords.java b/src/main/java/com/zcloud/entity/accident/AccidentRecords.java index 19622124..58987287 100644 --- a/src/main/java/com/zcloud/entity/accident/AccidentRecords.java +++ b/src/main/java/com/zcloud/entity/accident/AccidentRecords.java @@ -1,16 +1,27 @@ package com.zcloud.entity.accident; +import cn.hutool.core.util.IdUtil; +import cn.hutool.core.util.StrUtil; +import com.zcloud.entity.PageData; +import com.zcloud.entity.accident.dto.AccidentRecordsExcel; +import com.zcloud.service.system.DictionariesService; +import com.zcloud.util.Jurisdiction; import lombok.Data; import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import java.io.Serializable; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.Date; +import java.util.Optional; /** * 事故记录表 */ +@Slf4j @Data @NoArgsConstructor public class AccidentRecords implements Serializable { @@ -24,13 +35,13 @@ public class AccidentRecords implements Serializable { /** * 事故案号 */ - @Size(max = 50,message = "事故案号最大长度要小于 50") + @Size(max = 50, message = "事故案号最大长度要小于 50") private String incidentNumber; /** * 事故名称 */ - @Size(max = 100,message = "事故名称最大长度要小于 100") + @Size(max = 100, message = "事故名称最大长度要小于 100") private String incidentName; /** @@ -41,25 +52,25 @@ public class AccidentRecords implements Serializable { /** * 所属公司 */ - @Size(max = 100,message = "所属公司最大长度要小于 100") - private String companyName; + @Size(max = 100, message = "所属公司最大长度要小于 100") + private String corpinfoId; /** * 事故级别 */ - @Size(max = 50,message = "事故级别最大长度要小于 50") - private String incidentLevel; + @Size(max = 50, message = "事故级别最大长度要小于 50") + private Integer incidentLevel; /** * 事故性质 */ - @Size(max = 50,message = "事故性质最大长度要小于 50") + @Size(max = 50, message = "事故性质最大长度要小于 50") private String incidentNature; /** * 事故发生地点 */ - @Size(max = 200,message = "事故发生地点最大长度要小于 200") + @Size(max = 200, message = "事故发生地点最大长度要小于 200") private String location; /** @@ -70,8 +81,8 @@ public class AccidentRecords implements Serializable { /** * 直接经济损失(万元) */ - @Size(max = 100,message = "直接经济损失(万元)最大长度要小于 100") - private String directLoss; + @Size(max = 100, message = "直接经济损失(万元)最大长度要小于 100") + private Integer directLoss; /** * 受伤人数 @@ -91,7 +102,7 @@ public class AccidentRecords implements Serializable { /** * 事故起因 */ - @Size(max = 200,message = "事故起因最大长度要小于 200") + @Size(max = 200, message = "事故起因最大长度要小于 200") private String cause; /** @@ -122,7 +133,7 @@ public class AccidentRecords implements Serializable { /** * 填表人 */ - @Size(max = 50,message = "填表人最大长度要小于 50") + @Size(max = 50, message = "填表人最大长度要小于 50") private String creator; /** @@ -133,7 +144,7 @@ public class AccidentRecords implements Serializable { /** * 创建人 */ - @Size(max = 50,message = "创建人最大长度要小于 50") + @Size(max = 50, message = "创建人最大长度要小于 50") private String createdBy; /** @@ -144,7 +155,7 @@ public class AccidentRecords implements Serializable { /** * 修改人 */ - @Size(max = 50,message = "修改人最大长度要小于 50") + @Size(max = 50, message = "修改人最大长度要小于 50") private String updatedBy; /** @@ -159,4 +170,60 @@ public class AccidentRecords implements Serializable { private Integer isDeleted; private static final long serialVersionUID = 1L; + + public AccidentRecords(AccidentRecordsExcel reader, DictionariesService service) { + this.id = IdUtil.getSnowflake(1, 1).nextId(); + this.corpinfoId = reader.getCompanyName(); + this.incidentNumber = reader.getIncidentNumber(); + this.incidentName = reader.getIncidentName(); + this.incidentType = findByName(reader.getIncidentType(), service); + this.incidentLevel = findByName(reader.getIncidentLevel(), service); + this.incidentNature = reader.getIncidentNature(); + this.location = reader.getLocation(); + this.incidentDate = dateFormat(reader.getIncidentDate()); + this.directLoss = reader.getDirectLoss(); + this.injured = reader.getInjured(); + this.fatalities = reader.getFatalities(); + this.seriouslyInjured = reader.getSeriouslyInjured(); + this.cause = reader.getCause(); + this.summary = reader.getSummary(); + this.analysis = reader.getAnalysis(); + this.suggestions = reader.getSuggestions(); + this.measures = reader.getMeasures(); + this.creator = reader.getCreator(); + this.reportDate = dateFormat(reader.getReportDate()); + this.createdBy = Jurisdiction.getUsername(); + this.createdTime = new Date(); + } + + private Integer findByName(String name, DictionariesService service) { + if (StrUtil.isEmpty(name)) { + return null; + } + PageData pageData = new PageData(); + pageData.put("NAME", name); + try { + PageData data = service.findByName(pageData); + return Optional.ofNullable(data).map(pd -> (Integer) pd.get("BIANMA")).orElseThrow(() -> new RuntimeException("未找到字典数据:" + name)); + } catch (RuntimeException e) { + log.error("转换字典失败", e); + throw e; + } catch (Exception e) { + log.error("获取字典失败", e); + throw new RuntimeException("获取字典失败"); + } + } + + private Date dateFormat(String dateStr) { + if (StrUtil.isEmpty(dateStr)) { + return null; + } + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + try { + return formatter.parse(dateStr); + } catch (ParseException e) { + log.error("日期转换失败", e); + throw new RuntimeException("日期转换失败,支持的格式:yyyy-MM-dd HH:mm:ss"); + } + } } \ No newline at end of file diff --git a/src/main/java/com/zcloud/entity/accident/ExcelValue.java b/src/main/java/com/zcloud/entity/accident/ExcelValue.java new file mode 100644 index 00000000..07824770 --- /dev/null +++ b/src/main/java/com/zcloud/entity/accident/ExcelValue.java @@ -0,0 +1,16 @@ +package com.zcloud.entity.accident; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Target(FIELD) +@Retention(RUNTIME) +@Documented +public @interface ExcelValue { + + String value(); +} diff --git a/src/main/java/com/zcloud/entity/accident/dto/AccidentRecordsDto.java b/src/main/java/com/zcloud/entity/accident/dto/AccidentRecordsDto.java deleted file mode 100644 index 4bd85622..00000000 --- a/src/main/java/com/zcloud/entity/accident/dto/AccidentRecordsDto.java +++ /dev/null @@ -1,136 +0,0 @@ -package com.zcloud.entity.accident.dto; - -import cn.hutool.core.date.DatePattern; -import com.zcloud.entity.accident.validated.Create; -import com.zcloud.entity.accident.validated.Update; -import lombok.Data; -import org.hibernate.validator.constraints.Range; -import org.springframework.format.annotation.DateTimeFormat; - -import javax.validation.constraints.NotNull; -import javax.validation.constraints.Null; -import javax.validation.constraints.Size; -import java.io.Serializable; -import java.util.Date; - -@Data -public class AccidentRecordsDto implements Serializable { - - /** - * 主键ID - */ - @NotNull(message = "主键ID不能为null", groups = Update.class) - @Null(message = "主键ID必须null", groups = Create.class) - private Long id; - - /** - * 事故案号 - */ - @Size(max = 50, message = "事故案号最大长度要小于 50", groups = {Create.class, Update.class}) - private String incidentNumber; - - /** - * 事故名称 - */ - @Size(max = 100, message = "事故名称最大长度要小于 100", groups = {Create.class, Update.class}) - private String incidentName; - - /** - * 事故类型 - */ - @Range(min = 1, max = 21, message = "事故类型范围在1-21之间", groups = {Create.class, Update.class}) - private Integer incidentType; - - /** - * 所属公司 - */ - @Size(max = 100, message = "所属公司最大长度要小于 100", groups = {Create.class, Update.class}) - private String companyName; - - /** - * 事故级别 - */ - @Size(max = 50, message = "事故级别最大长度要小于 50", groups = {Create.class, Update.class}) - private String incidentLevel; - - /** - * 事故性质 - */ - @Size(max = 50, message = "事故性质最大长度要小于 50", groups = {Create.class, Update.class}) - private String incidentNature; - - /** - * 事故发生地点 - */ - @Size(max = 200, message = "事故发生地点最大长度要小于 200", groups = {Create.class, Update.class}) - private String location; - - /** - * 事故发生时间 - */ - @DateTimeFormat(pattern = DatePattern.NORM_DATETIME_PATTERN) - private Date incidentDate; - - /** - * 直接经济损失(万元) - */ - @Size(max = 100, message = "直接经济损失(万元)最大长度要小于 100", groups = {Create.class, Update.class}) - private String directLoss; - - /** - * 受伤人数 - */ - private Integer injured; - - /** - * 死亡人数 - */ - private Integer fatalities; - - /** - * 重伤人数 - */ - private Integer seriouslyInjured; - - /** - * 事故起因 - */ - @Size(max = 200, message = "事故起因最大长度要小于 200", groups = {Create.class, Update.class}) - private String cause; - - /** - * 事故概述 - */ - private String summary; - - /** - * 事故照片(可以存储图片路径或使用 BLOB 类型存储图片本身) - */ - private String photos; - - /** - * 原因分析及责任认定 - */ - private String analysis; - - /** - * 考核建议 - */ - private String suggestions; - - /** - * 整改措施 - */ - private String measures; - - /** - * 填表人 - */ - @Size(max = 50, message = "填表人最大长度要小于 50", groups = {Create.class, Update.class}) - private String creator; - - /** - * 报出日期 - */ - private Date reportDate; -} diff --git a/src/main/java/com/zcloud/entity/accident/dto/AccidentRecordsExcel.java b/src/main/java/com/zcloud/entity/accident/dto/AccidentRecordsExcel.java new file mode 100644 index 00000000..1fb330fd --- /dev/null +++ b/src/main/java/com/zcloud/entity/accident/dto/AccidentRecordsExcel.java @@ -0,0 +1,124 @@ +package com.zcloud.entity.accident.dto; + +import com.zcloud.entity.accident.ExcelValue; +import lombok.Data; + +import java.io.Serializable; + +@Data +public class AccidentRecordsExcel implements Serializable { + + /** + * 事故案号 + */ + @ExcelValue(value = " 事故案号") + private String incidentNumber; + + /** + * 事故名称 + */ + @ExcelValue(value = " 事故名称") + private String incidentName; + + /** + * 事故类型 + */ + @ExcelValue(value = " 事故类型") + private String incidentType; + + /** + * 所属公司 + */ + @ExcelValue(value = "所属公司") + private String companyName; + + /** + * 事故级别 + */ + @ExcelValue(value = "事故级别") + private String incidentLevel; + + /** + * 事故性质 + */ + @ExcelValue(value = "事故性质") + private String incidentNature; + + /** + * 事故发生地点 + */ + @ExcelValue(value = "事故发生地点") + private String location; + + /** + * 事故发生时间 + */ + @ExcelValue(value = "事故发生时间") + private String incidentDate; + + /** + * 直接经济损失(万元) + */ + @ExcelValue(value = "直接经济损失(万元)") + private Integer directLoss; + + /** + * 受伤人数 + */ + @ExcelValue(value = "受伤人数") + private Integer injured; + + /** + * 死亡人数 + */ + @ExcelValue(value = "死亡人数") + private Integer fatalities; + + /** + * 重伤人数 + */ + @ExcelValue(value = "重伤人数") + private Integer seriouslyInjured; + + /** + * 事故起因 + */ + @ExcelValue(value = "事故起因") + private String cause; + + /** + * 事故概述 + */ + @ExcelValue(value = "事故概述") + private String summary; + + /** + * 原因分析及责任认定 + */ + @ExcelValue(value = "原因分析及责任认定") + private String analysis; + + /** + * 考核建议 + */ + @ExcelValue(value = "考核建议") + private String suggestions; + + /** + * 整改措施 + */ + @ExcelValue(value = "整改措施") + private String measures; + + /** + * 填表人 + */ + @ExcelValue(value = "填表人") + private String creator; + + /** + * 报出日期 + */ + @ExcelValue(value = "报出日期") + private String reportDate; +} 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 d55f9a9e..db023cbb 100644 --- a/src/main/java/com/zcloud/mapper/datasource/accident/AccidentRecordsMapper.java +++ b/src/main/java/com/zcloud/mapper/datasource/accident/AccidentRecordsMapper.java @@ -3,6 +3,7 @@ package com.zcloud.mapper.datasource.accident; import com.zcloud.entity.Page; import com.zcloud.entity.PageData; import com.zcloud.entity.accident.AccidentRecords; +import com.zcloud.entity.accident.dto.AccidentRecordsExcel; import org.apache.ibatis.annotations.Param; import java.util.List; @@ -18,4 +19,8 @@ public interface AccidentRecordsMapper { int updateById(AccidentRecords accidentRecords); int delete(@Param("ids") Long[] ids); + + List listExcel(PageData pd); + + void saveExcel(AccidentRecordsExcel records, long id, String username); } \ No newline at end of file diff --git a/src/main/java/com/zcloud/service/accident/AccidentRecordsService.java b/src/main/java/com/zcloud/service/accident/AccidentRecordsService.java index 6e005c73..b718fe9e 100644 --- a/src/main/java/com/zcloud/service/accident/AccidentRecordsService.java +++ b/src/main/java/com/zcloud/service/accident/AccidentRecordsService.java @@ -3,7 +3,9 @@ package com.zcloud.service.accident; import com.zcloud.entity.Page; import com.zcloud.entity.PageData; import com.zcloud.entity.accident.AccidentRecords; +import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletResponse; import java.util.List; public interface AccidentRecordsService { @@ -17,4 +19,8 @@ public interface AccidentRecordsService { void update(AccidentRecords bean); void delete(Long[] ids); + + void exportExcel(PageData pd, HttpServletResponse response); + + void importExcel(MultipartFile file); } 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 7329da0b..20593392 100644 --- a/src/main/java/com/zcloud/service/accident/impl/AccidentRecordsServiceImpl.java +++ b/src/main/java/com/zcloud/service/accident/impl/AccidentRecordsServiceImpl.java @@ -1,31 +1,59 @@ package com.zcloud.service.accident.impl; import cn.hutool.core.lang.Assert; +import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.IdUtil; +import cn.hutool.core.util.ReflectUtil; +import cn.hutool.core.util.URLUtil; +import cn.hutool.poi.excel.ExcelReader; +import cn.hutool.poi.excel.ExcelUtil; +import cn.hutool.poi.excel.ExcelWriter; import com.zcloud.entity.Page; import com.zcloud.entity.PageData; import com.zcloud.entity.accident.AccidentRecords; -import com.zcloud.entity.accident.validated.Create; -import com.zcloud.entity.accident.validated.Update; +import com.zcloud.entity.accident.ExcelValue; +import com.zcloud.entity.accident.dto.AccidentRecordsExcel; import com.zcloud.mapper.datasource.accident.AccidentRecordsMapper; import com.zcloud.service.accident.AccidentRecordsService; +import com.zcloud.service.system.DictionariesService; import com.zcloud.util.Jurisdiction; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.ibatis.session.ExecutorType; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.validation.annotation.Validated; +import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.Arrays; import java.util.Date; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +@Slf4j @Service @RequiredArgsConstructor public class AccidentRecordsServiceImpl implements AccidentRecordsService { private final AccidentRecordsMapper accidentRecordsMapper; + private final SqlSessionFactory sqlSessionFactory; + private final DictionariesService dictionariesService; + private static final Map HEADER_ALIAS_CACHE; + + static { + HEADER_ALIAS_CACHE = Arrays.stream(ReflectUtil.getFields(AccidentRecordsExcel.class)) + .filter(field -> field.isAnnotationPresent(ExcelValue.class)) + .collect(Collectors.toMap(Field::getName, field -> field.getAnnotation(ExcelValue.class).value())); + } @Override public List queryPage(Page page) { + page.getPd().put("corpInfoId", Jurisdiction.getCORPINFO_ID()); return accidentRecordsMapper.listPage(page); } @@ -36,7 +64,7 @@ public class AccidentRecordsServiceImpl implements AccidentRecordsService { @Override @Transactional(rollbackFor = Exception.class) - public void save(@Validated(Create.class) AccidentRecords accidentRecords) { + public void save(AccidentRecords accidentRecords) { accidentRecords.setId(IdUtil.getSnowflake(1, 1).nextId()); accidentRecords.setCreatedBy(Jurisdiction.getUsername()); accidentRecords.setCreatedTime(new Date()); @@ -46,7 +74,7 @@ public class AccidentRecordsServiceImpl implements AccidentRecordsService { @Override @Transactional(rollbackFor = Exception.class) - public void update(@Validated(Update.class) AccidentRecords accidentRecords) { + public void update(AccidentRecords accidentRecords) { accidentRecords.setUpdatedBy(Jurisdiction.getUsername()); accidentRecords.setUpdatedTime(new Date()); Assert.isTrue(accidentRecordsMapper.updateById(accidentRecords) == 1, "更新事故记录失败"); @@ -57,4 +85,56 @@ public class AccidentRecordsServiceImpl implements AccidentRecordsService { public void delete(Long[] ids) { Assert.isTrue(accidentRecordsMapper.delete(ids) == ids.length, "删除事故记录失败"); } + + @Override + public void exportExcel(PageData pd, HttpServletResponse response) { + // 查询数据 + pd.put("corpInfoId", Jurisdiction.getCORPINFO_ID()); + List list = accidentRecordsMapper.listExcel(pd); + + // 检查查询结果是否为空 + if (list == null || list.isEmpty()) { + throw new RuntimeException("没有查询到数据"); + } + + // 设置响应头 + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + response.setCharacterEncoding("utf-8"); + response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + URLUtil.encode("事故调查表", CharsetUtil.UTF_8) + ".xlsx"); + + try (ExcelWriter writer = ExcelUtil.getBigWriter()) { + writer.setHeaderAlias(HEADER_ALIAS_CACHE) + .setOnlyAlias(true) + .write(list, true) + .flush(response.getOutputStream()); + } catch (IOException e) { + log.error("导出excel失败", e); + throw new RuntimeException("导出excel失败"); + } + } + + @Override + public void importExcel(MultipartFile file) { + // ExecutorType.BATCH: 这个执行器会批量执行所有更新语句。 + SqlSession sqlSession = null; + try (ExcelReader reader = ExcelUtil.getReader(file.getInputStream())) { + // 标记别名 + HEADER_ALIAS_CACHE.forEach((k, v) -> reader.addHeaderAlias(v, k)); + List recordsExcels = reader.readAll(AccidentRecordsExcel.class); + sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false); + AccidentRecordsMapper mapper = sqlSession.getMapper(AccidentRecordsMapper.class); + recordsExcels.forEach(accidentRecord -> mapper.save(new AccidentRecords(accidentRecord, dictionariesService))); + sqlSession.commit(); + } catch (Exception e) { + if (sqlSession != null) { + sqlSession.rollback(); + } + log.error("导入excel失败", e); + throw new RuntimeException("导入excel失败"); + } finally { + if (sqlSession != null) { + sqlSession.close(); + } + } + } } diff --git a/src/main/resources/mybatis/datasource/accident/AccidentRecordsMapper.xml b/src/main/resources/mybatis/datasource/accident/AccidentRecordsMapper.xml index 1994e374..4f5a3154 100644 --- a/src/main/resources/mybatis/datasource/accident/AccidentRecordsMapper.xml +++ b/src/main/resources/mybatis/datasource/accident/AccidentRecordsMapper.xml @@ -1,68 +1,35 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - id, incident_number, incident_name, incident_type, company_name, incident_level, - incident_nature, `location`, incident_date, direct_loss, injured, fatalities, seriously_injured, - cause, summary, photos, analysis, suggestions, measures, creator, report_date, created_by, - created_time, updated_by, updated_time, is_deleted - @@ -106,8 +75,8 @@ incident_type, - - company_name, + + corpinfo_id, incident_level, @@ -187,8 +156,8 @@ #{incidentType}, - - #{companyName}, + + #{corpinfoId}, #{incidentLevel}, @@ -262,7 +231,7 @@ incident_number = #{incidentNumber}, incident_name = #{incidentName}, incident_type = #{incidentType}, - company_name = #{companyName}, + corpinfo_id = #{corpinfoId}, incident_level = #{incidentLevel}, incident_nature = #{incidentNature}, location = #{location}, @@ -297,4 +266,54 @@ #{item} + + \ No newline at end of file