299 lines
9.2 KiB
Go
299 lines
9.2 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 {
|
|
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(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}
|
|
}
|
|
|
|
// 查询列车型号信息列表
|
|
func ListTrainModelQuery(query *dto.TrainManageReqDto) []*model.TrainModel {
|
|
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(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
return records
|
|
}
|
|
|
|
// 创建列车型号信息
|
|
func CreateTrainModel(td *dto.TrainModelDto) *model.TrainModel {
|
|
if err := checkTrainModel(td.Name, 0); err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
d := model.TrainModel{
|
|
Name: td.Name,
|
|
CreatedAt: time.Now(),
|
|
UpdateAt: time.Now(),
|
|
}
|
|
dt := dbquery.TrainModel
|
|
err := dt.Save(&d)
|
|
if err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
data, err2 := dt.Where(dt.Name.Eq(td.Name)).Order(dt.CreatedAt).Debug().First()
|
|
if err2 != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err2.Error()})
|
|
}
|
|
return data
|
|
}
|
|
|
|
// 查询列车型号信息
|
|
func QueryTrainModel(id int32) *model.TrainModel {
|
|
dt := dbquery.TrainModel
|
|
data, err := dt.Where(dt.ID.Eq(id)).Debug().First()
|
|
if err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
return data
|
|
}
|
|
|
|
// 更新列车型号信息
|
|
func UpdateTrainModel(id int32, td *dto.TrainModelDto) bool {
|
|
if err := checkTrainModel(td.Name, id); err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
findOldQuery := dbquery.TrainModel
|
|
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
|
|
if oldD == nil || err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
oldD.Name = td.Name
|
|
oldD.UpdateAt = time.Now()
|
|
_, err2 := dbquery.TrainModel.Updates(oldD)
|
|
if err2 != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err2.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 {
|
|
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(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}
|
|
}
|
|
|
|
// 查询列车尺寸信息列表
|
|
func ListTrainSizeQuery(query *dto.TrainManageReqDto) []*model.TrainSize {
|
|
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(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
return records
|
|
}
|
|
|
|
// 创建列车尺寸信息
|
|
func CreateTrainSize(td *dto.TrainSizeDto) *model.TrainSize {
|
|
d := model.TrainSize{
|
|
Name: td.Name,
|
|
CarriageLength: td.CarriageLength,
|
|
TotalLength: td.TotalLength,
|
|
Description: td.Description,
|
|
}
|
|
dt := dbquery.TrainSize
|
|
err := dt.Save(&d)
|
|
if err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
data, err := dt.Where(dt.Name.Eq(td.Name)).Order(dt.Name).Debug().First()
|
|
if err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
return data
|
|
}
|
|
|
|
// 查询列车尺寸信息
|
|
func QueryTrainSize(id int32) *model.TrainSize {
|
|
dt := dbquery.TrainSize
|
|
data, err := dt.Where(dt.ID.Eq(id)).Debug().First()
|
|
if err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
return data
|
|
}
|
|
|
|
// 更新列车尺寸信息
|
|
func UpdateTrainSize(id int32, td *dto.TrainSizeDto) bool {
|
|
findOldQuery := dbquery.TrainSize
|
|
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
|
|
if oldD == nil || err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
oldD.Name = td.Name
|
|
oldD.CarriageLength = td.CarriageLength
|
|
oldD.TotalLength = td.TotalLength
|
|
oldD.Description = td.Description
|
|
_, err2 := dbquery.TrainSize.Updates(oldD)
|
|
if err2 != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err2.Error()})
|
|
}
|
|
return true
|
|
}
|
|
|
|
// 删除列车尺寸
|
|
func DeleteTrainSizeById(id int) {
|
|
sid := int32(id)
|
|
_, _ = dbquery.TrainSize.Debug().Where(dbquery.TrainSize.ID.Eq(sid)).Delete()
|
|
dbquery.ProjectTrainSizeLink.Where(dbquery.ProjectTrainSizeLink.Sid.Eq(sid)).Delete()
|
|
}
|
|
|
|
func QueryProjectTrainSize(id int32) []*model.TrainSize {
|
|
// 获取项目关联的列车尺寸
|
|
ptsl := dbquery.ProjectTrainSizeLink
|
|
slinks, _ := ptsl.Select(ptsl.Sid).Distinct(ptsl.Sid).Where(ptsl.Pid.Eq(id)).Find()
|
|
if len(slinks) == 0 {
|
|
return nil
|
|
}
|
|
sids := make([]int32, len(slinks))
|
|
for i, sid := range slinks {
|
|
sids[i] = sid.Sid
|
|
}
|
|
ts := dbquery.TrainSize
|
|
trainSizes, _ := ts.Select(ts.ID, ts.Name, ts.TotalLength, ts.CarriageLength, ts.Description).Where(ts.ID.In(sids...)).Find()
|
|
return trainSizes
|
|
}
|
|
|
|
// 查询列车轮径信息列表
|
|
func PageTrainWheelDiameterQuery(query *dto.PageTrainManageReqDto) *dto.PageDto {
|
|
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(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}
|
|
}
|
|
|
|
// 查询列车轮径信息列表
|
|
func ListTrainWheelDiameterQuery(query *dto.TrainManageReqDto) []*model.TrainWheelDiameter {
|
|
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(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
return records
|
|
}
|
|
|
|
// 创建列车轮径信息
|
|
func CreateTrainWheelDiameter(twd *dto.TrainWheelDiameterDto) *model.TrainWheelDiameter {
|
|
d := model.TrainWheelDiameter{
|
|
Name: twd.Name,
|
|
Diameter: twd.Diameter,
|
|
MinDiameter: twd.MinDiameter,
|
|
MaxDiameter: twd.MaxDiameter,
|
|
AxialPosition: twd.AxialPosition,
|
|
InstallDirection: twd.InstallDirection,
|
|
}
|
|
dt := dbquery.TrainWheelDiameter
|
|
err := dt.Save(&d)
|
|
if err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
data, err := dt.Where(dt.Name.Eq(twd.Name)).Order(dt.Name).Debug().First()
|
|
if err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
return data
|
|
}
|
|
|
|
// 查询列车轮径信息
|
|
func QueryTrainWheelDiameter(id int32) *model.TrainWheelDiameter {
|
|
dt := dbquery.TrainWheelDiameter
|
|
data, err := dt.Where(dt.ID.Eq(id)).Debug().First()
|
|
if err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
return data
|
|
}
|
|
|
|
// 更新列车轮径信息
|
|
func UpdateTrainWheelDiameter(id int32, twd *dto.TrainWheelDiameterDto) bool {
|
|
findOldQuery := dbquery.TrainWheelDiameter
|
|
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
|
|
if oldD == nil || err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
oldD.Name = twd.Name
|
|
oldD.Diameter = twd.Diameter
|
|
oldD.MinDiameter = twd.MinDiameter
|
|
oldD.MaxDiameter = twd.MaxDiameter
|
|
oldD.AxialPosition = twd.AxialPosition
|
|
oldD.InstallDirection = twd.InstallDirection
|
|
_, err2 := dbquery.TrainWheelDiameter.Updates(oldD)
|
|
if err2 != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err2.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 {
|
|
if name == "" {
|
|
return nil
|
|
}
|
|
findNameQuery := dbquery.TrainModel
|
|
w := findNameQuery.Where()
|
|
if id != 0 {
|
|
w = w.Where(findNameQuery.ID.NotIn(id))
|
|
}
|
|
count, err := w.Where(findNameQuery.Name.Eq(name)).Debug().Count()
|
|
if err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
if count > 0 {
|
|
panic(dto.ErrorDto{Code: dto.DataAlreadyExist, Message: "名称已存在"})
|
|
}
|
|
return nil
|
|
}
|