1. 需求描述:
前端可编辑表格或者excel导入数据的时候, 对数据库中已有的数据以及新提交的数据进行重复数据判断. 即:不允许提交重复的数据.
2. Entity
@Data
public class MappingTableAccount extends BaseEntity {
private static final long serialVersionUID = 1L;
private Long id;
private String account;
private Date effectiveDate;
private String description;
private String accountType;
private String status;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MappingTableAccount that = (MappingTableAccount) o;
return new EqualsBuilder().append(account, that.account).append(effectiveDate, that.effectiveDate)
.append(description, that.description).append(accountType, that.accountType)
.append(status, that.status).isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37).append(account).append(effectiveDate).append(description)
.append(accountType).append(status).toHashCode();
}
public static void main(String[] args) {
List<MappingTableAccount> list = new ArrayList<>();
MappingTableAccount account1 = new MappingTableAccount();
account1.setId(1L);
account1.setAccount("3302");
account1.setEffectiveDate(DateUtil.parse("2022-11-11", "yyyy-MM-dd"));
account1.setDescription("123");
account1.setAccountType("444");
list.add(account1);
MappingTableAccount account2 = new MappingTableAccount();
account2.setId(2L);
account2.setAccount("3302");
account2.setEffectiveDate(DateUtil.parse("2022-11-11", "yyyy-MM-dd"));
account2.setDescription("123");
account2.setAccountType("444");
list.add(account2);
MappingTableAccount account3 = new MappingTableAccount();
account3.setId(3L);
account3.setAccount("3302");
account3.setEffectiveDate(DateUtil.parse("2022-11-12", "yyyy-MM-dd"));
account3.setDescription("123");
account3.setAccountType("444");
list.add(account3);
MappingTableAccount account4 = new MappingTableAccount();
account4.setId(4L);
account4.setAccount("3302");
account4.setEffectiveDate(DateUtil.parse("2022-11-12", "yyyy-MM-dd"));
account4.setDescription("123");
account4.setAccountType("444");
list.add(account4);
System.out.println(account1.equals(account2));
// 第一种方法: 去重(必须重写equals和hashCode),返回去重后的集合. 无法获取重复的数据
List<MappingTableAccount> collect = list.stream().distinct().collect(Collectors.toList());
// 第二中方法: 分组, 返回分组后的数据. 可以获取重复的数据
Map<String, Long> groupMap = list.stream().collect(Collectors.groupingBy(MappingTableAccount::getGroupKey, counting()));
for(Map.Entry<String, Long> entry : groupMap.entrySet()) {
System.out.println(entry.getKey().replaceAll(",null",""));
}
}
/**
* 自定义分组key的规则是拼接某几个参数
*/
private String getGroupKey() {
String groupKey = String.format("%s,%s,%s,%s,%s",
this.account, this.effectiveDate, this.description, this.accountType, this.status);
return groupKey;
}
}
3. Service
/**
* 重复数据检查
*
* @param list
* @return true: 没有重复记录, false: 有重复记录
*/
default boolean duplicateRecordsCheck(List<T> list) {
// 获取已经存储的数据
List<T> allList = this.list(null);
allList.addAll(list);
List<T> distinctList = list.stream().distinct().collect(Collectors.toList());
if (allList.size() == distinctList.size()) {
return true;
}
return false;
}