6b5e4c7cc3
【半实物信息解析BUG】
286 lines
7.8 KiB
Go
286 lines
7.8 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"joylink.club/bj-rtsts-server/db/dbquery"
|
|
"joylink.club/bj-rtsts-server/db/model"
|
|
"joylink.club/bj-rtsts-server/dto"
|
|
)
|
|
|
|
// 查询列车型号信息列表
|
|
func PageTrainModelQuery(query *dto.PageTrainManageReqDto) (*dto.PageDto, error) {
|
|
d := dbquery.TrainModel
|
|
dq := d.Where()
|
|
if query.Name != "" {
|
|
dq = dq.Where(d.Name.Like(fmt.Sprintf("%%%s%%", query.Name)))
|
|
}
|
|
records, total, err := dq.Debug().Select(d.ID, d.Name, d.UpdateAt, d.CreatedAt).FindByPage(query.Offset(), query.Size)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}, nil
|
|
}
|
|
|
|
// 查询列车型号信息列表
|
|
func ListTrainModelQuery(query *dto.TrainManageReqDto) ([]*model.TrainModel, error) {
|
|
d := dbquery.TrainModel
|
|
dq := d.Where()
|
|
if query.Name != "" {
|
|
dq = dq.Where(d.Name.Like(fmt.Sprintf("%%%s%%", query.Name)))
|
|
}
|
|
records, err := dq.Debug().Select(d.ID, d.Name, d.UpdateAt, d.CreatedAt).Find()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return records, nil
|
|
}
|
|
|
|
// 创建列车型号信息
|
|
func CreateTrainModel(dto *dto.TrainModelDto) (*model.TrainModel, error) {
|
|
if err := checkTrainModel(dto.Name, 0); err != nil {
|
|
panic(err)
|
|
}
|
|
d := model.TrainModel{
|
|
Name: dto.Name,
|
|
CreatedAt: time.Now(),
|
|
UpdateAt: time.Now(),
|
|
}
|
|
dt := dbquery.TrainModel
|
|
err := dt.Save(&d)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return dt.Where(dt.Name.Eq(dto.Name)).Order(dt.CreatedAt).Debug().First()
|
|
}
|
|
|
|
// 查询列车型号信息
|
|
func QueryTrainModel(id int32) *model.TrainModel {
|
|
dt := dbquery.TrainModel
|
|
data, err := dt.Where(dt.ID.Eq(id)).Debug().First()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return data
|
|
}
|
|
|
|
// 更新列车型号信息
|
|
func UpdateTrainModel(id int32, dto *dto.TrainModelDto) bool {
|
|
if err := checkTrainModel(dto.Name, id); err != nil {
|
|
panic(err)
|
|
}
|
|
findOldQuery := dbquery.TrainModel
|
|
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
|
|
if oldD == nil || err != nil {
|
|
panic(err)
|
|
}
|
|
if len(dto.Name) > 0 {
|
|
oldD.Name = dto.Name
|
|
}
|
|
oldD.UpdateAt = time.Now()
|
|
_, error := dbquery.TrainModel.Updates(oldD)
|
|
if error != nil {
|
|
panic(error)
|
|
}
|
|
return true
|
|
}
|
|
|
|
// 删除列车型号
|
|
func DeleteTrainModelById(id int) {
|
|
_, _ = dbquery.TrainModel.Debug().Where(dbquery.TrainModel.ID.Eq(int32(id))).Delete()
|
|
}
|
|
|
|
// 查询列车尺寸信息列表
|
|
func PageTrainSizeQuery(query *dto.PageTrainManageReqDto) (*dto.PageDto, error) {
|
|
d := dbquery.TrainSize
|
|
dq := d.Where()
|
|
if query.Name != "" {
|
|
dq = dq.Where(d.Name.Like(fmt.Sprintf("%%%s%%", query.Name)))
|
|
}
|
|
records, total, err := dq.Debug().Select(d.ID, d.Name, d.CarriageLength, d.TotalLength, d.Description).FindByPage(query.Offset(), query.Size)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}, nil
|
|
}
|
|
|
|
// 查询列车尺寸信息列表
|
|
func ListTrainSizeQuery(query *dto.TrainManageReqDto) ([]*model.TrainSize, error) {
|
|
d := dbquery.TrainSize
|
|
dq := d.Where()
|
|
if query.Name != "" {
|
|
dq = dq.Where(d.Name.Like(fmt.Sprintf("%%%s%%", query.Name)))
|
|
}
|
|
records, err := dq.Debug().Select(d.ID, d.Name, d.CarriageLength, d.TotalLength, d.Description).Find()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return records, nil
|
|
}
|
|
|
|
// 创建列车尺寸信息
|
|
func CreateTrainSize(dto *dto.TrainSizeDto) (*model.TrainSize, error) {
|
|
d := model.TrainSize{
|
|
Name: dto.Name,
|
|
CarriageLength: dto.CarriageLength,
|
|
TotalLength: dto.TotalLength,
|
|
Description: dto.Description,
|
|
}
|
|
dt := dbquery.TrainSize
|
|
err := dt.Save(&d)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return dt.Where(dt.Name.Eq(dto.Name)).Order(dt.Name).Debug().First()
|
|
}
|
|
|
|
// 查询列车尺寸信息
|
|
func QueryTrainSize(id int32) *model.TrainSize {
|
|
dt := dbquery.TrainSize
|
|
data, err := dt.Where(dt.ID.Eq(id)).Debug().First()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return data
|
|
}
|
|
|
|
// 更新列车尺寸信息
|
|
func UpdateTrainSize(id int32, dto *dto.TrainSizeDto) bool {
|
|
findOldQuery := dbquery.TrainSize
|
|
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
|
|
if oldD == nil || err != nil {
|
|
panic(err)
|
|
}
|
|
oldD.Name = dto.Name
|
|
oldD.CarriageLength = dto.CarriageLength
|
|
oldD.TotalLength = dto.TotalLength
|
|
oldD.Description = dto.Description
|
|
_, error := dbquery.TrainSize.Updates(oldD)
|
|
if error != nil {
|
|
panic(error)
|
|
}
|
|
return true
|
|
}
|
|
|
|
// 删除列车尺寸
|
|
func DeleteTrainSizeById(id int) {
|
|
_, _ = dbquery.TrainSize.Debug().Where(dbquery.TrainSize.ID.Eq(int32(id))).Delete()
|
|
}
|
|
|
|
func QueryProjectTrainSize(id int32) []*model.TrainSize {
|
|
// 获取项目关联的列车尺寸
|
|
ptsl := dbquery.ProjectTrainSizeLink
|
|
slinks, _ := ptsl.Select(ptsl.Sid).Where(ptsl.Pid.Eq(id)).Find()
|
|
if len(slinks) > 0 {
|
|
sids := []int32{}
|
|
for _, sid := range slinks {
|
|
sids = append(sids, sid.Sid)
|
|
}
|
|
ts := dbquery.TrainSize
|
|
trainSizes, _ := ts.Select(ts.ID, ts.Name).Where(ts.ID.In(sids...)).Find()
|
|
return trainSizes
|
|
}
|
|
return []*model.TrainSize{}
|
|
}
|
|
|
|
// 查询列车轮径信息列表
|
|
func PageTrainWheelDiameterQuery(query *dto.PageTrainManageReqDto) (*dto.PageDto, error) {
|
|
d := dbquery.TrainWheelDiameter
|
|
dq := d.Where()
|
|
if query.Name != "" {
|
|
dq = dq.Where(d.Name.Like(fmt.Sprintf("%%%s%%", query.Name)))
|
|
}
|
|
records, total, err := dq.Debug().Select(d.ID, d.Name, d.Diameter, d.MinDiameter, d.MaxDiameter, d.AxialPosition, d.InstallDirection).FindByPage(query.Offset(), query.Size)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}, nil
|
|
}
|
|
|
|
// 查询列车轮径信息列表
|
|
func ListTrainWheelDiameterQuery(query *dto.TrainManageReqDto) ([]*model.TrainWheelDiameter, error) {
|
|
d := dbquery.TrainWheelDiameter
|
|
dq := d.Where()
|
|
if query.Name != "" {
|
|
dq = dq.Where(d.Name.Like(fmt.Sprintf("%%%s%%", query.Name)))
|
|
}
|
|
records, err := dq.Debug().Select(d.ID, d.Name, d.Diameter, d.MinDiameter, d.MaxDiameter, d.AxialPosition, d.InstallDirection).Find()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return records, nil
|
|
}
|
|
|
|
// 创建列车轮径信息
|
|
func CreateTrainWheelDiameter(dto *dto.TrainWheelDiameterDto) (*model.TrainWheelDiameter, error) {
|
|
d := model.TrainWheelDiameter{
|
|
Name: dto.Name,
|
|
Diameter: dto.Diameter,
|
|
MinDiameter: dto.MinDiameter,
|
|
MaxDiameter: dto.MaxDiameter,
|
|
AxialPosition: dto.AxialPosition,
|
|
InstallDirection: dto.InstallDirection,
|
|
}
|
|
dt := dbquery.TrainWheelDiameter
|
|
err := dt.Save(&d)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return dt.Where(dt.Name.Eq(dto.Name)).Order(dt.Name).Debug().First()
|
|
}
|
|
|
|
// 查询列车轮径信息
|
|
func QueryTrainWheelDiameter(id int32) *model.TrainWheelDiameter {
|
|
dt := dbquery.TrainWheelDiameter
|
|
data, err := dt.Where(dt.ID.Eq(id)).Debug().First()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return data
|
|
}
|
|
|
|
// 更新列车轮径信息
|
|
func UpdateTrainWheelDiameter(id int32, dto *dto.TrainWheelDiameterDto) bool {
|
|
findOldQuery := dbquery.TrainWheelDiameter
|
|
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
|
|
if oldD == nil || err != nil {
|
|
panic(err)
|
|
}
|
|
oldD.Name = dto.Name
|
|
oldD.Diameter = dto.Diameter
|
|
oldD.MinDiameter = dto.MinDiameter
|
|
oldD.MaxDiameter = dto.MaxDiameter
|
|
oldD.AxialPosition = dto.AxialPosition
|
|
oldD.InstallDirection = dto.InstallDirection
|
|
_, error := dbquery.TrainWheelDiameter.Updates(oldD)
|
|
if error != nil {
|
|
panic(error)
|
|
}
|
|
return true
|
|
}
|
|
|
|
// 删除列车轮径
|
|
func DeleteTrainWheelDiameterById(id int) {
|
|
_, _ = dbquery.TrainWheelDiameter.Debug().Where(dbquery.TrainWheelDiameter.ID.Eq(int32(id))).Delete()
|
|
}
|
|
|
|
// 检查列车型号名称
|
|
func checkTrainModel(name string, id int32) error {
|
|
findNameQuery := dbquery.TrainModel
|
|
w := findNameQuery.Where()
|
|
if id != 0 {
|
|
w = w.Where(findNameQuery.ID.Eq(id))
|
|
}
|
|
if name != "" {
|
|
count, err := w.Where(findNameQuery.Name.Eq(name)).Debug().Count()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
if count > 0 {
|
|
panic("名称已存在")
|
|
}
|
|
}
|
|
return nil
|
|
}
|