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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 查询列车型号信息列表
|
2023-10-20 18:08:06 +08:00
|
|
|
func PageTrainModelQuery(query *dto.PageTrainManageReqDto) *dto.PageDto {
|
2023-08-25 10:41:13 +08:00
|
|
|
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
|
|
|
}
|
2023-10-20 18:08:06 +08:00
|
|
|
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 查询列车型号信息列表
|
2023-10-20 18:08:06 +08:00
|
|
|
func ListTrainModelQuery(query *dto.TrainManageReqDto) []*model.TrainModel {
|
2023-08-25 10:41:13 +08:00
|
|
|
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
|
|
|
}
|
2023-10-20 18:08:06 +08:00
|
|
|
return records
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 创建列车型号信息
|
2023-10-20 18:08:06 +08:00
|
|
|
func CreateTrainModel(td *dto.TrainModelDto) *model.TrainModel {
|
2023-08-30 18:05:11 +08:00
|
|
|
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-10-20 18:08:06 +08:00
|
|
|
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
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询列车尺寸信息列表
|
2023-10-20 18:08:06 +08:00
|
|
|
func PageTrainSizeQuery(query *dto.PageTrainManageReqDto) *dto.PageDto {
|
2023-08-25 10:41:13 +08:00
|
|
|
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
|
|
|
}
|
2023-10-20 18:08:06 +08:00
|
|
|
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 查询列车尺寸信息列表
|
2023-10-20 18:08:06 +08:00
|
|
|
func ListTrainSizeQuery(query *dto.TrainManageReqDto) []*model.TrainSize {
|
2023-08-25 10:41:13 +08:00
|
|
|
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
|
|
|
}
|
2023-10-20 18:08:06 +08:00
|
|
|
return records
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 创建列车尺寸信息
|
2023-10-20 18:08:06 +08:00
|
|
|
func CreateTrainSize(td *dto.TrainSizeDto) *model.TrainSize {
|
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-10-20 18:08:06 +08:00
|
|
|
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
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 查询列车轮径信息列表
|
2023-10-20 18:08:06 +08:00
|
|
|
func PageTrainWheelDiameterQuery(query *dto.PageTrainManageReqDto) *dto.PageDto {
|
2023-11-13 14:15:56 +08:00
|
|
|
d := dbquery.TrainWheel
|
2023-08-25 10:41:13 +08:00
|
|
|
dq := d.Where()
|
|
|
|
if query.Name != "" {
|
|
|
|
dq = dq.Where(d.Name.Like(fmt.Sprintf("%%%s%%", query.Name)))
|
|
|
|
}
|
2023-11-13 14:15:56 +08:00
|
|
|
records, total, err := dq.Debug().Select(d.ID, d.Name, d.MinDiameter, d.MaxDiameter).FindByPage(query.Offset(), query.Size)
|
2023-08-25 10:41:13 +08:00
|
|
|
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-10-20 18:08:06 +08:00
|
|
|
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 查询列车轮径信息列表
|
2023-11-13 14:15:56 +08:00
|
|
|
func ListTrainWheelDiameterQuery(query *dto.TrainManageReqDto) []*model.TrainWheel {
|
|
|
|
d := dbquery.TrainWheel
|
2023-08-25 10:41:13 +08:00
|
|
|
dq := d.Where()
|
|
|
|
if query.Name != "" {
|
|
|
|
dq = dq.Where(d.Name.Like(fmt.Sprintf("%%%s%%", query.Name)))
|
|
|
|
}
|
2023-11-13 14:15:56 +08:00
|
|
|
records, err := dq.Debug().Select(d.ID, d.Name, d.MinDiameter, d.MaxDiameter).Find()
|
2023-08-25 10:41:13 +08:00
|
|
|
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-10-20 18:08:06 +08:00
|
|
|
return records
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 创建列车轮径信息
|
2023-11-13 14:15:56 +08:00
|
|
|
func CreateTrainWheelDiameter(twd *dto.TrainWheelDto) *model.TrainWheel {
|
|
|
|
d := model.TrainWheel{
|
|
|
|
Name: twd.Name,
|
|
|
|
MinDiameter: twd.MinDiameter,
|
|
|
|
MaxDiameter: twd.MaxDiameter,
|
|
|
|
}
|
|
|
|
dt := dbquery.TrainWheel
|
2023-08-25 10:41:13 +08:00
|
|
|
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-10-20 18:08:06 +08:00
|
|
|
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
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 查询列车轮径信息
|
2023-11-13 14:15:56 +08:00
|
|
|
func QueryTrainWheelDiameter(id int32) *model.TrainWheel {
|
|
|
|
dt := dbquery.TrainWheel
|
2023-08-25 10:41:13 +08:00
|
|
|
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-11-13 14:15:56 +08:00
|
|
|
func UpdateTrainWheelDiameter(id int32, twd *dto.TrainWheelDto) bool {
|
|
|
|
findOldQuery := dbquery.TrainWheel
|
2023-08-25 10:41:13 +08:00
|
|
|
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.MinDiameter = twd.MinDiameter
|
|
|
|
oldD.MaxDiameter = twd.MaxDiameter
|
|
|
|
_, 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) {
|
2023-11-13 14:15:56 +08:00
|
|
|
_, _ = dbquery.TrainWheel.Debug().Where(dbquery.TrainWheel.ID.Eq(int32(id))).Delete()
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 检查列车型号名称
|
|
|
|
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
|
|
|
|
}
|