142 lines
4.6 KiB
Go
142 lines
4.6 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"joylink.club/bj-rtsts-server/dto/common_proto"
|
|
"time"
|
|
|
|
"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"
|
|
"joylink.club/bj-rtsts-server/sys_error"
|
|
)
|
|
|
|
var trainDataType = int32(data_proto.PictureType_TrainData)
|
|
|
|
// 查询列车信息列表
|
|
func PageTrainInfoByPublished(query *dto.PageTrainInfoReqDto) *dto.PageDto[*dto.TrainInfoDto] {
|
|
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 != "" {
|
|
dq = dq.Where(d.Code.Like(fmt.Sprintf("%%%s%%", query.Name)))
|
|
}
|
|
var records []*dto.PublishedDto
|
|
total, err := dq.Debug().ScanByPage(&records, query.Offset(), query.Size)
|
|
if err != nil {
|
|
panic(sys_error.New("列车信息查询错误", err))
|
|
}
|
|
return &dto.PageDto[*dto.TrainInfoDto]{Total: int(total), PageQueryDto: query.PageQueryDto, Records: dto.ConvertToTrainDto(records)}
|
|
}
|
|
|
|
// 根据项目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 != "" {
|
|
where = where.Where(p.Code.Like(fmt.Sprintf("%%%s%%", query.Name)))
|
|
}
|
|
if query.Pid != 0 {
|
|
where = where.Where(ppl.Pid.Eq(query.Pid))
|
|
}
|
|
var records []*dto.PublishedDto
|
|
err := where.Where(p.Type.Eq(trainDataType), p.Status.Eq(1)).Order(pv.PublishAt).Scan(&records)
|
|
if err != nil {
|
|
panic(sys_error.New("列车信息查询错误", err))
|
|
}
|
|
return dto.ConvertToTrainDto(records)
|
|
}
|
|
|
|
// 发布列车信息
|
|
func CreateTrain(td *dto.TrainInfoDto, user *model.User) bool {
|
|
publishData(&dto.PublishedDto{
|
|
Name: td.Name,
|
|
Proto: convertTrainDtoToProto(td),
|
|
Type: trainDataType,
|
|
UserID: user.ID,
|
|
Note: td.Description,
|
|
}, true)
|
|
return true
|
|
}
|
|
func TrainConfigToProtoConvert(t *dto.ConfigTrainData) *common_proto.TrainDynamicConfig {
|
|
if t == nil {
|
|
return nil
|
|
}
|
|
return &common_proto.TrainDynamicConfig{
|
|
DavisParamA: t.DavisParamA, DavisParamB: t.DavisParamB, DavisParamC: t.DavisParamC,
|
|
CurveResistanceParamR1: t.CurveResistanceParamR1, CurveResistanceParamR2: t.CurveResistanceParamR2, CurveResistanceParamR3: t.CurveResistanceParamR3, CurveResistanceParamR4: t.CurveResistanceParamR4,
|
|
RevolvingMassParam: t.RevolvingMassParam, Jump: t.Jump, SlipA: t.SlipA, SlipR: t.SlipR, SlipD: t.SlipD, IdlingA: t.IdlingA, IdlingR: t.IdlingR, IdlingD: t.IdlingD,
|
|
Slide: t.Slide, StopSign: t.StopSign,
|
|
}
|
|
}
|
|
|
|
// 转成列车proto
|
|
func convertTrainDtoToProto(t *dto.TrainInfoDto) []byte {
|
|
message := &data_proto.Train{
|
|
TrainModel: data_proto.Train_TrainModel(t.TrainModel),
|
|
CarriageLength: t.CarriageLength,
|
|
TotalLength: t.TotalLength,
|
|
//MinDiameter: t.MinDiameter,
|
|
//MaxDiameter: t.MaxDiameter,
|
|
TrainSets: t.TrainSets,
|
|
DynamicConfig: TrainConfigToProtoConvert(t.TrainConfigData),
|
|
}
|
|
b, _ := proto.Marshal(message)
|
|
return b
|
|
}
|
|
|
|
// 查询列车信息
|
|
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 {
|
|
panic(sys_error.New("列车信息查询错误", err))
|
|
}
|
|
return dto.ConvertDtoFromTrain(&record)
|
|
}
|
|
|
|
// 更新列车信息
|
|
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{
|
|
Proto: convertTrainDtoToProto(td),
|
|
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 {
|
|
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
|
|
}
|