// }
// return false;
// }
public OperResult importPCS9000Data(String date){
OperResult result = new OperResult();
String entHierarchyID = null;
String energyMediaID = null;
// /**!
// * 首先检测月表是否存在如果不存在的话那就重建月表
// */
// String tableName = null;
// try {
// Date dateYMD = sdfYMD.parse(date);
// String dateYM = sdfYM.format(dateYMD);
// tableName = "measure_table"+dateYM;
// if(!isTableExist(tableName)){
// result = createMeasureTable(tableName);
// if(result.isFAILED()){
// return result;
// }
// }
// } catch (ParseException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// result = new OperResult();
// result.setFailed(e1.getMessage());
// return result;
// }
List<Process> processes = processDAO.findAll();
Hashtable<String,MeasureTable> measureHash = new Hashtable<String,MeasureTable>();
for(Process process:processes){
EntHierarchy entHierarchy = process.getEntHierarchy();
if(entHierarchy==null){
Log.error("No such department:"+process.getEntHierarchy().getId());//bug!!exception
continue;
}else{
entHierarchyID = entHierarchy.getId();
List<PInMeasure> ins = pInMeasureDAO.findByProperty("process", process);
// Set<PInMeasure> ins = process.getPInMeasures();
for(PInMeasure in:ins){//对于所有工艺关联的输入计量点InMeasure
// Measure inMeasure = in.getMeasure();
Measure inMeasure = measureDAO.findById(in.getId().getMId());
if(inMeasure==null){
Log.error("No such in measure:"+in.getId().getMId());
continue;
}else{
//得到对应的计量点的能源介质类型
// EnergyMedia energyMedia = inMeasure.getEnergyMedia();
EnergyMedia energyMedia = energyMediaDAO.findById(inMeasure.getEnergyMedia().getId());//bug!!exception
if(energyMedia==null){
Log.error("No such in measure energy media:"+inMeasure.getEnergyMedia().getId());
continue;
}else{
energyMediaID = energyMedia.getId();
MeasureTable measureTable = null;
String measureTableIDHash = process.getId()+energyMedia.getId()+date;
if(measureHash.containsKey(measureTableIDHash)){
measureTable = measureHash.get(measureTableIDHash);
}else{
measureTable = new MeasureTable();//创建事实表中记录
measureTable.setId(new MeasureTableId(date,entHierarchyID,energyMediaID));
measureHash.put(measureTableIDHash, measureTable);
}
//获得指定计量点的指定时间的计量值
double value = getMeasureData(inMeasure,date);
String corType = inMeasure.getCorType();
//根据计量点类型进行计量校正,在计算时,输入为正,输出为负。
/**!
* mvalue:计量值
* svalue:人工调整量
* cvalue:计量折标量
* value: 计量总量
*/
if("P".equalsIgnoreCase(corType)){
//百分比校正
measureTable.setMValue(value);
measureTable.setSValue(value+value*Double.parseDouble(inMeasure.getCorValue()));
}else if("O".equalsIgnoreCase(corType)){
//绝对值校正
measureTable.setMValue(value);
measureTable.setSValue(value+Double.parseDouble(inMeasure.getCorValue()));
}else if("U".equalsIgnoreCase(corType)){
//计算公式校正
measureTable.setMValue(value);
try{
Object uresult = ScriptEvaluator.evalWithMVEL(inMeasure.getCorValue(), measureTable);
measureTable.setSValue((Double)uresult);
}catch(Exception e){
e.printStackTrace();
}
}else{
//不校正
measureTable.setMValue(value);
measureTable.setSValue(0.0);
}
//根据计量值和校正值设置对应的计量总量和计量折标量
measureTable.setValue(measureTable.getMValue()+measureTable.getSValue());
measureTable.setCValue(measureTable.getValue()*energyMedia.getScc());
}
}
}
List<POutMeasure> outs = pOutMeasureDAO.findByProperty("process", process);
// Set<POutMeasure> outs = process.getPOutMeasures();
for(POutMeasure out:outs){//对于所有工艺关联的输出计量点OutMeasure
Measure outMeasure = measureDAO.findById(out.getId().getMId());
// Measure outMeasure = out.getMeasure();
if(outMeasure==null){
Log.error("No such out measure:"+out.getMeasure().getId());
continue;
}else{
//得到对应的计量点的能源介质类型
// EnergyMedia energyMedia = outMeasure.getEnergyMedia();
EnergyMedia energyMedia = energyMediaDAO.findById(outMeasure.getEnergyMedia().getId());
if(energyMedia==null){
Log.error("No such out measure energy media:"+outMeasure.getEnergyMedia().getId());
continue;
}else{
energyMediaID = energyMedia.getId();
MeasureTable measureTable = null;
String measureTableIDHash = process.getId()+energyMedia.getId()+date;
if(measureHash.containsKey(measureTableIDHash)){
measureTable = measureHash.get(measureTableIDHash);
}else{
measureTable = new MeasureTable();
measureTable.setId(new MeasureTableId(date,entHierarchyID,energyMediaID));
measureHash.put(measureTableIDHash, measureTable);
}
// MeasureTable measureTable = null;
// if(measureHash.containsKey(process.getId()+energyMedia.getId()+date)){
// measureTable = measureHash.get(process.getId()+energyMedia.getId()+date);
// }else{
// measureTable = new MeasureTable();
// measureTable.setId(new MeasureTableId(date,entHierarchyID,energyMediaID));
// measureHash.put(process.getId()+energyMedia.getId()+date, measureTable);
// }
//获得指定计量点的指定时间的计量值
double value = getMeasureData(outMeasure,date);
String corType = outMeasure.getCorType();
//根据计量点类型进行计量校正,在计算时,输入为正,输出为负。
if("P".equalsIgnoreCase(corType)){
//百分比校正
measureTable.setMValue(-value);
measureTable.setSValue(-value-value*Double.parseDouble(outMeasure.getCorValue()));
}else if("O".equalsIgnoreCase(corType)){
//绝对值校正
measureTable.setMValue(-value);
measureTable.setSValue(-value-Double.parseDouble(outMeasure.getCorValue()));
}else if("U".equalsIgnoreCase(corType)){
//计算公式校正
measureTable.setMValue(value);
try{
Object uresult = ScriptEvaluator.evalWithMVEL(outMeasure.getCorValue(), measureTable);
measureTable.setSValue((Double)uresult);
}catch(Exception e){
e.printStackTrace();
}
}else{
//不校正
measureTable.setMValue(-value);
measureTable.setSValue(0.0);
}
//根据计量值和校正值设置对应的计量总量和计量折标量
measureTable.setValue(measureTable.getMValue()+measureTable.getSValue());
measureTable.setCValue(measureTable.getValue()*energyMedia.getScc());
}
}
}
}
}
/**!
* 处理MeasureHash中的所有对象并入库,暂时采用逐条的方式,需要改!!
*/
int i=0;
int total = measureHash.size();
for(MeasureTable measureTable:measureHash.values()){
try{
measureTableDAO.merge(measureTable);
i++;
Log.info("Merge measure table "+i+"/"+total);
}catch(Exception e){
log.error(new StringBuilder().append("Merge error for measure table record\nDepartment ID:").append(measureTable.getId().getHierarchyId())
.append("\nEM ID:").append(measureTable.getId().getEMId())
.append("\nDate ID:").append(measureTable.getId().getTimeId())
.append("\nMeasure Value:").append(measureTable.getMValue())
.append("\nCorrect Value:").append(measureTable.getSValue())
.append("\nTotal Value:").append(measureTable.getValue())
.append("\nSCC Value:").append(measureTable.getCValue()).toString());
log.error(e.getMessage());
e.printStackTrace();
}
}
if(i!=total){
log.error("Not all measure table record is saved to database, Please check log!");
}else{
Log.info("Merge measure table "+i+"/"+total);
}
if(i>0){
result.setSucceed();
result.setSucceed(MEASURE_TABLE_SAVE_OK, i,total);
}
return result;
}