Commit 69786df7 authored by jichao's avatar jichao

最后一次改bug,项目一期结束

parent bbdfd05f
...@@ -65,4 +65,10 @@ public class FormulaController { ...@@ -65,4 +65,10 @@ public class FormulaController {
public void downloadFormula(HttpServletResponse response, String formulaId, String start, String end) { public void downloadFormula(HttpServletResponse response, String formulaId, String start, String end) {
formulaServiceImpl.downloadFormula(response, formulaId, start, end); formulaServiceImpl.downloadFormula(response, formulaId, start, end);
} }
@ApiOperation(value="批量导出公式", notes="批量导出公式")
@RequestMapping("/downloadFormulas")
public void downloadFormulas(HttpServletResponse response, String proId, String formulaIds, String start, String end) {
formulaServiceImpl.downloadFormulas(response, proId, formulaIds, start, end);
}
} }
...@@ -34,4 +34,11 @@ public interface FormulaRepository extends Mapper<Formula> { ...@@ -34,4 +34,11 @@ public interface FormulaRepository extends Mapper<Formula> {
@ResultMap("formula") @ResultMap("formula")
List<Formula> selectFormulaList(@Param("proId") String proId); List<Formula> selectFormulaList(@Param("proId") String proId);
@Select("<script>select * from bus_formula where ID in " +
"<foreach collection=\"array\" item=\"item\" open=\"(\" separator=\",\" close=\")\">" +
"#{item}</foreach> " +
"order by CREATE_TIME desc</script>")
@ResultMap("formula")
List<Formula> selectFormulas(String[] ids);
} }
...@@ -32,8 +32,13 @@ public interface ReportRepository { ...@@ -32,8 +32,13 @@ public interface ReportRepository {
* @param params * @param params
* @return * @return
*/ */
@Select("<script>select t.DEV_NUM,t.TM,t.ORIGINAL_STR,s.SENSOR_JSON,m.ELEC,m.GAS from total_original t " + // @Select("<script>select t.DEV_NUM,t.TM,t.ORIGINAL_STR,s.SENSOR_JSON,m.ELEC,m.GAS from total_original t " +
"left join (select d.DEV_NUM,m.CREATE_TIME TM,m.ELEC,m.GAS from energy_count_m m,bus_device_meter d where m.PRO_ID=d.PRO_ID and m.MET_NUM=d.MET_NUM) m on t.TM=m.TM and t.DEV_NUM=m.DEV_NUM " + // "left join (select d.DEV_NUM,m.CREATE_TIME TM,m.ELEC,m.GAS from energy_count_m m,bus_device_meter d where m.PRO_ID=d.PRO_ID and m.MET_NUM=d.MET_NUM) m on t.TM=m.TM and t.DEV_NUM=m.DEV_NUM " +
// "left join total_sensor s on t.TM=s.TM and t.PRO_ID=s.PRO_ID " +
// "where t.WATER_V is null and t.ELEC_V is null and t.GAS_V is null " +
// "and t.PRO_ID=#{proId} and date_format(t.TM, '%Y-%c-%e')=#{time} " +
// "order by t.DEV_NUM,t.TM</script>")
@Select("<script>select t.DEV_NUM,t.TM,t.ORIGINAL_STR,s.SENSOR_JSON from total_original t " +
"left join total_sensor s on t.TM=s.TM and t.PRO_ID=s.PRO_ID " + "left join total_sensor s on t.TM=s.TM and t.PRO_ID=s.PRO_ID " +
"where t.WATER_V is null and t.ELEC_V is null and t.GAS_V is null " + "where t.WATER_V is null and t.ELEC_V is null and t.GAS_V is null " +
"and t.PRO_ID=#{proId} and date_format(t.TM, '%Y-%c-%e')=#{time} " + "and t.PRO_ID=#{proId} and date_format(t.TM, '%Y-%c-%e')=#{time} " +
......
...@@ -42,4 +42,11 @@ public interface VariableRepository extends Mapper<Variable> { ...@@ -42,4 +42,11 @@ public interface VariableRepository extends Mapper<Variable> {
@ResultMap("variable") @ResultMap("variable")
List<Variable> selectVariable(@Param("formulaId") String formulaId); List<Variable> selectVariable(@Param("formulaId") String formulaId);
@Select("<script>select * from bus_variable where FORMULA_ID in " +
"<foreach collection=\"array\" item=\"item\" open=\"(\" separator=\",\" close=\")\">" +
"#{item}</foreach> " +
" order by CREATE_TIME desc, POSITION asc</script>")
@ResultMap("variable")
List<Variable> selectVariables(String[] ids);
} }
...@@ -82,4 +82,12 @@ public interface FormulaService { ...@@ -82,4 +82,12 @@ public interface FormulaService {
*/ */
void downloadFormula(HttpServletResponse response, String formulaId, String start, String end); void downloadFormula(HttpServletResponse response, String formulaId, String start, String end);
/**
* 批量导出公式
* @param response
* @param formulaIds 公式ids,逗号分隔
* @param start
* @param end
*/
void downloadFormulas(HttpServletResponse response, String proId, String formulaIds, String start, String end);
} }
...@@ -30,4 +30,11 @@ public interface VariableService { ...@@ -30,4 +30,11 @@ public interface VariableService {
*/ */
List<Variable> selectVariable(String formulaId); List<Variable> selectVariable(String formulaId);
/**
* 根据公式ids查找所有变量
* @param ids
* @return
*/
List<Variable> selectVariables(String[] ids);
} }
...@@ -4,10 +4,11 @@ import com.alibaba.fastjson.JSON; ...@@ -4,10 +4,11 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.greenpineyu.fel.FelEngine; import com.greenpineyu.fel.FelEngine;
import com.greenpineyu.fel.FelEngineImpl; import com.greenpineyu.fel.FelEngineImpl;
import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.util.CellRangeAddress;
import org.rcisoft.business.device.report.dao.FormulaRepository; import org.rcisoft.business.device.report.dao.FormulaRepository;
import org.rcisoft.business.device.report.dao.TotalDataRepository; import org.rcisoft.business.device.report.dao.TotalDataRepository;
import org.rcisoft.business.device.report.entity.Formula; import org.rcisoft.business.device.report.entity.Formula;
...@@ -25,6 +26,7 @@ import javax.servlet.http.HttpServletResponse; ...@@ -25,6 +26,7 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
...@@ -141,52 +143,143 @@ public class FormulaServiceImpl implements FormulaService { ...@@ -141,52 +143,143 @@ public class FormulaServiceImpl implements FormulaService {
JSONObject jsonObject = JSON.parseObject(json); JSONObject jsonObject = JSON.parseObject(json);
// 循环变量list // 循环变量list
for (Variable variable : variableList) { for (Variable variable : variableList) {
// 公式中的变量 f = this.replace(f, variable, jsonObject);
String v = variable.getVariable(); }
// 来源 String eval = this.getResult(f, fel);
String s = variable.getSource(); // 放入row中
// 参数 row.createCell(0, CellType.STRING).setCellValue(sdf.format(totalData.getTm()));
String p = variable.getParam(); row.createCell(1, CellType.STRING).setCellValue(eval);
// 根据来源参数,获取json中对应的值 }
String value = null; //下载
JSONObject s_json = (JSONObject) jsonObject.get(s); this.download(response, workbook, formula.getName());
if (s_json != null) { }
JSONObject r_json = (JSONObject) s_json.get("REG_VAL");
if (r_json != null) { @Override
Object p_json = r_json.get(p); public void downloadFormulas(HttpServletResponse response, String proId, String formulaIds, String start, String end) {
if (p_json != null) { String[] ids = formulaIds.split(",");
value = p_json.toString(); // 查询时间
} SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
start = start + " 00:00:00";
end = end + " 23:59:59";
// 公式计算类
FelEngine fel = new FelEngineImpl();
// 根据ids查找公式列表
List<Formula> formulaList = formulaRepository.selectFormulas(ids);
// 根据id查找变量
List<Variable> variableList = variableServiceImpl.selectVariables(ids);
// 根据proId和时间区间,查找数据list
List<TotalData> totalDataList = totalDataRepository.selectTotalData(proId, start, end);
// 生成excel
HSSFWorkbook workbook = new HSSFWorkbook();
//定义sheet
HSSFSheet sheet = workbook.createSheet("sheet1");
// 放入标题
HSSFRow row0 = sheet.createRow(0);
row0.createCell(0, CellType.STRING).setCellValue("公式名称");
HSSFRow row1 = sheet.createRow(1);
row1.createCell(0, CellType.STRING).setCellValue("公式内容");
// 放入公式
for (int i = 0; i < formulaList.size(); i++) {
Formula formula = formulaList.get(i);
row0.createCell(i + 1, CellType.STRING).setCellValue(formula.getName());
row1.createCell(i + 1, CellType.STRING).setCellValue(formula.getFormula());
}
HSSFRow row2 = sheet.createRow(2);
row2.createCell(0, CellType.STRING).setCellValue("时间");
CellRangeAddress cra=new CellRangeAddress(2, 2, 1, formulaList.size());// 合并单元格
sheet.addMergedRegion(cra);
//合并的单元格样式
HSSFCellStyle boderStyle = workbook.createCellStyle();
//水平居中
boderStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell cell = row2.createCell(1, CellType.STRING);
cell.setCellStyle(boderStyle);
cell.setCellValue("数值");
// row2.createCell(1, CellType.STRING).setCellValue("数值");
// 循环数据list
for (int i = 0; i < totalDataList.size(); i++) {
TotalData totalData = totalDataList.get(i);
// 得到json对象
JSONObject jsonObject = JSON.parseObject(totalData.getJson());
// 创建row
HSSFRow row = sheet.createRow(i + 3);
// 放入时间
row.createCell(0, CellType.STRING).setCellValue(sdf.format(totalData.getTm()));
// 循环公式
for (int j = 0; j < formulaList.size(); j++) {
Formula formula = formulaList.get(j);
String formulaId = formula.getId();
// 公式
String formulaFormula = formula.getFormula();
// 循环变量list
for (int k = 0; k < variableList.size(); k++) {
Variable variable = variableList.get(k);
// 找到属于这个公式的参数
if (StringUtils.equals(variable.getFormulaId(), formulaId)) {
formulaFormula = this.replace(formulaFormula, variable, jsonObject);
} }
} }
if (value != null) // 计算出公式的最终结果
// 替换公式 String eval = this.getResult(formulaFormula, fel);
f = f.replaceAll(v, value); row.createCell(j + 1, CellType.STRING).setCellValue(eval);
} }
// 判断表达式是否由数字和运算符号组成 }
boolean flag = true; //下载
String[] split = f.split(""); this.download(response, workbook, "formula");
for (String s : split) { }
if (!s.matches("\\d|\\(|\\)|\\+|\\-|\\*|/|%|\\."))
flag = false; private String replace(String formula, Variable variable, JSONObject jsonObject) {
} // 公式中的变量
String eval = ""; String v = variable.getVariable();
// 计算出公式的最终结果 // 来源
if (flag) { String s = variable.getSource();
Object result = fel.eval(f); // 参数
if (result != null) { String p = variable.getParam();
eval = result.toString(); // 根据来源参数,获取json中对应的值
String value = null;
JSONObject s_json = (JSONObject) jsonObject.get(s);
if (s_json != null) {
JSONObject r_json = (JSONObject) s_json.get("REG_VAL");
if (r_json != null) {
Object p_json = r_json.get(p);
if (p_json != null) {
value = p_json.toString();
} }
} }
// String eval = fel.eval(f).toString();
// 放入row中
row.createCell(0, CellType.STRING).setCellValue(sdf.format(totalData.getTm()));
row.createCell(1, CellType.STRING).setCellValue(eval);
} }
if (value != null)
// 替换公式
formula = formula.replaceAll(v, value);
return formula;
}
private String getResult(String formula, FelEngine fel) {
// 结果
String eval = "";
// 判断表达式是否由数字和运算符号组成
boolean flag = true;
String[] split = formula.split("");
for (String s : split) {
if (!s.matches("\\d|\\(|\\)|\\+|\\-|\\*|/|%|\\.")) {
flag = false;
break;
}
}
// 计算出公式的最终结果
if (flag) {
Object result = fel.eval(formula);
if (result != null) {
eval = result.toString();
}
}
return eval;
}
private void download(HttpServletResponse response, HSSFWorkbook workbook, String formulaName) {
//下载 //下载
try(OutputStream outputStream = response.getOutputStream()) { try(OutputStream outputStream = response.getOutputStream()) {
//设置下载头 //设置下载头
response.setHeader("Content-disposition", "attachment;filename=" + formulaId + ".xls"); response.setHeader("Content-disposition", "attachment;filename=" + formulaName + ".xls");
workbook.write(outputStream); workbook.write(outputStream);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
...@@ -198,4 +291,5 @@ public class FormulaServiceImpl implements FormulaService { ...@@ -198,4 +291,5 @@ public class FormulaServiceImpl implements FormulaService {
} }
} }
} }
} }
...@@ -146,8 +146,8 @@ public class ReportServiceImpl implements ReportService { ...@@ -146,8 +146,8 @@ public class ReportServiceImpl implements ReportService {
cell.setCellValue(deviceList.get(i).getParamNm()); cell.setCellValue(deviceList.get(i).getParamNm());
} }
//加入两列,用电量,用气量 //加入两列,用电量,用气量
head.createCell(size + 1, CellType.STRING).setCellValue("用电量"); // head.createCell(size + 1, CellType.STRING).setCellValue("用电量");
head.createCell(size + 2, CellType.STRING).setCellValue("用气量"); // head.createCell(size + 2, CellType.STRING).setCellValue("用气量");
//定义一个增量 //定义一个增量
int x = 1; int x = 1;
//循环数据 //循环数据
...@@ -176,8 +176,8 @@ public class ReportServiceImpl implements ReportService { ...@@ -176,8 +176,8 @@ public class ReportServiceImpl implements ReportService {
row.createCell(j + 1, CellType.STRING).setCellValue(res); row.createCell(j + 1, CellType.STRING).setCellValue(res);
} }
//放入表具数据 //放入表具数据
row.createCell(size + 1, CellType.STRING).setCellValue(original.getElec()); // row.createCell(size + 1, CellType.STRING).setCellValue(original.getElec());
row.createCell(size + 2, CellType.STRING).setCellValue(original.getGas()); // row.createCell(size + 2, CellType.STRING).setCellValue(original.getGas());
//用完删除当前行的数据 //用完删除当前行的数据
// originalList.remove(i--); // originalList.remove(i--);
} }
......
...@@ -42,4 +42,9 @@ public class VariableServiceImpl implements VariableService { ...@@ -42,4 +42,9 @@ public class VariableServiceImpl implements VariableService {
public List<Variable> selectVariable(String formulaId) { public List<Variable> selectVariable(String formulaId) {
return variableRepository.selectVariable(formulaId); return variableRepository.selectVariable(formulaId);
} }
@Override
public List<Variable> selectVariables(String[] ids) {
return variableRepository.selectVariables(ids);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment