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.

178 lines
6.5 KiB

2 years ago
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.CpicHomeBean;
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.CpicHomeEntity;
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.Date;
import java.util.List;
public class CpicHomeService {
/**
* 新增
*/
public static void saveOrUpdate(CpicHomeBean bean) {
try {
CpicDBAccessProvider.getAccessor().runDMLAction(new DBAction<Boolean>() {
@Override
public Boolean run(DAOContext context) throws Exception {
CpicHomeEntity targetEntity = null;
// 尝试查询
if (bean.getId() != null && StringKit.isNotBlank(bean.getId())) {
targetEntity = context.getDAO(CpicHomeDao.class).findById(bean.getId());
}
if (targetEntity != null) {
// 更新
targetEntity.setName(bean.getName());
targetEntity.setThemeId(bean.getThemeId());
targetEntity.setThemeName(bean.getThemeName());
targetEntity.setPath(bean.getPath());
targetEntity.setMhpath(bean.getMhpath());
targetEntity.setEditDate(new Date());
context.getDAO(CpicHomeDao.class).update(targetEntity);
} else {
// 新增
targetEntity = new CpicHomeEntity();
targetEntity.setId(UUID.randomUUID().toString().toLowerCase());
targetEntity.setName(bean.getName());
targetEntity.setThemeId(bean.getThemeId());
targetEntity.setThemeName(bean.getThemeName());
targetEntity.setPath(bean.getPath());
targetEntity.setMhpath(bean.getMhpath());
targetEntity.setDel("0");
targetEntity.setEditDate(new Date());
context.getDAO(CpicHomeDao.class).add(targetEntity);
}
return true;
}
});
} catch (Exception e) {
LogKit.error("新增/更新失败:{}", e.getMessage());
}
}
/**
* 查询列表
*/
public static List<CpicHomeBean> findList() {
try {
return CpicDBAccessProvider.getAccessor().runQueryAction(new DBAction<List<CpicHomeBean>>() {
@Override
public List<CpicHomeBean> run(DAOContext context) throws Exception {
List<CpicHomeEntity> items = context.getDAO(CpicHomeDao.class).findList();
List<CpicHomeBean> result = new ArrayList<>();
for (CpicHomeEntity item : items) {
result.add(entityToBean(item));
}
return result;
}
});
} catch (Throwable e) {
LogKit.error("查询失败:{}", e.getMessage());
}
return Collections.emptyList();
}
/**
* 根据id查询
*/
public static CpicHomeBean findById(String id) {
try {
return CpicDBAccessProvider.getAccessor().runQueryAction(new DBAction<CpicHomeBean>() {
@Override
public CpicHomeBean run(DAOContext context) throws Exception {
CpicHomeEntity item = context.getDAO(CpicHomeDao.class).findById(id);
return entityToBean(item);
}
});
} catch (Throwable e) {
LogKit.error("查询失败:{}", e.getMessage());
}
return null;
}
/**
* 根据name查询
*/
public static CpicHomeBean findByName(String name, String id) {
try {
return CpicDBAccessProvider.getAccessor().runQueryAction(new DBAction<CpicHomeBean>() {
@Override
public CpicHomeBean run(DAOContext context) throws Exception {
CpicHomeEntity item = context.getDAO(CpicHomeDao.class).findByName(name, id);
return entityToBean(item);
}
});
} catch (Throwable e) {
LogKit.error("查询失败:{}", e.getMessage());
}
return null;
}
/**
* 根据path查询
*/
public static CpicHomeBean findByPath(String path, String id) {
try {
return CpicDBAccessProvider.getAccessor().runQueryAction(new DBAction<CpicHomeBean>() {
@Override
public CpicHomeBean run(DAOContext context) throws Exception {
CpicHomeEntity item = context.getDAO(CpicHomeDao.class).findByPath(path, id);
return entityToBean(item);
}
});
} catch (Throwable e) {
LogKit.error("查询失败:{}", e.getMessage());
}
return null;
}
/**
* 逻辑删除
*
* @return
*/
public static void logicDel(String id) {
try {
CpicDBAccessProvider.getAccessor().runDMLAction(new DBAction<Boolean>() {
@Override
public Boolean run(DAOContext context) throws Exception {
context.getDAO(CpicHomeDao.class).logicDel(id);
context.getDAO(CpicEntryDao.class).logicDelByHomeId(id);
context.getDAO(CpicUserDao.class).logicDelByHomeId(id);
return true;
}
});
} catch (Exception e) {
LogKit.error("更新失败:{}", e.getMessage());
}
}
public static CpicHomeBean entityToBean(CpicHomeEntity entity) {
if (null == entity) {
return null;
}
CpicHomeBean bean = new CpicHomeBean();
bean.setId(entity.getId());
bean.setName(entity.getName());
bean.setThemeId(entity.getThemeId());
bean.setThemeName(entity.getThemeName());
bean.setPath(null == entity.getPath() ? "" : entity.getPath());
bean.setMhpath(null == entity.getMhpath() ? "" : entity.getMhpath());
return bean;
}
}