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

123 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"
2024-01-30 15:16:58 +08:00
"joylink.club/bj-rtsts-server/sys_error"
)
// 查询项目列表
func PageProjectQuery(query *dto.PageProjectReqDto) *dto.PageDto[*model.Project] {
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 {
2024-01-30 15:16:58 +08:00
panic(sys_error.New("查询失败,数据库异常", err))
}
return &dto.PageDto[*model.Project]{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}
}
// 查询项目列表
2023-10-20 18:08:06 +08:00
func ListProjectQuery(query *dto.ProjectReqDto) []*model.Project {
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 {
2024-01-30 15:16:58 +08:00
panic(sys_error.New("查询失败,数据库异常", err))
}
2023-10-20 18:08:06 +08:00
return records
}
// 创建草稿
2023-10-20 18:08:06 +08:00
func CreateProject(pd *dto.ProjectDto) *model.Project {
2023-08-30 18:05:11 +08:00
if err := checkProjectInfo(pd.Code, 0); err != nil {
2024-01-30 15:16:58 +08:00
panic(sys_error.New("创建失败", err))
}
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 {
2024-01-30 15:16:58 +08:00
panic(sys_error.New("创建保存失败,数据库异常", err))
}
2023-10-20 18:08:06 +08:00
data, err := p.Where(p.Name.Eq(pd.Name)).Order(p.CreatedAt).Debug().First()
if err != nil {
2024-01-30 15:16:58 +08:00
panic(sys_error.New("创建失败,数据库异常", err))
2023-10-20 18:08:06 +08:00
}
return data
}
func QueryProject(id int32) *model.Project {
data, err := dbquery.Project.Where(dbquery.Project.ID.Eq(id)).Debug().First()
if err != nil {
2024-01-30 15:16:58 +08:00
panic(sys_error.New("查询失败,数据库异常", err))
}
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 {
2024-01-30 15:16:58 +08:00
panic(sys_error.New("更新失败", err))
}
findOldQuery := dbquery.Project
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
if oldD == nil || err != nil {
2024-01-30 15:16:58 +08:00
panic(sys_error.New("更新失败,数据库异常", err))
}
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 {
2024-01-30 15:16:58 +08:00
panic(sys_error.New("更新失败,数据库异常", err2))
}
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 {
2024-01-30 15:16:58 +08:00
return sys_error.New("数据库异常", err)
2023-08-31 16:16:18 +08:00
}
if count > 0 {
2024-01-30 15:16:58 +08:00
return fmt.Errorf("编码已存在")
}
return nil
}