Pārlūkot izejas kodu

主子表的保存

shiqian 4 gadi atpakaļ
vecāks
revīzija
47d5f18a6b

+ 11 - 0
boman-api/boman-domain/src/main/java/com.boman.domain/SysDept.java

@@ -54,6 +54,17 @@ public class SysDept extends BaseEntity
     /** 子部门 */
     private List<SysDept> children = new ArrayList<SysDept>();
 
+    /** 该部门下的所有人员 */
+    private List<SysUser> users;
+
+    public List<SysUser> getUsers() {
+        return users;
+    }
+
+    public void setUsers(List<SysUser> users) {
+        this.users = users;
+    }
+
     public Long getId() {
         return id;
     }

+ 7 - 1
boman-api/boman-domain/src/main/java/com.boman.domain/constant/FormDataConstant.java

@@ -90,6 +90,7 @@ public class FormDataConstant {
 
     public static final String CHAR = "char";
     public static final String DATETIME = "datetime";
+    public static final String DATE = "date";
     public static final String TIMESTAMP = "timestamp";
 
     /**  根据表名查询表单时,返回给前台的查询字段   */
@@ -150,7 +151,7 @@ public class FormDataConstant {
     public static final List<String> NEED_QUERY_DICT_LIST = Stream.of(RADIO, CHECKBOX, SELECT).collect(Collectors.toList());
 
     /**  需要转换时间类型的 */
-    public static final List<String> NEED_CONVERT_DATE_LIST = Stream.of(DATETIME).collect(Collectors.toList());
+    public static final List<String> NEED_CONVERT_DATE_LIST = Stream.of(DATETIME, DATE).collect(Collectors.toList());
 
     /**  hr 日志、基础信息..... */
     public static final String HR = "HR";
@@ -194,4 +195,9 @@ public class FormDataConstant {
     /** 前台有时候需要计算时长等的标识,用到的列 */
     public static final String IS_USE_EXTEND = "isUseExtend";
 
+    /** 复杂保存的时候,子表关联主表的id的标识:fk. */
+    public static final String FK_POINT = "fk.";
+    /** . */
+    public static final String POINT = "\\.";
+
 }

+ 9 - 3
boman-api/boman-domain/src/main/java/com.boman.domain/dto/FormDataDto.java

@@ -108,22 +108,28 @@ public class FormDataDto implements Serializable {
      */
     private Boolean stopProcess;
 
+    /******************************* 几个常量 **************************************/
+    private static final int MAX_PAGE_SIZE = 200;
+    private static final int DEFAULT_PAGE_SIZE = 10;
+    private static final int DEFAULT_PAGE_NO = 1;
+
 
     public int getLimit() {
         try {
             return pageNo == 0 ? 0 : (pageNo - 1) * pageSize;
         } catch (Exception e) {
             e.printStackTrace();
-            return 1;
+            return DEFAULT_PAGE_NO;
         }
     }
 
     public int getOffset(){
         try {
-            return pageSize == 0 ? 10 : pageSize;
+            int pageSize = this.pageSize == 0 ? DEFAULT_PAGE_SIZE : this.pageSize;
+            return Math.min(pageSize, MAX_PAGE_SIZE);
         } catch (Exception e) {
             e.printStackTrace();
-            return 10;
+            return DEFAULT_PAGE_SIZE;
         }
     }
 

+ 19 - 0
boman-common/boman-common-core/src/main/java/com/boman/common/core/utils/number/NumberUtils.java

@@ -13,6 +13,7 @@ public class NumberUtils {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(NumberUtils.class);
 
+    /***************************** int ***************************************/
     public static boolean gtZero(int input){
         return input > 0;
     }
@@ -25,4 +26,22 @@ public class NumberUtils {
         return input == 0;
     }
 
+
+    /***************************** Long ***************************************/
+    public static boolean gtZero(long input){
+        return input > 0;
+    }
+
+    public static boolean ltZero(long input){
+        return input < 0;
+    }
+
+    public static boolean ltEqZero(long input){
+        return input <= 0;
+    }
+
+    public static boolean eqZero(long input){
+        return input == 0;
+    }
+
 }

+ 13 - 0
boman-modules/boman-system/src/main/java/com/boman/system/controller/SysUserController.java

@@ -303,4 +303,17 @@ public class SysUserController extends BaseController
     public void addUser(@Validated @RequestBody List<JSONObject> result) {
          userService.addUser(result);
     }
+
+
+    /**
+     * 功能描述: 树形结构,部门下的人员
+     *
+     * @param dept dept
+     * @return java.util.List<com.boman.domain.SysDept>
+     */
+    @Log(title = "树形结构,部门下的人员", businessType = BusinessType.UPDATE)
+    @GetMapping("/listUserTree")
+    public AjaxResult listUserTree(SysDept dept) {
+        return AjaxResult.success(userService.listUserTree(dept));
+    }
 }

+ 5 - 2
boman-modules/boman-system/src/main/java/com/boman/system/mapper/SysUserMapper.java

@@ -127,6 +127,9 @@ public interface SysUserMapper
      * @return
      */
     List<SysUser>selectUserByDeptId(@Param("deptId") Long deptId);
-}
-
 
+    /**
+     * 部门下所有人
+     */
+    List<SysUser> listByDeptIdList(@Param("deptIdList") List<Long> deptIdList);
+}

