123 lines
3.2 KiB
Go
123 lines
3.2 KiB
Go
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"
|
|
"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 {
|
|
panic(sys_error.New("查询失败,数据库异常", err))
|
|
}
|
|
return &dto.PageDto[*model.Project]{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}
|
|
}
|
|
|
|
// 查询项目列表
|
|
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 {
|
|
panic(sys_error.New("查询失败,数据库异常", err))
|
|
}
|
|
return records
|
|
}
|
|
|
|
// 创建草稿
|
|
func CreateProject(pd *dto.ProjectDto) *model.Project {
|
|
if err := checkProjectInfo(pd.Code, 0); err != nil {
|
|
panic(sys_error.New("创建失败", err))
|
|
}
|
|
d := model.Project{
|
|
Name: pd.Name,
|
|
Code: pd.Code,
|
|
CreatedAt: time.Now(),
|
|
UpdateAt: time.Now(),
|
|
}
|
|
p := dbquery.Project
|
|
err := p.Save(&d)
|
|
if err != nil {
|
|
panic(sys_error.New("创建保存失败,数据库异常", err))
|
|
}
|
|
data, err := p.Where(p.Name.Eq(pd.Name)).Order(p.CreatedAt).Debug().First()
|
|
if err != nil {
|
|
panic(sys_error.New("创建失败,数据库异常", err))
|
|
}
|
|
return data
|
|
}
|
|
|
|
func QueryProject(id int32) *model.Project {
|
|
data, err := dbquery.Project.Where(dbquery.Project.ID.Eq(id)).Debug().First()
|
|
if err != nil {
|
|
panic(sys_error.New("查询失败,数据库异常", err))
|
|
}
|
|
return data
|
|
}
|
|
|
|
func UpdateProject(id int32, pd *dto.ProjectDto) bool {
|
|
if err := checkProjectInfo(pd.Code, id); err != nil {
|
|
panic(sys_error.New("更新失败", err))
|
|
}
|
|
findOldQuery := dbquery.Project
|
|
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
|
|
if oldD == nil || err != nil {
|
|
panic(sys_error.New("更新失败,数据库异常", err))
|
|
}
|
|
if pd.Code != "" {
|
|
oldD.Code = pd.Code
|
|
}
|
|
if pd.Name != "" {
|
|
oldD.Name = pd.Name
|
|
}
|
|
oldD.UpdateAt = time.Now()
|
|
_, err2 := dbquery.Project.Updates(oldD)
|
|
if err2 != nil {
|
|
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 {
|
|
if code == "" {
|
|
return nil
|
|
}
|
|
findNameQuery := dbquery.Project
|
|
w := findNameQuery.Where()
|
|
if id != 0 {
|
|
w = w.Where(findNameQuery.ID.NotIn(id))
|
|
}
|
|
count, err := w.Where(findNameQuery.Code.Eq(code)).Debug().Count()
|
|
if err != nil {
|
|
return sys_error.New("数据库异常", err)
|
|
}
|
|
if count > 0 {
|
|
return fmt.Errorf("编码已存在")
|
|
}
|
|
return nil
|
|
}
|