You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

116 lines
4.3 KiB

package com.fr.plugin.cpic.db.service;
import com.fanruan.api.log.LogKit;
import com.fanruan.api.util.StringKit;
import com.fr.plugin.cpic.db.CpicDBAccessProvider;
import com.fr.plugin.cpic.db.bean.CpicEntryBean;
import com.fr.plugin.cpic.db.dao.CpicEntryDao;
import com.fr.plugin.cpic.db.dao.CpicHomeDao;
import com.fr.plugin.cpic.db.dao.CpicUserDao;
import com.fr.plugin.cpic.db.entity.CpicEntryEntity;
import com.fr.stable.core.UUID;
import com.fr.stable.db.action.DBAction;
import com.fr.stable.db.dao.DAOContext;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CpicEntryService {
/**
* 新增
*/
public static void saveOrUpdate(CpicEntryBean bean) {
try {
CpicDBAccessProvider.getAccessor().runDMLAction(new DBAction<Boolean>() {
@Override
public Boolean run(DAOContext context) throws Exception {
CpicEntryEntity targetEntity = null;
// 尝试查询
if (bean.getId() != null && StringKit.isNotBlank(bean.getId())) {
targetEntity = context.getDAO(CpicEntryDao.class).findById(bean.getId());
}
if (targetEntity != null) {
// 更新
targetEntity.setHomeId(bean.getHomeId());
targetEntity.setHomeName(bean.getHomeName());
targetEntity.setEntryId(bean.getEntryId());
targetEntity.setEntryName(bean.getEntryName());
context.getDAO(CpicEntryDao.class).update(targetEntity);
} else {
// 新增
targetEntity = new CpicEntryEntity();
targetEntity.setId(UUID.randomUUID().toString().toLowerCase());
targetEntity.setHomeId(bean.getHomeId());
targetEntity.setHomeName(bean.getHomeName());
targetEntity.setEntryId(bean.getEntryId());
targetEntity.setEntryName(bean.getEntryName());
targetEntity.setDel("0");
context.getDAO(CpicEntryDao.class).add(targetEntity);
}
return true;
}
});
} catch (Exception e) {
LogKit.error("新增/更新失败:{}", e.getMessage());
}
}
/**
* 查询列表
*/
public static List<CpicEntryBean> findList(String homeId) {
try {
return CpicDBAccessProvider.getAccessor().runQueryAction(new DBAction<List<CpicEntryBean>>() {
@Override
public List<CpicEntryBean> run(DAOContext context) throws Exception {
List<CpicEntryEntity> items = context.getDAO(CpicEntryDao.class).findEntryList(homeId);
List<CpicEntryBean> result = new ArrayList<>();
for (CpicEntryEntity item : items) {
result.add(entityToBean(item));
}
return result;
}
});
} catch (Throwable e) {
LogKit.error("查询失败:{}", e.getMessage());
}
return Collections.emptyList();
}
/**
* 删除
*
* @return
*/
public static void realDel(String id) {
try {
CpicDBAccessProvider.getAccessor().runDMLAction(new DBAction<Boolean>() {
@Override
public Boolean run(DAOContext context) throws Exception {
context.getDAO(CpicEntryDao.class).realDel(id);
return true;
}
});
} catch (Exception e) {
LogKit.error("删除失败:{}", e.getMessage());
}
}
public static CpicEntryBean entityToBean(CpicEntryEntity entity) {
if (null == entity) {
return null;
}
CpicEntryBean bean = new CpicEntryBean();
bean.setId(entity.getId());
bean.setHomeId(entity.getHomeId());
bean.setHomeName(entity.getHomeName());
bean.setEntryId(entity.getEntryId());
bean.setEntryName(entity.getEntryName());
bean.setDel(entity.getDel());
return bean;
}
}