|
|
@@ -0,0 +1,390 @@
|
|
|
+package com.kym.service;
|
|
|
+
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import cn.hutool.core.util.ReflectUtil;
|
|
|
+import com.kym.common.ContextHelper;
|
|
|
+import com.kym.common.DiffVo;
|
|
|
+import com.kym.common.IUser;
|
|
|
+import com.kym.common.constant.ResponseEnum;
|
|
|
+import com.kym.common.exception.BusinessException;
|
|
|
+import com.kym.common.utils.BeanUtil;
|
|
|
+import com.kym.common.utils.CommUtil;
|
|
|
+import com.kym.dao.DbHandler;
|
|
|
+import com.kym.entity.admin.AdminUser;
|
|
|
+import com.kym.entity.common.SimpleVo;
|
|
|
+import com.kym.entity.common.SimpleVo.SimpleAttachVo;
|
|
|
+import com.kym.entity.common.SimpleVo.SimpleUserVo;
|
|
|
+import com.kym.entity.miniapp.Attachment;
|
|
|
+import com.kym.jdbc.BasicQuery;
|
|
|
+import com.kym.jdbc.Bean;
|
|
|
+import com.kym.jdbc.OBuilder;
|
|
|
+import com.kym.jdbc.annotations.DBF;
|
|
|
+import lombok.Getter;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.Modifier;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 公共注入服务
|
|
|
+ *
|
|
|
+ * @author yaop
|
|
|
+ */
|
|
|
+public class IService {
|
|
|
+
|
|
|
+ @Getter
|
|
|
+ protected DbHandler db;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ @Qualifier("dbHandler")
|
|
|
+ public void setDb(DbHandler db) {
|
|
|
+ this.db = db;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected <T> Bean<T> toBean(long count, List<T> list) {
|
|
|
+ Bean<T> bean = new Bean<>();
|
|
|
+ bean.count = count;
|
|
|
+ bean.list = list;
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected OBuilder oBuilder() {
|
|
|
+ IUser user = ContextHelper.getUser();
|
|
|
+ if (null == user) {
|
|
|
+ return OBuilder.build();
|
|
|
+ }
|
|
|
+ return OBuilder.build().eq("companyId", user.companyId);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected long getUserId() {
|
|
|
+ IUser user = ContextHelper.getUser();
|
|
|
+ if (null == user) {
|
|
|
+ throw new BusinessException(ResponseEnum.UNLOGIN);
|
|
|
+ }
|
|
|
+ return user.id;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected long getOid() {
|
|
|
+ IUser user = ContextHelper.getUser();
|
|
|
+ if (null == user) {
|
|
|
+ throw new BusinessException(ResponseEnum.UNLOGIN);
|
|
|
+ }
|
|
|
+ return ContextHelper.getOid();
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void setupQuery(BasicQuery query) {
|
|
|
+ IUser user = ContextHelper.getUser();
|
|
|
+ if (null != user) {
|
|
|
+ ReflectUtil.setFieldValue(query, "companyId", user.companyId);
|
|
|
+ }
|
|
|
+ ReflectUtil.setFieldValue(query, "deleted", false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 断言已登录
|
|
|
+ */
|
|
|
+ protected void assertLogin() {
|
|
|
+ if (null == ContextHelper.getUser()) {
|
|
|
+ throw new BusinessException(ResponseEnum.UNLOGIN);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected void setAssociateId(long id) {
|
|
|
+ ContextHelper.setAssoId(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void setId(Object bean) {
|
|
|
+ setId(bean, "id");
|
|
|
+ }
|
|
|
+
|
|
|
+ protected long getUserDepartmentId() {
|
|
|
+ IUser user = ContextHelper.getUser();
|
|
|
+ if (CommUtil.isNotEmptyAndNull(user.deptIdList)) {
|
|
|
+ return user.deptIdList.get(0);
|
|
|
+ }
|
|
|
+ return 0L;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 雪花算法ID生成(默认截取4位往后长度,防止前端展示不全精度丢失问题)
|
|
|
+ *
|
|
|
+ * @param bean
|
|
|
+ * @param key
|
|
|
+ */
|
|
|
+ protected void setId(Object bean, String key) {
|
|
|
+ ReflectUtil.setFieldValue(bean, key, CommUtil.null2Long(String.valueOf(IdUtil.getSnowflakeNextId()).substring(4)));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 雪花算法ID生成(默认截取4位往后长度,防止前端展示不全精度丢失问题)
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ protected long genId() {
|
|
|
+ return CommUtil.null2Long(String.valueOf(IdUtil.getSnowflakeNextId()).substring(4));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected List<DiffVo> diffLog(Object src, Object dest) {
|
|
|
+ return diffLog(src, dest, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 比较并获取差异字段
|
|
|
+ */
|
|
|
+ protected List<DiffVo> diffLog(Object src, Object dest, Set<String> includes, String... excludeFields) {
|
|
|
+ List<DiffVo> diffVos = new ArrayList<>();
|
|
|
+ if (CommUtil.isEmptyOrNull(src) && CommUtil.isEmptyOrNull(dest)) {
|
|
|
+ return diffVos;
|
|
|
+ }
|
|
|
+ if (CommUtil.isBasicType(src) || CommUtil.isBasicType(dest)) {
|
|
|
+ return diffVos;
|
|
|
+ }
|
|
|
+ List<String> excludes = Arrays.asList(excludeFields.clone());
|
|
|
+ Map<String, Object> map = new HashMap<>(16);
|
|
|
+ if (null == src) {
|
|
|
+ map.put("src", null);
|
|
|
+ map.put("dest", CommUtil.print(dest));
|
|
|
+ return diffVos;
|
|
|
+ }
|
|
|
+ if (null == dest) {
|
|
|
+ map.put("src", CommUtil.print(src));
|
|
|
+ map.put("dest", null);
|
|
|
+ return diffVos;
|
|
|
+ }
|
|
|
+ Field[] fields = src.getClass().getFields();
|
|
|
+ if (!CommUtil.isEmptyOrNull(fields)) {
|
|
|
+ for (Field field : fields) {
|
|
|
+ int modifier = field.getModifiers();
|
|
|
+ if (!Modifier.isPublic(modifier)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String fieldName = field.getName();
|
|
|
+ if (CommUtil.isNotEmptyAndNull(includes) && !includes.contains(fieldName)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //TODO 数组、集合数据结构处理
|
|
|
+ if (!CommUtil.isBasicType(field.getType()) || "id".equalsIgnoreCase(fieldName) || excludes.contains(fieldName)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (!BeanUtil.compare(src, dest, fieldName)) {
|
|
|
+ String fieldCommentName = fieldName;
|
|
|
+ if (field.isAnnotationPresent(DBF.class)) {
|
|
|
+ fieldCommentName = field.getAnnotation(DBF.class).comment();
|
|
|
+ }
|
|
|
+ diffVos.add(DiffVo.builder().field(fieldName).name(fieldCommentName).src(CommUtil.logNull2String(ReflectUtil.getFieldValue(src, fieldName))).dest(CommUtil.logNull2String(ReflectUtil.getFieldValue(dest, fieldName))).build());
|
|
|
+// logs.add(ImmutableMap.of("field", fieldName, "name", fieldCommentName, "src", CommUtil.logNull2String(CommUtil.getFieldValue(src, fieldName)), "dest", CommUtil.logNull2String(CommUtil.getFieldValue(dest, fieldName))));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ map.put("logs", diffVos);
|
|
|
+ }
|
|
|
+
|
|
|
+ ContextHelper.setRemark(CommUtil.print(map));
|
|
|
+ return diffVos;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 比较并获取差异字段
|
|
|
+ */
|
|
|
+ /**
|
|
|
+ * @param src
|
|
|
+ * @param dest
|
|
|
+ * @param field 字段key
|
|
|
+ * @param fieldName 字段名称
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ protected DiffVo diffLog(Object src, Object dest, String field, String fieldName) {
|
|
|
+ DiffVo diffVo = new DiffVo();
|
|
|
+ diffVo.name = fieldName;
|
|
|
+ diffVo.field = field;
|
|
|
+ if (CommUtil.isEmptyOrNull(src) && CommUtil.isEmptyOrNull(dest)) {
|
|
|
+ return diffVo;
|
|
|
+ }
|
|
|
+ if (CommUtil.isBasicType(src) || CommUtil.isBasicType(dest)) {
|
|
|
+ return diffVo;
|
|
|
+ }
|
|
|
+ if (null == src) {
|
|
|
+ diffVo.src = "";
|
|
|
+ diffVo.dest = CommUtil.print(dest);
|
|
|
+ return diffVo;
|
|
|
+ }
|
|
|
+ if (null == dest) {
|
|
|
+ diffVo.dest = "";
|
|
|
+ diffVo.src = CommUtil.print(src);
|
|
|
+ return diffVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!BeanUtil.compare(src, dest, field)) {
|
|
|
+ diffVo.src = CommUtil.logNull2String(ReflectUtil.getFieldValue(src, field));
|
|
|
+ diffVo.dest = CommUtil.logNull2String(ReflectUtil.getFieldValue(dest, field));
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return diffVo;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //region 实体转换
|
|
|
+ protected SimpleVo getSimpleTagVo(Object entity) {
|
|
|
+ return BeanUtil.copy(SimpleVo.class, entity);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected List<Long> getSimpleIdList(Collection<?> beanList) {
|
|
|
+ if (CommUtil.isEmptyOrNull(beanList)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ } else {
|
|
|
+ return beanList.stream().map(k -> CommUtil.null2Long(ReflectUtil.getFieldValue(k, "id"))).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected List<SimpleVo> getSimpleVoList(Collection<?> beanList) {
|
|
|
+ if (CommUtil.isEmptyOrNull(beanList)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ } else {
|
|
|
+ return beanList.stream().map(k -> new SimpleVo(CommUtil.null2Long(ReflectUtil.getFieldValue(k, "id")), CommUtil.null2String(CommUtil.getFieldAlternativeValue(k, "name", "title")))).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected List<SimpleAttachVo> getAttachmentVoList(List<Attachment> attachments) {
|
|
|
+ if (CommUtil.isEmptyOrNull(attachments)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ } else {
|
|
|
+ return attachments.stream().map(k -> new SimpleAttachVo(k.getId(), k.getName(), k.getName())).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected List<SimpleUserVo> getUserSimpleVoList(Collection<? extends Number> idList) {
|
|
|
+ if (CommUtil.isEmptyOrNull(idList)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ List<AdminUser> userList = db.selectList(AdminUser.class, oBuilder().in("id", idList.toArray()), "id", "name", "avatar");
|
|
|
+// idList.forEach(id -> userList.add(getCacheUser((Long) id)));
|
|
|
+ if (CommUtil.isEmptyOrNull(userList)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ } else {
|
|
|
+ return userList.stream().filter(CommUtil.distinctByKey(AdminUser::getId)).map(k -> new SimpleUserVo(k.getId(), k.getUsername(), k.getAvatar())).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected List<SimpleUserVo> getUserSimpleVoPropertiesList(Collection<?> beanList) {
|
|
|
+ if (CommUtil.isEmptyOrNull(beanList)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ } else {
|
|
|
+ return beanList.stream().map(k -> new SimpleUserVo(CommUtil.null2Long(CommUtil.getFieldAlternativeValue(k, "id", "userId")), CommUtil.null2String(CommUtil.getFieldAlternativeValue(k, "name", "userName")), CommUtil.null2String(CommUtil.getFieldAlternativeValue(k, "avatar", "userAvatar")))).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //endregion
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增记录后的日志信息写入
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ */
|
|
|
+ protected void afterAdd(long id) {
|
|
|
+ ContextHelper.setAssoId(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除记录后的日志信息写入
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ */
|
|
|
+ protected void afterRemove(long id) {
|
|
|
+ ContextHelper.setAssoId(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改记录后的日志信息写入
|
|
|
+ */
|
|
|
+ protected void afterModify(Object src, Object dest) {
|
|
|
+ diffLog(src, dest, null);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //region 权限校验
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验全局权限(非应用内权限)
|
|
|
+ *
|
|
|
+ * @param permissions 权限值
|
|
|
+ */
|
|
|
+ protected void validate(String... permissions) {
|
|
|
+ validate(getOid(), permissions);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected boolean isReadPermission(String permission) {
|
|
|
+ return permission.endsWith(".list");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验全局权限(非应用内权限)
|
|
|
+ *
|
|
|
+ * @param permissions 权限值
|
|
|
+ */
|
|
|
+ protected void validate(long orgId, String... permissions) {
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验应用权限
|
|
|
+ *
|
|
|
+ * @param appletId 应用ID
|
|
|
+ * @param permissions 权限码值
|
|
|
+ */
|
|
|
+ protected void validateApplet(long appletId, String... permissions) {
|
|
|
+ validateApplet(appletId, 0, permissions);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验应用权限
|
|
|
+ *
|
|
|
+ * @param appletId 应用ID
|
|
|
+ * @param permissions 权限码值
|
|
|
+ */
|
|
|
+ protected void validateApplet(long appletId, long modelId, String... permissions) {
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验租户信息是否匹配
|
|
|
+ *
|
|
|
+ * @param bean 数据实体
|
|
|
+ */
|
|
|
+ protected void validateOrg(Object bean) {
|
|
|
+ assertLogin();
|
|
|
+ long beanOrgId = CommUtil.null2Long(ReflectUtil.getFieldValue(bean, "companyId"));
|
|
|
+ CommUtil.asserts(beanOrgId == ContextHelper.getUser().companyId, "租户权限不足");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验租户信息是否匹配
|
|
|
+ *
|
|
|
+ * @param orgId
|
|
|
+ * @param bean
|
|
|
+ */
|
|
|
+ protected void validateOrg(long orgId, Object bean) {
|
|
|
+ long beanOrgId = CommUtil.null2Long(ReflectUtil.getFieldValue(bean, "companyId"));
|
|
|
+ CommUtil.asserts(beanOrgId == orgId, "租户权限不足");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|