package com.kingbase.management.service.impl;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.transaction.Transactional;
import org.apache.commons.collections.IteratorUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kingbase.management.dao.AdminResourceDao;
import com.kingbase.management.dao.DataSourceDao;
import com.kingbase.management.dao.IAssetDao;
import com.kingbase.management.entity.AdminResource;
import com.kingbase.management.entity.DataSource;
import com.kingbase.management.entity.DataSourceVo;
import com.kingbase.management.entity.DicType;
import com.kingbase.management.service.IDataSourceService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Transactional
@Service(value ="dataSourceServiceImpl")
public class DataSourceServiceImpl implements IDataSourceService{
@Autowired
private DataSourceDao dataSourceDao;
@Autowired
private AdminResourceDao adminResourceDao;
@Autowired
private IAssetDao assetDao;
@Override
public JSONObject checkSoftName(String name) throws Exception {
List<DataSource> dataSources = dataSourceDao.findBySoftName(name);
JSONObject obj = new JSONObject();
obj.put("isfalg", "0");
if(dataSources != null && dataSources.size()>0) {
obj.put("isfalg", "1");
}
return obj;
}
@Override
public JSONObject registerSoft(String json) throws Exception {
ObjectMapper om = new ObjectMapper();
DataSource dataSource = om.readValue(json, DataSource.class);
JSONObject obj = new JSONObject();
obj.put("id", "");
//校验是否为null
if(StringUtils.isBlank(dataSource.getAdminResouceId()) || StringUtils.isBlank(dataSource.getSoftName()) ||
dataSource.getSourcePartition() == null) {
throw new Exception("必录参数为空!");
}else {
//source_flag
dataSource.setSourceFlag(Integer.valueOf(dataSource.getSourcePartition()));
//ip port 集群名称 资源类型 是否为集群
AdminResource adminResource = adminResourceDao.findOne(dataSource.getAdminResouceId());
dataSource.setIp(adminResource.getIp());
dataSource.setPort(Integer.parseInt(adminResource.getPort()));
dataSource.setClusterName(adminResource.getClusterName());
dataSource.setIsCluster(adminResource.getIsCluster());
dataSource.setResourceStyle(adminResource.getTypeIsType());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dataSource.setInsertTime(sdf.format(new Date()));
//数据库实例先设置为默认
dataSource.setDataInstance("deafult");
String uuid = UUID.randomUUID().toString().replaceAll("_", "");
dataSource.setId(uuid);
//保存
dataSourceDao.save(dataSource);
obj.put("id", uuid);
}
return obj;
}
@Override
public JSONObject getSoftList(String page, String pageSize, String startIndex, String ip, String type)
throws Exception {
Sort sort = new Sort(Sort.Direction.DESC, "insertTime");
Pageable pageable = new PageRequest(Integer.parseInt(page)-1, Integer.parseInt(pageSize), sort);
Page<DataSource> pages = null;
if(StringUtils.isBlank(type)) {
pages = dataSourceDao.findByIpLike(ip,pageable);
}else {
pages = dataSourceDao.findBySoftTypeAndIpLike(type,ip,pageable);
}
JSONObject obj = new JSONObject();
obj.put("pageSize", pageSize);//pages.getSize()
//obj.put("page", page);//pages.getNumber();
obj.put("startIndex", startIndex);
obj.put("resultSize", pages.getNumberOfElements());//当前结果条数
obj.put("totalCount", pages.getTotalElements());//总条数
//pages.getTotalPages();总页数
List<DataSource> dataSources = IteratorUtils.toList(pages.iterator());
List<DataSourceVo> dataSourceVos = new ArrayList<DataSourceVo>();
if(dataSources!= null && dataSources.size() > 0) {
//查询数据字典 os 分区 软件类型
List<String> list = Arrays.asList("os","source_flag","soft_style");
List<DicType> dicTypes = assetDao.getByDicTypeIn(list);
//定义三个map
Map<String, String> os = new HashMap<String,String>();
Map<String, String> partition = new HashMap<String,String>();
Map<String, String> softType = new HashMap<String,String>();
if(dicTypes!=null && dicTypes.size()>0) {
for (DicType dicType : dicTypes) {
if("os".equals(dicType.getDicType())) {
os.put(String.valueOf(dicType.getId()), dicType.getName());
}else if("source_flag".equals(dicType.getDicType())) {
partition.put(String.valueOf(dicType.getId()), dicType.getName());
}else if("soft_style".equals(dicType.getDicType())) {
softType.put(String.valueOf(dicType.getId()), dicType.getName());
}
}
}
for (DataSource dataSource : dataSources) {
DataSourceVo vo = new DataSourceVo();
BeanUtils.copyProperties(dataSource, vo);
vo.setOs(os.get(dataSource.getOs()));
vo.setSourcePartitionValue(partition.get(String.valueOf(dataSource.getSourcePartition())));
if(softType.containsKey(dataSource.getSoftType())) {
vo.setSoftType(softType.get(dataSource.getSoftType()));
}
//vo.setSoftType(os.get(dataSource.getSoftType()));
dataSourceVos.add(vo);
}
}
JSONArray arr = JSONArray.fromObject(dataSourceVos);
obj.put("dataSource", arr);
// "startIndex": 0,
// "pageSize": 25,
// "totalCount": 4,
// "resultSize": 4,
// ObjectMapper om = new ObjectMapper();
// om.setSerializationInclusion(Include.NON_EMPTY);
// String json = om.writeValueAsString(dataSourceVos);
return obj;
}
//表单条数查询元数据获取
public void getCountMata() {
}
//表单条数查询根据元数据统计数据
public void getCountInfo() {
}
//表单条数信息保存
public void saveCountInfo() {
}
}
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.transaction.Transactional;
import org.apache.commons.collections.IteratorUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kingbase.management.dao.AdminResourceDao;
import com.kingbase.management.dao.DataSourceDao;
import com.kingbase.management.dao.IAssetDao;
import com.kingbase.management.entity.AdminResource;
import com.kingbase.management.entity.DataSource;
import com.kingbase.management.entity.DataSourceVo;
import com.kingbase.management.entity.DicType;
import com.kingbase.management.service.IDataSourceService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Transactional
@Service(value ="dataSourceServiceImpl")
public class DataSourceServiceImpl implements IDataSourceService{
@Autowired
private DataSourceDao dataSourceDao;
@Autowired
private AdminResourceDao adminResourceDao;
@Autowired
private IAssetDao assetDao;
@Override
public JSONObject checkSoftName(String name) throws Exception {
List<DataSource> dataSources = dataSourceDao.findBySoftName(name);
JSONObject obj = new JSONObject();
obj.put("isfalg", "0");
if(dataSources != null && dataSources.size()>0) {
obj.put("isfalg", "1");
}
return obj;
}
@Override
public JSONObject registerSoft(String json) throws Exception {
ObjectMapper om = new ObjectMapper();
DataSource dataSource = om.readValue(json, DataSource.class);
JSONObject obj = new JSONObject();
obj.put("id", "");
//校验是否为null
if(StringUtils.isBlank(dataSource.getAdminResouceId()) || StringUtils.isBlank(dataSource.getSoftName()) ||
dataSource.getSourcePartition() == null) {
throw new Exception("必录参数为空!");
}else {
//source_flag
dataSource.setSourceFlag(Integer.valueOf(dataSource.getSourcePartition()));
//ip port 集群名称 资源类型 是否为集群
AdminResource adminResource = adminResourceDao.findOne(dataSource.getAdminResouceId());
dataSource.setIp(adminResource.getIp());
dataSource.setPort(Integer.parseInt(adminResource.getPort()));
dataSource.setClusterName(adminResource.getClusterName());
dataSource.setIsCluster(adminResource.getIsCluster());
dataSource.setResourceStyle(adminResource.getTypeIsType());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dataSource.setInsertTime(sdf.format(new Date()));
//数据库实例先设置为默认
dataSource.setDataInstance("deafult");
String uuid = UUID.randomUUID().toString().replaceAll("_", "");
dataSource.setId(uuid);
//保存
dataSourceDao.save(dataSource);
obj.put("id", uuid);
}
return obj;
}
@Override
public JSONObject getSoftList(String page, String pageSize, String startIndex, String ip, String type)
throws Exception {
Sort sort = new Sort(Sort.Direction.DESC, "insertTime");
Pageable pageable = new PageRequest(Integer.parseInt(page)-1, Integer.parseInt(pageSize), sort);
Page<DataSource> pages = null;
if(StringUtils.isBlank(type)) {
pages = dataSourceDao.findByIpLike(ip,pageable);
}else {
pages = dataSourceDao.findBySoftTypeAndIpLike(type,ip,pageable);
}
JSONObject obj = new JSONObject();
obj.put("pageSize", pageSize);//pages.getSize()
//obj.put("page", page);//pages.getNumber();
obj.put("startIndex", startIndex);
obj.put("resultSize", pages.getNumberOfElements());//当前结果条数
obj.put("totalCount", pages.getTotalElements());//总条数
//pages.getTotalPages();总页数
List<DataSource> dataSources = IteratorUtils.toList(pages.iterator());
List<DataSourceVo> dataSourceVos = new ArrayList<DataSourceVo>();
if(dataSources!= null && dataSources.size() > 0) {
//查询数据字典 os 分区 软件类型
List<String> list = Arrays.asList("os","source_flag","soft_style");
List<DicType> dicTypes = assetDao.getByDicTypeIn(list);
//定义三个map
Map<String, String> os = new HashMap<String,String>();
Map<String, String> partition = new HashMap<String,String>();
Map<String, String> softType = new HashMap<String,String>();
if(dicTypes!=null && dicTypes.size()>0) {
for (DicType dicType : dicTypes) {
if("os".equals(dicType.getDicType())) {
os.put(String.valueOf(dicType.getId()), dicType.getName());
}else if("source_flag".equals(dicType.getDicType())) {
partition.put(String.valueOf(dicType.getId()), dicType.getName());
}else if("soft_style".equals(dicType.getDicType())) {
softType.put(String.valueOf(dicType.getId()), dicType.getName());
}
}
}
for (DataSource dataSource : dataSources) {
DataSourceVo vo = new DataSourceVo();
BeanUtils.copyProperties(dataSource, vo);
vo.setOs(os.get(dataSource.getOs()));
vo.setSourcePartitionValue(partition.get(String.valueOf(dataSource.getSourcePartition())));
if(softType.containsKey(dataSource.getSoftType())) {
vo.setSoftType(softType.get(dataSource.getSoftType()));
}
//vo.setSoftType(os.get(dataSource.getSoftType()));
dataSourceVos.add(vo);
}
}
JSONArray arr = JSONArray.fromObject(dataSourceVos);
obj.put("dataSource", arr);
// "startIndex": 0,
// "pageSize": 25,
// "totalCount": 4,
// "resultSize": 4,
// ObjectMapper om = new ObjectMapper();
// om.setSerializationInclusion(Include.NON_EMPTY);
// String json = om.writeValueAsString(dataSourceVos);
return obj;
}
//表单条数查询元数据获取
public void getCountMata() {
}
//表单条数查询根据元数据统计数据
public void getCountInfo() {
}
//表单条数信息保存
public void saveCountInfo() {
}
}