|
|
@@ -0,0 +1,577 @@
|
|
|
+package com.ruoyi.project.ps.inspection.controller;
|
|
|
+
|
|
|
+import com.ruoyi.framework.web.controller.BaseController;
|
|
|
+import com.ruoyi.framework.web.domain.AjaxResult;
|
|
|
+import com.ruoyi.framework.web.page.TableDataInfo;
|
|
|
+import com.ruoyi.project.ps.inspection.domain.*;
|
|
|
+import com.ruoyi.project.ps.inspection.dto.InspectionQueryRequest;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 巡检记录App共通Controller
|
|
|
+ * 提供给移动端App使用的统一接口
|
|
|
+ *
|
|
|
+ * @author system
|
|
|
+ * @date 2025-01-XX
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/app/ps/inspection")
|
|
|
+public class TPsInspectionAppController extends BaseController {
|
|
|
+
|
|
|
+ // 注入各个Controller
|
|
|
+ @Autowired
|
|
|
+ private TPsInspectionAlarmController alarmController;
|
|
|
+ @Autowired
|
|
|
+ private TPsInspectionExtinguisherController extinguisherController;
|
|
|
+ @Autowired
|
|
|
+ private TPsInspectionHydrantController hydrantController;
|
|
|
+ @Autowired
|
|
|
+ private TPsInspectionFiredoorController firedoorController;
|
|
|
+ @Autowired
|
|
|
+ private TPsInspectionLiftController liftController;
|
|
|
+ @Autowired
|
|
|
+ private TPsInspectionCabinetController cabinetController;
|
|
|
+ @Autowired
|
|
|
+ private TPsInspectionCallsystemController callsystemController;
|
|
|
+ @Autowired
|
|
|
+ private TPsInspectionFgsController fgsController;
|
|
|
+ @Autowired
|
|
|
+ private TPsInspectionCurtainController curtainController;
|
|
|
+ @Autowired
|
|
|
+ private TPsInspectionSteamController steamController;
|
|
|
+ @Autowired
|
|
|
+ private TPsInspectionMonitorController monitorController;
|
|
|
+ @Autowired
|
|
|
+ private TPsInspectionCofferController cofferController;
|
|
|
+ @Autowired
|
|
|
+ private TPsInspectionSumpController sumpController;
|
|
|
+ @Autowired
|
|
|
+ private TPsInspectionPitController pitController;
|
|
|
+ @Autowired
|
|
|
+ private TPsInspectionRainvalveController rainvalveController;
|
|
|
+ @Autowired
|
|
|
+ private TPsInspectionVentvalveController ventvalveController;
|
|
|
+ @Autowired
|
|
|
+ private TPsInspectionUtilitystationController utilitystationController;
|
|
|
+ @Autowired
|
|
|
+ private TPsInspectionEyewashController eyewashController;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询巡检记录列表(App接口)
|
|
|
+ *
|
|
|
+ * @param request 查询请求参数
|
|
|
+ * @return 查询结果
|
|
|
+ */
|
|
|
+ @PostMapping("/list")
|
|
|
+ public AjaxResult list(@RequestBody InspectionQueryRequest request) {
|
|
|
+ if (request.getType() == null || request.getType().isEmpty()) {
|
|
|
+ return AjaxResult.error("巡检记录类型不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ TableDataInfo dataTable = queryByType(request);
|
|
|
+ return AjaxResult.success(dataTable);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("查询巡检记录失败", e);
|
|
|
+ return AjaxResult.error("查询失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改巡检记录(App接口)
|
|
|
+ *
|
|
|
+ * @param request 修改请求参数(包含type和具体的数据对象)
|
|
|
+ * @return 修改结果
|
|
|
+ */
|
|
|
+ @PostMapping("/update")
|
|
|
+ public AjaxResult update(@RequestBody Map<String, Object> request) {
|
|
|
+ String type = (String) request.get("type");
|
|
|
+ if (type == null || type.isEmpty()) {
|
|
|
+ return AjaxResult.error("巡检记录类型不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ return updateByType(type, request);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("修改巡检记录失败", e);
|
|
|
+ return AjaxResult.error("修改失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据类型查询数据,调用对应Controller的list方法
|
|
|
+ */
|
|
|
+ private TableDataInfo queryByType(InspectionQueryRequest request) {
|
|
|
+ String type = request.getType().toLowerCase();
|
|
|
+
|
|
|
+ switch (type) {
|
|
|
+ case "alarm":
|
|
|
+ TPsInspectionAlarm alarm = new TPsInspectionAlarm();
|
|
|
+ copyCommonFields(request, alarm);
|
|
|
+ return alarmController.list(alarm);
|
|
|
+
|
|
|
+ case "extinguisher":
|
|
|
+ TPsInspectionExtinguisher extinguisher = new TPsInspectionExtinguisher();
|
|
|
+ copyCommonFields(request, extinguisher);
|
|
|
+ return extinguisherController.list(extinguisher);
|
|
|
+
|
|
|
+ case "hydrant":
|
|
|
+ TPsInspectionHydrant hydrant = new TPsInspectionHydrant();
|
|
|
+ copyCommonFields(request, hydrant);
|
|
|
+ return hydrantController.list(hydrant);
|
|
|
+
|
|
|
+ case "firedoor":
|
|
|
+ TPsInspectionFiredoor firedoor = new TPsInspectionFiredoor();
|
|
|
+ copyCommonFields(request, firedoor);
|
|
|
+ return firedoorController.list(firedoor);
|
|
|
+
|
|
|
+ case "lift":
|
|
|
+ TPsInspectionLift lift = new TPsInspectionLift();
|
|
|
+ copyCommonFields(request, lift);
|
|
|
+ return liftController.list(lift);
|
|
|
+
|
|
|
+ case "cabinet":
|
|
|
+ TPsInspectionCabinet cabinet = new TPsInspectionCabinet();
|
|
|
+ copyCommonFields(request, cabinet);
|
|
|
+ return cabinetController.list(cabinet);
|
|
|
+
|
|
|
+ case "callsystem":
|
|
|
+ TPsInspectionCallsystem callsystem = new TPsInspectionCallsystem();
|
|
|
+ copyCommonFields(request, callsystem);
|
|
|
+ return callsystemController.list(callsystem);
|
|
|
+
|
|
|
+ case "fgs":
|
|
|
+ TPsInspectionFgs fgs = new TPsInspectionFgs();
|
|
|
+ copyCommonFields(request, fgs);
|
|
|
+ return fgsController.list(fgs);
|
|
|
+
|
|
|
+ case "curtain":
|
|
|
+ TPsInspectionCurtain curtain = new TPsInspectionCurtain();
|
|
|
+ copyCommonFields(request, curtain);
|
|
|
+ return curtainController.list(curtain);
|
|
|
+
|
|
|
+ case "steam":
|
|
|
+ TPsInspectionSteam steam = new TPsInspectionSteam();
|
|
|
+ copyCommonFields(request, steam);
|
|
|
+ return steamController.list(steam);
|
|
|
+
|
|
|
+ case "monitor":
|
|
|
+ TPsInspectionMonitor monitor = new TPsInspectionMonitor();
|
|
|
+ copyCommonFields(request, monitor);
|
|
|
+ return monitorController.list(monitor);
|
|
|
+
|
|
|
+ case "coffer":
|
|
|
+ TPsInspectionCoffer coffer = new TPsInspectionCoffer();
|
|
|
+ copyCommonFields(request, coffer);
|
|
|
+ return cofferController.list(coffer);
|
|
|
+
|
|
|
+ case "sump":
|
|
|
+ TPsInspectionSump sump = new TPsInspectionSump();
|
|
|
+ copyCommonFields(request, sump);
|
|
|
+ return sumpController.list(sump);
|
|
|
+
|
|
|
+ case "pit":
|
|
|
+ TPsInspectionPit pit = new TPsInspectionPit();
|
|
|
+ copyCommonFields(request, pit);
|
|
|
+ return pitController.list(pit);
|
|
|
+
|
|
|
+ case "rainvalve":
|
|
|
+ TPsInspectionRainvalve rainvalve = new TPsInspectionRainvalve();
|
|
|
+ copyCommonFields(request, rainvalve);
|
|
|
+ return rainvalveController.list(rainvalve);
|
|
|
+
|
|
|
+ case "ventvalve":
|
|
|
+ TPsInspectionVentvalve ventvalve = new TPsInspectionVentvalve();
|
|
|
+ copyCommonFields(request, ventvalve);
|
|
|
+ return ventvalveController.list(ventvalve);
|
|
|
+
|
|
|
+ case "utilitystation":
|
|
|
+ TPsInspectionUtilitystation utilitystation = new TPsInspectionUtilitystation();
|
|
|
+ copyCommonFields(request, utilitystation);
|
|
|
+ return utilitystationController.list(utilitystation);
|
|
|
+
|
|
|
+ case "eyewash":
|
|
|
+ TPsInspectionEyewash eyewash = new TPsInspectionEyewash();
|
|
|
+ copyCommonFields(request, eyewash);
|
|
|
+ return eyewashController.list(eyewash);
|
|
|
+
|
|
|
+ default:
|
|
|
+ throw new IllegalArgumentException("不支持的巡检记录类型:" + type);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据类型更新数据,调用对应Controller的edit方法
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private AjaxResult updateByType(String type, Map<String, Object> request) {
|
|
|
+ type = type.toLowerCase();
|
|
|
+ Map<String, Object> data = (Map<String, Object>) request.get("data");
|
|
|
+
|
|
|
+ if (data == null) {
|
|
|
+ throw new IllegalArgumentException("更新数据不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ switch (type) {
|
|
|
+ case "alarm":
|
|
|
+ TPsInspectionAlarm alarm = convertToAlarm(data);
|
|
|
+ return alarmController.edit(alarm);
|
|
|
+
|
|
|
+ case "extinguisher":
|
|
|
+ TPsInspectionExtinguisher extinguisher = convertToExtinguisher(data);
|
|
|
+ return extinguisherController.edit(extinguisher);
|
|
|
+
|
|
|
+ case "hydrant":
|
|
|
+ TPsInspectionHydrant hydrant = convertToHydrant(data);
|
|
|
+ return hydrantController.edit(hydrant);
|
|
|
+
|
|
|
+ case "firedoor":
|
|
|
+ TPsInspectionFiredoor firedoor = convertToFiredoor(data);
|
|
|
+ return firedoorController.edit(firedoor);
|
|
|
+
|
|
|
+ case "lift":
|
|
|
+ TPsInspectionLift lift = convertToLift(data);
|
|
|
+ return liftController.edit(lift);
|
|
|
+
|
|
|
+ case "cabinet":
|
|
|
+ TPsInspectionCabinet cabinet = convertToCabinet(data);
|
|
|
+ return cabinetController.edit(cabinet);
|
|
|
+
|
|
|
+ case "callsystem":
|
|
|
+ TPsInspectionCallsystem callsystem = convertToCallsystem(data);
|
|
|
+ return callsystemController.edit(callsystem);
|
|
|
+
|
|
|
+ case "fgs":
|
|
|
+ TPsInspectionFgs fgs = convertToFgs(data);
|
|
|
+ return fgsController.edit(fgs);
|
|
|
+
|
|
|
+ case "curtain":
|
|
|
+ TPsInspectionCurtain curtain = convertToCurtain(data);
|
|
|
+ return curtainController.edit(curtain);
|
|
|
+
|
|
|
+ case "steam":
|
|
|
+ TPsInspectionSteam steam = convertToSteam(data);
|
|
|
+ return steamController.edit(steam);
|
|
|
+
|
|
|
+ case "monitor":
|
|
|
+ TPsInspectionMonitor monitor = convertToMonitor(data);
|
|
|
+ return monitorController.edit(monitor);
|
|
|
+
|
|
|
+ case "coffer":
|
|
|
+ TPsInspectionCoffer coffer = convertToCoffer(data);
|
|
|
+ return cofferController.edit(coffer);
|
|
|
+
|
|
|
+ case "sump":
|
|
|
+ TPsInspectionSump sump = convertToSump(data);
|
|
|
+ return sumpController.edit(sump);
|
|
|
+
|
|
|
+ case "pit":
|
|
|
+ TPsInspectionPit pit = convertToPit(data);
|
|
|
+ return pitController.edit(pit);
|
|
|
+
|
|
|
+ case "rainvalve":
|
|
|
+ TPsInspectionRainvalve rainvalve = convertToRainvalve(data);
|
|
|
+ return rainvalveController.edit(rainvalve);
|
|
|
+
|
|
|
+ case "ventvalve":
|
|
|
+ TPsInspectionVentvalve ventvalve = convertToVentvalve(data);
|
|
|
+ return ventvalveController.edit(ventvalve);
|
|
|
+
|
|
|
+ case "utilitystation":
|
|
|
+ TPsInspectionUtilitystation utilitystation = convertToUtilitystation(data);
|
|
|
+ return utilitystationController.edit(utilitystation);
|
|
|
+
|
|
|
+ case "eyewash":
|
|
|
+ TPsInspectionEyewash eyewash = convertToEyewash(data);
|
|
|
+ return eyewashController.edit(eyewash);
|
|
|
+
|
|
|
+ default:
|
|
|
+ throw new IllegalArgumentException("不支持的巡检记录类型:" + type);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 复制通用字段到实体对象
|
|
|
+ */
|
|
|
+ private void copyCommonFields(InspectionQueryRequest request, Object entity) {
|
|
|
+ if (request.getId() != null) {
|
|
|
+ setFieldValue(entity, "id", request.getId());
|
|
|
+ }
|
|
|
+ if (request.getDevNo() != null) {
|
|
|
+ setFieldValue(entity, "devNo", request.getDevNo());
|
|
|
+ }
|
|
|
+ if (request.getPosition() != null) {
|
|
|
+ setFieldValue(entity, "position", request.getPosition());
|
|
|
+ }
|
|
|
+ if (request.getMonth() != null) {
|
|
|
+ setFieldValue(entity, "month", request.getMonth());
|
|
|
+ }
|
|
|
+ if (request.getYear() != null) {
|
|
|
+ setFieldValue(entity, "year", request.getYear());
|
|
|
+ }
|
|
|
+ if (request.getPlanId() != null) {
|
|
|
+ setFieldValue(entity, "planId", request.getPlanId());
|
|
|
+ }
|
|
|
+ if (request.getDeptId() != null) {
|
|
|
+ setFieldValue(entity, "deptId", request.getDeptId());
|
|
|
+ }
|
|
|
+ if (request.getCheckStatus() != null) {
|
|
|
+ setFieldValue(entity, "checkStatus", request.getCheckStatus());
|
|
|
+ }
|
|
|
+ if (request.getIssuesStatus() != null) {
|
|
|
+ setFieldValue(entity, "issuesStatus", request.getIssuesStatus());
|
|
|
+ }
|
|
|
+ if (request.getIssuesFlag() != null) {
|
|
|
+ setFieldValue(entity, "issuesFlag", request.getIssuesFlag());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用反射设置字段值
|
|
|
+ */
|
|
|
+ private void setFieldValue(Object obj, String fieldName, Object value) {
|
|
|
+ try {
|
|
|
+ java.lang.reflect.Field field = obj.getClass().getDeclaredField(fieldName);
|
|
|
+ field.setAccessible(true);
|
|
|
+ field.set(obj, value);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 忽略不存在的字段
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为Alarm对象
|
|
|
+ */
|
|
|
+ private TPsInspectionAlarm convertToAlarm(Map<String, Object> data) {
|
|
|
+ TPsInspectionAlarm alarm = new TPsInspectionAlarm();
|
|
|
+ convertMapToObject(data, alarm);
|
|
|
+ return alarm;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为Extinguisher对象
|
|
|
+ */
|
|
|
+ private TPsInspectionExtinguisher convertToExtinguisher(Map<String, Object> data) {
|
|
|
+ TPsInspectionExtinguisher extinguisher = new TPsInspectionExtinguisher();
|
|
|
+ convertMapToObject(data, extinguisher);
|
|
|
+ return extinguisher;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为Hydrant对象
|
|
|
+ */
|
|
|
+ private TPsInspectionHydrant convertToHydrant(Map<String, Object> data) {
|
|
|
+ TPsInspectionHydrant hydrant = new TPsInspectionHydrant();
|
|
|
+ convertMapToObject(data, hydrant);
|
|
|
+ return hydrant;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为Firedoor对象
|
|
|
+ */
|
|
|
+ private TPsInspectionFiredoor convertToFiredoor(Map<String, Object> data) {
|
|
|
+ TPsInspectionFiredoor firedoor = new TPsInspectionFiredoor();
|
|
|
+ convertMapToObject(data, firedoor);
|
|
|
+ return firedoor;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为Lift对象
|
|
|
+ */
|
|
|
+ private TPsInspectionLift convertToLift(Map<String, Object> data) {
|
|
|
+ TPsInspectionLift lift = new TPsInspectionLift();
|
|
|
+ convertMapToObject(data, lift);
|
|
|
+ return lift;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为Cabinet对象
|
|
|
+ */
|
|
|
+ private TPsInspectionCabinet convertToCabinet(Map<String, Object> data) {
|
|
|
+ TPsInspectionCabinet cabinet = new TPsInspectionCabinet();
|
|
|
+ convertMapToObject(data, cabinet);
|
|
|
+ return cabinet;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为Callsystem对象
|
|
|
+ */
|
|
|
+ private TPsInspectionCallsystem convertToCallsystem(Map<String, Object> data) {
|
|
|
+ TPsInspectionCallsystem callsystem = new TPsInspectionCallsystem();
|
|
|
+ convertMapToObject(data, callsystem);
|
|
|
+ return callsystem;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为Fgs对象
|
|
|
+ */
|
|
|
+ private TPsInspectionFgs convertToFgs(Map<String, Object> data) {
|
|
|
+ TPsInspectionFgs fgs = new TPsInspectionFgs();
|
|
|
+ convertMapToObject(data, fgs);
|
|
|
+ return fgs;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为Curtain对象
|
|
|
+ */
|
|
|
+ private TPsInspectionCurtain convertToCurtain(Map<String, Object> data) {
|
|
|
+ TPsInspectionCurtain curtain = new TPsInspectionCurtain();
|
|
|
+ convertMapToObject(data, curtain);
|
|
|
+ return curtain;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为Steam对象
|
|
|
+ */
|
|
|
+ private TPsInspectionSteam convertToSteam(Map<String, Object> data) {
|
|
|
+ TPsInspectionSteam steam = new TPsInspectionSteam();
|
|
|
+ convertMapToObject(data, steam);
|
|
|
+ return steam;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为Monitor对象
|
|
|
+ */
|
|
|
+ private TPsInspectionMonitor convertToMonitor(Map<String, Object> data) {
|
|
|
+ TPsInspectionMonitor monitor = new TPsInspectionMonitor();
|
|
|
+ convertMapToObject(data, monitor);
|
|
|
+ return monitor;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为Coffer对象
|
|
|
+ */
|
|
|
+ private TPsInspectionCoffer convertToCoffer(Map<String, Object> data) {
|
|
|
+ TPsInspectionCoffer coffer = new TPsInspectionCoffer();
|
|
|
+ convertMapToObject(data, coffer);
|
|
|
+ return coffer;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为Sump对象
|
|
|
+ */
|
|
|
+ private TPsInspectionSump convertToSump(Map<String, Object> data) {
|
|
|
+ TPsInspectionSump sump = new TPsInspectionSump();
|
|
|
+ convertMapToObject(data, sump);
|
|
|
+ return sump;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为Pit对象
|
|
|
+ */
|
|
|
+ private TPsInspectionPit convertToPit(Map<String, Object> data) {
|
|
|
+ TPsInspectionPit pit = new TPsInspectionPit();
|
|
|
+ convertMapToObject(data, pit);
|
|
|
+ return pit;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为Rainvalve对象
|
|
|
+ */
|
|
|
+ private TPsInspectionRainvalve convertToRainvalve(Map<String, Object> data) {
|
|
|
+ TPsInspectionRainvalve rainvalve = new TPsInspectionRainvalve();
|
|
|
+ convertMapToObject(data, rainvalve);
|
|
|
+ return rainvalve;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为Ventvalve对象
|
|
|
+ */
|
|
|
+ private TPsInspectionVentvalve convertToVentvalve(Map<String, Object> data) {
|
|
|
+ TPsInspectionVentvalve ventvalve = new TPsInspectionVentvalve();
|
|
|
+ convertMapToObject(data, ventvalve);
|
|
|
+ return ventvalve;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为Utilitystation对象
|
|
|
+ */
|
|
|
+ private TPsInspectionUtilitystation convertToUtilitystation(Map<String, Object> data) {
|
|
|
+ TPsInspectionUtilitystation utilitystation = new TPsInspectionUtilitystation();
|
|
|
+ convertMapToObject(data, utilitystation);
|
|
|
+ return utilitystation;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为Eyewash对象
|
|
|
+ */
|
|
|
+ private TPsInspectionEyewash convertToEyewash(Map<String, Object> data) {
|
|
|
+ TPsInspectionEyewash eyewash = new TPsInspectionEyewash();
|
|
|
+ convertMapToObject(data, eyewash);
|
|
|
+ return eyewash;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为对象(使用反射)
|
|
|
+ */
|
|
|
+ private void convertMapToObject(Map<String, Object> map, Object obj) {
|
|
|
+ for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
|
+ String key = entry.getKey();
|
|
|
+ Object value = entry.getValue();
|
|
|
+
|
|
|
+ // 将下划线命名转换为驼峰命名
|
|
|
+ String fieldName = toCamelCase(key);
|
|
|
+
|
|
|
+ try {
|
|
|
+ java.lang.reflect.Field field = obj.getClass().getDeclaredField(fieldName);
|
|
|
+ field.setAccessible(true);
|
|
|
+
|
|
|
+ // 类型转换
|
|
|
+ if (value != null) {
|
|
|
+ Class<?> fieldType = field.getType();
|
|
|
+ if (fieldType == Long.class || fieldType == long.class) {
|
|
|
+ if (value instanceof Number) {
|
|
|
+ field.set(obj, ((Number) value).longValue());
|
|
|
+ } else if (value instanceof String) {
|
|
|
+ field.set(obj, Long.parseLong((String) value));
|
|
|
+ }
|
|
|
+ } else if (fieldType == Integer.class || fieldType == int.class) {
|
|
|
+ if (value instanceof Number) {
|
|
|
+ field.set(obj, ((Number) value).intValue());
|
|
|
+ } else if (value instanceof String) {
|
|
|
+ field.set(obj, Integer.parseInt((String) value));
|
|
|
+ }
|
|
|
+ } else if (fieldType == String.class) {
|
|
|
+ field.set(obj, value.toString());
|
|
|
+ } else {
|
|
|
+ field.set(obj, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 忽略不存在的字段
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下划线命名转驼峰命名
|
|
|
+ */
|
|
|
+ private String toCamelCase(String str) {
|
|
|
+ if (str == null || str.isEmpty()) {
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ boolean nextUpperCase = false;
|
|
|
+ for (int i = 0; i < str.length(); i++) {
|
|
|
+ char c = str.charAt(i);
|
|
|
+ if (c == '_') {
|
|
|
+ nextUpperCase = true;
|
|
|
+ } else {
|
|
|
+ if (nextUpperCase) {
|
|
|
+ result.append(Character.toUpperCase(c));
|
|
|
+ nextUpperCase = false;
|
|
|
+ } else {
|
|
|
+ result.append(c);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|