修改接口异常返回

This commit is contained in:
walker 2024-01-30 15:16:58 +08:00
parent a7edde37cd
commit c69c9e8323

View File

@ -7,6 +7,7 @@ import (
"joylink.club/bj-rtsts-server/db/dbquery" "joylink.club/bj-rtsts-server/db/dbquery"
"joylink.club/bj-rtsts-server/db/model" "joylink.club/bj-rtsts-server/db/model"
"joylink.club/bj-rtsts-server/dto" "joylink.club/bj-rtsts-server/dto"
"joylink.club/bj-rtsts-server/sys_error"
) )
// 查询项目列表 // 查询项目列表
@ -21,7 +22,7 @@ func PageProjectQuery(query *dto.PageProjectReqDto) *dto.PageDto {
} }
records, total, err := dq.Debug().Select(d.ID, d.Code, d.Name, d.UpdateAt, d.CreatedAt).FindByPage(query.Offset(), query.Size) records, total, err := dq.Debug().Select(d.ID, d.Code, d.Name, d.UpdateAt, d.CreatedAt).FindByPage(query.Offset(), query.Size)
if err != nil { if err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) panic(sys_error.New("查询失败,数据库异常", err))
} }
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records} return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}
} }
@ -38,7 +39,7 @@ func ListProjectQuery(query *dto.ProjectReqDto) []*model.Project {
} }
records, err := dq.Debug().Select(d.ID, d.Code, d.Name).Find() records, err := dq.Debug().Select(d.ID, d.Code, d.Name).Find()
if err != nil { if err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) panic(sys_error.New("查询失败,数据库异常", err))
} }
return records return records
} }
@ -46,7 +47,7 @@ func ListProjectQuery(query *dto.ProjectReqDto) []*model.Project {
// 创建草稿 // 创建草稿
func CreateProject(pd *dto.ProjectDto) *model.Project { func CreateProject(pd *dto.ProjectDto) *model.Project {
if err := checkProjectInfo(pd.Code, 0); err != nil { if err := checkProjectInfo(pd.Code, 0); err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) panic(sys_error.New("创建失败", err))
} }
d := model.Project{ d := model.Project{
Name: pd.Name, Name: pd.Name,
@ -57,11 +58,11 @@ func CreateProject(pd *dto.ProjectDto) *model.Project {
p := dbquery.Project p := dbquery.Project
err := p.Save(&d) err := p.Save(&d)
if err != nil { if err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) panic(sys_error.New("创建保存失败,数据库异常", err))
} }
data, err := p.Where(p.Name.Eq(pd.Name)).Order(p.CreatedAt).Debug().First() data, err := p.Where(p.Name.Eq(pd.Name)).Order(p.CreatedAt).Debug().First()
if err != nil { if err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) panic(sys_error.New("创建失败,数据库异常", err))
} }
return data return data
} }
@ -69,19 +70,19 @@ func CreateProject(pd *dto.ProjectDto) *model.Project {
func QueryProject(id int32) *model.Project { func QueryProject(id int32) *model.Project {
data, err := dbquery.Project.Where(dbquery.Project.ID.Eq(id)).Debug().First() data, err := dbquery.Project.Where(dbquery.Project.ID.Eq(id)).Debug().First()
if err != nil { if err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) panic(sys_error.New("查询失败,数据库异常", err))
} }
return data return data
} }
func UpdateProject(id int32, pd *dto.ProjectDto) bool { func UpdateProject(id int32, pd *dto.ProjectDto) bool {
if err := checkProjectInfo(pd.Code, id); err != nil { if err := checkProjectInfo(pd.Code, id); err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) panic(sys_error.New("更新失败", err))
} }
findOldQuery := dbquery.Project findOldQuery := dbquery.Project
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First() oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
if oldD == nil || err != nil { if oldD == nil || err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) panic(sys_error.New("更新失败,数据库异常", err))
} }
if pd.Code != "" { if pd.Code != "" {
oldD.Code = pd.Code oldD.Code = pd.Code
@ -92,7 +93,7 @@ func UpdateProject(id int32, pd *dto.ProjectDto) bool {
oldD.UpdateAt = time.Now() oldD.UpdateAt = time.Now()
_, err2 := dbquery.Project.Updates(oldD) _, err2 := dbquery.Project.Updates(oldD)
if err2 != nil { if err2 != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err2.Error()}) panic(sys_error.New("更新失败,数据库异常", err2))
} }
return true return true
} }
@ -112,10 +113,10 @@ func checkProjectInfo(code string, id int32) error {
} }
count, err := w.Where(findNameQuery.Code.Eq(code)).Debug().Count() count, err := w.Where(findNameQuery.Code.Eq(code)).Debug().Count()
if err != nil { if err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) return sys_error.New("数据库异常", err)
} }
if count > 0 { if count > 0 {
panic(dto.ErrorDto{Code: dto.DataAlreadyExist, Message: "编码已存在"}) return fmt.Errorf("编码已存在")
} }
return nil return nil
} }