2023-08-25 10:41:13 +08:00
|
|
|
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 {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
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 {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建列车型号信息
|
2023-08-30 18:05:11 +08:00
|
|
|
func CreateTrainModel(td *dto.TrainModelDto) (*model.TrainModel, error) {
|
|
|
|
if err := checkTrainModel(td.Name, 0); err != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
d := model.TrainModel{
|
2023-08-30 18:05:11 +08:00
|
|
|
Name: td.Name,
|
2023-08-25 10:41:13 +08:00
|
|
|
CreatedAt: time.Now(),
|
|
|
|
UpdateAt: time.Now(),
|
|
|
|
}
|
|
|
|
dt := dbquery.TrainModel
|
|
|
|
err := dt.Save(&d)
|
|
|
|
if err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
2023-08-30 18:05:11 +08:00
|
|
|
return dt.Where(dt.Name.Eq(td.Name)).Order(dt.CreatedAt).Debug().First()
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 查询列车型号信息
|
|
|
|
func QueryTrainModel(id int32) *model.TrainModel {
|
|
|
|
dt := dbquery.TrainModel
|
|
|
|
data, err := dt.Where(dt.ID.Eq(id)).Debug().First()
|
|
|
|
if err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
// 更新列车型号信息
|
2023-08-30 18:05:11 +08:00
|
|
|
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()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
findOldQuery := dbquery.TrainModel
|
|
|
|
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
|
|
|
|
if oldD == nil || err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
2023-08-31 16:16:18 +08:00
|
|
|
oldD.Name = td.Name
|
2023-08-25 10:41:13 +08:00
|
|
|
oldD.UpdateAt = time.Now()
|
2023-08-30 18:05:11 +08:00
|
|
|
_, err2 := dbquery.TrainModel.Updates(oldD)
|
|
|
|
if err2 != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err2.Error()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
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 {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
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 {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建列车尺寸信息
|
2023-08-30 18:05:11 +08:00
|
|
|
func CreateTrainSize(td *dto.TrainSizeDto) (*model.TrainSize, error) {
|
2023-08-25 10:41:13 +08:00
|
|
|
d := model.TrainSize{
|
2023-08-30 18:05:11 +08:00
|
|
|
Name: td.Name,
|
|
|
|
CarriageLength: td.CarriageLength,
|
|
|
|
TotalLength: td.TotalLength,
|
|
|
|
Description: td.Description,
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
dt := dbquery.TrainSize
|
|
|
|
err := dt.Save(&d)
|
|
|
|
if err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
2023-08-30 18:05:11 +08:00
|
|
|
return dt.Where(dt.Name.Eq(td.Name)).Order(dt.Name).Debug().First()
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 查询列车尺寸信息
|
|
|
|
func QueryTrainSize(id int32) *model.TrainSize {
|
|
|
|
dt := dbquery.TrainSize
|
|
|
|
data, err := dt.Where(dt.ID.Eq(id)).Debug().First()
|
|
|
|
if err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
// 更新列车尺寸信息
|
2023-08-30 18:05:11 +08:00
|
|
|
func UpdateTrainSize(id int32, td *dto.TrainSizeDto) bool {
|
2023-08-25 10:41:13 +08:00
|
|
|
findOldQuery := dbquery.TrainSize
|
|
|
|
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
|
|
|
|
if oldD == nil || err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
2023-08-30 18:05:11 +08:00
|
|
|
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()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 删除列车尺寸
|
|
|
|
func DeleteTrainSizeById(id int) {
|
2023-09-13 11:29:58 +08:00
|
|
|
sid := int32(id)
|
|
|
|
_, _ = dbquery.TrainSize.Debug().Where(dbquery.TrainSize.ID.Eq(sid)).Delete()
|
|
|
|
dbquery.ProjectTrainSizeLink.Where(dbquery.ProjectTrainSizeLink.Sid.Eq(sid)).Delete()
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func QueryProjectTrainSize(id int32) []*model.TrainSize {
|
|
|
|
// 获取项目关联的列车尺寸
|
|
|
|
ptsl := dbquery.ProjectTrainSizeLink
|
2023-08-31 16:16:18 +08:00
|
|
|
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
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 查询列车轮径信息列表
|
|
|
|
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 {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
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 {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建列车轮径信息
|
2023-08-30 18:05:11 +08:00
|
|
|
func CreateTrainWheelDiameter(twd *dto.TrainWheelDiameterDto) (*model.TrainWheelDiameter, error) {
|
2023-08-25 10:41:13 +08:00
|
|
|
d := model.TrainWheelDiameter{
|
2023-08-30 18:05:11 +08:00
|
|
|
Name: twd.Name,
|
|
|
|
Diameter: twd.Diameter,
|
|
|
|
MinDiameter: twd.MinDiameter,
|
|
|
|
MaxDiameter: twd.MaxDiameter,
|
|
|
|
AxialPosition: twd.AxialPosition,
|
|
|
|
InstallDirection: twd.InstallDirection,
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
dt := dbquery.TrainWheelDiameter
|
|
|
|
err := dt.Save(&d)
|
|
|
|
if err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
2023-08-30 18:05:11 +08:00
|
|
|
return dt.Where(dt.Name.Eq(twd.Name)).Order(dt.Name).Debug().First()
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 查询列车轮径信息
|
|
|
|
func QueryTrainWheelDiameter(id int32) *model.TrainWheelDiameter {
|
|
|
|
dt := dbquery.TrainWheelDiameter
|
|
|
|
data, err := dt.Where(dt.ID.Eq(id)).Debug().First()
|
|
|
|
if err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
// 更新列车轮径信息
|
2023-08-30 18:05:11 +08:00
|
|
|
func UpdateTrainWheelDiameter(id int32, twd *dto.TrainWheelDiameterDto) bool {
|
2023-08-25 10:41:13 +08:00
|
|
|
findOldQuery := dbquery.TrainWheelDiameter
|
|
|
|
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
|
|
|
|
if oldD == nil || err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
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()})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 删除列车轮径
|
|
|
|
func DeleteTrainWheelDiameterById(id int) {
|
|
|
|
_, _ = dbquery.TrainWheelDiameter.Debug().Where(dbquery.TrainWheelDiameter.ID.Eq(int32(id))).Delete()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 检查列车型号名称
|
|
|
|
func checkTrainModel(name string, id int32) error {
|
2023-08-31 16:16:18 +08:00
|
|
|
if name == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2023-08-25 10:41:13 +08:00
|
|
|
findNameQuery := dbquery.TrainModel
|
|
|
|
w := findNameQuery.Where()
|
|
|
|
if id != 0 {
|
2023-08-25 17:49:09 +08:00
|
|
|
w = w.Where(findNameQuery.ID.NotIn(id))
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
2023-08-31 16:16:18 +08:00
|
|
|
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: "名称已存在"})
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|