+ 13 - 0
boman-modules/boman-system/src/main/java/com/boman/system/service/ISysUserService.java

@@ -3,6 +3,7 @@ package com.boman.system.service;
 import java.util.List;
 
 import com.alibaba.fastjson.JSONObject;
+import com.boman.domain.SysDept;
 import com.boman.domain.SysUser;
 import com.boman.domain.dto.AjaxResult;
 
@@ -179,6 +180,10 @@ public interface ISysUserService
      * 部门下所有人
      */
     List<SysUser> listByDeptId(List<Long> deptIdList);
+    /**
+     * 部门下所有人
+     */
+    List<SysUser> listByDeptIdList(List<Long> deptIdList);
 
     /**
      * 为用户档案定制新增接口
@@ -186,4 +191,12 @@ public interface ISysUserService
      * @return
      */
     public void addUser(List<JSONObject> result);
+
+    /**
+     * 功能描述: 树形结构,部门下的人员
+     *
+     * @param dept dept
+     * @return java.util.List<com.boman.domain.SysDept>
+     */
+    List<SysDept> listUserTree(SysDept dept);
 }

+ 44 - 2
boman-modules/boman-system/src/main/java/com/boman/system/service/impl/SysUserServiceImpl.java

@@ -8,6 +8,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.boman.common.core.utils.DateUtils;
 import com.boman.common.core.utils.obj.ObjectUtils;
 import com.boman.domain.SysDept;
+import com.boman.system.service.ISysDeptService;
 import com.boman.domain.dto.AjaxResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -32,8 +33,8 @@ import com.boman.system.mapper.SysUserRoleMapper;
 import com.boman.system.service.ISysConfigService;
 import com.boman.system.service.ISysUserService;
 
-import javax.validation.constraints.NotBlank;
-import javax.validation.constraints.Size;
+import javax.annotation.Resource;
+
 
 /**
  * 用户 业务层处理
@@ -62,6 +63,8 @@ public class SysUserServiceImpl implements ISysUserService
 
     @Autowired
     private ISysConfigService configService;
+    @Resource
+    private ISysDeptService deptService;
 
     /**
      * 根据条件分页查询用户列表
@@ -511,6 +514,19 @@ public class SysUserServiceImpl implements ISysUserService
         return userMapper.listByDeptId(deptIdList);
     }
 
+
+    /**
+     * 部门下所有人
+     */
+    @Override
+    public List<SysUser> listByDeptIdList(List<Long> deptIdList) {
+        if (ObjectUtils.isEmpty(deptIdList)) {
+            return Collections.emptyList();
+        }
+
+        return userMapper.listByDeptIdList(deptIdList);
+    }
+
     /**
      * 为用户档案定制新增接口
      * @param result
@@ -535,4 +551,30 @@ public class SysUserServiceImpl implements ISysUserService
             userMapper.insertUser(user);
         }
     }
+
+    /**
+     * 功能描述: 树形结构,部门下的人员
+     *
+     * @param dept dept
+     * @return java.util.List<com.boman.domain.SysDept>
+     */
+    @Override
+    public List<SysDept> listUserTree(SysDept dept) {
+        List<SysDept> sysDepts = deptService.selectDeptList(dept);
+        List<Long> deptIdList = ObjectUtils.map(sysDepts, SysDept::getId);
+        List<SysUser> sysUsers = listByDeptIdList(deptIdList);
+
+        for (SysDept sysDept : sysDepts) {
+            List<SysUser> users = new ArrayList<>(16);
+            for (SysUser sysUser : sysUsers) {
+                if (sysDept.getId().equals(sysUser.getDeptId())) {
+                    users.add(sysUser);
+                }
+            }
+            sysDept.setUsers(users);
+        }
+
+        return sysDepts;
+    }
+
 }

