当前位置: 首页 > news >正文

广东做网站公司有哪些阿里云虚拟主机wordpress建站教程

广东做网站公司有哪些,阿里云虚拟主机wordpress建站教程,东莞营销型网站建设,百度网站认证v1前言 平时做一些统计数据#xff0c;经常从数据库或者是从接口获取出来的数据#xff0c;单位是跟业务需求不一致的。 比如#xff0c; 我们拿出来的 分#xff0c; 实际上要是元 又比如#xff0c;我们拿到的数据需要 乘以100 返回给前端做 百分比展示 又比如#xff…前言 平时做一些统计数据经常从数据库或者是从接口获取出来的数据单位是跟业务需求不一致的。 比如 我们拿出来的 分 实际上要是元 又比如我们拿到的数据需要 乘以100 返回给前端做 百分比展示 又比如 千分比转换 又比如拿出来的金额需要变成 万为单位 又比如需要保留2位小数 ...... 等等等等。 平时我们怎么搞 很多时候拿到的是一个数据集合list就需要去遍历然后根据每个DTO的属性去做相关单位转换。 一直get 完 set get 完 set get 完 set get 完 set get 完 set 人都麻了。 就像这样 所以如果通过反射自动匹配出来一些操作转换是不是就看代码看起来舒服一点人也轻松一点。 答案 是的 然后我就搞了。 正文 本篇内容简要 ① 初步的封装通过map去标记需要转换的 类属性字段 ② 进一步的封装 配合老朋友自定义注解搞事情 产品 支付总金额 换成万 为单位 方便运营统计 那个什么计数要是百分比的 然后还有一个是千分比 另外还有2个要保留2位小数 还有啊那个。。。。。。 我 别说了喝口水吧。 拿到的数据都在这个DTO里面 开始封装   ① 初步的封装通过map去标记需要转换的 类属性字段 思路玩法  a.通过反射拿出字段 b.配合传入的转换标记Map 匹配哪些字段需要操作 c.然后从map取出相关字段的具体操作是什么然后执行转换操作 d.重新赋值  ① 简单弄个枚举列出现在需求上的转换操作类型 UnitConvertType.java /*** Author : JCccc* CreateTime : 2023/01/14* Description :**/ public enum UnitConvertType {/*** 精度*/R,/*** 万元*/B,/*** 百分*/PERCENTAGE,/*** 千分*/PERMIL } ② 核心封装的转换函数  UnitConvertUtil.java import lombok.extern.slf4j.Slf4j; import java.lang.reflect.Field; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;/*** Author : JCccc* CreateTime : 2023/01/14* Description :**/ Slf4j public class UnitConvertUtil {public static T void unitMapConvert(ListT list, MapString, UnitConvertType propertyMap) {for (T t : list) {Field[] declaredFields t.getClass().getDeclaredFields();for (Field declaredField : declaredFields) {if (propertyMap.keySet().stream().anyMatch(x - x.equals(declaredField.getName()))) {try {declaredField.setAccessible(true);Object o declaredField.get(t);UnitConvertType unitConvertType propertyMap.get(declaredField.getName());if (o ! null) {if (unitConvertType.equals(UnitConvertType.PERCENTAGE)) {BigDecimal bigDecimal ((BigDecimal) o).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);declaredField.set(t, bigDecimal);}if (unitConvertType.equals(UnitConvertType.PERMIL)) {BigDecimal bigDecimal ((BigDecimal) o).multiply(new BigDecimal(1000)).setScale(2, BigDecimal.ROUND_HALF_UP);declaredField.set(t, bigDecimal);}if (unitConvertType.equals(UnitConvertType.B)) {BigDecimal bigDecimal ((BigDecimal) o).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP);declaredField.set(t, bigDecimal);}if (unitConvertType.equals(UnitConvertType.R)) {BigDecimal bigDecimal ((BigDecimal) o).setScale(2, BigDecimal.ROUND_HALF_UP);declaredField.set(t, bigDecimal);}}} catch (Exception ex) {log.error(处理失败);continue;}}}}}public static void main(String[] args) {//获取模拟数据ListMySumReportDTO list getMySumReportList();MapString, UnitConvertType map new HashMap();map.put(payTotalAmount, UnitConvertType.B);map.put(jcAmountPercentage, UnitConvertType.PERCENTAGE);map.put(jcCountPermillage, UnitConvertType.PERMIL);map.put(length, UnitConvertType.R);map.put(width, UnitConvertType.R);unitMapConvert(list,map);System.out.println(通过map标识的自动转换玩法list.toString());}private static ListMySumReportDTO getMySumReportList() {MySumReportDTO mySumReportDTO new MySumReportDTO();mySumReportDTO.setPayTotalAmount(new BigDecimal(1100000));mySumReportDTO.setJcAmountPercentage(BigDecimal.valueOf(0.695));mySumReportDTO.setJcCountPermillage(BigDecimal.valueOf(0.7894));mySumReportDTO.setLength(BigDecimal.valueOf(1300.65112));mySumReportDTO.setWidth(BigDecimal.valueOf(6522.12344));MySumReportDTO mySumReportDTO1 new MySumReportDTO();mySumReportDTO1.setPayTotalAmount(new BigDecimal(2390000));mySumReportDTO1.setJcAmountPercentage(BigDecimal.valueOf(0.885));mySumReportDTO1.setJcCountPermillage(BigDecimal.valueOf(0.2394));mySumReportDTO1.setLength(BigDecimal.valueOf(1700.64003));mySumReportDTO1.setWidth(BigDecimal.valueOf(7522.12344));ListMySumReportDTO list new ArrayList();list.add(mySumReportDTO);list.add(mySumReportDTO1);return list;}} 代码简析  看看怎么调用的 public static void main(String[] args) {//获取模拟数据ListMySumReportDTO list getMySumReportList();System.out.println(转换前list.toString());MapString, UnitConvertType map new HashMap();map.put(payTotalAmount, UnitConvertType.B);map.put(jcAmountPercentage, UnitConvertType.PERCENTAGE);map.put(jcCountPermillage, UnitConvertType.PERMIL);map.put(length, UnitConvertType.R);map.put(width, UnitConvertType.R);unitMapConvert(list,map);System.out.println(通过map标识的自动转换玩法list.toString());}   代码简析  效果 整个集合list的 对应字段都自动转换成功转换逻辑想怎么样就自己在对应if里面调整、拓展  ② 进一步的封装 配合老朋友自定义注解搞事情 实说实话第一步的封装程度已经够用了就是传map标识出来哪些需要转换对应转换枚举类型是什么。 其实我感觉是够用的。 但是么为了用起来更加方便或者说 更加地可拓展 那么配合自定义注解是更nice的。 开搞。 创建一个自定义注解 JcBigDecConvert.java import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;/*** Author : JCccc* CreateTime : 2023/01/14* Description :**/ Target(ElementType.FIELD) Retention(RetentionPolicy.RUNTIME) public interface JcBigDecConvert {UnitConvertType name(); } 怎么用 就是在我们的报表DTO里面去标记字段。 示例 MyYearSumReportDTO.javaps 可以看到我们在字段上面使用了自定义注解 import lombok.Data; import java.io.Serializable; import java.math.BigDecimal;/*** Author : JCccc* CreateTime : 2023/2/03* Description :**/Data public class MyYearSumReportDTO implements Serializable {private static final long serialVersionUID 5285987517581372888L;//支付总金额JcBigDecConvert(nameUnitConvertType.B)private BigDecimal payTotalAmount;//jc金额百分比JcBigDecConvert(nameUnitConvertType.PERCENTAGE)private BigDecimal jcAmountPercentage;//jc计数千分比JcBigDecConvert(nameUnitConvertType.PERMIL)private BigDecimal jcCountPermillage;//保留2位JcBigDecConvert(nameUnitConvertType.R)private BigDecimal length;//保留2位JcBigDecConvert(nameUnitConvertType.R)private BigDecimal width;} 然后针对配合我们的自定义封一个转换函数反射获取属性字段然后解析注解然后做对应转换操作。 代码 public static T void unitAnnotateConvert(ListT list) {for (T t : list) {Field[] declaredFields t.getClass().getDeclaredFields();for (Field declaredField : declaredFields) {try {if (declaredField.getName().equals(serialVersionUID)){continue;}JcBigDecConvert myFieldAnn declaredField.getAnnotation(JcBigDecConvert.class);if(Objects.isNull(myFieldAnn)){continue;}UnitConvertType unitConvertType myFieldAnn.name();declaredField.setAccessible(true);Object o declaredField.get(t);if (Objects.nonNull(o)) {if (unitConvertType.equals(UnitConvertType.PERCENTAGE)) {BigDecimal bigDecimal ((BigDecimal) o).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);declaredField.set(t, bigDecimal);}if (unitConvertType.equals(UnitConvertType.PERMIL)) {BigDecimal bigDecimal ((BigDecimal) o).multiply(new BigDecimal(1000)).setScale(2, BigDecimal.ROUND_HALF_UP);declaredField.set(t, bigDecimal);}if (unitConvertType.equals(UnitConvertType.B)) {BigDecimal bigDecimal ((BigDecimal) o).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP);declaredField.set(t, bigDecimal);}if (unitConvertType.equals(UnitConvertType.R)) {BigDecimal bigDecimal ((BigDecimal) o).setScale(2, BigDecimal.ROUND_HALF_UP);declaredField.set(t, bigDecimal);}}} catch (Exception ex) {log.error(处理失败);}}}} 写个调用示例看看效果   public static void main(String[] args) {ListMyYearSumReportDTO yearsList getMyYearSumReportList();unitAnnotateConvert(yearsList);System.out.println(通过注解标识的自动转换玩法yearsList.toString());}private static ListMyYearSumReportDTO getMyYearSumReportList() {MyYearSumReportDTO mySumReportDTO new MyYearSumReportDTO();mySumReportDTO.setPayTotalAmount(new BigDecimal(1100000));mySumReportDTO.setJcAmountPercentage(BigDecimal.valueOf(0.695));mySumReportDTO.setJcCountPermillage(BigDecimal.valueOf(0.7894));mySumReportDTO.setLength(BigDecimal.valueOf(1300.65112));mySumReportDTO.setWidth(BigDecimal.valueOf(6522.12344));MyYearSumReportDTO mySumReportDTO1 new MyYearSumReportDTO();mySumReportDTO1.setPayTotalAmount(new BigDecimal(2390000));mySumReportDTO1.setJcAmountPercentage(BigDecimal.valueOf(0.885));mySumReportDTO1.setJcCountPermillage(BigDecimal.valueOf(0.2394));mySumReportDTO1.setLength(BigDecimal.valueOf(1700.64003));mySumReportDTO1.setWidth(BigDecimal.valueOf(7522.12344));ListMyYearSumReportDTO list new ArrayList();list.add(mySumReportDTO);list.add(mySumReportDTO1);return list;} 效果也是很OK 抛砖引玉传递‘玩’代码思想学编程哎我就是玩。 好了该篇就到这。
http://www.w-s-a.com/news/246160/

