Commit 42f2dd98 authored by jichao's avatar jichao

公式

parent f93f15b5
package org.rcisoft.business.device.report.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.rcisoft.business.device.report.service.FormulaService;
import org.rcisoft.business.device.report.vo.FormulaVo;
import org.rcisoft.core.constant.MessageConstant;
import org.rcisoft.core.model.PersistModel;
import org.rcisoft.core.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
/**
* Created by JiChao on 2018/8/29.
* 公式
*/
@Api(tags = "设备报表--公式")
@RestController
@RequestMapping("formula")
public class FormulaController {
@Autowired
private FormulaService formulaServiceImpl;
@ApiOperation(value="新增公式和变量", notes="新增公式和变量")
@RequestMapping("/addFormulaVariable")
public Result addFormulaVariable(@RequestBody FormulaVo formulaVo) {
Integer result = formulaServiceImpl.addFormulaVariable(formulaVo);
return Result.builder(new PersistModel(result), MessageConstant.MESSAGE_ALERT_SUCCESS, MessageConstant.MESSAGE_ALERT_ERROR, result);
}
@ApiOperation(value="修改公式和变量", notes="修改公式和变量")
@RequestMapping("/updateFormulaVariable")
public Result updateFormulaVariable(@RequestBody FormulaVo formulaVo) {
Integer result = formulaServiceImpl.updateFormulaVariable(formulaVo);
return Result.builder(new PersistModel(result), MessageConstant.MESSAGE_ALERT_SUCCESS, MessageConstant.MESSAGE_ALERT_ERROR, result);
}
@ApiOperation(value="删除公式和变量", notes="删除公式和变量")
@RequestMapping("/deleteFormulaVariable")
public Result deleteFormulaVariable(@RequestParam String formulaId) {
Integer result = formulaServiceImpl.deleteFormulaVariable(formulaId);
return Result.builder(new PersistModel(result), MessageConstant.MESSAGE_ALERT_SUCCESS, MessageConstant.MESSAGE_ALERT_ERROR, result);
}
@ApiOperation(value="查询公式和变量", notes="查询公式和变量")
@RequestMapping("/selectFormulaVariable")
public Result selectFormulaVariable(@RequestParam String formulaId) {
return Result.builder(new PersistModel(1), MessageConstant.MESSAGE_ALERT_SUCCESS, MessageConstant.MESSAGE_ALERT_ERROR, formulaServiceImpl.selectFormulaVariable(formulaId));
}
@ApiOperation(value="查询公式列表", notes="查询公式列表")
@RequestMapping("/selectFormulaList")
public Result selectFormulaList(@RequestParam String proId) {
return Result.builder(new PersistModel(1), MessageConstant.MESSAGE_ALERT_SUCCESS, MessageConstant.MESSAGE_ALERT_ERROR, formulaServiceImpl.selectFormulaList(proId));
}
@ApiOperation(value="导出公式", notes="导出公式")
@RequestMapping("/downloadFormula")
public void downloadFormula(HttpServletResponse response, String formulaId, String start, String end) {
formulaServiceImpl.downloadFormula(response, formulaId, start, end);
}
}
package org.rcisoft.business.device.report.dao;
import org.apache.ibatis.annotations.*;
import org.rcisoft.business.device.report.entity.Formula;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;
/**
* Created by JiChao on 2018/8/29.
*/
@Repository
public interface FormulaRepository extends Mapper<Formula> {
@Insert("<script>insert into bus_formula(ID, NAME, FORMULA, PRO_ID, CREATE_TIME) " +
"values(#{id}, #{name}, #{formula}, #{proId}, #{createTime})</script>")
@ResultType(Integer.class)
Integer addFormula(Formula formula);
@Update("<script>update bus_formula set NAME=#{name},FORMULA=#{formula} where ID=#{id}</script>")
@ResultType(Integer.class)
Integer updateFormula(Formula formula);
@Delete("<script>delete from bus_formula where ID=#{id}</script>")
@ResultType(Integer.class)
Integer deleteFormula(@Param("id") String id);
@Select("<script>select * from bus_formula where id = #{id}</script>")
@ResultMap("formula")
Formula selectFormula(@Param("id") String id);
@Select("<script>select * from bus_formula where PRO_ID=#{proId} order by CREATE_TIME desc</script>")
@ResultMap("formula")
List<Formula> selectFormulaList(@Param("proId") String proId);
}
package org.rcisoft.business.device.report.dao;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.rcisoft.business.device.report.entity.TotalData;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;
/**
* Created by JiChao on 2018/9/3.
*/
@Repository
public interface TotalDataRepository extends Mapper<TotalData> {
@Select("<script>select * from total_data where PRO_ID=#{proId} and tm between #{start} and #{end} order by tm asc</script>")
@ResultMap("totalData")
List<TotalData> selectTotalData(@Param("proId") String proId, @Param("start") String start, @Param("end") String end);
}
package org.rcisoft.business.device.report.dao;
import org.apache.ibatis.annotations.*;
import org.rcisoft.business.device.report.entity.Variable;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;
/**
* Created by JiChao on 2018/8/29.
*/
@Repository
public interface VariableRepository extends Mapper<Variable> {
/**
* 批量新增
* @param list
* @return
*/
@Insert("<script><foreach collection=\"list\" item=\"item\" separator=\";\">" +
"insert into bus_variable(ID, VARIABLE, SOURCE, PARAM, FORMULA_ID, PRO_ID, CREATE_TIME, POSITION) " +
"values(#{item.id}, #{item.variable}, #{item.source}, #{item.param}, #{item.formulaId}, #{item.proId}, #{item.createTime}, #{item.position})" +
"</foreach></script>")
@ResultType(Integer.class)
Integer addVariable(List<Variable> list);
/**
* 根据公式id删除变量
* @param formulaId
* @return
*/
@Delete("<script>delete from bus_variable where FORMULA_ID = #{formulaId}</script>")
Integer deleteVariable(@Param("formulaId") String formulaId);
/**
* 根据公式id查找所有变量
* @param formulaId
* @return
*/
@Select("<script>select * from bus_variable where FORMULA_ID = #{formulaId} order by POSITION asc</script>")
@ResultMap("variable")
List<Variable> selectVariable(@Param("formulaId") String formulaId);
}
package org.rcisoft.business.device.report.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import java.util.Date;
/**
* Created by JiChao on 2018/8/29.
* 公式
*/
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Formula {
private String id;
private String name;
private String formula;
private String proId;
private Date createTime;
}
package org.rcisoft.business.device.report.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.Date;
/**
* Created by JiChao on 2018/8/28.
*/
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TotalData {
private Integer id;
private String proId;
private Date tm;
private String json;
}
package org.rcisoft.business.device.report.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import java.util.Date;
/**
* Created by JiChao on 2018/8/29.
* 变量
*/
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Variable {
private String id;
private String variable;
private String source;
private String param;
private String formulaId;
private String proId;
private Date createTime;
private Integer position;
}
package org.rcisoft.business.device.report.service;
import org.rcisoft.business.device.report.entity.Formula;
import org.rcisoft.business.device.report.vo.FormulaVo;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
* Created by JiChao on 2018/8/31.
*/
public interface FormulaService {
/**
* 新增公式
* @param formula
* @return
*/
Integer addFormula(Formula formula);
/**
* 编辑公式
* @param formula
* @return
*/
Integer updateFormula(Formula formula);
/**
* 删除公式
* @param id
* @return
*/
Integer deleteFormula(String id);
/**
* 根据id查询
* @param id
* @return
*/
Formula selectFormula(String id);
/**
* 根据项目id查询公式的list
* @param proId
* @return
*/
List<Formula> selectFormulaList(String proId);
/**
* 新增公式和变量
* @param formulaVo
* @return
*/
Integer addFormulaVariable(FormulaVo formulaVo);
/**
* 编辑公式和变量
* @param formulaVo
* @return
*/
Integer updateFormulaVariable(FormulaVo formulaVo);
/**
* 删除公式和变量
* @param formulaId
* @return
*/
Integer deleteFormulaVariable(String formulaId);
/**
* 根据公式id查询公式和其所拥有的变量
* @param formulaId
* @return
*/
FormulaVo selectFormulaVariable(String formulaId);
/**
* 导出公式excel数据
* @param formulaId 公式id
* @param start 开始时间
* @param end 结束时间
*/
void downloadFormula(HttpServletResponse response, String formulaId, String start, String end);
}
package org.rcisoft.business.device.report.service;
import org.rcisoft.business.device.report.entity.Variable;
import java.util.List;
/**
* Created by JiChao on 2018/8/31.
*/
public interface VariableService {
/**
* 批量新增变量
* @param list
* @return
*/
Integer addVariable(List<Variable> list, String formulaId, String proId);
/**
* 根据公式id删除所有变量
* @param formulaId
* @return
*/
Integer deleteVariable(String formulaId);
/**
* 根据公式id查找所有变量
* @param formulaId
* @return
*/
List<Variable> selectVariable(String formulaId);
}
package org.rcisoft.business.device.report.service.impl;
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.poi.ss.usermodel.CellType;
import org.rcisoft.business.device.report.dao.FormulaRepository;
import org.rcisoft.business.device.report.dao.TotalDataRepository;
import org.rcisoft.business.device.report.entity.Formula;
import org.rcisoft.business.device.report.entity.TotalData;
import org.rcisoft.business.device.report.entity.Variable;
import org.rcisoft.business.device.report.service.FormulaService;
import org.rcisoft.business.device.report.service.VariableService;
import org.rcisoft.business.device.report.vo.FormulaVo;
import org.rcisoft.core.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* Created by JiChao on 2018/8/31.
*/
@Service
public class FormulaServiceImpl implements FormulaService {
@Autowired
private FormulaRepository formulaRepository;
@Autowired
private VariableService variableServiceImpl;
@Autowired
private TotalDataRepository totalDataRepository;
@Override
public Integer addFormula(Formula formula) {
formula.setCreateTime(new Date());
return formulaRepository.addFormula(formula);
}
@Override
public Integer updateFormula(Formula formula) {
return formulaRepository.updateFormula(formula);
}
@Override
public Integer deleteFormula(String id) {
return formulaRepository.deleteFormula(id);
}
@Override
public Formula selectFormula(String id) {
return formulaRepository.selectFormula(id);
}
@Override
public List<Formula> selectFormulaList(String proId) {
return formulaRepository.selectFormulaList(proId);
}
@Transactional
@Override
public Integer addFormulaVariable(FormulaVo formulaVo) {
Formula formula = formulaVo.getFormula();
String uuid = UuidUtil.create32();
formula.setId(uuid);
Integer i = this.addFormula(formula);
Integer k = variableServiceImpl.addVariable(formulaVo.getVariableList(), uuid, formula.getProId());
return i;
}
@Transactional
@Override
public Integer updateFormulaVariable(FormulaVo formulaVo) {
Formula formula = formulaVo.getFormula();
String id = formula.getId();
Integer i = this.updateFormula(formula);
Integer j = variableServiceImpl.deleteVariable(id);
Integer k = variableServiceImpl.addVariable(formulaVo.getVariableList(), id, formula.getProId());
return i;
}
@Transactional
@Override
public Integer deleteFormulaVariable(String formulaId) {
Integer i = this.deleteFormula(formulaId);
Integer j = variableServiceImpl.deleteVariable(formulaId);
return i;
}
@Transactional
@Override
public FormulaVo selectFormulaVariable(String formulaId) {
Formula formula = this.selectFormula(formulaId);
List<Variable> variableList = variableServiceImpl.selectVariable(formulaId);
return new FormulaVo(formula, variableList);
}
@Override
public void downloadFormula(HttpServletResponse response, String formulaId, String start, String end) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 公式计算
FelEngine fel = new FelEngineImpl();
// 根据id查找公式
Formula formula = this.selectFormula(formulaId);
// 根据id查找变量
List<Variable> variableList = variableServiceImpl.selectVariable(formulaId);
// 根据proId和时间区间,查找数据list
String proId = formula.getProId();
start = start + " 00:00:00";
end = end + " 23:59:59";
List<TotalData> totalDataList = totalDataRepository.selectTotalData(proId, start, end);
// 生成excel
HSSFWorkbook workbook = new HSSFWorkbook();
//定义sheet
HSSFSheet sheet = workbook.createSheet("sheet1");
// 创建第一行
int i = 0;
HSSFRow head = sheet.createRow(i++);
head.createCell(0, CellType.STRING).setCellValue("时间");
head.createCell(1, CellType.STRING).setCellValue("数值");
// 循环数据list
for (TotalData totalData : totalDataList) {
// 创建row
HSSFRow row = sheet.createRow(i++);
// 公式
String f = formula.getFormula();
// 得到原始数据
String json = totalData.getJson();
// 变成json对象
JSONObject jsonObject = JSON.parseObject(json);
// 循环变量list
for (Variable variable : variableList) {
// 公式中的变量
String v = variable.getVariable();
// 来源
String s = variable.getSource();
// 参数
String p = variable.getParam();
// 根据来源参数,获取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();
}
}
}
if (value != null)
// 替换公式
f = f.replaceAll(v, value);
}
// 判断表达式是否由数字和运算符号组成
boolean flag = true;
String[] split = f.split("");
for (String s : split) {
if (!s.matches("\\d|\\(|\\)|\\+|\\-|\\*|/|%|\\."))
flag = false;
}
String eval = "";
// 计算出公式的最终结果
if (flag) {
Object result = fel.eval(f);
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);
}
//下载
try(OutputStream outputStream = response.getOutputStream()) {
//设置下载头
response.setHeader("Content-disposition", "attachment;filename=" + formulaId + ".xls");
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
......@@ -67,10 +67,11 @@ public class ReportServiceImpl implements ReportService {
if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
try(OutputStream outputStream = response.getOutputStream()) {
// 如果已经存在文件,直接下载
if (!file.exists()) {
// if (!file.exists()) {
// 不存在直接生成一个文件,生成文件太耗时间
// 总会出问题,还是每次都生成吧
file = this.createExcel(proId, year, month, day, file);
}
// }
//转成字节
bytes = FileUtils.readFileToByteArray(file);
//设置下载头
......
package org.rcisoft.business.device.report.service.impl;
import org.rcisoft.business.device.report.dao.VariableRepository;
import org.rcisoft.business.device.report.entity.Variable;
import org.rcisoft.business.device.report.service.VariableService;
import org.rcisoft.core.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
/**
* Created by JiChao on 2018/8/31.
*/
@Service
public class VariableServiceImpl implements VariableService {
@Autowired
private VariableRepository variableRepository;
@Override
public Integer addVariable(List<Variable> list, String formulaId, String proId) {
for (int i = 0; i < list.size(); i++) {
Variable variable = list.get(i);
variable.setId(UuidUtil.create32());
variable.setFormulaId(formulaId);
variable.setProId(proId);
variable.setCreateTime(new Date());
variable.setPosition(i);
}
return variableRepository.addVariable(list);
}
@Override
public Integer deleteVariable(String formulaId) {
return variableRepository.deleteVariable(formulaId);
}
@Override
public List<Variable> selectVariable(String formulaId) {
return variableRepository.selectVariable(formulaId);
}
}
package org.rcisoft.business.device.report.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.rcisoft.business.device.report.entity.Formula;
import org.rcisoft.business.device.report.entity.Variable;
import java.util.List;
/**
* Created by JiChao on 2018/8/31.
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class FormulaVo {
private Formula formula;
private List<Variable> variableList;
}
package org.rcisoft.core.util;
import java.util.UUID;
/**
* Created by JiChao on 2018/8/31.
*/
public class UuidUtil {
public static String create32() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.rcisoft.business.device.report.dao.FormulaRepository">
<resultMap id="formula" type="org.rcisoft.business.device.report.entity.Formula">
<id column="ID" jdbcType="VARCHAR" property="id"></id>
<result column="NAME" jdbcType="VARCHAR" property="name"/>
<result column="FORMULA" jdbcType="VARCHAR" property="formula"/>
<result column="PRO_ID" jdbcType="VARCHAR" property="proId"/>
<result column="CREATE_TIME" jdbcType="TIMESTAMP" property="createTime"/>
</resultMap>
</mapper>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.rcisoft.business.device.report.dao.TotalDataRepository">
<resultMap id="totalData" type="org.rcisoft.business.device.report.entity.TotalData">
<id column="ID" jdbcType="VARCHAR" property="id"></id>
<result column="PRO_ID" jdbcType="VARCHAR" property="proId"/>
<result column="TM" jdbcType="TIMESTAMP" property="tm"/>
<result column="JSON" jdbcType="VARCHAR" property="json"/>
</resultMap>
</mapper>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.rcisoft.business.device.report.dao.VariableRepository">
<resultMap id="variable" type="org.rcisoft.business.device.report.entity.Variable">
<id column="ID" jdbcType="VARCHAR" property="id"></id>
<result column="VARIABLE" jdbcType="VARCHAR" property="variable"/>
<result column="SOURCE" jdbcType="VARCHAR" property="source"/>
<result column="FORMULA_ID" jdbcType="VARCHAR" property="formulaId"/>
<result column="PRO_ID" jdbcType="VARCHAR" property="proId"/>
<result column="CREATE_TIME" jdbcType="TIMESTAMP" property="createTime"/>
<result column="POSITION" jdbcType="INTEGER" property="position"/>
</resultMap>
</mapper>
\ No newline at end of file
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