From c007f0ca1785db093d48f4846cda82fe8e955765 Mon Sep 17 00:00:00 2001
From: kimi <kimi42345@gmail.com>
Date: 星期三, 27 五月 2020 09:59:29 +0800
Subject: [PATCH] merage

---
 src/main/java/com/highdatas/mdm/service/impl/MasterAuthorServiceImpl.java |  799 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 723 insertions(+), 76 deletions(-)

diff --git a/src/main/java/com/highdatas/mdm/service/impl/MasterAuthorServiceImpl.java b/src/main/java/com/highdatas/mdm/service/impl/MasterAuthorServiceImpl.java
index d0c18b9..0dbf36b 100644
--- a/src/main/java/com/highdatas/mdm/service/impl/MasterAuthorServiceImpl.java
+++ b/src/main/java/com/highdatas/mdm/service/impl/MasterAuthorServiceImpl.java
@@ -1,16 +1,24 @@
 package com.highdatas.mdm.service.impl;
 
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.baomidou.mybatisplus.mapper.Wrapper;
 import com.baomidou.mybatisplus.service.impl.ServiceImpl;
 import com.highdatas.mdm.controller.MasterAuthorController;
+import com.highdatas.mdm.entity.Character;
 import com.highdatas.mdm.entity.*;
 import com.highdatas.mdm.mapper.MasterAuthorMapper;
+import com.highdatas.mdm.mapper.TableInfoMapper;
+import com.highdatas.mdm.pojo.ActivitiStatus;
 import com.highdatas.mdm.pojo.MasterAuthorType;
+import com.highdatas.mdm.pojo.Page;
+import com.highdatas.mdm.pojo.kettle.UnBigDataDataSourceInfo;
 import com.highdatas.mdm.service.*;
 import com.highdatas.mdm.util.Constant;
 import com.highdatas.mdm.util.ContentBuilder;
 import com.highdatas.mdm.util.DbUtils;
+import com.highdatas.mdm.util.RedisClient;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -28,12 +36,11 @@
  * @since 2020-03-23
  */
 @Service
+@Slf4j
 public class MasterAuthorServiceImpl extends ServiceImpl<MasterAuthorMapper, MasterAuthor> implements IMasterAuthorService {
     public static final String characterId = "characterId";
     @Autowired
     IMasterAuthorDetailService authorDetailService;
-    @Autowired
-    ITUserRoleService userRoleService;
     @Autowired
     ISysMenuService menuService;
     @Autowired
@@ -42,9 +49,88 @@
     IMaintainFieldService maintainFieldService;
     @Autowired
     IMaintainService maintainService;
-
+    @Autowired
+    IFlowsService flowsService;
+    @Autowired
+    UnBigDataDataSourceInfo unBigDataDataSourceInfo;
+    @Autowired
+    IMenuMappingService menuMappingService;
+    @Autowired
+    IMaintainDetailService maintainDetailService;
+    @Autowired
+    MasterDataService masterDataService;
+    @Autowired
+    TableInfoMapper tableInfoMapper;
+    @Autowired
+    RedisClient redisClient;
+    /**
+     *
+     * @description:  鏍规嵁鏉冮檺鑾峰彇鍒嗛〉瀵硅薄
+     * @param user 鐢ㄦ埛
+     * @param masterAuthor 鏉冮檺瀵硅薄
+     * @param maintain 鏁版嵁鐗堟湰
+     * @return: 鑾峰彇鍒嗛〉瀵硅薄
+     *
+     */
     @Override
-    public HashMap<String, MasterAuthor> merageRoleAuthor(List<String> roleIds) {
+    public Page getInitPageInfo(MasterAuthor masterAuthor, Maintain maintain, TUser user, boolean getIncrement) {
+        String realRedisKey = RedisClient.getRealRedisKey(user.getUserId() + "-" + maintain.getId() + "-" + getIncrement);
+        Object redisValObj = redisClient.getRedisValObj(realRedisKey);
+        if (redisValObj != null) {
+            JSONObject object = (JSONObject) redisValObj;
+            Page page = JSONObject.parseObject(object.toJSONString(), Page.class);
+            return page;
+        }
+        Page page = new Page(0);
+        List<TableSchemaResult> tableField = tableInfoMapper.getTableField(masterAuthor.getTableName());
+
+        List<SysField> fieldByMaintain = fieldService.getFieldByMaintain(maintain.getId());
+        List<String> authorFieldList = fieldByMaintain.stream().map(sysField -> sysField.getField()).collect(Collectors.toList());
+        int totalLength = 0;
+        for (TableSchemaResult tableSchemaResult : tableField) {
+            String fieldName = tableSchemaResult.getFieldName();
+            if (!authorFieldList.contains(fieldName)) {
+                continue;
+            }
+            int length = tableSchemaResult.getLength();
+            totalLength += length;
+        }
+
+        int pageSize = Constant.MaxDispenseSize / totalLength;
+        page.setPageSize(pageSize);
+        long totalCnt;
+        if (getIncrement) {
+            totalCnt = maintainDetailService.selectCount(new EntityWrapper<MaintainDetail>().eq(Constant.PARENT_ID, maintain.getId()));
+        }else {
+            String filter = getFilter(user, maintain.getId());
+            totalCnt = masterDataService.getCountByVersion(user, maintain.getTableName(), filter, maintain.getVersion(), false);
+        }
+        page.setRecordCount(Long.valueOf(totalCnt).intValue());
+        if (totalCnt == 0) {
+            page.setRecordCount(0);
+            redisClient.putRedisValObj(realRedisKey, page);
+            return page;
+        }
+        int pages;
+        if (totalCnt % pageSize == 0) {
+            pages = Long.valueOf(totalCnt/pageSize).intValue();
+        }else  {
+            pages =  (Long.valueOf(totalCnt/pageSize).intValue())  + 1;
+        }
+        page.setPages(pages);
+
+        redisClient.putRedisValObj(realRedisKey, page);
+        return page;
+    }
+    /**
+     *
+     * @description:  鏍规嵁瑙掕壊id鑾峰彇娣峰悎鍚庣殑鏉冮檺
+     * @param roleIds 瑙掕壊id
+     * @return: 娣峰悎鍚庣殑鏉冮檺
+     *
+     */
+    @Override
+    public HashMap<String, MasterAuthor> merageRoleAuthor(Set<String> roleIds) {
         List<MasterAuthor> masterAuthors = selectList(new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.role).in(MasterAuthorController.character_id, roleIds));
         HashMap<String, MasterAuthor> resultMap = new HashMap<>();
 
@@ -64,119 +150,587 @@
         }
         return resultMap;
     }
