-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...3-center-data/src/main/java/io/github/pnoker/center/data/mapper/TaosPointValueMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.github.pnoker.center.data.mapper; | ||
|
||
import com.baomidou.dynamic.datasource.annotation.DS; | ||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; | ||
import io.github.pnoker.common.entity.point.TaosPointValue; | ||
import org.apache.ibatis.annotations.Insert; | ||
import org.apache.ibatis.annotations.Mapper; | ||
import org.apache.ibatis.annotations.Param; | ||
import org.apache.ibatis.annotations.Update; | ||
|
||
import java.util.List; | ||
|
||
@Mapper | ||
@DS("taos") | ||
public interface TaosPointValueMapper extends BaseMapper<TaosPointValue> { | ||
|
||
@Update("CREATE STABLE IF NOT EXISTS point_value (create_time TIMESTAMP, point_value NCHAR(32), raw_value NCHAR(32), origin_time TIMESTAMP) TAGS (device_id NCHAR(32), point_id NCHAR(32))") | ||
int createSuperTable(); | ||
|
||
@Update("CREATE TABLE IF NOT EXISTS point_value_${deviceId} using point_value TAGS (#{deviceId},#{pointId})") | ||
int createDeviceTable(@Param("deviceId") String deviceId, @Param("pointId") String pointId); | ||
|
||
@Insert("INSERT INTO point_value_${deviceId} (create_time,point_value,raw_value,origin_time) VALUES (#{createTime},#{pointValue},#{rawValue},#{originTime})") | ||
int insertOne(TaosPointValue taosPointValue); | ||
|
||
@Insert("<script>INSERT INTO point_value_${deviceId} (create_time,point_value,raw_value,origin_time) VALUES <foreach collection='list' item='item' index='index' separator=','>(#{item.createTime},#{item.pointValue},#{item.rawValue},#{item.originTime})</foreach></script>") | ||
int batchInsert(List<TaosPointValue> collect); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...c/main/java/io/github/pnoker/center/data/service/impl/repository/TDengineServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.github.pnoker.center.data.service.impl.repository; | ||
|
||
import io.github.pnoker.center.data.mapper.TaosPointValueMapper; | ||
import io.github.pnoker.center.data.service.RepositoryService; | ||
import io.github.pnoker.center.data.strategy.RepositoryStrategyFactory; | ||
import io.github.pnoker.common.constant.driver.StrategyConstant; | ||
import io.github.pnoker.common.entity.point.PointValue; | ||
import io.github.pnoker.common.entity.point.TaosPointValue; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.event.ContextRefreshedEvent; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.annotation.Resource; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Slf4j | ||
@Service | ||
@ConditionalOnProperty(name = "data.point.sava.tdengine.enable", havingValue = "true") | ||
public class TDengineServiceImpl implements RepositoryService, InitializingBean { | ||
|
||
@Resource | ||
TaosPointValueMapper taosPointValueMapper; | ||
|
||
@EventListener | ||
public void initDatabase(ContextRefreshedEvent event) { | ||
taosPointValueMapper.createSuperTable(); | ||
} | ||
|
||
@Override | ||
public String getRepositoryName() { | ||
return StrategyConstant.Storage.TDENGINE; | ||
} | ||
|
||
@Override | ||
public void savePointValue(PointValue pointValue) throws IOException { | ||
taosPointValueMapper.createDeviceTable(pointValue.getDeviceId(), pointValue.getPointId()); | ||
taosPointValueMapper.insertOne(new TaosPointValue(pointValue)); | ||
} | ||
|
||
@Override | ||
public void savePointValues(String deviceId, List<PointValue> pointValues) throws IOException { | ||
taosPointValueMapper.createDeviceTable(deviceId, pointValues.get(0).getPointId()); | ||
taosPointValueMapper.batchInsert(pointValues.stream().map(TaosPointValue::new).collect(Collectors.toList())); | ||
|
||
} | ||
|
||
@Override | ||
public void afterPropertiesSet() throws Exception { | ||
RepositoryStrategyFactory.put(StrategyConstant.Storage.TDENGINE, this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters