|
@@ -0,0 +1,100 @@
|
|
|
+package com.simuwang.base.common.support;
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.simuwang.base.common.exception.APIException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author wangzaijun
|
|
|
+ * @date 2024/9/13 14:10
|
|
|
+ * @description 数据库操作的基础service
|
|
|
+ */
|
|
|
+public interface BaseService<VO extends BaseVO, T extends BaseEntity<VO>> {
|
|
|
+ String[] DEFAULT_COLUMNS = {"creatorid", "createtime"};
|
|
|
+
|
|
|
+ String[] DEFAULT_SELECT_COLUMNS = extColumns(DEFAULT_COLUMNS, "isvalid", "remark", "updaterid", "updatetime");
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 扩展查询的字段
|
|
|
+ *
|
|
|
+ * @param defaultColumns 默认的通用字段
|
|
|
+ * @param ext 子类扩展的字段
|
|
|
+ * @return /
|
|
|
+ */
|
|
|
+ static String[] extColumns(String[] defaultColumns, String... ext) {
|
|
|
+ String[] newColumns = new String[defaultColumns.length + ext.length];
|
|
|
+ System.arraycopy(defaultColumns, 0, newColumns, 0, defaultColumns.length);
|
|
|
+ System.arraycopy(ext, 0, newColumns, defaultColumns.length, ext.length);
|
|
|
+ return newColumns;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页方法
|
|
|
+ *
|
|
|
+ * @param query 分页查询对象
|
|
|
+ * @return /
|
|
|
+ */
|
|
|
+ default <Q extends PageQuery> MybatisPage<VO> findPage(Q query) {
|
|
|
+ Page<T> page = new Page<>(query.getCurrent(), query.getPageSize());
|
|
|
+ page.setOrders(query.getOrderItems());
|
|
|
+ QueryWrapper<T> wrapper = Wrappers.<T>query().select(this.selectColumns());
|
|
|
+ this.wrapQuery(wrapper, query);
|
|
|
+ if (StrUtil.isAllNotBlank(query.getBeginTime(), query.getEndTime())) {
|
|
|
+ wrapper.between("createtime", query.getBeginTime(), query.getEndTime());
|
|
|
+ }
|
|
|
+ if (query.getOrderItems() != null) {
|
|
|
+ query.getOrderItems().forEach(e -> wrapper.orderBy(true, e.isAsc(), e.getColumn()));
|
|
|
+ } else {
|
|
|
+ wrapper.orderByDesc("createtime");
|
|
|
+ }
|
|
|
+ return this.convertPage(page, wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取查询列,默认只获取 {@see DEFAULT_SELECT_COLUMNS} 列字段
|
|
|
+ *
|
|
|
+ * @return /
|
|
|
+ */
|
|
|
+ default String[] selectColumns() {
|
|
|
+ return DEFAULT_SELECT_COLUMNS;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 包装查询条件,默认空实现
|
|
|
+ *
|
|
|
+ * @param wrapper 查询包装器
|
|
|
+ * @param query 待处理条件
|
|
|
+ */
|
|
|
+ default <Q extends PageQuery> void wrapQuery(QueryWrapper<T> wrapper, Q query) {
|
|
|
+ throw new APIException("not impl.");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页结果转换
|
|
|
+ *
|
|
|
+ * @param page 分页参数
|
|
|
+ * @param wrapper 查询条件
|
|
|
+ * @return 分页结果
|
|
|
+ */
|
|
|
+ MybatisPage<VO> convertPage(Page<T> page, QueryWrapper<T> wrapper);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据id更新状态
|
|
|
+ *
|
|
|
+ * @param command /
|
|
|
+ */
|
|
|
+ default <BSC extends BaseIdStatusCmd> void updateStatus(BSC command) {
|
|
|
+ throw new APIException("not impl.");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据id删除记录
|
|
|
+ *
|
|
|
+ * @param command /
|
|
|
+ */
|
|
|
+ default <BIC extends BaseIdCmd> void delete(BIC command) {
|
|
|
+ throw new APIException("not impl.");
|
|
|
+ }
|
|
|
+}
|