69 lines
2.8 KiB
Java
69 lines
2.8 KiB
Java
|
|
package com.quantai.trader.persistence;
|
||
|
|
|
||
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||
|
|
import com.quantai.trader.domain.TraderTrainingSample;
|
||
|
|
import com.quantai.trader.infrastructure.entity.TraderTrainingSampleEntity;
|
||
|
|
import com.quantai.trader.infrastructure.mapper.TraderTrainingSampleMapper;
|
||
|
|
import org.springframework.stereotype.Repository;
|
||
|
|
|
||
|
|
import java.time.Instant;
|
||
|
|
import java.util.List;
|
||
|
|
|
||
|
|
@Repository
|
||
|
|
public class MybatisTraderSampleRepository implements TraderSampleRepository {
|
||
|
|
|
||
|
|
private final TraderTrainingSampleMapper mapper;
|
||
|
|
|
||
|
|
public MybatisTraderSampleRepository(TraderTrainingSampleMapper mapper) {
|
||
|
|
this.mapper = mapper;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void insert(TraderTrainingSample sample) {
|
||
|
|
TraderTrainingSampleEntity entity = new TraderTrainingSampleEntity();
|
||
|
|
entity.setRunId(sample.runId());
|
||
|
|
entity.setCycleId(sample.cycleId());
|
||
|
|
entity.setSampleId(sample.sampleId());
|
||
|
|
entity.setActionId(sample.actionId());
|
||
|
|
entity.setPositionId(sample.positionId());
|
||
|
|
entity.setFeatureVersion(sample.featureVersion());
|
||
|
|
entity.setLabelVersion(sample.labelVersion());
|
||
|
|
entity.setSampleTime(TraderPersistenceCodec.local(sample.sampleTime()));
|
||
|
|
entity.setFeaturesJson(TraderPersistenceCodec.json(sample.features()));
|
||
|
|
entity.setLabelsJson(TraderPersistenceCodec.json(sample.labels()));
|
||
|
|
entity.setNetReturnBps1x(sample.netReturnBps1x());
|
||
|
|
entity.setNetReturnBps10x(sample.netReturnBps10x());
|
||
|
|
entity.setProxyOnly(sample.proxyOnly());
|
||
|
|
entity.setCreatedAt(TraderPersistenceCodec.local(Instant.now()));
|
||
|
|
mapper.insert(entity);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public List<TraderTrainingSample> findByRunId(String runId) {
|
||
|
|
return mapper.selectList(
|
||
|
|
Wrappers.<TraderTrainingSampleEntity>lambdaQuery()
|
||
|
|
.eq(TraderTrainingSampleEntity::getRunId, runId)
|
||
|
|
.orderByAsc(TraderTrainingSampleEntity::getSampleTime)
|
||
|
|
.orderByAsc(TraderTrainingSampleEntity::getId)
|
||
|
|
).stream().map(this::toDomain).toList();
|
||
|
|
}
|
||
|
|
|
||
|
|
private TraderTrainingSample toDomain(TraderTrainingSampleEntity entity) {
|
||
|
|
return new TraderTrainingSample(
|
||
|
|
entity.getRunId(),
|
||
|
|
entity.getCycleId(),
|
||
|
|
entity.getSampleId(),
|
||
|
|
entity.getActionId(),
|
||
|
|
entity.getPositionId(),
|
||
|
|
entity.getFeatureVersion(),
|
||
|
|
entity.getLabelVersion(),
|
||
|
|
TraderPersistenceCodec.instant(entity.getSampleTime()),
|
||
|
|
TraderPersistenceCodec.map(entity.getFeaturesJson()),
|
||
|
|
TraderPersistenceCodec.map(entity.getLabelsJson()),
|
||
|
|
entity.getNetReturnBps1x(),
|
||
|
|
entity.getNetReturnBps10x(),
|
||
|
|
Boolean.TRUE.equals(entity.getProxyOnly())
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|