123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- package com.rongwei.luckysheet.util;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.alibaba.fastjson.serializer.SerializerFeature;
- import lombok.extern.slf4j.Slf4j;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- /**
- *
- * @author Administrator
- */
- @Slf4j
- public class JfGridFileUtil {
- /**
- * 按工作簿index获取对应集合
- * @param dbObject
- * @param index
- * @return
- */
- public static JSONArray getSheetByIndex(JSONObject dbObject, Integer index){
- JSONArray _resultModel=null;
- if(dbObject!=null && dbObject instanceof List){
- List<JSONObject> _list=(List<JSONObject>)dbObject;
- for(JSONObject _o:_list){
- if(_o.containsKey("index")){
- try{
- if(index.equals(_o.get("index").toString())){
- if(_o.containsKey("celldata")){
- _resultModel=_o.getJSONArray("celldata");
- }
- }
- }catch (Exception ex){
- log.error(ex.toString());
- }
- }
- }
- }
- return _resultModel;
- }
- /**
- * 从对象中获取数据calldata信息
- * @param dbObject
- * @return
- */
- public static JSONArray getSheetByIndex(JSONObject dbObject){
- JSONArray _resultModel=null;
- if(dbObject!=null){
- if(dbObject.containsKey("celldata")){
- _resultModel=dbObject.getJSONArray("celldata");
- }
- }
- if(_resultModel == null) {
- return new JSONArray();
- }
- return _resultModel;
- }
- /**
- * 按工作簿数量
- * @param dbObject
- * @return
- */
- public static Integer getSheetCount(JSONObject dbObject){
- Integer _resultModel=0;
- if(dbObject!=null){
- if(dbObject.containsKey("jfgridfile")){
- JSONObject _bs=dbObject.getJSONObject("jfgridfile");
- return getSheetCount(_bs);
- }else if(dbObject instanceof List){
- List<JSONObject> _list=(List<JSONObject>)dbObject;
- if(_list!=null){
- _resultModel=_list.size();
- }
- }
- }
- return _resultModel;
- }
- /**
- * 按工作簿index获取对应集合的序号
- * @param dbObject
- * @param index
- * @return
- */
- public static Integer getSheetPositionByIndex(JSONObject dbObject,Integer index){
- Integer _resultModel=null;
- if(dbObject!=null && dbObject instanceof List){
- List<JSONObject> _list=(List<JSONObject>)dbObject;
- //for(DBObject _o:_list){
- for(int x=0;x<_list.size();x++){
- JSONObject _o=_list.get(x);
- if(_o.containsKey("index")){
- try{
- if(index.equals(_o.get("index").toString())){
- _resultModel=x;
- break;
- }
- }catch (Exception ex){
- log.error(ex.toString());
- }
- }
- }
- }
- return _resultModel;
- }
- public static Integer getSheetPositionByIndex(List<JSONObject> _list,String index){
- Integer _resultModel=null;
- if(_list!=null ){
- for(int x=0;x<_list.size();x++){
- JSONObject _o=_list.get(x);
- if(_o.containsKey("index")){
- try{
- if(index.equals(_o.get("index").toString())){
- _resultModel=x;
- break;
- }
- }catch (Exception ex){
- log.error(ex.toString());
- }
- }
- }
- }
- return _resultModel;
- }
- /**
- * 按index值获取mongodb的key
- * @param _list
- * @param index
- * @return
- */
- public static List<String> getSheetKeyByIndex(List<JSONObject> _list,Integer index){
- List<String> _resultModel=new ArrayList<String>();
- if(_list!=null ){
- for(int x=0;x<_list.size();x++){
- JSONObject _o=_list.get(x);
- if(_o.containsKey("index")){
- try{
- if(index.equals(_o.get("index").toString())){
- if(_o.containsKey("_id")){
- //_resultModel=_o.get("_id").toString();
- _resultModel.add(_o.get("_id").toString());
- }
- //break;
- }
- }catch (Exception ex){
- log.error(ex.toString());
- }
- }
- }
- }
- return _resultModel;
- }
- /**
- * 按工作簿index 获取对象每一个工作簿的第一层对象
- * @param dbObject
- * @param index
- * @param k
- * @return
- */
- public static JSONObject getObjectByIndex(JSONObject dbObject,Integer index,String k){
- JSONObject _resultModel=null;
- if(dbObject!=null && dbObject instanceof List){
- List<JSONObject> _list=(List<JSONObject>)dbObject;
- for(JSONObject _o:_list){
- if(_o.containsKey("index")){
- try{
- if(index.equals(_o.get("index").toString())){
- if(_o.containsKey(k)){
- _resultModel=_o.getJSONObject(k);
- }
- break;
- }
- }catch (Exception ex){
- log.error(ex.toString());
- }
- }
- }
- }
- return _resultModel;
- }
- /**
- * 按工作簿index 获取对象每一个工作簿的第一层对象(传入为单sheet)
- * @param dbObject
- * @param k
- * @return
- */
- public static Object getObjectByIndex(JSONObject dbObject,String k){
- Object _resultModel=null;
- if(dbObject!=null){
- if(dbObject.containsKey(k)){
- _resultModel=dbObject.get(k);
- }
- }
- return _resultModel;
- }
- public static JSONObject getJSONObjectByIndex(JSONObject dbObject,String k){
- JSONObject _resultModel=null;
- if(dbObject!=null){
- if(dbObject.containsKey(k)){
- if(dbObject.get(k) instanceof JSONObject) {
- _resultModel=dbObject.getJSONObject(k);
- } else if(dbObject.get(k) instanceof String) {
- _resultModel = dbObject.getJSONObject(k);
- }
- }
- }
- return _resultModel;
- }
- public static JSONArray getJSONArrayByIndex(JSONObject dbObject,String k){
- JSONArray _resultModel=null;
- if(dbObject!=null){
- if(dbObject.containsKey(k)&& dbObject.get(k) instanceof JSONArray){
- _resultModel=dbObject.getJSONArray(k);
- }
- }
- return _resultModel;
- }
- /**
- * 按工作簿index 获取对象每一个工作簿的第一层对象
- * @param dbObject
- * @param index
- * @param k
- * @return
- */
- public static Integer getIntegerByIndex(JSONObject dbObject,Integer index,String k){
- Integer _resultModel=null;
- if(dbObject!=null && dbObject instanceof List){
- List<JSONObject> _list=(List<JSONObject>)dbObject;
- for(JSONObject _o:_list){
- if(_o.containsKey("index")){
- try{
- if(index.equals(_o.get("index").toString())){
- if(_o.containsKey(k)){
- _resultModel=(Integer)_o.get(k);
- }
- break;
- }
- }catch (Exception ex){
- log.error(ex.toString());
- }
- }
- }
- }
- return _resultModel;
- }
- /**
- * 按工作簿index 获取对象每一个工作簿的第一层对象(单个工作簿)
- * @param dbObject
- * @param k
- * @return
- */
- public static Integer getIntegerByIndex(JSONObject dbObject,String k){
- Integer _resultModel=null;
- if(dbObject!=null){
- if(dbObject.containsKey(k)){
- _resultModel=(Integer)dbObject.get(k);
- }
- }
- return _resultModel;
- }
- /**
- * 从对象中获取一个指定的对象
- * @param dbObject
- * @param k
- * @return
- */
- public static JSONObject getObjectByObject(JSONObject dbObject,String k){
- if(dbObject!=null){
- if(dbObject.containsKey(k)){
- return dbObject.getJSONObject(k);
- }
- }
- return null;
- }
- /**
- * /获取测试用二维数组
- * @param rc
- * @param len
- * @return
- */
- public static JSONArray getTestData(String rc,int len){
- int row=0;
- int column=0;
- if(rc.equals("r")){
- //增加行
- row=len;
- column=5;
- }else{
- //增加列
- row=5;
- column=len;
- }
- Object[][] strs=new String[row][column];
- for(int x=0;x<row;x++){
- for(int x1=0;x1<column;x1++){
- if(x1==x){
- //continue;
- }
- strs[x][x1]=x+""+x1;
- }
- }
- System.out.println(JSONArray.toJSONString(strs));
- List<Object> strs1=JSONObject.parseObject(JSONArray.toJSONString(strs),ArrayList.class);
- JSONArray _db=new JSONArray();
- _db.addAll(strs1);
- return _db;
- }
- /**
- * 给出查询条件
- * @param query
- * @return
- */
- public static String getCondition(JSONObject query){
- return getCondition(query, false);
- }
- /**
- * 给出查询条件
- * @param query
- * @return
- */
- public static String getCondition(JSONObject query, boolean escapeFlag){
- String condition="";
- if(query!=null) {
- Map<String, Object> queryDB = query.getInnerMap();
- for (String key : queryDB.keySet()) {
- if(escapeFlag) {
- condition = condition + " and t.\"" + key + "\"='" + queryDB.get(key) + "'";
- continue;
- }
- condition = condition + " and t." + key + "='" + queryDB.get(key) + "'";
- }
- }
- return condition;
- }
- /**
- * 给出查询条件
- * @param query
- * @return
- */
- public static String getCondition(JSONObject query,List arr){
- return getCondition(query, arr, false);
- }
- /**
- * 给出查询条件
- * @param query
- * @return
- */
- public static String getCondition(JSONObject query,List arr, boolean escapeFlag){
- String condition="";
- if(query!=null&&arr!=null) {
- Map<String,Object> queryDB=query.getInnerMap();
- for (String key : queryDB.keySet()) {
- arr.add(queryDB.get(key));
- if(escapeFlag) {
- condition=condition+" and t.\""+key+"\"=? ";
- continue;
- }
- condition=condition+" and t."+key+"=? ";
- }
- }
- return condition;
- }
- /**
- * 从jsonObject中找到第一个key的名字
- * @param jsonObject
- * @return
- */
- public static String getKeyName(JSONObject jsonObject){
- try{
- if(jsonObject!=null&&jsonObject.size()>0) {
- Map<String, Object> map = jsonObject.getInnerMap();
- return map.keySet().stream().findFirst().get();
- // for(String key:map.keySet()){
- // return key;
- // }
- }
- }catch (Exception ex){
- log.error("jsonObject:{};{}",jsonObject.toString(SerializerFeature.WriteMapNullValue),ex.toString());
- }
- return "";
- }
- }
|