package com.ideabobo.controller;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.ideabobo.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson.JSON;
import com.ideabobo.model.Dbservice;
import com.ideabobo.model.Dbtablemapping;
import com.ideabobo.service.DatabaseService;
@CrossOrigin(origins="*")
@Controller
@RequestMapping(value = "/database")
public class DatabaseController {
//private static final Logger logger = Logger.getLogger(DatabaseController.class);
@Autowired
private DatabaseService databaseService;
@RequestMapping(value = "/list", produces = "application/json; charset=utf-8", method = { RequestMethod.GET,
RequestMethod.POST })
@ResponseBody
public List<Map<String,Object>> list(HttpServletRequest req) {
Dbservice dbm = new Dbservice(databaseService);
String table = Dbservice.getTableName(req.getParameter("table"));
Object objectObj = Common.getByRequest(Dbtablemapping.getModelByTable(table), req, false);
//Robj robj = new Robj();
List<Map<String,Object>> list = null;
try {
String sql = dbm.list(table,objectObj,null);
list = databaseService.find(sql);
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
@RequestMapping(value = "/listJ", produces = "text/plain; charset=utf-8", method = { RequestMethod.GET,
RequestMethod.POST })
@ResponseBody
public String listJ(HttpServletRequest req) {
Dbservice dbm = new Dbservice(databaseService);
String table = Dbservice.getTableName(req.getParameter("table"));
Object objectObj = Common.getByRequest(Dbtablemapping.getModelByTable(table), req, false);
//Robj robj = new Robj();
List<Map<String,Object>> list = null;
try {
String ordersql = null;
String sort = req.getParameter("sort");
String order = req.getParameter("order");
if (StringUtil.isNotNullOrEmpty(order)&&StringUtil.isNotNullOrEmpty(sort)){
ordersql = " order by "+sort+" "+order;
}
String sql = dbm.list(table,objectObj,ordersql);
list = databaseService.find(sql);
} catch (Exception e) {
e.printStackTrace();
}
return renderJsonp(list, req);
}
@RequestMapping(value = "/find", produces = "application/json; charset=utf-8", method = { RequestMethod.GET,
RequestMethod.POST })
@ResponseBody
public Map<String,Object> find(HttpServletRequest req) {
Dbservice dbm = new Dbservice(databaseService);
String table = Dbservice.getTableName(req.getParameter("table"));
Object objectObj = Common.getByRequest(Dbtablemapping.getModelByTable(table), req, false);
//Robj robj = new Robj();
List<Map<String,Object>> list = null;
try {
String sql = dbm.list(table,objectObj,true);
list = databaseService.find(sql);
} catch (Exception e) {
e.printStackTrace();
}
if (list!=null && list.size()>0) {
return list.get(0);
}else{
return null;
}
}
@RequestMapping(value = "/findJ", produces = "text/plain; charset=utf-8", method = { RequestMethod.GET,
RequestMethod.POST })
@ResponseBody
public String findJ(HttpServletRequest req) {
Dbservice dbm = new Dbservice(databaseService);
String table = Dbservice.getTableName(req.getParameter("table"));
Object objectObj = Common.getByRequest(Dbtablemapping.getModelByTable(table), req, false);
//Robj robj = new Robj();
List<Map<String,Object>> list = null;
try {
String sql = dbm.list(table,objectObj,true);
list = databaseService.find(sql);
} catch (Exception e) {
e.printStackTrace();
}
if (list!=null && list.size()>0) {
return renderJsonp(list.get(0), req);
}else{
return renderJsonp(null, req);
}
}
@RequestMapping(value = "/listApp", produces = "application/json; charset=utf-8", method = { RequestMethod.GET,
RequestMethod.POST })
@ResponseBody
public List<Map<String,Object>> listApp(HttpServletRequest req) {
Dbservice dbm = new Dbservice(databaseService);
String table = Dbservice.getTableName(req.getParameter("table"));
//Robj robj = new Robj();
List<Map<String,Object>> list = null;
try {
String object = req.getParameter("object");
//if(StringUtil.isNotNullOrEmpty(object)){
Object objectObj = Dbtablemapping.parseStringModel(object, table);
String sql = dbm.list(table,objectObj,null);
list = databaseService.find(sql);
//robj.setData(databaseService.find(sql));
//}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
@RequestMapping(value = "/listC", produces = "text/plain; charset=utf-8", method = { RequestMethod.GET,
RequestMethod.POST })
@ResponseBody
public String listC(HttpServletRequest req) {
Dbservice dbm = new Dbservice(databaseService);
String table = Dbservice.getTableName(req.getParameter("table"));
//Robj robj = new Robj();
List<Map<String,Object>> list = null;
try {
String object = req.getParameter("object");
//if(StringUtil.isNotNullOrEmpty(object)){
Object objectObj = Dbtablemapping.parseStringModel(object, table);
String sql = dbm.list(table,objectObj,null);
list = databaseService.find(sql);
//robj.setData(databaseService.find(sql));
//}
} catch (Exception e) {
e.printStackTrace();
}
return renderJsonp(list, req);
}
@RequestMapping(value = "/findApp", produces = "application/json; charset=utf-8", method = { RequestMethod.GET,
RequestMethod.POST })
@ResponseBody
public Map<String,Object> findApp(HttpServletRequest req) {
Dbservice dbm = new Dbservice(databaseService);
String table = Dbservice.getTableName(req.getParameter("table"));
//Robj robj = new Robj();
List<Map<String,Object>> list = null;
try {
String object = req.getParameter("object");
//if(StringUtil.isNotNullOrEmpty(object)){
Object objectObj = Dbtablemapping.parseStringModel(object, table);
String sql = dbm.list(table,objectObj,null);
list = databaseService.find(sql);
//robj.setData(databaseService.find(sql));
//}
} catch (Exception e) {
e.printStackTrace();
}
if (list!=null && list.size()>0) {
return list.get(0);
}else{
return null;
}
}
@RequestMapping(value = "/findC", produces = "text/plain; charset=utf-8", method = { RequestMethod.GET,
RequestMethod.POST })
@ResponseBody
public String findC(HttpServletRequest req) {
Dbservice dbm = new Dbservice(databaseService);
String table = Dbservice.getTableName(req.getParameter("table"));
//Robj robj = new Robj();
List<Map<String,Object>> list = null;
try {
String object = req.getParameter("object");
//if(StringUtil.isNotNullOrEmpty(object)){
Object objectObj = Dbtablemapping.parseStringModel(object, table);
String sql = dbm.list(table,objectObj,null);
list = databaseService.find(sql);
//robj.setData(databaseService.find(sql));
//}
} catch (Exception e) {
e.printStackTrace();
}
if (list!=null && list.size()>0) {
return renderJsonp(list.get(0), req);
}else{
return renderJsonp(