+ 8 - 1
boman-modules/boman-system/src/main/resources/mapper/system/SysUserMapper.xml

@@ -199,5 +199,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 		and u.status = '0' and u.del_flag = '0'
 	</select>
 
-	
+
+ 	<select id="listByDeptIdList" resultType="com.boman.domain.SysUser">
+ 		select id, dept_id deptId, user_name userName from sys_user where dept_id in
+ 		<foreach collection="deptIdList" item="deptId" open="(" separator="," close=")">
+ 			#{deptId}
+        </foreach>
+ 	</select>
+
 </mapper> 

+ 7 - 4
boman-web-core/src/main/java/com/boman/web/core/domain/TableContext.java

@@ -64,7 +64,7 @@ public class TableContext {
         this.tableName = tableName;
 
         GenTable table = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);
-        this.table = requireNonNull(table, "redis中没有此表,表名: " + tableName);
+        this.table = requireNonNull(table, "此表不存在,表名: " + tableName);
         this.functionName = table.getFunctionName();
 
         JSONObject fixedData = requireNonNull(dto.getFixedData(), "fixedData is empty");
@@ -108,14 +108,17 @@ public class TableContext {
         // 过滤掉hr字段
         columns = withoutHr(columns);
         for (GenTableColumn column : columns) {
-            String[] maskArray = requireNonNull(column.getMask(), "mask is empty, columnName = " + column.getColumnName()).split("");
+            String columnName = column.getColumnName();
+            String[] maskArray = requireNonNull(column.getMask()
+                    , String.format("mask is empty, columnName = [%s], tableName = [%s]", columnName, column.getTableName()))
+                    .split("");
             assert maskArray.length == 6;
             // 新增可见 修改可见
             String insertVisible = maskArray[0], updateVisible = maskArray[2];
             // 新增可修改 修改可修改
             String insertCanEdit = maskArray[1], updateCanEdit = maskArray[3];
             for (Map.Entry<String, Object> entry : fixedData.entrySet()) {
-                if (column.getColumnName().equalsIgnoreCase(entry.getKey())) {
+                if (columnName.equalsIgnoreCase(entry.getKey())) {
                     // 新增
                     if (ActionType.INSERT.equals(context.getActionType())) {
                         // 新增可可见, 并且新增修改,把新增的字段单独拿出来
@@ -145,7 +148,7 @@ public class TableContext {
         return commitData;
     }
 
-    /**                 get and set                               **/
+    /**************************************  get and set  **************************************/
 
     public Long getId() {
         return id;

+ 20 - 3
boman-web-core/src/main/java/com/boman/web/core/service/TableServiceCmdService.java

@@ -3,6 +3,7 @@ package com.boman.web.core.service;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.boman.common.core.utils.number.NumberUtils;
 import com.boman.domain.constant.CacheConstants;
 import com.boman.common.core.utils.SecurityUtils;
 import com.boman.common.core.utils.StringUtils;
@@ -156,7 +157,7 @@ public class TableServiceCmdService {
     }
 
     /**
-     * 功能描述: 复杂对象保存(本地事务)
+     * 功能描述: 复杂对象保存(本地事务) todo 暂且如此处理,后期优化
      *
      * @param dto dto
      * @return com.boman.domain.dto.AjaxResult
@@ -168,8 +169,24 @@ public class TableServiceCmdService {
         recursionAllChildrenFromDto(dto, result);
 
         List<AjaxResult> resultList = new ArrayList<>(result.size());
-        for (FormDataDto formDataDto : result) {
-            AjaxResult ajaxResult = objectSave(formDataDto);
+        Map<String, Long> tableNameIdMap = new HashMap<>(result.size());
+        AjaxResult ajaxResult;
+        for (int i = 0; i < result.size(); i++) {
+            FormDataDto formDataDto = result.get(i);
+            putFkValue(tableNameIdMap, formDataDto, i == 0);
+
+            String tableName = formDataDto.getTable();
+            ajaxResult = objectSave(formDataDto);
+            if (AjaxResult.checkFail(ajaxResult)) {
+                throw new RuntimeException(String.format("保存失败, 表名为: %s ", tableName));
+            }
+
+            Long fkId = (Long) ajaxResult.get(AjaxResult.DATA_TAG);
+            if (null == fkId || NumberUtils.ltEqZero(fkId)) {
+                throw new RuntimeException(String.format("保存失败, 表名为: %s ,主表id: %s", tableName, fkId));
+            }
+
+            tableNameIdMap.put(tableName, fkId);
             resultList.add(ajaxResult);
         }
 

+ 60 - 0
boman-web-core/src/main/java/com/boman/web/core/utils/ColumnUtils.java

@@ -13,14 +13,17 @@ import com.boman.common.redis.service.RedisService;
 import com.boman.domain.GenTable;
 import com.boman.domain.GenTableColumn;
 import com.boman.domain.constant.MysqlDataTypeConst;
+import com.boman.domain.dto.FormDataDto;
 import com.boman.domain.exception.UnknownColumnException;
 import com.boman.web.core.domain.TableContext;
+import com.boman.web.core.service.TableServiceCmdService;
 import com.google.common.base.Joiner;
 import com.google.common.collect.Lists;
 import org.apache.commons.collections4.MapUtils;
 import org.apache.commons.lang3.math.NumberUtils;
 import org.springframework.web.client.RestTemplate;
 
+import javax.validation.constraints.NotNull;
 import java.sql.Timestamp;
 import java.util.*;
 import java.util.function.Predicate;
@@ -456,4 +459,61 @@ public class ColumnUtils {
         requireNonNull(genTable, String.format("表名为: %s 的表不存在缓存中", tableName));
         return genTable.getColumns();
     }
+
+    public static List<String> fkName(List<GenTableColumn> columns){
+        List<String> result = new ArrayList<>(columns.size());
+        for (GenTableColumn column : columns) {
+            Long foreignKey = column.getForeignKey();
+            if (isEmpty(foreignKey)) {
+                continue;
+            }
+            result.add(column.getColumnName());
+        }
+
+        return result;
+    }
+
+    public static List<String> fkNames(String tableName) {
+        TableServiceCmdService cmdService = SpringUtils.getBean(TableServiceCmdService.class);
+        GenTable genTable = cmdService.getTableFromRedisByTableName(RedisKey.TABLE_INFO, tableName);
+        if (genTable == null) {
+            throw new IllegalArgumentException(String.format("表名: %s 为空", tableName));
+        }
+
+        return fkName(genTable.getColumns());
+    }
+
+    /**
+     * 功能描述: 复杂对象保存时,设置子表关联主表的外键值,
+     *
+     * @param tableNameIdMap 主表的map key:tableName value:id
+     * @param dto            需要保存的对象
+     * @param first          复杂对象保存,是否是最外层的
+     */
+    public static void putFkValue(Map<String, Long> tableNameIdMap, FormDataDto dto, boolean first) {
+        if (first) {
+            return;
+        }
+
+        JSONObject fixedData = dto.getFixedData();
+        for (Map.Entry<String, Object> entry : fixedData.entrySet()) {
+            Object value = entry.getValue();
+            if (value instanceof String) {
+                String strValue = (String) value;
+                if (!strValue.contains(FK_POINT)) {
+                    continue;
+                }
+                String[] split = strValue.split(POINT);
+                String lastTableName = split[split.length - 1];
+                Long maxId = tableNameIdMap.get(lastTableName);
+                if (maxId == null) {
+                    throw new RuntimeException(String.format("保存失败, 主表id: null, 表名为: %s ", lastTableName));
+                }
+                fixedData.put(entry.getKey(), maxId);
+                dto.setFixedData(fixedData);
+                break;
+            }
+        }
+    }
+
 }