Commit 69786df7 authored by jichao's avatar jichao

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

parent bbdfd05f
......@@ -65,4 +65,10 @@ public class FormulaController {
public void downloadFormula(HttpServletResponse response, String formulaId, String start, String 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> {
@ResultMap("formula")
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 {
* @param params
* @return
*/
@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 " +
// @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 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 " +
"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} " +
......
......@@ -42,4 +42,11 @@ public interface VariableRepository extends Mapper<Variable> {
@ResultMap("variable")
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 {
*/
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 {
*/
List<Variable> selectVariable(String formulaId);
/**
* 根据公式ids查找所有变量
* @param ids
* @return
*/
List<Variable> selectVariables(String[] ids);
}
......@@ -4,10 +4,11 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.greenpineyu.fel.FelEngine;
import com.greenpineyu.fel.FelEngineImpl;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellStyle;
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.TotalDataRepository;
import org.rcisoft.business.device.report.entity.Formula;
......@@ -25,6 +26,7 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
......@@ -141,6 +143,92 @@ public class FormulaServiceImpl implements FormulaService {
JSONObject jsonObject = JSON.parseObject(json);
// 循环变量list
for (Variable variable : variableList) {
f = this.replace(f, variable, jsonObject);
}
String eval = this.getResult(f, fel);
// 放入row中
row.createCell(0, CellType.STRING).setCellValue(sdf.format(totalData.getTm()));
row.createCell(1, CellType.STRING).setCellValue(eval);
}
//下载
this.download(response, workbook, formula.getName());
}
@Override
public void downloadFormulas(HttpServletResponse response, String proId, String formulaIds, String start, String end) {
String[] ids = formulaIds.split(",");
// 查询时间
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);
}
}
// 计算出公式的最终结果
String eval = this.getResult(formulaFormula, fel);
row.createCell(j + 1, CellType.STRING).setCellValue(eval);
}
}
//下载
this.download(response, workbook, "formula");
}
private String replace(String formula, Variable variable, JSONObject jsonObject) {
// 公式中的变量
String v = variable.getVariable();
// 来源
......@@ -161,32 +249,37 @@ public class FormulaServiceImpl implements FormulaService {
}
if (value != null)
// 替换公式
f = f.replaceAll(v, value);
formula = formula.replaceAll(v, value);
return formula;
}
private String getResult(String formula, FelEngine fel) {
// 结果
String eval = "";
// 判断表达式是否由数字和运算符号组成
boolean flag = true;
String[] split = f.split("");
String[] split = formula.split("");
for (String s : split) {
if (!s.matches("\\d|\\(|\\)|\\+|\\-|\\*|/|%|\\."))
if (!s.matches("\\d|\\(|\\)|\\+|\\-|\\*|/|%|\\.")) {
flag = false;
break;
}
}
String eval = "";
// 计算出公式的最终结果
if (flag) {
Object result = fel.eval(f);
Object result = fel.eval(formula);
if (result != null) {
eval = result.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);
return eval;
}
private void download(HttpServletResponse response, HSSFWorkbook workbook, String formulaName) {
//下载
try(OutputStream outputStream = response.getOutputStream()) {
//设置下载头
response.setHeader("Content-disposition", "attachment;filename=" + formulaId + ".xls");
response.setHeader("Content-disposition", "attachment;filename=" + formulaName + ".xls");
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
......@@ -198,4 +291,5 @@ public class FormulaServiceImpl implements FormulaService {
}
}
}
}
......@@ -146,8 +146,8 @@ public class ReportServiceImpl implements ReportService {
cell.setCellValue(deviceList.get(i).getParamNm());
}
//加入两列,用电量,用气量
head.createCell(size + 1, CellType.STRING).setCellValue("用电量");
head.createCell(size + 2, CellType.STRING).setCellValue("用气量");
// head.createCell(size + 1, CellType.STRING).setCellValue("用电量");
// head.createCell(size + 2, CellType.STRING).setCellValue("用气量");
//定义一个增量
int x = 1;
//循环数据
......@@ -176,8 +176,8 @@ public class ReportServiceImpl implements ReportService {
row.createCell(j + 1, CellType.STRING).setCellValue(res);
}
//放入表具数据
row.createCell(size + 1, CellType.STRING).setCellValue(original.getElec());
row.createCell(size + 2, CellType.STRING).setCellValue(original.getGas());
// row.createCell(size + 1, CellType.STRING).setCellValue(original.getElec());
// row.createCell(size + 2, CellType.STRING).setCellValue(original.getGas());
//用完删除当前行的数据
// originalList.remove(i--);
}
......
......@@ -42,4 +42,9 @@ public class VariableServiceImpl implements VariableService {
public List<Variable> selectVariable(String 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