rts-sim-testing-service/service/project.go

118 lines
3.2 KiB
Go
Raw Normal View History

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 PageProjectQuery(query *dto.PageProjectReqDto) (*dto.PageDto, error) {
d := dbquery.Project
dq := d.Where()
if query.Name != "" {
dq = dq.Where(d.Name.Like(fmt.Sprintf("%%%s%%", query.Name)))
}
if query.Code != "" {
dq = dq.Where(d.Code.Like(fmt.Sprintf("%%%s%%", query.Code)))
}
records, total, err := dq.Debug().Select(d.ID, d.Code, 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()})
}
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}, nil
}
// 查询项目列表
func ListProjectQuery(query *dto.ProjectReqDto) ([]*model.Project, error) {
d := dbquery.Project
dq := d.Where()
if query.Name != "" {
dq = dq.Where(d.Name.Like(fmt.Sprintf("%%%s%%", query.Name)))
}
if query.Code != "" {
dq = dq.Where(d.Code.Like(fmt.Sprintf("%%%s%%", query.Code)))
}
records, err := dq.Debug().Select(d.ID, d.Code, d.Name).Find()
if err != nil {
2023-08-30 18:05:11 +08:00
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
}
return records, nil
}
// 创建草稿
2023-08-30 18:05:11 +08:00
func CreateProject(pd *dto.ProjectDto) (*model.Project, error) {
if err := checkProjectInfo(pd.Code, 0); err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
}
d := model.Project{
2023-08-30 18:05:11 +08:00
Name: pd.Name,
Code: pd.Code,
CreatedAt: time.Now(),
UpdateAt: time.Now(),
}
p := dbquery.Project
err := p.Save(&d)
if err != nil {
2023-08-30 18:05:11 +08:00
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
}
2023-08-30 18:05:11 +08:00
return p.Where(p.Name.Eq(pd.Name)).Order(p.CreatedAt).Debug().First()
}
func QueryProject(id int32) *model.Project {
data, err := dbquery.Project.Where(dbquery.Project.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()})
}
return data
}
2023-08-30 18:05:11 +08:00
func UpdateProject(id int32, pd *dto.ProjectDto) bool {
if err := checkProjectInfo(pd.Code, id); err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
}
findOldQuery := dbquery.Project
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-30 18:05:11 +08:00
if pd.Code != "" {
oldD.Code = pd.Code
}
2023-08-30 18:05:11 +08:00
if pd.Name != "" {
oldD.Name = pd.Name
}
oldD.UpdateAt = time.Now()
2023-08-30 18:05:11 +08:00
_, err2 := dbquery.Project.Updates(oldD)
if err2 != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err2.Error()})
}
return true
}
func DeleteProjectById(id int) {
_, _ = dbquery.Project.Debug().Where(dbquery.Project.ID.Eq(int32(id))).Delete()
}
func checkProjectInfo(code string, id int32) error {
2023-08-31 16:16:18 +08:00
if code == "" {
return nil
}
findNameQuery := dbquery.Project
w := findNameQuery.Where()
if id != 0 {
w = w.Where(findNameQuery.ID.NotIn(id))
}
2023-08-31 16:16:18 +08:00
count, err := w.Where(findNameQuery.Code.Eq(code)).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
}