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"
|
2024-01-30 15:16:58 +08:00
|
|
|
"joylink.club/bj-rtsts-server/sys_error"
|
2023-08-25 10:41:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 查询项目列表
|
2024-02-23 09:22:07 +08:00
|
|
|
func PageProjectQuery(query *dto.PageProjectReqDto) *dto.PageDto[*model.Project] {
|
2023-08-25 10:41:13 +08:00
|
|
|
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))
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
2024-02-23 09:22:07 +08:00
|
|
|
return &dto.PageDto[*model.Project]{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 ListProjectQuery(query *dto.ProjectReqDto) []*model.Project {
|
2023-08-25 10:41:13 +08:00
|
|
|
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-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 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))
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
d := model.Project{
|
2023-08-30 18:05:11 +08:00
|
|
|
Name: pd.Name,
|
|
|
|
Code: pd.Code,
|
2023-08-25 10:41:13 +08:00
|
|
|
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-08-25 10:41:13 +08:00
|
|
|
}
|
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
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
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))
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
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-25 10:41:13 +08:00
|
|
|
}
|
2023-08-30 18:05:11 +08:00
|
|
|
if pd.Code != "" {
|
|
|
|
oldD.Code = pd.Code
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
2023-08-30 18:05:11 +08:00
|
|
|
if pd.Name != "" {
|
|
|
|
oldD.Name = pd.Name
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
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))
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2023-08-25 10:41:13 +08:00
|
|
|
findNameQuery := dbquery.Project
|
|
|
|
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.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("编码已存在")
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|