117 lines
2.7 KiB
Go
117 lines
2.7 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"
|
||
|
)
|
||
|
|
||
|
// 查询项目列表
|
||
|
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 {
|
||
|
panic(err)
|
||
|
}
|
||
|
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 {
|
||
|
panic(err)
|
||
|
}
|
||
|
return records, nil
|
||
|
}
|
||
|
|
||
|
// 创建草稿
|
||
|
func CreateProject(dto *dto.ProjectDto) (*model.Project, error) {
|
||
|
if err := checkProjectInfo(dto.Code, 0); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
d := model.Project{
|
||
|
Name: dto.Name,
|
||
|
Code: dto.Code,
|
||
|
CreatedAt: time.Now(),
|
||
|
UpdateAt: time.Now(),
|
||
|
}
|
||
|
p := dbquery.Project
|
||
|
err := p.Save(&d)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return p.Where(p.Name.Eq(dto.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 {
|
||
|
panic(err)
|
||
|
}
|
||
|
return data
|
||
|
}
|
||
|
|
||
|
func UpdateProject(id int32, dto *dto.ProjectDto) bool {
|
||
|
if err := checkProjectInfo(dto.Code, id); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
findOldQuery := dbquery.Project
|
||
|
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
|
||
|
if oldD == nil || err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
if dto.Code != "" {
|
||
|
oldD.Code = dto.Code
|
||
|
}
|
||
|
if dto.Name != "" {
|
||
|
oldD.Name = dto.Name
|
||
|
}
|
||
|
oldD.UpdateAt = time.Now()
|
||
|
_, error := dbquery.Project.Updates(oldD)
|
||
|
if error != nil {
|
||
|
panic(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 {
|
||
|
findNameQuery := dbquery.Project
|
||
|
w := findNameQuery.Where()
|
||
|
if id != 0 {
|
||
|
w = w.Where(findNameQuery.ID.Eq(id))
|
||
|
}
|
||
|
if code != "" {
|
||
|
count, err := w.Where(findNameQuery.Code.Eq(code)).Debug().Count()
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
if count > 0 {
|
||
|
panic("编码已存在")
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|