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 { 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(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) } return &dto.PageDto{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(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) } return records } // 创建草稿 func CreateProject(pd *dto.ProjectDto) *model.Project { if err := checkProjectInfo(pd.Code, 0); err != nil { panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) } 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(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) } data, err := p.Where(p.Name.Eq(pd.Name)).Order(p.CreatedAt).Debug().First() if err != nil { panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) } return data } func QueryProject(id int32) *model.Project { data, err := dbquery.Project.Where(dbquery.Project.ID.Eq(id)).Debug().First() if err != nil { panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) } return data } func UpdateProject(id int32, pd *dto.ProjectDto) bool { if err := checkProjectInfo(pd.Code, id); err != nil { panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) } findOldQuery := dbquery.Project oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First() if oldD == nil || err != nil { panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) } 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(dto.ErrorDto{Code: dto.QueryDBError, Message: err2.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 { 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 { panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()}) } if count > 0 { panic(dto.ErrorDto{Code: dto.DataAlreadyExist, Message: "编码已存在"}) } return nil }