gpt4 book ai didi

java - 在java代码中添加 boolean 值来检查表状态条件

转载 作者:行者123 更新时间:2023-11-30 07:34:14 25 4
gpt4 key购买 nike

我试图在java中迭代sdid的记录列表。 sdid 记录是一组最多 8 条具有相同 sdid 的记录,但需要检查每个 sdid 的状态列以查看其 PEND_ADDPEND_DELACTIVE

因此,对于第一个条件:状态是否为 PEND_ADDPEND_DEL。然后删除记录。

如果状态仅为ACTIVE,则删除记录。

如果状态为 ACTIVEPEND_ADD 和/或 PEND_DEL,则删除 PEND_ADD 和/或 >PEND_DEL 并保持ACTIVE

        private void disconnectClassOfService(List<Integer> sdidList) throws Exception {

if (sdidList == null || sdidList.isEmpty()){
return;
}

logger.info("Start deleting sdid : " + sdidList);
for (Integer sdid : sdidList ) {

List<CosPolicy2Interface> cosPolicy2InterfaceBySdid = getCosPolicy2InterfaceDAO().getCosPolicy2InterfaceBySdid(sdid);
if (cosPolicy2InterfaceBySdid != null && !cosPolicy2InterfaceBySdid.isEmpty()){
for (CosPolicy2Interface cosPolicySdid: cosPolicy2InterfaceBySdid){
if (cosPolicySdid.getStatus().equals("PEND_ADD") ){
getPersistMgr().delete(cosPolicySdid);
}
if (cosPolicySdid.getStatus().equals("PEND_DEL") ){
getPersistMgr().delete(cosPolicySdid);
}
if (cosPolicySdid.getStatus().equals("ACTIVE") ){
getPersistMgr().delete(cosPolicySdid);
}
}
}

我的 hibernate 查询

            protected static final String queryBySdid = " SELECT * "
+ " FROM cos_policy2interface "
+ " WHERE connection_type = 'SDID' AND connection_id = ?";

@Override
public List<CosPolicy2Interface> getCosPolicy2InterfaceBySdid(
long sdid) throws Exception {
EntityManager em = getEntityManager();
CosPolicy2Interface CosPolicy2Interface = null;
Query query = em.createNativeQuery(queryBySdid, CosPolicy2Interface.class);
query.setParameter(1, sdid);

@SuppressWarnings("unchecked")
List<CosPolicy2Interface> cosPolicy2InterfaceList = query.getResultList();
return cosPolicy2InterfaceList;
}

表格示例:

CosPolicy2Interface_id,connection_type, connection_id, status
29, 'SDID', 13538806, ACTIVE
30, 'SDID', 13538806, PEND_DEL
31, 'SDID', 13538806, ACTIVE
32, 'SDID', 13538806, PEND_ADD
33, 'SDID', 13538806, PEND_ADD
34, 'SDID', 13538806, ACTIVE

My logic will erased all of it since it will iterate one record at a time. Could I use a boolean? some thing like this how can I implemented.

1) Pen Add -> dont care delete (But set PendingFlag to true)
2)Pend Del --> dont care delete (Set Pending Flag to true)
3) Active --> Store them in a list to check later
Come out of the for-loop and check if pendingFlag is false and activeCosList is not empty --> delete them

最佳答案

我想修改您的 disconnectClassOfService 方法。

private void disconnectClassOfService(List<Integer> sdidList) throws Exception {

if (sdidList == null || sdidList.isEmpty()) {
return;
}

logger.info("Start deleting sdid : " + sdidList);
for (Integer sdid : sdidList) {

List<CosPolicy2Interface> cosPolicy2InterfaceBySdid = getCosPolicy2InterfaceDAO().getCosPolicy2InterfaceBySdid(sdid);
if (cosPolicy2InterfaceBySdid != null && !cosPolicy2InterfaceBySdid.isEmpty()) {

List<CosPolicy2Interface> recordsACTIVE = new ArrayList<>();
List<CosPolicy2Interface> recordsNonACTIVE = new ArrayList<>();

for (CosPolicy2Interface cosPolicySdid : cosPolicy2InterfaceBySdid) {
if (cosPolicySdid.getStatus().equals("PEND_ADD")) {
recordsNonACTIVE.add(cosPolicySdid);
}
if (cosPolicySdid.getStatus().equals("PEND_DEL")) {
recordsNonACTIVE.add(cosPolicySdid);
}
if (cosPolicySdid.getStatus().equals("ACTIVE")) {
recordsACTIVE.add(cosPolicySdid);
}
}
if ((recordsACTIVE.size() == cosPolicy2InterfaceBySdid.size())) {
getPersistMgr().deleteAll(cosPolicy2InterfaceBySdid);// Delete all records
} else {
// delete non active records
getPersistMgr().deleteAll(recordsNonACTIVE);
}
}
}
}

这样您就不必单独删除记录。

关于java - 在java代码中添加 boolean 值来检查表状态条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35639450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com