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.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.highdatas.mdm.entity.Maintain;
import com.highdatas.mdm.entity.MenuMapping;
import com.highdatas.mdm.entity.SysMenu;
import com.highdatas.mdm.entity.TUser;
import com.highdatas.mdm.mapper.MenuMappingMapper;
import com.highdatas.mdm.mapper.TableInfoMapper;
import com.highdatas.mdm.pojo.CodeMsg;
import com.highdatas.mdm.pojo.MaintainDataType;
import com.highdatas.mdm.pojo.Result;
import com.highdatas.mdm.service.ActivitiService;
import com.highdatas.mdm.service.IMaintainService;
import com.highdatas.mdm.service.IMenuMappingService;
import com.highdatas.mdm.service.ISysMenuService;
import com.highdatas.mdm.util.Constant;
import com.highdatas.mdm.util.DbUtils;
import com.highdatas.mdm.util.WorkflowUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;
/**
*
* 服务实现类
*
*
* @author kimi
* @since 2019-12-16
*/
@Service
public class MenuMappingServiceImpl extends ServiceImpl implements IMenuMappingService {
@Autowired
TableInfoMapper tableInfoMapper;
@Autowired
MenuMappingMapper menuMappingMapper;
@Autowired
IMaintainService maintainService;
@Autowired
ActivitiService activitiService;
@Autowired
ISysMenuService menuService;
public static final String key = "process";
@Override
public MenuMapping create(String json, HttpSession session) {
JSONObject jsonObject = JSONObject.parseObject(json);
// //1
// Flows flows = activitiService.start(key, session);
// if (flows == null) {
// return false;
// }
// //2
// String flowsId = flows.getId();
TUser user = (TUser)session.getAttribute(Constant.USER);
String userId = user.getUserId();
//Maintain maintain = createMaintain(userId, jsonObject, null);
/*if (maintain == null) {
return false;
}*/
//String maintainId = maintain.getId();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MenuMapping menuMapping = null;
try {
menuMapping = objectMapper.readValue(json, MenuMapping.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
//menuMapping.setMaintainFieldId(maintainId);
return create(menuMapping);
}
private Maintain createMaintain(String userId, JSONObject jsonObject, String flowsId) {
Maintain maintain = new Maintain();
maintain.setCreateTime(new Date());
maintain.setChargeId(userId);
// maintain.setFlowId(flowsId);
maintain.setVersion(Constant.VERSION_Default);
maintain.setId(DbUtils.getUUID());
String desp = jsonObject.getString("desp");
String tableName = jsonObject.getString("tableName");
if(StringUtils.isEmpty(tableName)) {
return null;
}
maintain.setDesp(desp);
maintain.setTableName(tableName);
boolean insert = maintainService.insert(maintain);
if (insert) {
return maintain;
}
return null;
}
public MenuMapping create(MenuMapping menuMapping) {
menuMapping.setCreateTime(new Date());
menuMapping.setId(DbUtils.getUUID());
String name = menuMapping.getName();
name = DbUtils.getChineseOrEnglishOrNumber(name);
String tableName = WorkflowUtils.toFirstChar(name.toLowerCase());
tableName = Constant.MD + tableName;
String tableByName = tableInfoMapper.selectTableByName(tableName);
if (!StringUtils.isEmpty(tableByName)){
tableName = tableName + "_" + DbUtils.getUUID(5);
}
menuMapping.setTableName(tableName);
boolean insert = this.insert(menuMapping);
if (insert) {
return menuMapping;
} else {
return null;
}
}
@Override
public boolean update(String json) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MenuMapping menuMapping = null;
try {
menuMapping = objectMapper.readValue(json, MenuMapping.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
return false;
}
//only update no join
return this.updateById(menuMapping);
}
@Override
public Result getMapping(HttpSession session, String id) {
SysMenu sysMenu = menuService.selectById(id);
MaintainDataType dataType = sysMenu.getDataType();
String menuType = sysMenu.getMenuType();
if (!StringUtils.isEmpty(menuType) && menuType.equalsIgnoreCase("DataMenu")) {
Map mapping = menuMappingMapper.getMapping(id);
return Result.success(mapping);
}else if (!StringUtils.isEmpty(menuType) && menuType.equalsIgnoreCase("StructureMenu")) {
return Result.success(null);
}
Wrapper menuWrapper = new EntityWrapper().eq("menu_id", sysMenu.getParentId());
MenuMapping menuMapping = selectOne(menuWrapper);
if (menuMapping == null) {
return Result.error(new CodeMsg(10008,"当前主题无版本数据"));
}
String tableName = menuMapping.getTableName();
Maintain resultMaintain = null;
switch (dataType) {
case beforeData:
resultMaintain = maintainService.getRecentHistoryVersion(tableName);
break;
case afterData:
break;
case currentData:
resultMaintain = maintainService.getNowVersion(tableName);
break;
default:
Result.error(new CodeMsg(10008,"当前主题无版本数据"));
break;
}
Map mapping = menuMappingMapper.getMapping(sysMenu.getParentId());
if (resultMaintain != null) {
mapping.put("version", resultMaintain.getVersion());
mapping.put("maintainId", resultMaintain.getId());
}
mapping.put("dataType", sysMenu.getDataType());
return Result.success(mapping);
}
}