修复对象转换问题

hyx_2024-10-12_xgfPerson
shanao 2024-09-14 19:43:49 +08:00
parent e932aacc86
commit 415bfd3086
3 changed files with 53 additions and 11 deletions

View File

@ -1,6 +1,7 @@
package com.zcloud.controller.accident;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.zcloud.controller.base.BaseController;
@ -71,8 +72,11 @@ public class AccidentRecordsController extends BaseController {
@ResponseBody
@RequestMapping("/save")
public Map<String, Object> save(HttpServletRequest request) {
AccidentRecords accidentRecords = BeanUtil.mapToBean(new PageData(request), AccidentRecords.class, true);
accidentRecordsService.save(accidentRecords);
PageData pageData = new PageData(request);
if (CollUtil.isEmpty(pageData)){
throw new RuntimeException("参数不能为空");
}
accidentRecordsService.save(pageData);
Map<String, Object> result = new HashMap<>();
result.put("result", "success");
return result;
@ -88,9 +92,13 @@ public class AccidentRecordsController extends BaseController {
@RequestMapping("/update")
public Map<String, Object> update(HttpServletRequest request) {
PageData pageData = new PageData(request);
AccidentRecords accidentRecords = BeanUtil.mapToBean(pageData, AccidentRecords.class, true);
accidentRecords.setId(Optional.of(accidentRecords.getId()).orElseThrow(() -> new RuntimeException("id不能为空")));
accidentRecordsService.update(accidentRecords);
if (CollUtil.isEmpty(pageData)){
throw new RuntimeException("参数不能为空");
}
if (pageData.get("id") != null) {
throw new RuntimeException("id不能为空");
}
accidentRecordsService.update(pageData);
Map<String, Object> result = new HashMap<>();
result.put("result", "success");
return result;

View File

@ -2,7 +2,6 @@ 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 java.util.List;
@ -29,16 +28,16 @@ public interface AccidentRecordsService {
/**
*
*
* @param accidentRecords
* @param pageData
*/
void save(AccidentRecords accidentRecords);
void save(PageData pageData);
/**
*
*
* @param bean
*/
void update(AccidentRecords bean);
void update(PageData pageData);
/**
*

View File

@ -1,5 +1,6 @@
package com.zcloud.service.accident.impl;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
@ -58,7 +59,8 @@ public class AccidentRecordsServiceImpl implements AccidentRecordsService {
@Override
@Transactional(rollbackFor = Exception.class)
public void save(AccidentRecords accidentRecords) {
public void save(PageData pageData) {
AccidentRecords accidentRecords = new AccidentRecords();
accidentRecords.setId(IdUtil.fastSimpleUUID());
accidentRecords.setCreatedBy(Jurisdiction.getUsername());
Date date = new Date();
@ -71,12 +73,45 @@ public class AccidentRecordsServiceImpl implements AccidentRecordsService {
accidentRecords.setIsDeleted(0);
String corpinfoId = StrUtil.isEmpty(accidentRecords.getCorpinfoId()) ? Jurisdiction.getCORPINFO_ID() : accidentRecords.getCorpinfoId();
accidentRecords.setCorpinfoId(corpinfoId);
accidentRecords.setIncidentNumber(pageData.getString("incidentNumber"));
accidentRecords.setIncidentName(pageData.getString("incidentName"));
accidentRecords.setIncidentType(pageData.getString("incidentType"));
accidentRecords.setIncidentLevel(pageData.getString("incidentLevel"));
accidentRecords.setLocation(pageData.getString("location"));
accidentRecords.setIncidentDate(DateUtil.parse(pageData.getString("incidentDate"), DatePattern.UTC_MS_PATTERN));
accidentRecords.setDirectLoss(Convert.toInt(pageData.get("directLoss")));
accidentRecords.setInjured(Convert.toInt(pageData.get("injured")));
accidentRecords.setSeriouslyInjured(Convert.toInt(pageData.get("seriouslyInjured")));
accidentRecords.setCause(pageData.getString("cause"));
accidentRecords.setSummary(pageData.getString("summary"));
accidentRecords.setAnalysis(pageData.getString("analysis"));
accidentRecords.setSuggestions(pageData.getString("suggestions"));
accidentRecords.setMeasures(pageData.getString("measures"));
accidentRecords.setCreator(pageData.getString("creator"));
accidentRecords.setReportDate(DateUtil.parse(pageData.getString("reportDate"), DatePattern.UTC_MS_PATTERN));
Assert.isTrue(accidentRecordsMapper.save(accidentRecords) == 1, "新增事故记录失败");
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(AccidentRecords accidentRecords) {
public void update(PageData pageData) {
AccidentRecords accidentRecords = new AccidentRecords();
accidentRecords.setIncidentNumber(pageData.getString("incidentNumber"));
accidentRecords.setIncidentName(pageData.getString("incidentName"));
accidentRecords.setIncidentType(pageData.getString("incidentType"));
accidentRecords.setIncidentLevel(pageData.getString("incidentLevel"));
accidentRecords.setLocation(pageData.getString("location"));
accidentRecords.setIncidentDate(DateUtil.parse(pageData.getString("incidentDate"), DatePattern.UTC_MS_PATTERN));
accidentRecords.setDirectLoss(Convert.toInt(pageData.get("directLoss")));
accidentRecords.setInjured(Convert.toInt(pageData.get("injured")));
accidentRecords.setSeriouslyInjured(Convert.toInt(pageData.get("seriouslyInjured")));
accidentRecords.setCause(pageData.getString("cause"));
accidentRecords.setSummary(pageData.getString("summary"));
accidentRecords.setAnalysis(pageData.getString("analysis"));
accidentRecords.setSuggestions(pageData.getString("suggestions"));
accidentRecords.setMeasures(pageData.getString("measures"));
accidentRecords.setCreator(pageData.getString("creator"));
accidentRecords.setReportDate(DateUtil.parse(pageData.getString("reportDate"), DatePattern.UTC_MS_PATTERN));
accidentRecords.setUpdatedBy(Jurisdiction.getUsername());
accidentRecords.setUpdatedTime(new Date());
String corpinfoId = StrUtil.isEmpty(accidentRecords.getCorpinfoId()) ? Jurisdiction.getCORPINFO_ID() : accidentRecords.getCorpinfoId();