@ -5,28 +5,33 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Snowflake ;
import cn.hutool.core.util.ObjectUtil ;
import com.ccsens.util.JacksonUtil ;
import com.ccsens.util.PoiUtil ;
import com.ccsens.util.RedisUtil ;
import com.ccsens.util.bean.message.common.InMessage ;
import com.ccsens.util.bean.message.common.MessageConstant ;
import com.ccsens.util.bean.message.common.MessageRule ;
import com.ccsens.wisdomcar.bean.dto.Message.CarRecordMessageDto ;
import com.ccsens.wisdomcar.bean.dto.RecordDto ;
import com.ccsens.wisdomcar.bean.po.* ;
import com.ccsens.wisdomcar.bean.vo.Message.CarRecordMessageVo ;
import com.ccsens.wisdomcar.bean.vo.RecordVo ;
import com.ccsens.wisdomcar.bean.vo.StepVo ;
import com.ccsens.wisdomcar.persist.dao.StepDao ;
import com.ccsens.wisdomcar.persist.dao.WisdomCarRecordDao ;
import com.ccsens.wisdomcar.persist.mapper.* ;
import com.ccsens.wisdomcar.util.Constant ;
import lombok.extern.slf4j.Slf4j ;
import org.apache.poi.ss.usermodel.Workbook ;
import org.apache.poi.xssf.usermodel.XSSFWorkbook ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Propagation ;
import org.springframework.transaction.annotation.Transactional ;
import javax.annotation.Resource ;
import java.math.BigDecimal ;
import java.util.HashSet ;
import java.util.List ;
import java.util.Random ;
import java.util.Set ;
import java.text.SimpleDateFormat ;
import java.util.* ;
/ * *
* @author 逗
@ -53,6 +58,12 @@ public class RecordService implements IRecordService{
private ScreenMapper screenMapper ;
@Resource
private RedisUtil redisUtil ;
@Resource
private WisdomCarRecordDao wisdomCarRecordDao ;
@Value ( "#{T(java.lang.Long).parseLong('${wisdom.weight:10000}')}" )
private Long weightMinus ;
@Value ( "#{T(java.lang.Long).parseLong('${wisdom.time:300000}')}" )
private Long timeMinus ;
@Override
public void disposeMessage ( CarRecordMessageDto carRecordMessageDto ) throws Exception {
@ -138,6 +149,104 @@ public class RecordService implements IRecordService{
saveAidRecord ( carRecordMessageDto , wisdomCar . getId ( ) , step , userIdSet ) ;
}
@Override
public List < RecordVo . WeightAndRfid > queryWeightAndRfid ( RecordDto . WeightAndRfid param , Long userId ) {
List < RecordVo . WeightAndRfid > list = queryWeightAndRfid ( param . getCarId ( ) , param . getStartTime ( ) , param . getEndTime ( ) ) ;
return list ;
}
/ * *
* 查询数据
* @param carId 平车ID
* @param startTime 开始时间
* @param endTime 结束时间
* @return 记录
* /
private List < RecordVo . WeightAndRfid > queryWeightAndRfid ( Long carId , Long startTime , Long endTime ) {
RecordVo . WeightAndRfid weight = null ;
RecordVo . WeightAndRfid rfid = null ;
List < RecordVo . WeightAndRfid > list = wisdomCarRecordDao . queryWeightAndRfid ( carId , startTime , endTime ) ;
Iterator < RecordVo . WeightAndRfid > iterator = list . iterator ( ) ;
while ( iterator . hasNext ( ) ) {
RecordVo . WeightAndRfid next = iterator . next ( ) ;
if ( next . getType ( ) = = Constant . CAR_RECORD_WEIGHT ) {
// 称重
if ( weight = = null ) {
weight = next ;
continue ;
}
// 体重变化和上一条体重相似且间隔时间较短,删除
if ( Integer . parseInt ( next . getValue ( ) ) - Integer . parseInt ( weight . getValue ( ) ) < = weightMinus & & next . getTime ( ) - weight . getTime ( ) < = timeMinus ) {
iterator . remove ( ) ;
} else {
weight = next ;
}
} else {
if ( weight ! = null ) {
next . setValue ( weight . getValue ( ) ) ;
}
// rfid
if ( rfid = = null ) {
rfid = next ;
continue ;
}
// rfid和上一个一样且间隔时间较短,删除
if ( next . getValue ( ) . equals ( rfid . getValue ( ) ) & & next . getTime ( ) - rfid . getTime ( ) < = timeMinus ) {
iterator . remove ( ) ;
} else {
rfid = next ;
}
}
}
return list ;
}
@Override
public Workbook exportWeightAndRfid ( RecordDto . WeightAndRfidExport param ) {
log . info ( "数据导出:{}" , param ) ;
List < RecordVo . WeightAndRfid > list = queryWeightAndRfid ( param . getCarId ( ) , param . getStartTime ( ) , param . getEndTime ( ) ) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
// 参数封装
List < List < PoiUtil . PoiUtilCell > > rows = new ArrayList < > ( ) ;
// 标题
List < PoiUtil . PoiUtilCell > headRow = new ArrayList < > ( ) ;
PoiUtil . PoiUtilCell headCell = new PoiUtil . PoiUtilCell ( ) ;
headCell . setColspan ( 4 ) ;
headCell . setValue ( "智慧平车记录" ) ;
headRow . add ( headCell ) ;
rows . add ( headRow ) ;
log . info ( "标题:{}" , headCell ) ;
// 表头
List < PoiUtil . PoiUtilCell > titleRow = new ArrayList < > ( ) ;
titleRow . add ( new PoiUtil . PoiUtilCell ( "序号" ) ) ;
titleRow . add ( new PoiUtil . PoiUtilCell ( "时间" ) ) ;
titleRow . add ( new PoiUtil . PoiUtilCell ( "类型" ) ) ;
titleRow . add ( new PoiUtil . PoiUtilCell ( "数据" ) ) ;
rows . add ( titleRow ) ;
log . info ( "表头:{}" , titleRow ) ;
for ( int i = 0 ; i < list . size ( ) ; i + + ) {
RecordVo . WeightAndRfid record = list . get ( i ) ;
List < PoiUtil . PoiUtilCell > row = new ArrayList < > ( ) ;
row . add ( new PoiUtil . PoiUtilCell ( "" + ( i + 1 ) ) ) ;
Date time = new Date ( ) ;
time . setTime ( record . getTime ( ) ) ;
row . add ( new PoiUtil . PoiUtilCell ( sdf . format ( time ) ) ) ;
row . add ( new PoiUtil . PoiUtilCell ( record . getType ( ) = = Constant . CAR_RECORD_WEIGHT ? "称重" : "rfid" ) ) ;
row . add ( new PoiUtil . PoiUtilCell ( record . getType ( ) = = Constant . CAR_RECORD_WEIGHT ? new BigDecimal ( record . getValue ( ) ) . divide ( new BigDecimal ( 1000 ) , 3 ) . toString ( ) + "Kg" : record . getName ( ) ) ) ;
rows . add ( row ) ;
}
log . info ( "数据:{}" , list . size ( ) ) ;
//生成一个excel文件
Workbook workbook = new XSSFWorkbook ( ) ;
PoiUtil . exportWB ( "智慧平车记录" , rows , workbook ) ;
return workbook ;
}
private void doseAndSensorMessage ( CarRecordMessageDto carRecordMessageDto , Set < String > userIdSet ) throws Exception {
//体重 单位g
BigDecimal weightInt = BigDecimal . valueOf ( Double . parseDouble ( carRecordMessageDto . getValue ( ) ) ) ;