相关文章:

  • seo网站培训优化怎么做如何给网站做下载附件
  • php网站建设文献综述怎么样提高网站排名
  • 专用车网站建设wordpress半透明
  • 石狮网站建设哪家好wordpress 3.9 漏洞
  • 为何建设单位网站找网络推广策畿
  • 用网站模板做网站动漫制作专业学校前十名
  • 网页 代码怎么做网站网站建设与维护课程设计
  • 网站制作哪家公司好企业名录联系电话
  • 做的网站怎么上传到网上wordpress图片之间空一行
  • 腾讯云建设网站视频vi报价单
  • 个人网站发布怎么做建设银行网站收款怎么打明细
  • 网站整体色调网站建设都有什么类型
  • 比较简洁大方的网站软件工程四大方向
  • 大家称赞的网站建设常德小学报名网站
  • 做网站怎么建文件夹百度网盘搜索神器
  • 企业有域名怎么做网站淘宝推广平台
  • 学网站开发去哪学药品销售推广方案
  • 哔哩哔哩h5播放器深圳网站seo外包公司哪家好
  • asp做的手机网站wordpress 文章title
  • 网站验证码目录wordpress内嵌播放器
  • 文明网网站建设南昌市建设规费标准网站
  • 安康有建网站的公司吗做网站用什么网名好
  • 济南网站制作哪家专业西安市城乡建设网官方网站
  • 网站建设有趣小游戏怎样让网站优化的方式
  • 昭通做网站儿童编程教学入门教程
  • eclipse静态网站开发软文广告投放平台
  • 网站建设教学视频济南做网站需要多少钱
  • 网站免费做软件市工商联官方网站建设方案
  • 网站建设大体包含英铭长沙网站建设
  • 网站建设培训学校北京如何搜索网站