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;
import java.text.MessageFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 服务实现类
*
*
* @author kimi
* @since 2020-03-23
*/
@Service
@Slf4j
public class MasterAuthorServiceImpl extends ServiceImpl implements IMasterAuthorService {
public static final String characterId = "characterId";
@Autowired
IMasterAuthorDetailService authorDetailService;
@Autowired
ISysMenuService menuService;
@Autowired
ISysFieldService fieldService;
@Autowired
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 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 tableField = tableInfoMapper.getTableField(masterAuthor.getTableName());
List fieldByMaintain = fieldService.getFieldByMaintain(maintain.getId());
List 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().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 merageRoleAuthor(Set roleIds) {
List masterAuthors = selectList(new EntityWrapper().eq(Constant.TYPE, MasterAuthorType.role).in(MasterAuthorController.character_id, roleIds));
HashMap resultMap = new HashMap<>();
for (MasterAuthor masterAuthor : masterAuthors) {
List masterAuthorDetails = authorDetailService.selectList(new EntityWrapper().eq(Constant.PARENT_ID, masterAuthor.getId()));
masterAuthor.setFields(masterAuthorDetails);
String key = DbUtils.getFieldRedisKey(masterAuthor.getTableName(), masterAuthor.getMaintainFieldId());
MasterAuthor preMerageMasterAuthor = resultMap.get(key);
if (preMerageMasterAuthor == null) {
resultMap.put(key, masterAuthor);
}
else {
preMerageMasterAuthor = merage(preMerageMasterAuthor, masterAuthor);
resultMap.put(key, preMerageMasterAuthor);
}
}
return resultMap;
}
/**
*
* @description: 根据用户获取有权限的主题
* @param character 用户
* @return: 主题列表
*
*/
@Override
public List getMenu(Character character) {
return getMenu(character, false);
}
/**
*
* @description: 根据用户获取有权限的主题 不包括父主题
* @param character 用户
* @return: 不包括夫主题的主题列表
*
*/
@Override
public List getMenuUnParent(Character character) {
return getMenuUnParent(character, false);
}
/**
*
* @description: 根据用户获取有权限的主题 不包括父主题
* @param character 用户
* @param isTableMenu 是否已存在表的主题
* @return: 不包括夫主题的主题列表
*
*/
@Override
public List getMenuUnParent(Character character, boolean isTableMenu) {
String key = DbUtils.StrJoinLink(Constant.UnderLine,character.getId(),"menuUnParent", String.valueOf(isTableMenu));
List