JfGridFileUtil.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. package com.rongwei.luckysheet.util;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.alibaba.fastjson.serializer.SerializerFeature;
  5. import lombok.extern.slf4j.Slf4j;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Map;
  9. /**
  10. *
  11. * @author Administrator
  12. */
  13. @Slf4j
  14. public class JfGridFileUtil {
  15. /**
  16. * 按工作簿index获取对应集合
  17. * @param dbObject
  18. * @param index
  19. * @return
  20. */
  21. public static JSONArray getSheetByIndex(JSONObject dbObject, Integer index){
  22. JSONArray _resultModel=null;
  23. if(dbObject!=null && dbObject instanceof List){
  24. List<JSONObject> _list=(List<JSONObject>)dbObject;
  25. for(JSONObject _o:_list){
  26. if(_o.containsKey("index")){
  27. try{
  28. if(index.equals(_o.get("index").toString())){
  29. if(_o.containsKey("celldata")){
  30. _resultModel=_o.getJSONArray("celldata");
  31. }
  32. }
  33. }catch (Exception ex){
  34. log.error(ex.toString());
  35. }
  36. }
  37. }
  38. }
  39. return _resultModel;
  40. }
  41. /**
  42. * 从对象中获取数据calldata信息
  43. * @param dbObject
  44. * @return
  45. */
  46. public static JSONArray getSheetByIndex(JSONObject dbObject){
  47. JSONArray _resultModel=null;
  48. if(dbObject!=null){
  49. if(dbObject.containsKey("celldata")){
  50. _resultModel=dbObject.getJSONArray("celldata");
  51. }
  52. }
  53. if(_resultModel == null) {
  54. return new JSONArray();
  55. }
  56. return _resultModel;
  57. }
  58. /**
  59. * 按工作簿数量
  60. * @param dbObject
  61. * @return
  62. */
  63. public static Integer getSheetCount(JSONObject dbObject){
  64. Integer _resultModel=0;
  65. if(dbObject!=null){
  66. if(dbObject.containsKey("jfgridfile")){
  67. JSONObject _bs=dbObject.getJSONObject("jfgridfile");
  68. return getSheetCount(_bs);
  69. }else if(dbObject instanceof List){
  70. List<JSONObject> _list=(List<JSONObject>)dbObject;
  71. if(_list!=null){
  72. _resultModel=_list.size();
  73. }
  74. }
  75. }
  76. return _resultModel;
  77. }
  78. /**
  79. * 按工作簿index获取对应集合的序号
  80. * @param dbObject
  81. * @param index
  82. * @return
  83. */
  84. public static Integer getSheetPositionByIndex(JSONObject dbObject,Integer index){
  85. Integer _resultModel=null;
  86. if(dbObject!=null && dbObject instanceof List){
  87. List<JSONObject> _list=(List<JSONObject>)dbObject;
  88. //for(DBObject _o:_list){
  89. for(int x=0;x<_list.size();x++){
  90. JSONObject _o=_list.get(x);
  91. if(_o.containsKey("index")){
  92. try{
  93. if(index.equals(_o.get("index").toString())){
  94. _resultModel=x;
  95. break;
  96. }
  97. }catch (Exception ex){
  98. log.error(ex.toString());
  99. }
  100. }
  101. }
  102. }
  103. return _resultModel;
  104. }
  105. public static Integer getSheetPositionByIndex(List<JSONObject> _list,String index){
  106. Integer _resultModel=null;
  107. if(_list!=null ){
  108. for(int x=0;x<_list.size();x++){
  109. JSONObject _o=_list.get(x);
  110. if(_o.containsKey("index")){
  111. try{
  112. if(index.equals(_o.get("index").toString())){
  113. _resultModel=x;
  114. break;
  115. }
  116. }catch (Exception ex){
  117. log.error(ex.toString());
  118. }
  119. }
  120. }
  121. }
  122. return _resultModel;
  123. }
  124. /**
  125. * 按index值获取mongodb的key
  126. * @param _list
  127. * @param index
  128. * @return
  129. */
  130. public static List<String> getSheetKeyByIndex(List<JSONObject> _list,Integer index){
  131. List<String> _resultModel=new ArrayList<String>();
  132. if(_list!=null ){
  133. for(int x=0;x<_list.size();x++){
  134. JSONObject _o=_list.get(x);
  135. if(_o.containsKey("index")){
  136. try{
  137. if(index.equals(_o.get("index").toString())){
  138. if(_o.containsKey("_id")){
  139. //_resultModel=_o.get("_id").toString();
  140. _resultModel.add(_o.get("_id").toString());
  141. }
  142. //break;
  143. }
  144. }catch (Exception ex){
  145. log.error(ex.toString());
  146. }
  147. }
  148. }
  149. }
  150. return _resultModel;
  151. }
  152. /**
  153. * 按工作簿index 获取对象每一个工作簿的第一层对象
  154. * @param dbObject
  155. * @param index
  156. * @param k
  157. * @return
  158. */
  159. public static JSONObject getObjectByIndex(JSONObject dbObject,Integer index,String k){
  160. JSONObject _resultModel=null;
  161. if(dbObject!=null && dbObject instanceof List){
  162. List<JSONObject> _list=(List<JSONObject>)dbObject;
  163. for(JSONObject _o:_list){
  164. if(_o.containsKey("index")){
  165. try{
  166. if(index.equals(_o.get("index").toString())){
  167. if(_o.containsKey(k)){
  168. _resultModel=_o.getJSONObject(k);
  169. }
  170. break;
  171. }
  172. }catch (Exception ex){
  173. log.error(ex.toString());
  174. }
  175. }
  176. }
  177. }
  178. return _resultModel;
  179. }
  180. /**
  181. * 按工作簿index 获取对象每一个工作簿的第一层对象(传入为单sheet)
  182. * @param dbObject
  183. * @param k
  184. * @return
  185. */
  186. public static Object getObjectByIndex(JSONObject dbObject,String k){
  187. Object _resultModel=null;
  188. if(dbObject!=null){
  189. if(dbObject.containsKey(k)){
  190. _resultModel=dbObject.get(k);
  191. }
  192. }
  193. return _resultModel;
  194. }
  195. public static JSONObject getJSONObjectByIndex(JSONObject dbObject,String k){
  196. JSONObject _resultModel=null;
  197. if(dbObject!=null){
  198. if(dbObject.containsKey(k)){
  199. if(dbObject.get(k) instanceof JSONObject) {
  200. _resultModel=dbObject.getJSONObject(k);
  201. } else if(dbObject.get(k) instanceof String) {
  202. _resultModel = dbObject.getJSONObject(k);
  203. }
  204. }
  205. }
  206. return _resultModel;
  207. }
  208. public static JSONArray getJSONArrayByIndex(JSONObject dbObject,String k){
  209. JSONArray _resultModel=null;
  210. if(dbObject!=null){
  211. if(dbObject.containsKey(k)&& dbObject.get(k) instanceof JSONArray){
  212. _resultModel=dbObject.getJSONArray(k);
  213. }
  214. }
  215. return _resultModel;
  216. }
  217. /**
  218. * 按工作簿index 获取对象每一个工作簿的第一层对象
  219. * @param dbObject
  220. * @param index
  221. * @param k
  222. * @return
  223. */
  224. public static Integer getIntegerByIndex(JSONObject dbObject,Integer index,String k){
  225. Integer _resultModel=null;
  226. if(dbObject!=null && dbObject instanceof List){
  227. List<JSONObject> _list=(List<JSONObject>)dbObject;
  228. for(JSONObject _o:_list){
  229. if(_o.containsKey("index")){
  230. try{
  231. if(index.equals(_o.get("index").toString())){
  232. if(_o.containsKey(k)){
  233. _resultModel=(Integer)_o.get(k);
  234. }
  235. break;
  236. }
  237. }catch (Exception ex){
  238. log.error(ex.toString());
  239. }
  240. }
  241. }
  242. }
  243. return _resultModel;
  244. }
  245. /**
  246. * 按工作簿index 获取对象每一个工作簿的第一层对象(单个工作簿)
  247. * @param dbObject
  248. * @param k
  249. * @return
  250. */
  251. public static Integer getIntegerByIndex(JSONObject dbObject,String k){
  252. Integer _resultModel=null;
  253. if(dbObject!=null){
  254. if(dbObject.containsKey(k)){
  255. _resultModel=(Integer)dbObject.get(k);
  256. }
  257. }
  258. return _resultModel;
  259. }
  260. /**
  261. * 从对象中获取一个指定的对象
  262. * @param dbObject
  263. * @param k
  264. * @return
  265. */
  266. public static JSONObject getObjectByObject(JSONObject dbObject,String k){
  267. if(dbObject!=null){
  268. if(dbObject.containsKey(k)){
  269. return dbObject.getJSONObject(k);
  270. }
  271. }
  272. return null;
  273. }
  274. /**
  275. * /获取测试用二维数组
  276. * @param rc
  277. * @param len
  278. * @return
  279. */
  280. public static JSONArray getTestData(String rc,int len){
  281. int row=0;
  282. int column=0;
  283. if(rc.equals("r")){
  284. //增加行
  285. row=len;
  286. column=5;
  287. }else{
  288. //增加列
  289. row=5;
  290. column=len;
  291. }
  292. Object[][] strs=new String[row][column];
  293. for(int x=0;x<row;x++){
  294. for(int x1=0;x1<column;x1++){
  295. if(x1==x){
  296. //continue;
  297. }
  298. strs[x][x1]=x+""+x1;
  299. }
  300. }
  301. System.out.println(JSONArray.toJSONString(strs));
  302. List<Object> strs1=JSONObject.parseObject(JSONArray.toJSONString(strs),ArrayList.class);
  303. JSONArray _db=new JSONArray();
  304. _db.addAll(strs1);
  305. return _db;
  306. }
  307. /**
  308. * 给出查询条件
  309. * @param query
  310. * @return
  311. */
  312. public static String getCondition(JSONObject query){
  313. return getCondition(query, false);
  314. }
  315. /**
  316. * 给出查询条件
  317. * @param query
  318. * @return
  319. */
  320. public static String getCondition(JSONObject query, boolean escapeFlag){
  321. String condition="";
  322. if(query!=null) {
  323. Map<String, Object> queryDB = query.getInnerMap();
  324. for (String key : queryDB.keySet()) {
  325. if(escapeFlag) {
  326. condition = condition + " and t.\"" + key + "\"='" + queryDB.get(key) + "'";
  327. continue;
  328. }
  329. condition = condition + " and t." + key + "='" + queryDB.get(key) + "'";
  330. }
  331. }
  332. return condition;
  333. }
  334. /**
  335. * 给出查询条件
  336. * @param query
  337. * @return
  338. */
  339. public static String getCondition(JSONObject query,List arr){
  340. return getCondition(query, arr, false);
  341. }
  342. /**
  343. * 给出查询条件
  344. * @param query
  345. * @return
  346. */
  347. public static String getCondition(JSONObject query,List arr, boolean escapeFlag){
  348. String condition="";
  349. if(query!=null&&arr!=null) {
  350. Map<String,Object> queryDB=query.getInnerMap();
  351. for (String key : queryDB.keySet()) {
  352. arr.add(queryDB.get(key));
  353. if(escapeFlag) {
  354. condition=condition+" and t.\""+key+"\"=? ";
  355. continue;
  356. }
  357. condition=condition+" and t."+key+"=? ";
  358. }
  359. }
  360. return condition;
  361. }
  362. /**
  363. * 从jsonObject中找到第一个key的名字
  364. * @param jsonObject
  365. * @return
  366. */
  367. public static String getKeyName(JSONObject jsonObject){
  368. try{
  369. if(jsonObject!=null&&jsonObject.size()>0) {
  370. Map<String, Object> map = jsonObject.getInnerMap();
  371. return map.keySet().stream().findFirst().get();
  372. // for(String key:map.keySet()){
  373. // return key;
  374. // }
  375. }
  376. }catch (Exception ex){
  377. log.error("jsonObject:{};{}",jsonObject.toString(SerializerFeature.WriteMapNullValue),ex.toString());
  378. }
  379. return "";
  380. }
  381. }