rts-sim-testing-service/service/trainManage.go

142 lines
4.5 KiB
Go
Raw Normal View History

package service
import (
"fmt"
2024-01-17 17:02:17 +08:00
"joylink.club/bj-rtsts-server/dto/common_proto"
2023-11-22 17:05:38 +08:00
"time"
2023-11-22 17:05:38 +08:00
"google.golang.org/protobuf/proto"
"joylink.club/bj-rtsts-server/db/dbquery"
"joylink.club/bj-rtsts-server/db/model"
"joylink.club/bj-rtsts-server/dto"
"joylink.club/bj-rtsts-server/dto/data_proto"
2023-11-22 17:05:38 +08:00
"joylink.club/bj-rtsts-server/sys_error"
)
var trainDataType = int32(data_proto.PictureType_TrainData)
2023-11-22 17:05:38 +08:00
// 查询列车信息列表
2023-11-22 17:05:38 +08:00
func PageTrainInfoByPublished(query *dto.PageTrainInfoReqDto) *dto.PageDto {
d, dv := dbquery.Published, dbquery.PublishedVersion
dq := d.Select(d.ID, d.Code.As("name"), dv.Note, dv.Proto).
LeftJoin(dv, d.DataID.EqCol(dv.ID)).
Where(d.Type.Eq(trainDataType), d.Status.Eq(1)).
Order(dv.PublishAt)
if query.Name != "" {
2023-11-22 17:05:38 +08:00
dq = dq.Where(d.Code.Like(fmt.Sprintf("%%%s%%", query.Name)))
}
2023-11-22 17:05:38 +08:00
var records []*dto.PublishedDto
total, err := dq.Debug().ScanByPage(&records, query.Offset(), query.Size)
if err != nil {
2023-11-22 17:05:38 +08:00
panic(sys_error.New("列车信息查询错误", err))
}
2023-11-22 17:05:38 +08:00
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: dto.ConvertToTrainDto(records)}
}
2023-11-22 17:05:38 +08:00
// 根据项目Id查询列车信息
func ListTrainByProject(query *dto.TrainInfoReqDto) []*dto.TrainInfoDto {
ppl, p, pv := dbquery.ProjectPublishLink, dbquery.Published, dbquery.PublishedVersion
where := ppl.Select(p.ID, p.Code.As("name"), pv.Note, pv.Proto).
LeftJoin(p, ppl.Mid.EqCol(p.ID)).
LeftJoin(pv, p.DataID.EqCol(pv.ID))
if query.Name != "" {
2023-11-22 17:05:38 +08:00
where = where.Where(p.Code.Like(fmt.Sprintf("%%%s%%", query.Name)))
}
if query.Pid != 0 {
where = where.Where(ppl.Pid.Eq(query.Pid))
}
2023-11-22 17:05:38 +08:00
var records []*dto.PublishedDto
err := where.Where(p.Type.Eq(trainDataType), p.Status.Eq(1)).Order(pv.PublishAt).Scan(&records)
if err != nil {
2023-11-22 17:05:38 +08:00
panic(sys_error.New("列车信息查询错误", err))
}
2023-11-22 17:05:38 +08:00
return dto.ConvertToTrainDto(records)
}
2023-11-22 17:05:38 +08:00
// 发布列车信息
func CreateTrain(td *dto.TrainInfoDto, user *model.User) bool {
publishData(&dto.PublishedDto{
Name: td.Name,
2024-01-18 10:48:04 +08:00
Proto: convertTrainDtoToProto(td),
2023-11-22 17:05:38 +08:00
Type: trainDataType,
UserID: user.ID,
Note: td.Description,
}, true)
return true
}
2024-01-18 10:48:04 +08:00
func TrainConfigToProtoConvert(t *dto.ConfigTrainData) *common_proto.TrainDynamicConfig {
if t == nil {
return nil
}
2024-01-17 17:02:17 +08:00
return &common_proto.TrainDynamicConfig{
2024-01-18 13:57:57 +08:00
BaseResistanceParamA: t.BaseResistanceParamA, BaseResistanceParamB: t.BaseResistanceParamB, BaseResistanceParamC: t.BaseResistanceParamC,
CurveResistanceParamR1: t.CurveResistanceParamR1, CurveResistanceParamR2: t.CurveResistanceParamR2, CurveResistanceParamR3: t.CurveResistanceParamR3, CurveResistanceParamR4: t.CurveResistanceParamR4,
Jump: t.Jump, Slip: t.Slip,
Slide: t.Slide, StopSign: t.StopSign,
2024-01-17 17:02:17 +08:00
}
}
2023-11-22 17:05:38 +08:00
// 转成列车proto
2024-01-18 10:48:04 +08:00
func convertTrainDtoToProto(t *dto.TrainInfoDto) []byte {
message := &data_proto.Train{
TrainModel: data_proto.Train_TrainModel(t.TrainModel),
2023-11-22 17:05:38 +08:00
CarriageLength: t.CarriageLength,
TotalLength: t.TotalLength,
2024-01-18 16:33:27 +08:00
//MinDiameter: t.MinDiameter,
//MaxDiameter: t.MaxDiameter,
TrainSets: t.TrainSets,
DynamicConfig: TrainConfigToProtoConvert(t.TrainConfigData),
2023-11-22 17:05:38 +08:00
}
b, _ := proto.Marshal(message)
return b
}
// 查询列车信息
2023-11-22 17:05:38 +08:00
func QueryTrain(id int32) *dto.TrainInfoDto {
d, dv := dbquery.Published, dbquery.PublishedVersion
var record dto.PublishedDto
err := d.Select(d.ID, d.Code.As("name"), dv.Note, dv.Proto).
LeftJoin(dv, d.DataID.EqCol(dv.ID)).
Where(d.Type.Eq(trainDataType), d.ID.Eq(id)).
Scan(&record)
if err != nil {
2023-11-22 17:05:38 +08:00
panic(sys_error.New("列车信息查询错误", err))
}
2023-11-22 17:05:38 +08:00
return dto.ConvertDtoFromTrain(&record)
}
// 更新列车信息
2023-11-22 17:05:38 +08:00
func UpdateTrain(id int32, td *dto.TrainInfoDto, user *model.User) bool {
p, pv := dbquery.Published, dbquery.PublishedVersion
pd, err := p.Select(p.ID, p.Code, p.DataID).Where(p.ID.Eq(id)).First()
if err != nil {
panic(sys_error.New("更新列车信息错误", err))
}
if td.Name != pd.Code {
ChangePublishCode(id, td.Name)
}
pvd, err := pv.Select(pv.Version).Where(pv.ID.Eq(pd.DataID)).First()
if err != nil {
panic(sys_error.New("更新列车信息错误", err))
}
pvdn := &model.PublishedVersion{
2024-01-18 10:48:04 +08:00
Proto: convertTrainDtoToProto(td),
2023-11-22 17:05:38 +08:00
UserID: user.ID,
PublishAt: time.Now(),
Note: td.Description,
Version: pvd.Version + 1,
Code: td.Name,
PublishID: id,
Type: trainDataType,
}
err2 := pv.Create(pvdn)
if err2 != nil {
2023-11-22 17:05:38 +08:00
panic(sys_error.New("更新列车信息错误", err2))
}
_, err3 := p.Where(p.ID.Eq(id)).UpdateColumn(p.DataID, pvdn.ID)
if err3 != nil {
panic(sys_error.New("更新列车信息错误", err3))
}
return true
}