package cn.com.lzt.budget.data.util; import cn.com.lzt.budget.costbreakdown.entity.BudgetCostBreakdownEntity; import cn.com.lzt.budget.data.dto.BudgetSpreadCellData; import cn.com.lzt.budget.inst.entity.BudgetInstEntity; import cn.com.lzt.budget.instmeasure.entity.BudgetInstMeasureEntity; import cn.com.lzt.budget.tempsheet.entity.BudgetTempSheetEntity; import org.apache.commons.lang3.StringUtils; import org.codehaus.jackson.map.ObjectMapper; import org.jeecgframework.core.constant.Globals; import org.jeecgframework.core.util.ApplicationContextUtil; import org.jeecgframework.core.util.oConvertUtils; import org.jeecgframework.web.system.service.SystemService; import org.springframework.context.ApplicationContext; import java.io.IOException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class SpreadJson { private String id; public String json ; ObjectMapper mapper = new ObjectMapper(); private Map jsonMap ; private Map sheets ; private Map> allCellMap;//所有单元格 private Integer rowCount; private Integer colCount; private boolean change =false; private static Map holderMap = new HashMap<>(); private String tempsheetId; public SpreadJson(String json){ this(json,false); } public SpreadJson(String json,boolean init){ this.json = json; try { jsonMap = mapper.readValue(json,Map.class); sheets = (Map) jsonMap.get("sheets"); if(init){ initCellData(); } } catch (IOException e) { e.printStackTrace(); } } /** * * @param id tempsheet#id cost#id * @return */ public static SpreadJson getCacheInstance(BudgetConstant.BudgetType budgetType,String id, String tempsheetId,String json, boolean init){ synchronized (holderMap) { SpreadJson spread = holderMap.get(id); if (spread == null) { if(StringUtils.isBlank(json)){ SystemService systemService = ApplicationContextUtil.getContext().getBean(SystemService.class); switch (budgetType){ case tempsheet: BudgetInstEntity instEntity = systemService.getEntity(BudgetInstEntity.class, id); json = instEntity.getJson(); tempsheetId = instEntity.getTempSheetId(); break; case costbreakdown: BudgetCostBreakdownEntity breakdownEntity = systemService.getEntity(BudgetCostBreakdownEntity.class, id); json = breakdownEntity.getJson(); break; } } if(StringUtils.isBlank(json)) return null; spread = new SpreadJson(json, init); spread.id = id; spread.setTempsheetId(tempsheetId); holderMap.put(id, spread); } return spread; } } public static void clear(){ holderMap.clear(); } private void initCellData() { allCellMap = new HashMap<>(); Map dataMap = data(1); Map dataTable = (Map) dataMap.get("dataTable"); for(Map.Entry entry :dataTable.entrySet()) { String row = entry.getKey(); rowCount = Integer.valueOf(row)+1; Map rowCols = (Map) entry.getValue(); for (Map.Entry rowColsEntry : rowCols.entrySet()) { String col = rowColsEntry.getKey(); colCount = Integer.valueOf(col)+1; String key = row+"#"+col; Map cellMap = (Map) rowColsEntry.getValue(); allCellMap.put(key,cellMap); } } } public Map data(Integer sheetIndex){ String key = "Sheet"+sheetIndex; Map sheetMap = (Map) sheets.get(key); Map dataMap = (Map) sheetMap.get("data"); return dataMap; } public String getJson() { return json; } public void setJson(String json) { this.json = json; } public String toJson() throws IOException { return mapper.writeValueAsString(jsonMap); } public Map getJsonMap() { return jsonMap; } public List getFillData(){ return getFillData(BudgetConstant.EXPECT_YES); } //直接写值 public synchronized void writeDataValue(Integer dataRow ,Integer dataCol,BigDecimal dataValue){ String key = dataRow+"#"+dataCol; Map cellMap = allCellMap.get(key); if(dataValue==null) dataValue=BigDecimal.ZERO; if(cellMap!=null){ cellMap.put("value", dataValue.doubleValue()); change = true; } } public synchronized void writeDataValue(Integer dataRow ,Integer dataCol,String dataValue){ String key = dataRow+"#"+dataCol; Map cellMap = allCellMap.get(key); if(dataValue==null) dataValue=""; if(cellMap!=null){ cellMap.put("value", dataValue); change = true; } } public synchronized void writeFormula(Integer dataRow ,Integer dataCol,String formula){ _writeCellValue(dataRow,dataCol,"formula",formula); } private synchronized void _writeCellValue(Integer row ,Integer col,String key, String value){ Map cellMap = getDataCell(row,col); if(cellMap!=null){ cellMap.put(key, value); change = true; } } //获得单元格 public synchronized Map getDataCell(Integer row,Integer col){ String key = row+"#"+col; Map cellMap = allCellMap.get(key); return cellMap; } public String getString(Integer row ,Integer col){ return oConvertUtils.getString(getDataValue(row,col)); } public BigDecimal getDecimal(Integer row ,Integer col){ String sValue = getString(row,col); try { if (StringUtils.isNotBlank(sValue)) { return new BigDecimal(sValue); } }catch(Exception e){ return BigDecimal.ZERO; } return null; } private synchronized Object getDataValue(Integer row ,Integer col){ Map cellMap =getDataCell(row,col); if(cellMap!=null){ return cellMap.get("value"); } return null; } //获得tag的值 public String getTagValue(Integer row ,Integer col,String tagKey){ Map cellMap = getDataCell(row,col ); if(cellMap!=null){ Map cellTag = (Map) cellMap.get("tag"); if(cellTag!=null){ return oConvertUtils.getString(cellTag.get(tagKey)); } } return null; } /** * * @param defalutExcpet 默认预实维度 * @return */ public List getFillData(Integer defalutExcpet){ Integer sheetIndex = 1; Map dataMap = data(sheetIndex); Map dataTable = (Map) dataMap.get("dataTable"); List list = new ArrayList<>(); for(Map.Entry entry :dataTable.entrySet()){ String row = entry.getKey(); Map rowCols = (Map) entry.getValue(); for(Map.Entry rowColsEntry :rowCols.entrySet()) { String col = rowColsEntry.getKey(); BudgetSpreadCellData cellData = new BudgetSpreadCellData(); Map cellMap = (Map) rowColsEntry.getValue(); Map cellTag = (Map) cellMap.get("tag"); if (cellTag == null) continue; Boolean dataCell = (Boolean) cellTag.get("dataCell"); if (dataCell == null || !dataCell) continue; Object value = cellMap.get("value"); String formula =oConvertUtils.getString(cellMap.get("formula")); Integer expect = oConvertUtils.getInt(cellTag.get("expect"),defalutExcpet); cellData.setFormula(formula); cellData.setExpect(expect); String dataType =oConvertUtils.getString(cellTag.get("datatype")); cellData.setDataType(dataType); String showType =oConvertUtils.getString(cellTag.get("showType")); cellData.setShowType(showType); if(StringUtils.equals(dataType,"txt") || dataType==null){ cellData.setTxtValue(value==null?null:value.toString()); }else{ if(value!=null && StringUtils.isNotBlank(value.toString())){ try { cellData.setValue(BudgetUtils.measureShowValueToDb(new BigDecimal(StringUtils.trim(value.toString())), dataType, showType)); }catch (NumberFormatException ne){ System.out.println(row+","+col+","+value.toString()); throw ne; } } } String functionId = oConvertUtils.getString(cellTag.get("functionId")); cellData.setFunctionId(functionId); String measureId = oConvertUtils.getString(cellTag.get("measureId")); cellData.setMeasureId(measureId); String measureName = oConvertUtils.getString(cellTag.get("measureName")); cellData.setMeasureName(measureName); String entityId = oConvertUtils.getString(cellTag.get("entityId")); cellData.setEntityId(entityId); String entityName = oConvertUtils.getString(cellTag.get("entityName")); cellData.setEntityName(entityName); Integer includeTax = oConvertUtils.getInt(cellTag.get("includeTax"),Integer.valueOf(Globals.YES)); cellData.setIncludeTax(includeTax); String controlFlag = oConvertUtils.getString(cellTag.get("controlFlag")); cellData.setControlFlag(controlFlag); Integer readOnly = oConvertUtils.getInt(cellTag.get("readOnly"),Integer.valueOf(Globals.NO)); cellData.setReadOnly(readOnly); cellData.addDataRule("readOnly", readOnly); String fractionNum = oConvertUtils.getString(cellTag.get("fractionNum")); String currencySymbol = oConvertUtils.getString(cellTag.get("currencySymbol")); String thousandth = oConvertUtils.getString(cellTag.get("thousandth")); String saveAuth = oConvertUtils.getString(cellTag.get("saveAuth")); cellData.addDataRule("fractionNum", fractionNum); cellData.addDataRule("currencySymbol", currencySymbol); cellData.addDataRule("thousandth", thousandth); cellData.addDataRule("saveAuth", saveAuth); cellData.setRow(Integer.valueOf(row)); cellData.setCol(Integer.valueOf(col)); String findKey =oConvertUtils.getString(cellTag.get("findKey")); cellData.setFindKey(findKey); cellData.setCellMapRef(cellMap); list.add(cellData); } } return list; } public Integer getRowCount() { return rowCount; } public void setRowCount(Integer rowCount) { this.rowCount = rowCount; } public Integer getColCount() { return colCount; } public void setColCount(Integer colCount) { this.colCount = colCount; } public boolean isChange() { return change; } public void setChange(boolean change) { this.change = change; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTempsheetId() { return tempsheetId; } public void setTempsheetId(String tempsheetId) { this.tempsheetId = tempsheetId; } }