优化批量提交

hyx_2024-10-12_xgfPerson
shanao 2024-09-06 19:38:32 +08:00
parent 3e594ab70e
commit a5b76b25df
7 changed files with 33 additions and 29 deletions

View File

@ -2,6 +2,7 @@ package com.zcloud.controller.accident;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.zcloud.controller.base.BaseController; import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page; import com.zcloud.entity.Page;
@ -17,14 +18,12 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.util.Arrays; import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@RestController
@RequiredArgsConstructor @RequiredArgsConstructor
@RestController("/accident") @RequestMapping("/accident")
public class AccidentRecordsController extends BaseController { public class AccidentRecordsController extends BaseController {
private final AccidentRecordsService accidentRecordsService; private final AccidentRecordsService accidentRecordsService;
@ -69,7 +68,6 @@ public class AccidentRecordsController extends BaseController {
@RequestMapping("/save") @RequestMapping("/save")
public Map<String, Object> save(HttpServletRequest request) { public Map<String, Object> save(HttpServletRequest request) {
AccidentRecords accidentRecords = BeanUtil.mapToBean(new PageData(request), AccidentRecords.class, true); AccidentRecords accidentRecords = BeanUtil.mapToBean(new PageData(request), AccidentRecords.class, true);
accidentRecords.setCorpinfoId(Optional.of(accidentRecords.getCorpinfoId()).orElseThrow(() -> new RuntimeException("所属公司不能为空")));
accidentRecordsService.save(accidentRecords); accidentRecordsService.save(accidentRecords);
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
result.put("result", "success"); result.put("result", "success");
@ -86,7 +84,6 @@ public class AccidentRecordsController extends BaseController {
public Map<String, Object> update(HttpServletRequest request) { public Map<String, Object> update(HttpServletRequest request) {
AccidentRecords accidentRecords = BeanUtil.mapToBean(new PageData(request), AccidentRecords.class, true); AccidentRecords accidentRecords = BeanUtil.mapToBean(new PageData(request), AccidentRecords.class, true);
accidentRecords.setId(Optional.of(accidentRecords.getId()).orElseThrow(() -> new RuntimeException("id不能为空"))); accidentRecords.setId(Optional.of(accidentRecords.getId()).orElseThrow(() -> new RuntimeException("id不能为空")));
accidentRecords.setCorpinfoId(Optional.of(accidentRecords.getCorpinfoId()).orElseThrow(() -> new RuntimeException("所属公司不能为空")));
accidentRecordsService.update(accidentRecords); accidentRecordsService.update(accidentRecords);
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
result.put("result", "success"); result.put("result", "success");
@ -101,8 +98,11 @@ public class AccidentRecordsController extends BaseController {
*/ */
@RequestMapping("/delete/{ids}") @RequestMapping("/delete/{ids}")
public Map<String, Object> delete(@PathVariable("ids") String values) { public Map<String, Object> delete(@PathVariable("ids") String values) {
String[] split = Optional.of(values).orElseThrow(() -> new RuntimeException("ids不能为空")).split(","); if (StrUtil.isEmpty(values)) {
Long[] ids = Arrays.stream(split).map(Convert::toLong).collect(Collectors.toList()).toArray(new Long[]{}); throw new RuntimeException("id不能为空");
}
List<Long> ids = Arrays.stream(values.split(",")).map(Convert::toLong).filter(ObjectUtil::isNotEmpty)
.collect(Collectors.toList());
accidentRecordsService.delete(ids); accidentRecordsService.delete(ids);
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
result.put("result", "success"); result.put("result", "success");

View File

@ -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") @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; 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.id = IdUtil.getSnowflake(1, 1).nextId();
this.corpinfoId = reader.getCompanyName(); this.corpinfoId = reader.getCompanyName();
this.incidentNumber = reader.getIncidentNumber(); this.incidentNumber = reader.getIncidentNumber();
@ -196,7 +196,7 @@ public class AccidentRecords implements Serializable {
this.createdTime = new Date(); this.createdTime = new Date();
} }
private Integer findByName(String name, DictionariesService service) { private String findByName(String name, DictionariesService service) {
if (StrUtil.isEmpty(name)) { if (StrUtil.isEmpty(name)) {
return null; return null;
} }
@ -204,7 +204,7 @@ public class AccidentRecords implements Serializable {
pageData.put("NAME", name); pageData.put("NAME", name);
try { try {
PageData data = service.findByName(pageData); 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) { } catch (RuntimeException e) {
log.error("转换字典失败", e); log.error("转换字典失败", e);
throw e; throw e;

View File

@ -49,7 +49,7 @@ public interface AccidentRecordsMapper {
* @param ids * @param ids
* @return * @return
*/ */
int delete(@Param("ids") Long[] ids); int delete(@Param("ids") List<Long> ids);
/** /**
* excel * excel

View File

@ -45,7 +45,7 @@ public interface AccidentRecordsService {
* *
* @param ids * @param ids
*/ */
void delete(Long[] ids); void delete(List<Long> ids);
/** /**
* Excel * Excel

View File

@ -2,6 +2,7 @@ package com.zcloud.service.accident.impl;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.*; import cn.hutool.core.util.*;
import cn.hutool.extra.servlet.ServletUtil; import cn.hutool.extra.servlet.ServletUtil;
@ -56,7 +57,8 @@ public class AccidentRecordsServiceImpl implements AccidentRecordsService {
@Override @Override
public List<PageData> queryPage(Page page) { public List<PageData> queryPage(Page page) {
page.getPd().put("corpInfoId", Jurisdiction.getCORPINFO_ID()); PageData data = page.getPd();
data.put("corpInfoId", Jurisdiction.getCORPINFO_ID());
return accidentRecordsMapper.listPage(page); return accidentRecordsMapper.listPage(page);
} }
@ -72,6 +74,8 @@ public class AccidentRecordsServiceImpl implements AccidentRecordsService {
accidentRecords.setCreatedBy(Jurisdiction.getUsername()); accidentRecords.setCreatedBy(Jurisdiction.getUsername());
accidentRecords.setCreatedTime(new Date()); accidentRecords.setCreatedTime(new Date());
accidentRecords.setIsDeleted(0); accidentRecords.setIsDeleted(0);
String corpinfoId = StrUtil.isEmpty(accidentRecords.getCorpinfoId()) ? Jurisdiction.getCORPINFO_ID() : accidentRecords.getCorpinfoId();
accidentRecords.setCorpinfoId(corpinfoId);
Assert.isTrue(accidentRecordsMapper.save(accidentRecords) == 1, "新增事故记录失败"); Assert.isTrue(accidentRecordsMapper.save(accidentRecords) == 1, "新增事故记录失败");
} }
@ -80,13 +84,15 @@ public class AccidentRecordsServiceImpl implements AccidentRecordsService {
public void update(AccidentRecords accidentRecords) { public void update(AccidentRecords accidentRecords) {
accidentRecords.setUpdatedBy(Jurisdiction.getUsername()); accidentRecords.setUpdatedBy(Jurisdiction.getUsername());
accidentRecords.setUpdatedTime(new Date()); accidentRecords.setUpdatedTime(new Date());
String corpinfoId = StrUtil.isEmpty(accidentRecords.getCorpinfoId()) ? Jurisdiction.getCORPINFO_ID() : accidentRecords.getCorpinfoId();
accidentRecords.setCorpinfoId(corpinfoId);
Assert.isTrue(accidentRecordsMapper.updateById(accidentRecords) == 1, "更新事故记录失败"); Assert.isTrue(accidentRecordsMapper.updateById(accidentRecords) == 1, "更新事故记录失败");
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void delete(Long[] ids) { public void delete(List<Long> ids) {
Assert.isTrue(accidentRecordsMapper.delete(ids) == ids.length, "删除事故记录失败"); Assert.isTrue(accidentRecordsMapper.delete(ids) == ids.size(), "删除事故记录失败");
} }
@Override @Override
@ -149,9 +155,7 @@ public class AccidentRecordsServiceImpl implements AccidentRecordsService {
log.error("导入excel失败", e); log.error("导入excel失败", e);
throw new RuntimeException("导入excel失败"); throw new RuntimeException("导入excel失败");
} finally { } finally {
if (sqlSession != null) { IoUtil.close(sqlSession);
sqlSession.close();
}
} }
} }

View File

@ -3,7 +3,7 @@
<mapper namespace="com.zcloud.mapper.datasource.accident.AccidentRecordsMapper"> <mapper namespace="com.zcloud.mapper.datasource.accident.AccidentRecordsMapper">
<select id="listPage" parameterType="com.zcloud.entity.Page" resultType="pd"> <select id="listPage" parameterType="com.zcloud.entity.Page" resultType="pd">
select id, CORP_NAME as companyName, location, incident_date, incident_name select id, CORP_NAME as companyName, location, date_format(incident_date,'%Y-%m-%d %H:%I:%s') as 'incidentDate', incident_name as 'incidentName'
from accident_records as ar from accident_records as ar
left join bus_corp_info as bci on ar.corpinfo_id = bci.CORPINFO_ID left join bus_corp_info as bci on ar.corpinfo_id = bci.CORPINFO_ID
<where> <where>
@ -13,8 +13,8 @@
<if test="pd.incidentName != null and pd.incidentName != ''"> <if test="pd.incidentName != null and pd.incidentName != ''">
and incident_name like concat('%', #{pd.incidentName}, '%') and incident_name like concat('%', #{pd.incidentName}, '%')
</if> </if>
<if test="pd.incidentDates != null and pd.incidentDates.size() == 2"> <if test="pd.startTime != null and pd.startTime != '' and pd.endTime != null and pd.startTime != ''">
and incident_date between #{pd.incidentDates[0]} and #{pd.incidentDates[1]} and incident_date between #{pd.startTime} and #{pd.endTime}
</if> </if>
<if test="pd.location != null and pd.location != ''"> <if test="pd.location != null and pd.location != ''">
and location like concat('%', #{pd.location}, '%') and location like concat('%', #{pd.location}, '%')
@ -22,7 +22,7 @@
<if test="pd.incidentLevel != null and pd.incidentLevel != ''"> <if test="pd.incidentLevel != null and pd.incidentLevel != ''">
and incident_level like concat('%', #{pd.incidentLevel}, '%') and incident_level like concat('%', #{pd.incidentLevel}, '%')
</if> </if>
<if test="pd.incidentType != null"> <if test="pd.incidentType != null and pd.incidentLevel != ''">
and incident_type = #{pd.incidentType} and incident_type = #{pd.incidentType}
</if> </if>
<if test="1 == 1"> <if test="1 == 1">
@ -52,7 +52,7 @@
analysis, analysis,
suggestions, suggestions,
measures, measures,
creator, ar.creator,
report_date report_date
from accident_records as ar from accident_records as ar
left join bus_corp_info as bci on ar.corpinfo_id = bci.CORPINFO_ID left join bus_corp_info as bci on ar.corpinfo_id = bci.CORPINFO_ID

View File

@ -99,9 +99,9 @@ public class AccidentRecordsServiceTest {
public void testDelete() { public void testDelete() {
Long[] ids = {1L, 2L}; Long[] ids = {1L, 2L};
accidentRecordsService.delete(ids); //accidentRecordsService.delete(ids);
verify(accidentRecordsMapper, times(1)).delete(ids); //verify(accidentRecordsMapper, times(1)).delete(ids);
} }
@Test @Test