-
+    /**
+     *
+     * @description:  鏍规嵁鐢ㄦ埛鑾峰彇鏈夋潈闄愮殑涓婚
+     * @param character 鐢ㄦ埛
+     * @return: 涓婚鍒楄〃
+     *
+     */
     @Override
-    public List<SysMenu> getMenu(String userId) {
-        List<MasterAuthor> masterAuthors = selectList(new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.user).eq(MasterAuthorController.character_id, userId));
-        if (masterAuthors.size() == 0) {
-            //user 鑾峰彇role
-            List<TUserRole> roles = userRoleService.selectList(new EntityWrapper<TUserRole>().eq("user_id", userId));
-            List<String> roleIds = roles.stream().map(tUserRole -> tUserRole.getRoleId()).collect(Collectors.toList());
-            masterAuthors = selectList(new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.role).in(MasterAuthorController.character_id, roleIds));
-        }
-        List<String> menuIds = masterAuthors.stream().map(masterAuthor -> masterAuthor.getMenuId()).collect(Collectors.toList());
-        LinkedHashSet<String> strings = new LinkedHashSet<>(menuIds);
-        LinkedHashSet<String> byParentId = menuService.getByParentId(strings);
-        List<SysMenu> sysMenus = menuService.selectBatchIds(byParentId);
-        return sysMenus;
+    public List<SysMenu> getMenu(Character character) {
+        return getMenu(character, false);
+    }
+    /**
+     *
+     * @description:  鏍规嵁鐢ㄦ埛鑾峰彇鏈夋潈闄愮殑涓婚 涓嶅寘鎷埗涓婚
+     * @param character 鐢ㄦ埛
+     * @return: 涓嶅寘鎷か涓婚鐨勪富棰樺垪琛�
+     *
+     */
+    @Override
+    public List<SysMenu> getMenuUnParent(Character character) {
+        return getMenuUnParent(character, false);
     }
 
+    /**
+     *
+     * @description:  鏍规嵁鐢ㄦ埛鑾峰彇鏈夋潈闄愮殑涓婚 涓嶅寘鎷埗涓婚
+     * @param character 鐢ㄦ埛
+     * @param isTableMenu 鏄惁宸插瓨鍦ㄨ〃鐨勪富棰�
+     * @return: 涓嶅寘鎷か涓婚鐨勪富棰樺垪琛�
+     *
+     */
     @Override
