您的位置:首页 > 其它

保存修改前后的数据历史记录

2020-06-19 15:51 363 查看

我公司最近要我开发销售退货/款单,其中就有编辑退货款时需要保存修改数据的历史记录。我也是一个新入职场的小白,研究了一上午终于解决了,若有不对的地方麻烦私聊找我。要求如图所示:

下面是我解决问题的代码:
主要代码

/**
* 比较修改前后销售退款单
*
* @param oldBean 修改前数据
* @param newBean 修改后数据
* @param saleSn
* @param <T>
* @return
*/
public static <T> List<WpOrderSaleReturnRecord> updateLog(Object oldBean, Object newBean, String saleSn) {
List<WpOrderSaleReturnRecord> recordList = new ArrayList<>();
StringBuilder str = new StringBuilder();
T pojo1 = (T) oldBean;
T pojo2 = (T) newBean;
try {
// 通过反射获取类的类类型及字段属性
Class clazz = pojo1.getClass();
Field[] fields = clazz.getDeclaredFields();
int i = 1;
for (Field field : fields) {
// 排除序列化属性
if ("serialVersionUID".equals(field.getName())) {
continue;
}
//1、获取属性上的指定类型的注解
Annotation annotation = field.getAnnotation(XmlElement.class);
//有该类型的注解存在
if (annotation != null) {
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
// 获取对应属性值
Method getMethod = pd.getReadMethod();
Object o1 = getMethod.invoke(pojo1);
Object o2 = getMethod.invoke(pojo2);
if (o1 == null || o2 == null) {
continue;
}
//bigDecimal 类型的数据要去掉小数点后尾部的0不一致造成数据比对差异
String type = field.getType().getName();
if(type.equals("java.math.BigDecimal") && o1!=null && o2!=null ) {
BigDecimal val_old_big = new BigDecimal(String.valueOf(o1));
BigDecimal val_new_big = new BigDecimal(String.valueOf(o2));
if (String.valueOf(val_old_big).indexOf(".") != -1 || String.valueOf(val_new_big).indexOf(".") != -1) {//由于无法获取精度值,只能对所有带小数点的数据进行处理
DecimalFormat formatter1 = new DecimalFormat("0.00");
o1 = formatter1.format(val_old_big);
o2 = formatter1.format(val_new_big);
}
}
if (!o1.toString().equals(o2.toString())) {
//强制转化为相应的注解
XmlElement xmlElement = (XmlElement) annotation;
WpOrderSaleReturnRecord record = new WpOrderSaleReturnRecord();
record.setSaleSn(saleSn);
if (xmlElement.name().equals("商品数量")) {
for (Field field1 : fields) {
if (field1.getName().equals("goodsName")) {
PropertyDescriptor pd1 = new PropertyDescriptor(field1.getName(), clazz);
// 获取对应属性值
Method getMethod1 = pd1.getReadMethod();
Object goodsName = getMethod1.invoke(pojo1);
record.setAlterContent(goodsName + ",申请退货数量");
}
}
} else {
record.setAlterContent(xmlElement.name());
}
record.setAlterBefore(o1.toString());
record.setAlterAfter(o2.toString());
SaveSaleReturnRecord updateLogUtil = new SaveSaleReturnRecord();
updateLogUtil.saveOrderSaleReturnRecord(record, 2);

str.append(i + "、字段名称:" + xmlElement.name() + ",旧值:" + o1 + ",新值:" + o2 + ";");
i++;
}
}
}
} catch (Exception e) {
logger.error("异常:" + e.getMessage());
e.printStackTrace();
}
logger.info("修改对象内容结果:==========>" + str.toString());
return recordList;
}

其中这指定类型的注解是不可缺少的,如果不写这注解后面编辑时的变更内容则为字段名称
注解代码示例:

将获取到的数据保存到数据库

@Autowired
WpOrderSaleReturnRecordMapper wpOrderSaleReturnRecordMapper;

public static SaveSaleReturnRecord saveSaleReturnRecord;

@PostConstruct
public void init() {
saveSaleReturnRecord = this;
saveSaleReturnRecord.wpOrderSaleReturnRecordMapper = this.wpOrderSaleReturnRecordMapper;
}

以上此段代码是因为我这里获取不到dao层,你们可忽略此段代码

/**
* 保存销售退款单据记录
*
* @param records
* @param type    1.新增,2.编辑,3.审核通过,4.审核不通过,5.删除
*/
@Transactional("no2TransactionManager")
public void saveOrderSaleReturnRecord(WpOrderSaleReturnRecord records, Integer type) {
//type:1.新增,2.编辑,3.审核通过,4.审核不通过,5.删除
switch (type) {
case 1:
records.setOperationType("新增");
records.setAlterContent("新增单据");
break;
case 2:
records.setOperationType("编辑");
break;
case 3:
records.setOperationType("审核");
records.setAlterContent("审核通过");
break;
case 4:
records.setOperationType("审核");
records.setAlterContent("审核拒绝");
break;
case 5:
records.setOperationType("删除");
records.setAlterContent("删除单据");
break;
}
records.setCreateTime(new Date().getTime()); //操作时间
records.setOperationPerson(UserUtil.getUser().getId().toString());//操作人
saveSaleReturnRecord.wpOrderSaleReturnRecordMapper.insertSelective(records);
}

因为我们的业务中销售退货/款一对多了退款商品表,因此我这里特地将退款商品拆分出来重新调用updateLog 方法

/**
* 订单退款商品
*
* @param oldGoods 旧的退款商品
* @param newGoods 新的退款商品
*/
public static void returnGoods(String oldGoods, String newGoods, String saleSn) {
Gson gson = new Gson();
//String字符串类型转list
String refundGoodsOld = oldGoods.substring(oldGoods.indexOf("["), oldGoods.lastIndexOf("}"));
List<WpOrderGoods> oldRefundGoodsList = gson.fromJson(refundGoodsOld,
new TypeToken<List<WpOrderGoods>>() {
}.getType());

//String字符串类型转list
String refundGoodsNew = newGoods.substring(newGoods.indexOf("["), newGoods.lastIndexOf("}"));
List<WpOrderGoods> newRefundGoodsList = gson.fromJson(refundGoodsNew,
new TypeToken<List<WpOrderGoods>>() {
}.getType());

for (WpOrderGoods newOrderGoods : newRefundGoodsList) { //新
List<Integer> oldGoodsId = new ArrayList<>();
for (WpOrderGoods oldOrderGoods : oldRefundGoodsList) { //旧
if (oldOrderGoods.getId().equals(newOrderGoods.getId())) {
updateLog(oldOrderGoods, newOrderGoods, saleSn);
}
oldGoodsId.add(oldOrderGoods.getId());
}
//判断就退款商品id是否含于新退款商品id,
// 如不含于 此条新退款商品数据判断为新增加的退款商品
if (oldGoodsId.contains(newOrderGoods.getId()) == false) {
WpOrderSaleReturnRecord record = new WpOrderSaleReturnRecord();
record.setAlterContent(newOrderGoods.getGoodsName() + ",新增退款商品");
record.setSaleSn(saleSn);
SaveSaleReturnRecord updateLogUtil = new SaveSaleReturnRecord();
updateLogUtil.saveOrderSaleReturnRecord(record, 2);
}
}
}

ps:对于新增,删除,审核的单据直接保存销售退款单据记录(saveOrderSaleReturnRecord)就好,无需调用updateLog(比较修改前后销售退款单)方法

最后直接调用

保存结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: