| | |
| | | package com.java110.common.bmo.assetImportLogDetail.impl; |
| | | |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.java110.common.bmo.assetImportLogDetail.IGetAssetImportLogDetailBMO; |
| | | import com.java110.dto.log.AssetImportLogDetailDto; |
| | | import com.java110.dto.log.AssetImportLogTypeDto; |
| | | import com.java110.intf.common.IAssetImportLogDetailInnerServiceSMO; |
| | | import com.java110.utils.util.BeanConvertUtil; |
| | | import com.java110.utils.util.StringUtil; |
| | | import com.java110.vo.ResultVo; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.HttpStatus; |
| | |
| | | int count = assetImportLogDetailInnerServiceSMOImpl.queryAssetImportLogDetailsCount(assetImportLogDetailDto); |
| | | |
| | | List<AssetImportLogDetailDto> assetImportLogDetailDtos = null; |
| | | |
| | | JSONArray datas = null; |
| | | if (count > 0) { |
| | | assetImportLogDetailDtos = assetImportLogDetailInnerServiceSMOImpl.queryAssetImportLogDetails(assetImportLogDetailDto); |
| | | // todo 转换为jsonArray |
| | | datas = covertToData(assetImportLogDetailDtos); |
| | | } else { |
| | | assetImportLogDetailDtos = new ArrayList<>(); |
| | | datas = new JSONArray(); |
| | | } |
| | | |
| | | ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) assetImportLogDetailDto.getRow()), count, assetImportLogDetailDtos); |
| | | ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) assetImportLogDetailDto.getRow()), count, datas); |
| | | |
| | | ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK); |
| | | |
| | | return responseEntity; |
| | | } |
| | | |
| | | private JSONArray covertToData(List<AssetImportLogDetailDto> assetImportLogDetailDtos) { |
| | | JSONArray datas = new JSONArray(); |
| | | |
| | | // 空列表直接返回空JSONArray |
| | | if (assetImportLogDetailDtos == null || assetImportLogDetailDtos.size() < 1) { |
| | | return datas; |
| | | } |
| | | |
| | | JSONObject data = null; |
| | | Object contentObj = null; |
| | | |
| | | for (AssetImportLogDetailDto assetImportLogDetailDto : assetImportLogDetailDtos) { |
| | | // 将DTO转换为JSONObject |
| | | data = BeanConvertUtil.beanCovertJson(assetImportLogDetailDto); |
| | | contentObj = assetImportLogDetailDto.getContent(); |
| | | |
| | | // 处理content字段(真实值为String[],包含null、字符串、数字、布尔值) |
| | | if (contentObj != null && contentObj instanceof String[]) { |
| | | String[] contentArray = (String[]) contentObj; |
| | | // 转换String[]为JSONArray,保留原有数据类型(null、字符串等) |
| | | JSONArray contentJsonArray = new JSONArray(); |
| | | for (Object item : contentArray) { // 用Object接收,兼容数组内的null和各种类型 |
| | | contentJsonArray.add(item); |
| | | } |
| | | // 将数组存入JSONObject,key为"content" |
| | | data.put("content", contentJsonArray); |
| | | } else if (contentObj != null && contentObj instanceof String) { |
| | | // 兼容可能的字符串格式(保留原有逻辑) |
| | | String contentStr = (String) contentObj; |
| | | if (!StringUtil.isEmpty(contentStr)) { |
| | | try { |
| | | // 若字符串是JSON对象/数组,分别解析 |
| | | if (contentStr.startsWith("{")) { |
| | | data.putAll(JSONObject.parseObject(contentStr)); |
| | | } else if (contentStr.startsWith("[")) { |
| | | data.put("content", JSONArray.parseArray(contentStr)); |
| | | } else { |
| | | data.put("content", contentStr); |
| | | } |
| | | } catch (Exception e) { |
| | | // 非JSON格式字符串直接存入 |
| | | data.put("content", contentStr); |
| | | } |
| | | } |
| | | } |
| | | |
| | | datas.add(data); |
| | | } |
| | | |
| | | return datas; |
| | | } |
| | | |
| | | } |