2023-07-14 16:47:59 +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"
|
2023-11-21 13:13:35 +08:00
|
|
|
"joylink.club/bj-rtsts-server/sys_error"
|
2023-07-14 16:47:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 查询草稿列表
|
2024-02-23 09:22:07 +08:00
|
|
|
func PageDraftingQuery(query *dto.PageDraftingReqDto) *dto.PageDto[*dto.DraftingResDto] {
|
2023-11-21 13:13:35 +08:00
|
|
|
d, u := dbquery.Drafting, dbquery.User
|
|
|
|
dq := d.LeftJoin(u, u.ID.EqCol(d.CreatorID)).
|
|
|
|
Select(d.ID, d.Name, d.Category, d.Type, d.CreatorID, d.CreatedAt, d.UpdateAt, u.Name.As("creator")).
|
|
|
|
Where()
|
2023-07-14 16:47:59 +08:00
|
|
|
if query.Name != "" {
|
|
|
|
dq = dq.Where(d.Name.Like(fmt.Sprintf("%%%s%%", query.Name)))
|
|
|
|
}
|
2023-11-21 13:13:35 +08:00
|
|
|
var records []*dto.DraftingResDto
|
|
|
|
total, err := dq.Debug().Order(d.UpdateAt.Desc()).ScanByPage(&records, query.Offset(), query.Size)
|
2023-07-14 16:47:59 +08:00
|
|
|
if err != nil {
|
2023-11-21 13:13:35 +08:00
|
|
|
panic(sys_error.New("草稿查询错误", err))
|
2023-07-14 16:47:59 +08:00
|
|
|
}
|
2024-02-23 09:22:07 +08:00
|
|
|
return &dto.PageDto[*dto.DraftingResDto]{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}
|
2023-07-14 16:47:59 +08:00
|
|
|
}
|
|
|
|
|
2023-10-20 18:08:06 +08:00
|
|
|
func ListDraftingQuery(query *dto.ListDraftingReqDto) []*model.Drafting {
|
2023-10-12 17:55:41 +08:00
|
|
|
d := dbquery.Drafting
|
|
|
|
dq := d.Where()
|
|
|
|
if query.Type != 0 {
|
|
|
|
dq = dq.Where(d.Type.Eq(query.Type))
|
|
|
|
}
|
2023-11-21 10:27:12 +08:00
|
|
|
records, err := dq.Debug().Omit(d.Proto).Order(d.UpdateAt.Desc()).Find()
|
2023-10-12 17:55:41 +08:00
|
|
|
if err != nil {
|
2023-11-21 13:13:35 +08:00
|
|
|
panic(sys_error.New("草稿查询错误", err))
|
2023-10-12 17:55:41 +08:00
|
|
|
}
|
2023-10-20 18:08:06 +08:00
|
|
|
return records
|
2023-10-12 17:55:41 +08:00
|
|
|
}
|
|
|
|
|
2023-07-14 16:47:59 +08:00
|
|
|
// 创建草稿
|
2023-10-20 18:08:06 +08:00
|
|
|
func CreateDrafting(createId int32, dd *dto.DraftingDto) *model.Drafting {
|
2023-08-30 18:05:11 +08:00
|
|
|
if err := checkDraftingInfo(dd.Name); err != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-07-28 14:13:03 +08:00
|
|
|
}
|
2023-07-14 16:47:59 +08:00
|
|
|
d := model.Drafting{
|
2023-08-30 18:05:11 +08:00
|
|
|
Name: dd.Name,
|
|
|
|
Category: dd.Category,
|
|
|
|
Type: int32(dd.Type),
|
|
|
|
Proto: dd.Proto,
|
2023-07-14 16:47:59 +08:00
|
|
|
CreatorID: createId,
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
UpdateAt: time.Now(),
|
|
|
|
}
|
2023-07-28 14:13:03 +08:00
|
|
|
err := dbquery.Drafting.Save(&d)
|
|
|
|
if err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-07-28 14:13:03 +08:00
|
|
|
}
|
2023-10-20 18:08:06 +08:00
|
|
|
data, err := dbquery.Drafting.Where(
|
2023-08-30 18:05:11 +08:00
|
|
|
dbquery.Drafting.Name.Eq(dd.Name),
|
2023-07-28 14:13:03 +08:00
|
|
|
dbquery.Drafting.CreatorID.Eq(createId),
|
|
|
|
).Order(dbquery.Drafting.CreatedAt).Debug().First()
|
2023-10-20 18:08:06 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
|
|
}
|
|
|
|
return data
|
2023-07-14 16:47:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 另存为
|
2023-10-20 18:08:06 +08:00
|
|
|
func SaveAsDrafting(createId int32, oldId int32, dd *dto.DraftingDto) *model.Drafting {
|
2023-08-30 18:05:11 +08:00
|
|
|
if err := checkDraftingInfo(dd.Name); err != nil {
|
2023-10-20 18:08:06 +08:00
|
|
|
return nil
|
2023-07-14 16:47:59 +08:00
|
|
|
}
|
2023-07-28 14:13:03 +08:00
|
|
|
oldD, err := dbquery.Drafting.Where(dbquery.Drafting.ID.Eq(oldId)).Debug().First()
|
2023-07-14 16:47:59 +08:00
|
|
|
if oldD == nil || err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-07-14 16:47:59 +08:00
|
|
|
}
|
|
|
|
newD := model.Drafting{
|
2023-08-30 18:05:11 +08:00
|
|
|
Name: dd.Name,
|
2023-07-14 16:47:59 +08:00
|
|
|
Proto: oldD.Proto,
|
|
|
|
CreatorID: createId,
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
UpdateAt: time.Now(),
|
2023-08-03 17:01:28 +08:00
|
|
|
Category: oldD.Category,
|
2023-09-19 10:23:25 +08:00
|
|
|
Type: oldD.Type,
|
2023-07-14 16:47:59 +08:00
|
|
|
}
|
2023-07-28 14:13:03 +08:00
|
|
|
if err = dbquery.Drafting.Save(&newD); err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-07-28 14:13:03 +08:00
|
|
|
}
|
2023-10-20 18:08:06 +08:00
|
|
|
data, err := dbquery.Drafting.Where(
|
2023-08-30 18:05:11 +08:00
|
|
|
dbquery.Drafting.Name.Eq(dd.Name),
|
2023-07-28 14:13:03 +08:00
|
|
|
dbquery.Drafting.CreatorID.Eq(createId),
|
|
|
|
).Order(dbquery.Drafting.CreatedAt).Debug().First()
|
2023-10-20 18:08:06 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
|
|
}
|
|
|
|
return data
|
2023-07-14 16:47:59 +08:00
|
|
|
}
|
2023-07-14 18:09:07 +08:00
|
|
|
|
2023-07-28 14:13:03 +08:00
|
|
|
func QueryDrafting(id int32) *model.Drafting {
|
2023-07-14 18:09:07 +08:00
|
|
|
query := dbquery.Drafting
|
|
|
|
data, err := query.Where(query.ID.Eq(id)).Debug().First()
|
|
|
|
if err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-07-14 18:09:07 +08:00
|
|
|
}
|
2023-07-28 14:13:03 +08:00
|
|
|
return data
|
2023-07-14 18:09:07 +08:00
|
|
|
}
|
|
|
|
|
2023-08-30 18:05:11 +08:00
|
|
|
func UpdateDrafting(id int32, dd *dto.DraftingDto) bool {
|
2023-07-14 18:09:07 +08:00
|
|
|
findOldQuery := dbquery.Drafting
|
|
|
|
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
|
|
|
|
if oldD == nil || err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-07-14 18:09:07 +08:00
|
|
|
}
|
2023-08-30 18:05:11 +08:00
|
|
|
if dd.Proto != nil {
|
|
|
|
oldD.Proto = dd.Proto
|
2023-07-14 18:09:07 +08:00
|
|
|
}
|
2023-08-30 18:05:11 +08:00
|
|
|
if len(dd.Name) > 0 {
|
|
|
|
oldD.Name = dd.Name
|
2023-07-14 18:09:07 +08:00
|
|
|
}
|
|
|
|
oldD.UpdateAt = time.Now()
|
2023-08-28 14:16:14 +08:00
|
|
|
_, err2 := dbquery.Drafting.Updates(oldD)
|
|
|
|
if err2 != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err2.Error()})
|
2023-07-28 14:13:03 +08:00
|
|
|
}
|
|
|
|
return true
|
2023-07-14 18:09:07 +08:00
|
|
|
}
|
2023-07-25 17:18:37 +08:00
|
|
|
|
|
|
|
func DeleteDraftingById(id int) {
|
|
|
|
_, _ = dbquery.Drafting.Debug().Where(dbquery.Drafting.ID.Eq(int32(id))).Delete()
|
|
|
|
}
|
2023-07-28 14:13:03 +08:00
|
|
|
|
|
|
|
func checkDraftingInfo(name string) error {
|
|
|
|
findNameQuery := dbquery.Drafting
|
|
|
|
count, err := findNameQuery.Where(findNameQuery.Name.Eq(name)).Debug().Count()
|
|
|
|
if err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-07-28 14:13:03 +08:00
|
|
|
}
|
|
|
|
if count > 0 {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.DataAlreadyExist, Message: "名称已存在"})
|
2023-07-28 14:13:03 +08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|