-    public List<SysField> getField(String userId, String maintainFieldId, String maintainId) {
-        if (maintainFieldId.equalsIgnoreCase(Constant.All)) {
-            List<SysField> total = fieldService.getFieldByMaintain(maintainId);
-            return total;
+    public List<SysMenu> getMenuUnParent(Character character, boolean isTableMenu) {
+        String key = DbUtils.StrJoinLink(Constant.UnderLine,character.getId(),"menuUnParent",  String.valueOf(isTableMenu));
+        List<Object> redisList = redisClient.getRedisList(key);
+        if (redisList != null) {
+            List<SysMenu> result = new ArrayList<>();
+            for (Object o : redisList) {
+                JSONObject object = (JSONObject) o;
+                SysMenu menu = JSONObject.parseObject(object.toJSONString(), SysMenu.class);
+                result.add(menu);
+            }
+            return result;
         }
-        List<MasterAuthor> masterAuthors = selectList(new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.user).eq(this.characterId, userId).eq("maintain_field_id", maintainFieldId));
-        if (masterAuthors.size() == 0) {
-            //user 鑾峰彇role
-            List<TUserRole> roles = userRoleService.selectList(new EntityWrapper<TUserRole>().eq("user_id", userId));
-            List<String> roleIds = roles.stream().map(tUserRole -> tUserRole.getRoleId()).collect(Collectors.toList());
-            masterAuthors = selectList(new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.role).in(this.characterId, roleIds).eq("maintain_field_id", maintainFieldId));
+        MasterAuthorType type = character.getType();
+        List<MasterAuthor> masterAuthors = null;
+        switch (type){
+            case role:
+                masterAuthors = getRoleAuthors(character.getId(), null);
+                break;
+            case groupInfo:
+                masterAuthors = getOneGroupAuthors(character.getId(), null);
+                break;
+            case user:
+                masterAuthors = getUserAuthor(character.getId(), null);
         }
-        if (masterAuthors.isEmpty()) {
+        if (masterAuthors == null || masterAuthors.isEmpty()) {
             return null;
         }
+        List<String> menuIds = masterAuthors.stream().map(masterAuthor -> masterAuthor.getMenuId()).collect(Collectors.toList());
+
+        if (isTableMenu) {
+            List<String> tempList = new ArrayList<>();
+            for (String menuId : menuIds) {
+                String tableNameByMenu = menuMappingService.getTableNameByMenu(menuId);
+                boolean exists = unBigDataDataSourceInfo.checkTableExists(tableNameByMenu);
+                if (exists) {
+                    tempList.add(menuId);
+                }
+            }
+            menuIds = tempList;
+        }
+        List<SysMenu> sysMenus = menuService.selectBatchIds(menuIds);
+        redisClient.putRedisList(key, (sysMenus));
+        return sysMenus;
+    }
+    /**
+     *
+     * @description:  鏍规嵁鐢ㄦ埛鑾峰彇鏈夋潈闄愮殑涓婚
+     * @param character 鐢ㄦ埛
+     * @param isTableMenu 鏄惁涓哄凡缁忕敓鎴愯〃鐨勪富棰�
+     * @return: 涓婚鍒楄〃
+     *
+     */
+    @Override
+    public List<SysMenu> getMenu(Character character, boolean isTableMenu) {
+
+        String key = DbUtils.StrJoinLink(Constant.UnderLine, character.getId(),"menu", String.valueOf(isTableMenu));
+        List<Object> redisList = redisClient.getRedisList(key);
+        if (redisList != null) {
+            List<SysMenu> result = new ArrayList<>();
+            for (Object o : redisList) {
+                JSONObject object = (JSONObject) o;
+                SysMenu menu = JSONObject.parseObject(object.toJSONString(), SysMenu.class);
+                result.add(menu);
+            }
+            return result;
+        }
+
+        MasterAuthorType type = character.getType();
+        List<MasterAuthor> masterAuthors = null;
+        switch (type){
+            case role:
+                masterAuthors = getRoleAuthors(character.getId(), null);
+                break;
+            case groupInfo:
+                masterAuthors = getOneGroupAuthors(character.getId(), null);
+                break;
+            case user:
+                masterAuthors = getUserAuthor(character.getId(), null);
+        }
+        if (masterAuthors == null || masterAuthors.isEmpty()) {
+            return null;
+        }
+        List<String> menuIds = masterAuthors.stream().map(masterAuthor -> masterAuthor.getMenuId()).collect(Collectors.toList());
+
+        if (isTableMenu) {
+            List<String> tempList = new ArrayList<>();
+            for (String menuId : menuIds) {
+                String tableNameByMenu = menuMappingService.getTableNameByMenu(menuId);
+                boolean exists = unBigDataDataSourceInfo.checkTableExists(tableNameByMenu);
+                if (exists) {
+                    tempList.add(menuId);
+                }
+            }
+            menuIds = tempList;
+        }
+
+        LinkedHashSet<String> strings = new LinkedHashSet<>(menuIds);
+        List<SysMenu> sysMenus = menuService.getMenuByParentId(strings);
+        if (sysMenus == null) {
+            return null;
+        }
+        redisClient.putRedisList(key, (sysMenus));
+        return sysMenus;
+    }
+    /**
+     *
+     * @description:  鏍规嵁鐢ㄦ埛锛屽瓧娈电増鏈幏鍙栨潈闄愬垪琛�
+     * @param characterId 鐢ㄦ埛id
+     * @param maintainField 瀛楁鐗堟湰
+     * @return: 涓嶅寘鎷か涓婚鐨勪富棰樺垪琛�
+     *
+     */
+    @Override
+    public List<MasterAuthor> getUserAuthor(String characterId, MaintainField maintainField) {
+        return getUserAuthor(characterId, maintainField, null);
+    }
+    /**
+     *
+     * @description:  鏍规嵁鐢ㄦ埛锛屽瓧娈电増鏈幏鍙栨潈闄愬垪琛�
+     * @param characterId 鐢ㄦ埛id
+     * @param maintainField 瀛楁鐗堟湰
+     * @param uuid 鏃ュ織浣跨敤
+     * @return: 鏉冮檺鍒楄〃
+     *
+     */
+    @Override
+    public List<MasterAuthor> getUserAuthor(String characterId, MaintainField maintainField, String uuid) {
+        Date startDate = new Date();
+        Wrapper<MasterAuthor> userWrapper = new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.user).eq(MasterAuthorController.character_id, characterId);
+        int i = selectCount(userWrapper);
+        boolean isUser;
+        if (i == 0) {
+            isUser = false;
+        }else {
+            isUser = true;
+        }
+        if (maintainField != null) {
+            if (maintainField.getId() != null) {
+                userWrapper.eq("maintain_field_id", maintainField.getId());
+            }
+            if (maintainField.getTableName() != null) {
+                userWrapper.eq("table_name",maintainField.getTableName());
+            }
+        }
+        List<MasterAuthor> masterAuthors = selectList(userWrapper);
+        Date endDate = new Date();
+        log.info(MessageFormat.format("master tag:{0}  select user author:{1} ms",uuid, (endDate.getTime() - startDate.getTime())));
+
+        if (masterAuthors.isEmpty() && !isUser) {
+            //user 鑾峰彇role
+            Set<String> roleIdSet = DbUtils.getRoleByUser(characterId, uuid);
+            if (roleIdSet.isEmpty()) {
+                return new ArrayList<>();
+            }else {
+                Wrapper<MasterAuthor> roleWrapper = new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.role).in(MasterAuthorController.character_id, roleIdSet);
+                if (maintainField != null) {
+                    if (!StringUtils.isEmpty(maintainField.getId())) {
+                        roleWrapper.eq("maintain_field_id", maintainField.getId());
+                    }
+                    if (!StringUtils.isEmpty(maintainField.getTableName())) {
+                        roleWrapper.eq("table_name",maintainField.getTableName());
+                    }
+                }
+                masterAuthors = selectList(roleWrapper);
+            }
+
+            startDate = new Date();
+            log.info(MessageFormat.format("master tag:{0}  select role author by user:{1} ms",uuid, (startDate.getTime() - endDate.getTime())));
+
+        }
+        List<MasterAuthor> groupAuthors = getUserGroupAuthor(characterId, null, uuid);
+
+
+        endDate = new Date();
+        log.info(MessageFormat.format("master tag:{0}  merage group author:{1} ms",uuid, (endDate.getTime() - startDate.getTime())));
+
+        masterAuthors.addAll(groupAuthors);
+        return masterAuthors;
+    }
+    /**
+     *
+     * @description:  鏍规嵁瑙掕壊锛屽瓧娈电増鏈幏鍙栨潈闄愬垪琛�
+     * @param characterId 瑙掕壊id
+     * @param maintainField 瀛楁鐗堟湰
+     * @return: 鏉冮檺鍒楄〃
+     *
+     */
+    private List<MasterAuthor> getRoleAuthors(String characterId, MaintainField maintainField) {
+        Wrapper<MasterAuthor> roleWrapper = new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.role).eq(MasterAuthorController.character_id, characterId);
+        if (maintainField != null) {
+            if (maintainField.getId() != null) {
+                roleWrapper.eq("maintain_field_id", maintainField.getId());
+            }
+            if (maintainField.getTableName() != null) {
+                roleWrapper.eq("table_name",maintainField.getTableName());
+            }
+
+        }
+
+        return selectList(roleWrapper);
+    }
+
+    /**
+     *
+     * @description:  鏍规嵁鐢ㄦ埛锛屽瓧娈电増鏈幏鍙栨潈闄愬垪琛�
+     * @param userId 鐢ㄦ埛id
+     * @param maintainField 瀛楁鐗堟湰
+     * @param uuid 鏃ュ織浣跨敤
+     * @return: 鏉冮檺鍒楄〃
+     *
+     */
+        private List<MasterAuthor> getUserGroupAuthor(String userId, MaintainField maintainField, String uuid) {
+        Set<String> groupIdList = DbUtils.getGroupByUser(userId, uuid);
+        List<MasterAuthor> result = new ArrayList<>();
+        if (groupIdList == null) {
+            return result;
+        }
+
+        for (String groupId : groupIdList) {
+            List<MasterAuthor> oneGroupAuthors = getOneGroupAuthors(groupId, maintainField, uuid);
+            if (oneGroupAuthors == null || oneGroupAuthors.isEmpty()) {
+                continue;
+            }
+            result.addAll(oneGroupAuthors);
+        }
+
+        return result;
+    }
+    /**
+     *
+     * @description:  鏍规嵁鐢ㄦ埛缁勶紝瀛楁鐗堟湰鑾峰彇鏉冮檺鍒楄〃
+     * @param groupId 鐢ㄦ埛缁刬d
+     * @param maintainField 瀛楁鐗堟湰
+     * @return: 鏉冮檺鍒楄〃
+     *
+     */
+    @Override
+    public List<MasterAuthor> getOneGroupAuthors(String groupId, MaintainField maintainField) {
+        return getOneGroupAuthors(groupId, maintainField, null);
+    }
+    /**
+     *
+     * @description:  鏍规嵁鐢ㄦ埛缁勶紝瀛楁鐗堟湰鑾峰彇鏉冮檺鍒楄〃
+     * @param groupId 鐢ㄦ埛缁刬d
+     * @param maintainField 瀛楁鐗堟湰
+     * @param uuid 鏃ュ織浣跨敤
+     * @return: 鏉冮檺鍒楄〃
+     *
+     */
+        @Override
+    public List<MasterAuthor> getOneGroupAuthors(String groupId, MaintainField maintainField, String uuid) {
+            Date startDate = new Date();
+            Wrapper<MasterAuthor> userWrapper = new EntityWrapper<MasterAuthor>().eq("user_group", true).eq(Constant.TYPE, MasterAuthorType.user).eq(MasterAuthorController.character_id, groupId);
+            Wrapper<MasterAuthor> roleWrapper = new EntityWrapper<MasterAuthor>().eq("user_group", true).eq(Constant.TYPE, MasterAuthorType.role).eq(MasterAuthorController.character_id, groupId);
+            int i = selectCount(userWrapper);
+            boolean isUser;
+            if (i == 0) {
+                isUser = false;
+            }else {
+                isUser = true;
+            }
+            if (maintainField != null) {
+            if (maintainField.getId() != null) {
+                userWrapper.eq("maintain_field_id", maintainField.getId());
+                roleWrapper.eq("maintain_field_id", maintainField.getId());
+            }
+            if (maintainField.getTableName() != null) {
+                userWrapper.eq("table_name",maintainField.getTableName());
+                roleWrapper.eq("table_name",maintainField.getTableName());
+            }
+
+        }
+        List<MasterAuthor> groupAuthors = selectList(userWrapper);
+            Date endDate = new Date();
+            log.info(MessageFormat.format("master tag:{0}  select group author:{1} ms",uuid, (endDate.getTime() - startDate.getTime())));
+
+            if (groupAuthors.isEmpty()  && !isUser) {
+            Set<String> roleByGroup = DbUtils.getRoleByGroup(groupId, uuid);
+            if(roleByGroup.isEmpty()) {
+                groupAuthors = new ArrayList<>();
+            }else {
+                groupAuthors = selectList(roleWrapper);
+            }
+
+                startDate = new Date();
+                log.info(MessageFormat.format("master tag:{0} select role author by group :{1} ms",uuid, (startDate.getTime() - endDate.getTime())));
+
+            }
+        return groupAuthors;
+    }
+    /**
+     *
+     * @description:  鏍规嵁鐢ㄦ埛锛屽瓧娈电増鏈幏鍙栧瓧娈靛垪琛�
+     * @param character 鐢ㄦ埛
+     * @param maintainId 鏁版嵁鐗堟湰id
+     * @return: 瀛楁鍒楄〃
+     *
+     */
+    @Override
+    public List<SysField> getField(Character character, String maintainId) {
+        return getField(character, maintainId, null);
+    }
+    /**
+     *
+     * @description:  鏍规嵁鐢ㄦ埛锛屽瓧娈电増鏈幏鍙栧瓧娈靛垪琛�
+     * @param character 鐢ㄦ埛
+     * @param maintainId 鏁版嵁鐗堟湰id
+     * @param uuid 鏃ュ織浣跨敤
+     * @return: 瀛楁鍒楄〃
+     *
+     */
+    @Override
+    public List<SysField> getField(Character character, String maintainId, String uuid) {
+        String key = DbUtils.StrJoinLink(Constant.UnderLine, character.getId(),"field", maintainId);
+        List<Object> redisList = redisClient.getRedisList(key);
+        if (redisList != null) {
+            List<SysField> result = new ArrayList<>();
+            for (Object o : redisList) {
+                JSONObject object = (JSONObject) o;
+                SysField sysField = JSONObject.parseObject(object.toJSONString(), SysField.class);
+                result.add(sysField);
+            }
+            return result;
+        }
+        Maintain maintain = maintainService.selectById(maintainId);
+        if (maintain == null){
+            return new ArrayList<>();
+        }
+        Date startDate = new Date();
+        boolean isAll = checkUnFilterAuthor(character, maintain.getTableName(), uuid);
+        Date endDate = new Date();
+        log.info(MessageFormat.format("master tag:{0} field check isAll:{1} ms",uuid, (endDate.getTime() - startDate.getTime())));
+
+        if (isAll) {
+            List<SysField> total = fieldService.getFieldByMaintain(maintainId);
+            redisClient.putRedisList(key, (total));
+            return total;
+        }
+
+        MaintainField maintainField = fieldService.getMaintainFieldByMaintain(maintainId);
+        if (maintainField == null){
+            return new ArrayList<>();
+        }
+        String maintainFieldId = maintainField.getId();
+
+        MasterAuthorType type = character.getType();
+        List<MasterAuthor> masterAuthors = null;
+        switch (type){
+            case role:
+                masterAuthors = getRoleAuthors(character.getId(), maintainField);
+                break;
+            case groupInfo:
+                masterAuthors = getOneGroupAuthors(character.getId(), maintainField, uuid);
+                break;
+            case user:
+                masterAuthors = getUserAuthor(character.getId(), maintainField, uuid);
+        }
+        startDate = new Date();
+        log.info(MessageFormat.format("master tag:{0} field get author:{1} ms",uuid, (startDate.getTime() - endDate.getTime())));
+
+        if (masterAuthors == null || masterAuthors.isEmpty()) {
+            return null;
+        }
+
         List<String> authorIds = masterAuthors.stream().map(masterAuthor -> masterAuthor.getId()).collect(Collectors.toList());
         List<MasterAuthorDetail> masterAuthorDetails = authorDetailService.selectList(new EntityWrapper<MasterAuthorDetail>().in(Constant.PARENT_ID, authorIds));
         Set<String> codes = masterAuthorDetails.stream().map(masterAuthorDetail -> masterAuthorDetail.getField()).collect(Collectors.toSet());
         if (codes.isEmpty()) {
-            return null;
+            return new ArrayList<>();
         }
-        Wrapper<SysField> wrapper = new EntityWrapper<SysField>().in(Constant.FIELD, codes);
+        Wrapper<SysField> wrapper = new EntityWrapper<SysField>().in(Constant.FIELD, codes).eq("table_name", maintain.getTableName());
 
-        if (maintainFieldId.equalsIgnoreCase(Constant.Default)) {
+        if (!maintainFieldId.equalsIgnoreCase(Constant.Default)) {
             wrapper.eq("maintain_field_id", maintainFieldId);
         }else {
             wrapper.isNull("maintain_field_id");
         }
+        wrapper.orderBy("order_no");
+
         List<SysField> fieldList = fieldService.selectList(wrapper);
+        endDate = new Date();
+        log.info(MessageFormat.format("master tag:{0} field get field:{1} ms",uuid, (endDate.getTime() - startDate.getTime())));
 
-
-
+        redisClient.putRedisList(key, ((fieldList)));
         return fieldList;
     }
+    /**
+     *
+     * @description:  妫�娴嬫槸鍚﹂渶瑕佺瓫閫夋暟鎹�
+     * @param character 鐢ㄦ埛
+     * @param tableName 琛ㄥ悕
+     * @param uuid 鏃ュ織浣跨敤
+     * @return: 鏄惁闇�瑕佺瓫閫夋暟鎹�
+     *
+     */
+    private boolean checkUnFilterAuthor(Character character, String tableName, String uuid) {
+        String key = DbUtils.StrJoinLink(Constant.UnderLine,  character.getId(),"checkUnFilterAuthor", tableName);
+        String redisVal = redisClient.getRedisVal(key);
+        if (!StringUtils.isEmpty(redisVal)) {
+            return Boolean.valueOf(redisVal);
+        }
+        MasterAuthorType type = character.getType();
+        List<MasterAuthor> masterAuthors = null;
+        MaintainField maintainField = new MaintainField().setTableName(tableName);
+        switch (type){
+            case role:
+                masterAuthors = getRoleAuthors(character.getId(), maintainField);
+                break;
+            case groupInfo:
+                masterAuthors = getOneGroupAuthors(character.getId(), maintainField, uuid);
+                break;
+            case user:
+                masterAuthors = getUserAuthor(character.getId(), maintainField, uuid);
+        }
+        if (masterAuthors.isEmpty()) {
+            return false;
+        }
+        long isAll = masterAuthors.stream()
+                .filter(masterAuthor -> !StringUtils.isEmpty(masterAuthor.getMaintainFieldId()))
+                .filter(masterAuthor -> masterAuthor.getMaintainFieldId().equalsIgnoreCase(Constant.All)).count();
+        if (isAll > 0) {
+            redisClient.putRedisVal(key, "true");
+            return true;
+        }else {
+            redisClient.putRedisVal(key, "false");
+            return false;
+        }
 
+    }
+    /**
+     *
+     * @description:  妫�楠岀敤鎴锋槸鍚︽嫢鏈夋暟鎹増鏈殑鏉冮檺
+     * @param character 鐢ㄦ埛
+     * @param maintainId 鏁版嵁鐗堟湰id
+     * @return: 鏄惁鏈夋潈闄�
+     *
+     */
     @Override
-    public boolean checkMaintainAuthor(String userId, String maintainId) {
+    public boolean checkMaintainAuthor(Character character, String maintainId) {
+        return checkMaintainAuthor(character, maintainId, null);
+    }
+    @Override
+    public boolean checkMaintainAuthor(Character character, String maintainId, String uuid) {
+        String key = DbUtils.StrJoinLink(Constant.UnderLine, character.getId(), "checkMaintainAuthor",  maintainId);
+        String redisVal = redisClient.getRedisVal(key);
+        if (!StringUtils.isEmpty(redisVal)) {
+            return Boolean.valueOf(redisVal);
+        }
         Maintain maintain = maintainService.selectById(maintainId);
         if (maintain == null) {
             return false;
         }
-        int checked = selectCount(new EntityWrapper<MasterAuthor>()
-                .eq(Constant.TYPE, MasterAuthorType.user)
-                .eq(MasterAuthorController.character_id, userId)
-                .eq("table_name", maintain.getTableName())
-                .eq("maintain_field_id", Constant.All));
-        if(checked > 0) {
+        int checked = 0;
+        boolean isAll = checkUnFilterAuthor(character, maintain.getTableName(), uuid);
+        if (isAll){
             return true;
         }
         MaintainField maintainField = fieldService.getMaintainFieldByMaintain(maintainId);
         if (maintainField == null) {
             return false;
         }
-        //get user author
-        checked = selectCount(new EntityWrapper<MasterAuthor>()
-                .eq(Constant.TYPE, MasterAuthorType.user)
-                .eq(MasterAuthorController.character_id, userId)
-                .eq("table_name", maintain.getTableName())
-                .eq("maintain_field_id", maintainField.getId()));
-        if (checked > 0) {
+        MasterAuthorType type = character.getType();
+        List<MasterAuthor> masterAuthors = null;
+        switch (type){
+            case role:
+                masterAuthors = getRoleAuthors(character.getId(), maintainField);
+                break;
+            case groupInfo:
+                masterAuthors = getOneGroupAuthors(character.getId(), maintainField, uuid);
+                break;
+            case user:
+                masterAuthors = getUserAuthor(character.getId(), maintainField, uuid);
+        }
+        if (masterAuthors.isEmpty()) {
+            redisClient.putRedisVal(key, "false");
+            return false;
+        }else {
+            redisClient.putRedisVal(key, "true");
             return true;
         }
-        List<TUserRole> roles = userRoleService.selectList(new EntityWrapper<TUserRole>().eq("user_id", userId));
-        Set<String> collect = roles.stream().map(tUserRole -> tUserRole.getRoleId()).collect(Collectors.toSet());
-        checked = selectCount(new EntityWrapper<MasterAuthor>()
-                .eq(Constant.TYPE, MasterAuthorType.role)
-                .in(MasterAuthorController.character_id, collect)
-                .eq("table_name", maintain.getTableName())
-                .eq("maintain_field_id", maintainField.getId()));
-        if (checked > 0) {
-            return true;
-        }
-        return false;
     }
-
+    /**
+     *
+     * @description:  鏍规嵁鐢ㄦ埛锛屽瓧娈电増鏈幏鍙栫瓫閫夋潯浠�
+     * @param character 鐢ㄦ埛
+     * @param maintainId 鏁版嵁鐗堟湰id
+     * @return: 绛涢�夋潯浠�
+     *
+     */
     @Override
-    public String getFilter(String userId, String maintainId) {
-        MaintainField maintainField = fieldService.getMaintainFieldByMaintain(maintainId);
-        if (maintainField)
-        if (maintainFieldId.equalsIgnoreCase(Constant.All)) {
-            return Constant.WHERE_DEFAULT;
+    public String getFilter(Character character, String maintainId) {
+
+        return getFilter(character, maintainId, null);
+    }
+    /**
+     *
+     * @description:  鏍规嵁鐢ㄦ埛锛屽瓧娈电増鏈幏鍙栫瓫閫夋潯浠�
+     * @param character 鐢ㄦ埛
+     * @param maintainId 鏁版嵁鐗堟湰id
+     * @param uuid 鏃ュ織浣跨敤
+     * @return: 绛涢�夋潯浠�
+     *
+     */
+    @Override
+    public String getFilter(Character character, String maintainId, String uuid) {
+        String key = DbUtils.StrJoinLink(Constant.UnderLine, character.getId(),"filter",maintainId);
+        String redisVal = redisClient.getRedisVal(key);
+        if (!StringUtils.isEmpty(redisVal)) {
+            return redisVal;
         }
-        List<MasterAuthor> masterAuthors = selectList(new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.user).eq(this.characterId, userId).eq("maintain_field_id", maintainFieldId));
-        if (masterAuthors.size() == 0) {
-            //user 鑾峰彇role
-            List<TUserRole> roles = userRoleService.selectList(new EntityWrapper<TUserRole>().eq("user_id", userId));
-            List<String> roleIds = roles.stream().map(tUserRole -> tUserRole.getRoleId()).collect(Collectors.toList());
-            masterAuthors = selectList(new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.role).in(this.characterId, roleIds).eq("maintain_field_id", maintainFieldId));
-        }
-        if (masterAuthors.isEmpty()){
+        Maintain maintain = maintainService.selectById(maintainId);
+        if (maintain == null){
             return Constant.WHERE_DEFAULTUN;
         }
+        Date startDate = new Date();
+
+        boolean isAll = checkUnFilterAuthor(character, maintain.getTableName(), uuid);
+        Date endDate = new Date();
+        log.info(MessageFormat.format("master tag:{0} filter check isAll:{1} ms",uuid, (endDate.getTime() - startDate.getTime())));
+
+        if (isAll) {
+            redisClient.putRedisVal(key, Constant.WHERE_DEFAULT);
+            return Constant.WHERE_DEFAULT;
+        }
+        MaintainField maintainField = fieldService.getMaintainFieldByMaintain(maintainId);
+        MasterAuthorType type = character.getType();
+        List<MasterAuthor> masterAuthors = null;
+        switch (type){
+            case role:
+                masterAuthors = getRoleAuthors(character.getId(), maintainField);
+                break;
+            case groupInfo:
+                masterAuthors = getOneGroupAuthors(character.getId(), maintainField, uuid);
+                break;
+            case user:
+                masterAuthors = getUserAuthor(character.getId(), maintainField, uuid);
+        }
+        startDate = new Date();
+        log.info(MessageFormat.format("master tag:{0} filter get author:{1} ms",uuid, (startDate.getTime() - endDate.getTime())));
+
+        if (masterAuthors.isEmpty()) {
+            redisClient.putRedisVal(key, Constant.WHERE_DEFAULTUN);
+            return Constant.WHERE_DEFAULTUN;
+        }
+
         List<String> authorIds = masterAuthors.stream().map(masterAuthor -> masterAuthor.getId()).collect(Collectors.toList());
         List<MasterAuthorDetail> masterAuthorDetails = authorDetailService.selectList(new EntityWrapper<MasterAuthorDetail>().in(Constant.PARENT_ID, authorIds));
         if (masterAuthorDetails.isEmpty()) {
+            redisClient.putRedisVal(key, Constant.WHERE_DEFAULTUN);
             return Constant.WHERE_DEFAULTUN;
         }
         HashMap<String, List<String>> segmentMap = new HashMap<>();
@@ -190,8 +744,11 @@
             }
 
             Boolean preAll = segmentAllMap.get(field);
-            if (preAll && segmentMap.containsKey(field)) {
-                segmentMap.remove(field);
+            if (preAll) {
+                if (segmentMap.containsKey(field)) {
+                    segmentMap.remove(field);
+                }
+                continue;
             }
 
             String val = detail.getVal();
@@ -205,13 +762,103 @@
             String val = vals.stream()
                     .filter(s -> !StringUtils.isEmpty(s))
                     .map(s -> DbUtils.quotedStr(s)).collect(Collectors.joining(Constant.COMMA));
-            String format = MessageFormat.format(Constant.MYSQL_UUID, code, val);
+            String format = MessageFormat.format(Constant.InSql, code, val);
             builder.append(format);
         }
         String filter = builder.toString();
+        endDate = new Date();
+        log.info(MessageFormat.format("master tag:{0} filter merage filter:{1} ms",uuid, (endDate.getTime() - startDate.getTime())));
+
+        if (StringUtils.isEmpty(filter)) {
+            redisClient.putRedisVal(key, Constant.WHERE_DEFAULT);
+            return Constant.WHERE_DEFAULT;
+        }
+
+        redisClient.putRedisVal(key, filter);
         return filter;
     }
+    /**
+     *
+     * @description:  鏍规嵁鐢ㄦ埛鑾峰彇鏈�澶ф潈闄愮殑鏁版嵁鐗堟湰
+     * @param character 鐢ㄦ埛
+     * @param tableName 琛ㄥ悕
+     * @return: 鏈�澶ф潈闄愮殑鏁版嵁鐗堟湰
+     *
+     */
+    @Override
+    public Maintain getMaxVersionMaintain(Character character, String tableName) {
+        List<Maintain> maintainList = maintainService.selectList(new EntityWrapper<Maintain>().eq("table_name", tableName).orderBy("order_no desc"));
+        for (Maintain maintain : maintainList) {
+            ActivitiStatus status = flowsService.getStatusByBusinessId(maintain.getId());
+            if (!ActivitiStatus.open.equals(status)) {
+                continue;
+            }
+            boolean b = checkMaintainAuthor(character, maintain.getId());
+            if (b) {
+                return maintain;
+            }
+        }
+        return null;
+    }
+    /**
+     *
+     * @description:  鏍规嵁鐢ㄦ埛锛屽瓧娈电増鏈幏鍙栨暟鎹増鏈垪琛�
+     * @param user 鐢ㄦ埛
+     * @param tableName 鏁版嵁鐗堟湰id
+     * @return: 鏁版嵁鐗堟湰鍒楄〃
+     *
+     */
+    @Override
+    public LinkedHashSet<Maintain> getMaintainSet(String tableName, TUser user) {
+        List<MasterAuthor> userAuthor = this.getUserAuthor(user.getUserId(), null);
+        LinkedHashSet<Maintain> maintainSet = new LinkedHashSet<>();
+        Collections.reverse(userAuthor);
 
+        for (MasterAuthor masterAuthor : userAuthor) {
+            String tabName = masterAuthor.getTableName();
+            String maintainFieldId = masterAuthor.getMaintainFieldId();
+            if (StringUtils.isEmpty(tabName) || !tableName.equalsIgnoreCase(tableName) || StringUtils.isEmpty(maintainFieldId)) {
+                continue;
+            }
+            List<Maintain> maintainByMaintainField = maintainFieldService.getMaintainByMaintainField(maintainFieldId, tableName);
+            if (maintainByMaintainField != null) {
+                maintainSet.addAll(maintainByMaintainField);
+            }
+
+        }
+        return maintainSet;
+    }
+    /**
+     *
+     * @description:  鐢熸垚鐗堟湰鐨勯渶瑕佹娴嬪摢浜涢渶瑕佽嚜鍔ㄥ姞涓婃潈闄�
+     * @param status 娴佺▼鐘舵��
+     * @param maintainId 鏁版嵁鐗堟湰id
+     * @return:
+     *
+     */
+    public void dealFlow(String maintainId, ActivitiStatus status) {
+        Maintain maintain = maintainService.selectById(maintainId);
+        MaintainField maintainFieldByMaintain = fieldService.getMaintainFieldByMaintain(maintainId);
+        SysMenu menuByTableName = menuMappingService.getMenuByTableName(maintain.getTableName());
+
+        List<MasterAuthor> masterAuthors = selectList(new EntityWrapper<MasterAuthor>()
+                .eq("maintain_field_id", maintainFieldByMaintain.getId())
+                .eq("table_name", maintain.getTableName())
+                .eq("menu_id", menuByTableName.getId())
+                .eq("maintain_auto", true));
+        for (MasterAuthor masterAuthor : masterAuthors) {
+
+        }
+    }
+
+    /**
+     *
+     * @description:  涓や釜鏉冮檺娣峰悎
+     * @param preMerageMasterAuthor 娣峰悎鍓�
+     * @param masterAuthor 寰呮贩鍚�
+     * @return: 娣峰悎鍚庣殑鏉冮檺
+     *
+     */
     private MasterAuthor merage(MasterAuthor preMerageMasterAuthor, MasterAuthor masterAuthor) {
         // table name  masterField 涓�鏍� 鍙湁瀛楁涓嶅悓浜�
         List<MasterAuthorDetail> preFields = preMerageMasterAuthor.getFields();

--
Gitblit v1.8.0