java-springboot通过字符串获取或者设置值

1. 设置一个map集合

  /**
     * 设置费用项的map集合
     *
     * @return Map
     */
    public Map<String, String> generatePerformanceFeeFyxMap() {
        // 说明:根据费用项id(sap_fyitemid)查询费用项表(fee_fyx)中的金额(fyline),存入业务部门业绩报表(water_fee_business_unit)所对应的字段。
        // 需要判断费用项id等于几来确定存入业务部门业绩报表中的那个字段。所以有了以下对应关系:费用项id 和 业务部门业绩报表所对应字段。
        // 初始化map集合 String1:fee_fyx费用项表中的费用项ID=>sap_fyitemid    String2:water_fee_business_unit业务部门业绩报表所对应的字段
        Map<String, String> someMap = new HashMap<>();
        someMap.put("14", "hwf");//会务费
        someMap.put("19", "tdjs");//团队建设
        someMap.put("20", "wljssyf");//网络建设使用费
        someMap.put("23", "fyyClfy");//非营运-车辆费用
        someMap.put("29", "fyyLqf");//非营运-路桥费
        someMap.put("3", "zeroOne");//01
        someMap.put("32", "tjf");//探家费
        someMap.put("34", "clfZsf");//差旅费-住宿费
        someMap.put("36", "wlYzf");//物流-运杂费
        someMap.put("4", "zeroTwo");//02
        someMap.put("442", "zeroSix");//06
        someMap.put("444", "jbcf");//加班餐费
        someMap.put("46", "clfClbt");//差旅费-差旅补贴
        someMap.put("464", "wlClbxf");//物流-车辆保险费
        someMap.put("465", "wlYf");//物流-油费
        someMap.put("468", "wlClltf");//物流-车辆轮胎费
        someMap.put("469", "wlClbyf");//物流-车辆保养费
        someMap.put("472", "wlClwxf");//物流-车辆维修费
        someMap.put("474", "wlCldxf");//物流-车辆大修费
        someMap.put("478", "wlLqf");//物流-路桥费
        someMap.put("480", "wlCpf");//物流-船票费
        someMap.put("481", "wlZxf");//物流-装卸费
        someMap.put("484", "wlGpsSyf");//物流-GPS使用费
        someMap.put("485", "wlTcf");//物流-停车费
        someMap.put("487", "wlClsyf");//物流-车辆审验费
        someMap.put("490", "wlAqscfy");//物流-安全生产费用
        someMap.put("495", "wlClwzfk");//物流-车辆违章罚款
        someMap.put("51", "bgfQt");//办公费-其他
        someMap.put("522", "itSbzlf");//IT设备租赁费
        someMap.put("545", "clfHcp");//差旅费-火车票
        someMap.put("547", "clfBzcl");//差旅费-标准差旅
        someMap.put("548", "clfSnjt");//差旅费-市内交通
        someMap.put("56", "dzyhp");//低值易耗品
        someMap.put("563", "clf");//差旅费
        someMap.put("58", "dhf");//电话费
        someMap.put("604", "zdf");//招待费
        someMap.put("615", "wxf");//维修费
        someMap.put("62", "flf");//福利费
        someMap.put("644", "wlFz");//物流-房租
        someMap.put("645", "wlSdf");//物流-水电费
        someMap.put("647", "wlhcfQt");//物流耗材费-其他
        someMap.put("651", "wlhcf");//物流耗材费
        someMap.put("658", "bgfKdf");//办公费-快递费
        someMap.put("659", "bgf");//办公费
        someMap.put("665", "grfz");//个人房租
        someMap.put("683", "wlClzlf");//物流-车辆租赁费
        someMap.put("773", "fyLdbhf");//费用-劳动保护费
        someMap.put("804", "lzbc");//离职补偿
        someMap.put("90", "gpsTyf");//GSP体检费
        someMap.put("96", "zgjyjf");//职工教育经费

        return someMap;
    }

1. 通过反射获得值

    /**
     * 获取对象指定属性的值
     *
     * @param o         对象
     * @param fieldName 要获取值的属性
     *                  返回值:对象指定属性的值
     */
    public Object getFieldValueByName(Object o, String fieldName) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(o, new Object[]{});
            return value;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

2. 调用获得值

/**
     * 费用项合计
     *
     * @param waterFeeBusinessUnitInfo 业务部门业绩报表数据
     * @return BigDecimal
     */
    public BigDecimal generatePerformanceTotalExpenses(WaterFeeBusinessUnit waterFeeBusinessUnitInfo) {
        BigDecimal total = BigDecimal.ZERO;
        // 获取项目费用集合
        Map<String, String> map = generatePerformanceFeeFyxMap();
        //遍历费用项
        for (String key : map.keySet()) {
            //System.out.println("key= " + key + " and value= " + map.get(key));
            //获取对象指定属性的值
            Object fieldValueByName = getFieldValueByName(waterFeeBusinessUnitInfo, map.get(key));
            // System.out.println("fieldValueByName= " + fieldValueByName);
            if (fieldValueByName != null) {
                BigDecimal number = (BigDecimal) fieldValueByName;
                total = total.add(number);
            }
        }
        return total;
    }

3. 通过反射获取值


    /**
     * 给对象指定的属性设置值
     *
     * @param obj       要设置值的对象
     * @param fieldName 要设置值的属性
     * @param value     值
     */
    public Object setFieldValueByName(Object obj, String fieldName, Object value) {
        try {
            // 获取obj类的字节文件对象
            Class c = obj.getClass();
            // 获取该类的成员变量
            Field f = c.getDeclaredField(fieldName);
            // 取消语言访问检查
            f.setAccessible(true);
            // 给变量赋值
            f.set(obj, value);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return obj;
    }

4. 调用设置值

/**
     * 根据费用项类型判断当前金额
     *
     * @param feeFyxeList 费用项
     * @return
     */
 public WaterFeeBusinessUnit generatePerformanceFeeFyxType(List<FeeFyx> feeFyxeList,  WaterFeeBusinessUnit waterFeeBusinessUnit) {
        // 获取项目费用集合
        Map<String, String> map = generatePerformanceFeeFyxMap();
        // 遍历集合
        for (String key : map.keySet()) {
            System.out.println("key= " + key + " and value= " + map.get(key));
            // 过滤数据
            List<FeeFyx> comps = feeFyxeList.stream().filter(p -> p.getSapFyitemid() != null && p.getSapFyitemid().equals(key)).collect(Collectors.toList());
            //统计数据
            BigDecimal sumFyline = comps.stream().filter(FeeFyx -> FeeFyx.getFyline() != null).map(FeeFyx::getFyline).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(4, BigDecimal.ROUND_HALF_UP);
			//设置实体类属性值,并返回当前实体类数据
            Object o = setFieldValueByName(waterFeeBusinessUnit, map.get(key), sumFyline.add(fyLine));
            //覆盖实体类数据
            waterFeeBusinessUnit = (WaterFeeBusinessUnit) o;
            waterFeeBusinessUnit.setStatus(waterFeeBusinessUnitmUncertainFy.getStatus());
            waterFeeBusinessUnit.setRemark(waterFeeBusinessUnitmUncertainFy.getRemark());
       
        }
        return waterFeeBusinessUnit;
    }
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值