Ver código fonte

代码提交 修改产品信息

cxd 6 meses atrás
pai
commit
d7f18dfa30

+ 202 - 0
gqy-common/src/main/java/com/gqy/common/utils/bean/BeanUtils.java

@@ -3,7 +3,10 @@ package com.gqy.common.utils.bean;
 import java.beans.BeanInfo;
 import java.beans.IntrospectionException;
 import java.beans.Introspector;
+import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.math.BigDecimal;
+import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -32,6 +35,205 @@ public class BeanUtils extends org.springframework.beans.BeanUtils
     /** * 匹配setter方法的正则表达式 */
     private static final Pattern SET_PATTERN = Pattern.compile("set(\\p{javaUpperCase}\\w*)");
 
+
+    /**
+     * Map属性填充到Bean对象
+     */
+    public static <T> T mapToBean(Map<String, Object> map, T bean) {
+        if (map == null || bean == null) {
+            return bean;
+        }
+
+        try {
+            Class<?> beanClass = bean.getClass();
+            Field[] fields = beanClass.getDeclaredFields();
+
+            for (Field field : fields) {
+                field.setAccessible(true);
+                String fieldName = field.getName();
+                Object value = map.get(fieldName);
+
+                // 尝试下划线命名
+                if (value == null) {
+                    value = map.get(camelToUnderscore(fieldName));
+                }
+
+                if (value != null) {
+                    Object convertedValue = convertValueToFieldType(value, field.getType());
+                    if (convertedValue != null) {
+                        field.set(bean, convertedValue);
+                    }
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return bean;
+    }
+
+    /**
+     * 转换值到指定的类型,增强的数值处理
+     */
+    private static Object convertValueToFieldType(Object value, Class<?> targetType) {
+        if (value == null) {
+            return null;
+        }
+
+        try {
+            // BigDecimal类型转换增强处理
+            if (targetType == BigDecimal.class) {
+                return toBigDecimal(value);
+            }
+
+            // 其他基本类型转换
+            if (targetType == Integer.class || targetType == int.class) {
+                return toInteger(value);
+            }
+            if (targetType == Long.class || targetType == long.class) {
+                return toLong(value);
+            }
+            if (targetType == Double.class || targetType == double.class) {
+                return toDouble(value);
+            }
+            if (targetType == String.class) {
+                return value.toString();
+            }
+
+            // 如果值的类型就是目标类型,直接返回
+            if (targetType.isAssignableFrom(value.getClass())) {
+                return value;
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    /**
+     * 增强的BigDecimal转换,处理各种可能的格式
+     */
+    public static BigDecimal toBigDecimal(Object value) {
+        if (value == null) {
+            return BigDecimal.ZERO;
+        }
+
+        if (value instanceof BigDecimal) {
+            return (BigDecimal) value;
+        }
+
+        String strValue = value.toString().trim();
+
+        // 如果为空字符串,返回0
+        if (strValue.isEmpty()) {
+            return BigDecimal.ZERO;
+        }
+
+        try {
+            // 处理百分比格式
+            if (strValue.endsWith("%")) {
+                strValue = strValue.substring(0, strValue.length() - 1);
+                return new BigDecimal(strValue).divide(new BigDecimal("100"));
+            }
+
+            // 处理金额格式,移除货币符号和逗号
+            strValue = strValue.replaceAll("[¥$,]", "")
+                    .replaceAll("\\s+", "");
+
+            // 处理科学计数法
+            if (strValue.toLowerCase().contains("e")) {
+                return new BigDecimal(new BigDecimal(strValue).toPlainString());
+            }
+
+            // 处理普通数字格式
+            return new BigDecimal(strValue);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return BigDecimal.ZERO;
+        }
+    }
+
+    /**
+     * Integer转换
+     */
+    private static Integer toInteger(Object value) {
+        if (value == null) {
+            return null;
+        }
+        if (value instanceof Number) {
+            return ((Number) value).intValue();
+        }
+        try {
+            String strValue = value.toString().trim();
+            if (strValue.contains(".")) {
+                return (int) Double.parseDouble(strValue);
+            }
+            return Integer.parseInt(strValue);
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    /**
+     * Long转换
+     */
+    private static Long toLong(Object value) {
+        if (value == null) {
+            return null;
+        }
+        if (value instanceof Number) {
+            return ((Number) value).longValue();
+        }
+        try {
+            String strValue = value.toString().trim();
+            if (strValue.contains(".")) {
+                return (long) Double.parseDouble(strValue);
+            }
+            return Long.parseLong(strValue);
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    /**
+     * Double转换
+     */
+    private static Double toDouble(Object value) {
+        if (value == null) {
+            return null;
+        }
+        if (value instanceof Number) {
+            return ((Number) value).doubleValue();
+        }
+        try {
+            return Double.parseDouble(value.toString().trim());
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    /**
+     * 驼峰转下划线
+     */
+    private static String camelToUnderscore(String str) {
+        if (str == null || str.isEmpty()) {
+            return str;
+        }
+        StringBuilder sb = new StringBuilder();
+        for (int i = 0; i < str.length(); i++) {
+            char c = str.charAt(i);
+            if (Character.isUpperCase(c)) {
+                if (i > 0) {
+                    sb.append('_');
+                }
+                sb.append(Character.toLowerCase(c));
+            } else {
+                sb.append(c);
+            }
+        }
+        return sb.toString();
+    }
+
+
     /**
      * Bean属性复制工具方法。
      *

+ 5 - 5
gqy-system/src/main/java/com/gqy/document/service/impl/DcProductServiceImpl.java

@@ -53,19 +53,19 @@ public class DcProductServiceImpl implements IDcProductService {
     public int add(Map<String, Object> params) {
 
         try {
-            //Map<String, Object> content = params.get("content");
+            Map<String, Object> content = (Map<String, Object>) params.get("content");
 
             DcProduct product = new DcProduct();
             // 拷贝参数
-            BeanUtils.copyProperties(params, product);
+            BeanUtils.mapToBean(content,product );
             // 保存
             dcProductMapper.insertDcProduct(product);
             // 产品id
             int dcpId = product.getDcp_id();
             //系统类别id
-            int  sys_no = Integer.parseInt((String) params.get("sys_no"));
+            int  sys_no = Integer.parseInt(content.get("sys_no").toString());
             //工程id
-            int wr_id = Integer.parseInt((String) params.get("wr_id"));
+            int wr_id = Integer.parseInt(content.get("wr_id").toString());
 
 
             // 工程系统表 保存信息 dc_work_system
@@ -78,7 +78,7 @@ public class DcProductServiceImpl implements IDcProductService {
             // 系统产品表 保存信息 dc_system_product
             DcSystemProduct systemProduct = new DcSystemProduct();
             systemProduct.setSys_no(sys_no);
-            systemProduct.setP_id(dcpId);
+            systemProduct.setDcp_id(dcpId);
             dcSystemProductMapper.insertDcSystemProduct(systemProduct);
 
             // 规格管理

+ 0 - 6
gqy-system/src/main/resources/mapper/document/DcProductMapper.xml

@@ -111,8 +111,6 @@
             <if test="dcp_cost_price != null">dcp_cost_price,</if>
             <if test="dcm_id != null">dcm_id,</if>
             <if test="dcp_oa_code != null">dcp_oa_code,</if>
-            <if test="createBy != null">create_by,</if>
-            <if test="createTime != null">create_time,</if>
         </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
             <if test="dcp_name != null">#{dcp_name},</if>
@@ -127,8 +125,6 @@
             <if test="dcp_cost_price != null">#{dcp_cost_price},</if>
             <if test="dcm_id != null">#{dcm_id},</if>
             <if test="dcp_oa_code != null">#{dcp_oa_code},</if>
-            <if test="createBy != null">#{createBy},</if>
-            <if test="createTime != null">#{createTime},</if>
         </trim>
     </insert>
 
@@ -147,8 +143,6 @@
             <if test="dcp_cost_price != null">dcp_cost_price = #{dcp_cost_price},</if>
             <if test="dcm_id != null">dcm_id = #{dcm_id},</if>
             <if test="dcp_oa_code != null">dcp_oa_code = #{dcp_oa_code},</if>
-            <if test="updateBy != null">update_by = #{updateBy},</if>
-            <if test="updateTime != null">update_time = #{updateTime},</if>
         </trim>
         where dcp_id = #{dcp_id}
     </update>

+ 3 - 3
gqy-system/src/main/resources/mapper/document/DcSystemProductMapper.xml

@@ -9,7 +9,7 @@
         <result property="sys_no"       column="sys_no"      />
         <result property="sys_name"     column="sys_name"    />
         <result property="sys_remark"   column="sys_remark"  />
-        <result property="p_id"       column="p_id"      />
+        <result property="dcp_id"       column="dcp_id"      />
         <result property="sys_status"   column="sys_status"  />
     </resultMap>
 
@@ -22,14 +22,14 @@
             <if test="sys_no != null">sys_no,</if>
             <if test="sys_name != null">sys_name,</if>
             <if test="sys_remark != null">sys_remark,</if>
-            <if test="p_id != null">p_id,</if>
+            <if test="dcp_id != null">dcp_id,</if>
             <if test="sys_status != null">sys_status,</if>
         </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
             <if test="sys_no != null">#{sys_no},</if>
             <if test="sys_name != null">#{sys_name},</if>
             <if test="sys_remark != null">#{sys_remark},</if>
-            <if test="p_id != null">#{p_id},</if>
+            <if test="dcp_id != null">#{dcp_id},</if>
             <if test="sys_status != null">#{sys_status},</if>
         </trim>
     </insert>