|
|
@@ -13,16 +13,16 @@ import com.github.pagehelper.PageHelper;
|
|
|
import io.renren.config.MongoManager;
|
|
|
import io.renren.dao.GeneratorDao;
|
|
|
import io.renren.dao.MongoDBGeneratorDao;
|
|
|
-import io.renren.entity.mongo.MongoDefinition;
|
|
|
import io.renren.factory.MongoDBCollectionFactory;
|
|
|
import io.renren.utils.GenUtils;
|
|
|
import io.renren.utils.PageUtils;
|
|
|
import io.renren.utils.Query;
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.zip.ZipOutputStream;
|
|
|
@@ -34,46 +34,49 @@ import java.util.zip.ZipOutputStream;
|
|
|
*/
|
|
|
@Service
|
|
|
public class SysGeneratorService {
|
|
|
- @Autowired
|
|
|
- private GeneratorDao generatorDao;
|
|
|
+ @Resource
|
|
|
+ private GeneratorDao _generator;
|
|
|
|
|
|
|
|
|
public PageUtils queryList(Query query) {
|
|
|
- Page<?> page = PageHelper.startPage(query.getPage(), query.getLimit());
|
|
|
- List<Map<String, Object>> list = generatorDao.queryList(query);
|
|
|
- int total = (int) page.getTotal();
|
|
|
- if (generatorDao instanceof MongoDBGeneratorDao) {
|
|
|
- total = MongoDBCollectionFactory.getCollectionTotal(query);
|
|
|
+ try (Page<?> page = PageHelper.startPage(query.getPage(), query.getLimit())) {
|
|
|
+ List<Map<String, Object>> list = _generator.queryList(query);
|
|
|
+ int total = (int) page.getTotal();
|
|
|
+ if (_generator instanceof MongoDBGeneratorDao) {
|
|
|
+ total = MongoDBCollectionFactory.getCollectionTotal(query);
|
|
|
+ }
|
|
|
+ return new PageUtils(list, total, query.getLimit(), query.getPage());
|
|
|
}
|
|
|
- return new PageUtils(list, total, query.getLimit(), query.getPage());
|
|
|
}
|
|
|
|
|
|
public Map<String, String> queryTable(String tableName) {
|
|
|
- return generatorDao.queryTable(tableName);
|
|
|
+ return _generator.queryTable(tableName);
|
|
|
}
|
|
|
|
|
|
public List<Map<String, String>> queryColumns(String tableName) {
|
|
|
- return generatorDao.queryColumns(tableName);
|
|
|
+ return _generator.queryColumns(tableName);
|
|
|
}
|
|
|
|
|
|
|
|
|
public byte[] generatorCode(String[] tableNames) {
|
|
|
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
- ZipOutputStream zip = new ZipOutputStream(outputStream);
|
|
|
- for (String tableName : tableNames) {
|
|
|
- //查询表信息
|
|
|
- Map<String, String> table = queryTable(tableName);
|
|
|
- //查询列信息
|
|
|
- List<Map<String, String>> columns = queryColumns(tableName);
|
|
|
- //生成代码
|
|
|
- GenUtils.generatorCode(table, columns, zip);
|
|
|
+ try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
|
|
|
+ try (ZipOutputStream zip = new ZipOutputStream(os)) {
|
|
|
+ for (String tableName : tableNames) {
|
|
|
+ //查询表信息
|
|
|
+ Map<String, String> table = queryTable(tableName);
|
|
|
+ //查询列信息
|
|
|
+ List<Map<String, String>> columns = queryColumns(tableName);
|
|
|
+ //生成代码
|
|
|
+ GenUtils.generatorCode(table, columns, zip);
|
|
|
+ }
|
|
|
+ if (MongoManager.isMongo()) {
|
|
|
+ GenUtils.generatorMongoCode(tableNames, zip);
|
|
|
+ }
|
|
|
+ IOUtils.closeQuietly(zip);
|
|
|
+ }
|
|
|
+ return os.toByteArray();
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
- if (MongoManager.isMongo()) {
|
|
|
- GenUtils.generatorMongoCode(tableNames, zip);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- IOUtils.closeQuietly(zip);
|
|
|
- return outputStream.toByteArray();
|
|
|
}
|
|
|
}
|