Initial commit (code only without large binaries)
This commit is contained in:
116
EdgeAPI/internal/db/models/tickets/user_ticket_category_dao.go
Normal file
116
EdgeAPI/internal/db/models/tickets/user_ticket_category_dao.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package tickets
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
const (
|
||||
UserTicketCategoryStateEnabled = 1 // 已启用
|
||||
UserTicketCategoryStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type UserTicketCategoryDAO dbs.DAO
|
||||
|
||||
func NewUserTicketCategoryDAO() *UserTicketCategoryDAO {
|
||||
return dbs.NewDAO(&UserTicketCategoryDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeUserTicketCategories",
|
||||
Model: new(UserTicketCategory),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*UserTicketCategoryDAO)
|
||||
}
|
||||
|
||||
var SharedUserTicketCategoryDAO *UserTicketCategoryDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedUserTicketCategoryDAO = NewUserTicketCategoryDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableUserTicketCategory 启用条目
|
||||
func (this *UserTicketCategoryDAO) EnableUserTicketCategory(tx *dbs.Tx, id uint32) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", UserTicketCategoryStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// DisableUserTicketCategory 禁用条目
|
||||
func (this *UserTicketCategoryDAO) DisableUserTicketCategory(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", UserTicketCategoryStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledUserTicketCategory 查找启用中的条目
|
||||
func (this *UserTicketCategoryDAO) FindEnabledUserTicketCategory(tx *dbs.Tx, id int64) (*UserTicketCategory, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Attr("state", UserTicketCategoryStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*UserTicketCategory), err
|
||||
}
|
||||
|
||||
// FindUserTicketCategoryName 根据主键查找名称
|
||||
func (this *UserTicketCategoryDAO) FindUserTicketCategoryName(tx *dbs.Tx, id int64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// CreateCategory 创建分类
|
||||
func (this *UserTicketCategoryDAO) CreateCategory(tx *dbs.Tx, name string) (int64, error) {
|
||||
var op = NewUserTicketCategoryOperator()
|
||||
op.Name = name
|
||||
op.IsOn = true
|
||||
op.State = UserTicketCategoryStateEnabled
|
||||
return this.SaveInt64(tx, op)
|
||||
}
|
||||
|
||||
// UpdateCategory 修改分类
|
||||
func (this *UserTicketCategoryDAO) UpdateCategory(tx *dbs.Tx, categoryId int64, name string, isOn bool) error {
|
||||
if categoryId <= 0 {
|
||||
return errors.New("invalid categoryId")
|
||||
}
|
||||
var op = NewUserTicketCategoryOperator()
|
||||
op.Id = categoryId
|
||||
op.Name = name
|
||||
op.IsOn = isOn
|
||||
return this.Save(tx, op)
|
||||
}
|
||||
|
||||
// FindAllEnabledCategories 查找所有分类
|
||||
func (this *UserTicketCategoryDAO) FindAllEnabledCategories(tx *dbs.Tx) (result []*UserTicketCategory, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(UserTicketCategoryStateEnabled).
|
||||
Desc("order").
|
||||
AscPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// FindAllEnabledAndOnCategories 查找所有分类
|
||||
func (this *UserTicketCategoryDAO) FindAllEnabledAndOnCategories(tx *dbs.Tx) (result []*UserTicketCategory, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(UserTicketCategoryStateEnabled).
|
||||
Attr("isOn", true).
|
||||
Desc("order").
|
||||
AscPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package tickets_test
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
@@ -0,0 +1,22 @@
|
||||
package tickets
|
||||
|
||||
// UserTicketCategory 工单分类
|
||||
type UserTicketCategory struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
Name string `field:"name"` // 分类名
|
||||
IsOn bool `field:"isOn"` // 是否启用
|
||||
Order uint32 `field:"order"` // 排序
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type UserTicketCategoryOperator struct {
|
||||
Id interface{} // ID
|
||||
Name interface{} // 分类名
|
||||
IsOn interface{} // 是否启用
|
||||
Order interface{} // 排序
|
||||
State interface{} // 状态
|
||||
}
|
||||
|
||||
func NewUserTicketCategoryOperator() *UserTicketCategoryOperator {
|
||||
return &UserTicketCategoryOperator{}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package tickets
|
||||
167
EdgeAPI/internal/db/models/tickets/user_ticket_dao.go
Normal file
167
EdgeAPI/internal/db/models/tickets/user_ticket_dao.go
Normal file
@@ -0,0 +1,167 @@
|
||||
package tickets
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
UserTicketStateEnabled = 1 // 已启用
|
||||
UserTicketStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type UserTicketDAO dbs.DAO
|
||||
|
||||
func NewUserTicketDAO() *UserTicketDAO {
|
||||
return dbs.NewDAO(&UserTicketDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeUserTickets",
|
||||
Model: new(UserTicket),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*UserTicketDAO)
|
||||
}
|
||||
|
||||
var SharedUserTicketDAO *UserTicketDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedUserTicketDAO = NewUserTicketDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableUserTicket 启用条目
|
||||
func (this *UserTicketDAO) EnableUserTicket(tx *dbs.Tx, id uint64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", UserTicketStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// DisableUserTicket 禁用条目
|
||||
func (this *UserTicketDAO) DisableUserTicket(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", UserTicketStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledUserTicket 查找启用中的条目
|
||||
func (this *UserTicketDAO) FindEnabledUserTicket(tx *dbs.Tx, id int64) (*UserTicket, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Attr("state", UserTicketStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*UserTicket), err
|
||||
}
|
||||
|
||||
// CreateTicket 创建工单
|
||||
func (this *UserTicketDAO) CreateTicket(tx *dbs.Tx, userId int64, categoryId int64, subject string, body string) (int64, error) {
|
||||
var op = NewUserTicketOperator()
|
||||
op.UserId = userId
|
||||
op.CategoryId = categoryId
|
||||
op.Subject = subject
|
||||
op.Body = body
|
||||
op.Status = userconfigs.UserTicketStatusNone
|
||||
op.LastLogAt = time.Now().Unix()
|
||||
op.State = UserTicketStateEnabled
|
||||
return this.SaveInt64(tx, op)
|
||||
}
|
||||
|
||||
// UpdateTicket 修改工单
|
||||
func (this *UserTicketDAO) UpdateTicket(tx *dbs.Tx, ticketId int64, categoryId int64, subject string, body string) error {
|
||||
if ticketId <= 0 {
|
||||
return errors.New("invalid categoryId")
|
||||
}
|
||||
var op = NewUserTicketOperator()
|
||||
op.Id = ticketId
|
||||
op.CategoryId = categoryId
|
||||
op.Subject = subject
|
||||
op.Body = body
|
||||
return this.Save(tx, op)
|
||||
}
|
||||
|
||||
// UpdateTicketStatus 设置工单状态
|
||||
func (this *UserTicketDAO) UpdateTicketStatus(tx *dbs.Tx, ticketId int64, status userconfigs.UserTicketStatus) error {
|
||||
return this.Query(tx).
|
||||
Pk(ticketId).
|
||||
Set("status", status).
|
||||
Set("lastLogAt", time.Now().Unix()).
|
||||
UpdateQuickly()
|
||||
}
|
||||
|
||||
// CountAllTickets 计算工单数量
|
||||
func (this *UserTicketDAO) CountAllTickets(tx *dbs.Tx, userId int64, categoryId int64, status userconfigs.UserTicketStatus) (int64, error) {
|
||||
var query = this.Query(tx)
|
||||
query.State(UserTicketStateEnabled)
|
||||
if userId > 0 {
|
||||
query.Attr("userId", userId)
|
||||
}
|
||||
if categoryId > 0 {
|
||||
query.Attr("categoryId", categoryId)
|
||||
}
|
||||
if len(status) > 0 {
|
||||
query.Attr("status", status)
|
||||
}
|
||||
return query.Count()
|
||||
}
|
||||
|
||||
// ListTickets 列出单页工单
|
||||
func (this *UserTicketDAO) ListTickets(tx *dbs.Tx, userId int64, categoryId int64, status userconfigs.UserTicketStatus, offset int64, size int64) (result []*UserTicket, err error) {
|
||||
var query = this.Query(tx)
|
||||
query.State(UserTicketStateEnabled)
|
||||
if userId > 0 {
|
||||
query.Attr("userId", userId)
|
||||
}
|
||||
if categoryId > 0 {
|
||||
query.Attr("categoryId", categoryId)
|
||||
}
|
||||
if len(status) > 0 {
|
||||
query.Attr("status", status)
|
||||
}
|
||||
_, err = query.
|
||||
DescPk().
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// CheckUserTicket 检查工单是否属于用户
|
||||
func (this *UserTicketDAO) CheckUserTicket(tx *dbs.Tx, userId int64, ticketId int64) error {
|
||||
if ticketId <= 0 {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
b, err := this.Query(tx).
|
||||
Pk(ticketId).
|
||||
Attr("userId", userId).
|
||||
State(UserTicketStateEnabled).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !b {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindTicketStatus 查找工单状态
|
||||
func (this *UserTicketDAO) FindTicketStatus(tx *dbs.Tx, ticketId int64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(ticketId).
|
||||
Result("status").
|
||||
FindStringCol("")
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package tickets_test
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
132
EdgeAPI/internal/db/models/tickets/user_ticket_log_dao.go
Normal file
132
EdgeAPI/internal/db/models/tickets/user_ticket_log_dao.go
Normal file
@@ -0,0 +1,132 @@
|
||||
package tickets
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
const (
|
||||
UserTicketLogStateEnabled = 1 // 已启用
|
||||
UserTicketLogStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type UserTicketLogDAO dbs.DAO
|
||||
|
||||
func NewUserTicketLogDAO() *UserTicketLogDAO {
|
||||
return dbs.NewDAO(&UserTicketLogDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeUserTicketLogs",
|
||||
Model: new(UserTicketLog),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*UserTicketLogDAO)
|
||||
}
|
||||
|
||||
var SharedUserTicketLogDAO *UserTicketLogDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedUserTicketLogDAO = NewUserTicketLogDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// CreateLog 创建日志
|
||||
func (this *UserTicketLogDAO) CreateLog(tx *dbs.Tx, adminId int64, userId int64, ticketId int64, status userconfigs.UserTicketStatus, comment string, isReadonly bool) (int64, error) {
|
||||
if ticketId <= 0 {
|
||||
return 0, errors.New("invalid ticketId")
|
||||
}
|
||||
|
||||
var op = NewUserTicketLogOperator()
|
||||
op.AdminId = adminId
|
||||
op.UserId = userId
|
||||
op.TicketId = ticketId
|
||||
op.Status = status
|
||||
op.Comment = comment
|
||||
op.IsReadonly = isReadonly
|
||||
op.State = UserTicketLogStateEnabled
|
||||
logId, err := this.SaveInt64(tx, op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
err = SharedUserTicketDAO.UpdateTicketStatus(tx, ticketId, status)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return logId, nil
|
||||
}
|
||||
|
||||
// CountTicketLogs 查询日志数量
|
||||
func (this *UserTicketLogDAO) CountTicketLogs(tx *dbs.Tx, ticketId int64) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Attr("ticketId", ticketId).
|
||||
State(UserTicketLogStateEnabled).
|
||||
Count()
|
||||
}
|
||||
|
||||
// ListTicketLogs 列出单页日志
|
||||
func (this *UserTicketLogDAO) ListTicketLogs(tx *dbs.Tx, ticketId int64, offset int64, size int64) (result []*UserTicketLog, err error) {
|
||||
_, err = this.Query(tx).
|
||||
Attr("ticketId", ticketId).
|
||||
State(UserTicketLogStateEnabled).
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
DescPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// CheckUserLog 检查工单日志是否属于用户
|
||||
func (this *UserTicketLogDAO) CheckUserLog(tx *dbs.Tx, userId int64, logId int64) error {
|
||||
if logId <= 0 {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
b, err := this.Query(tx).
|
||||
Pk(logId).
|
||||
Attr("userId", userId).
|
||||
State(UserTicketStateEnabled).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !b {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckLogReadonly 检查日志是否为只读
|
||||
func (this *UserTicketLogDAO) CheckLogReadonly(tx *dbs.Tx, logId int64) (bool, error) {
|
||||
return this.Query(tx).
|
||||
Result("isReadonly").
|
||||
Pk(logId).
|
||||
FindBoolCol()
|
||||
}
|
||||
|
||||
// DisableLog 禁用日志
|
||||
func (this *UserTicketLogDAO) DisableLog(tx *dbs.Tx, logId int64) error {
|
||||
return this.Query(tx).
|
||||
Pk(logId).
|
||||
Set("state", UserTicketLogStateDisabled).
|
||||
UpdateQuickly()
|
||||
}
|
||||
|
||||
// FindLatestTicketLog 读取最新一条日志
|
||||
func (this *UserTicketLogDAO) FindLatestTicketLog(tx *dbs.Tx, ticketId int64) (*UserTicketLog, error) {
|
||||
one, err := this.Query(tx).
|
||||
Attr("ticketId", ticketId).
|
||||
State(UserTicketLogStateEnabled).
|
||||
DescPk().
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return one.(*UserTicketLog), nil
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package tickets_test
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
30
EdgeAPI/internal/db/models/tickets/user_ticket_log_model.go
Normal file
30
EdgeAPI/internal/db/models/tickets/user_ticket_log_model.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package tickets
|
||||
|
||||
// UserTicketLog 工单日志
|
||||
type UserTicketLog struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
AdminId uint64 `field:"adminId"` // 管理员ID
|
||||
UserId uint64 `field:"userId"` // 用户ID
|
||||
TicketId uint64 `field:"ticketId"` // 工单ID
|
||||
Status string `field:"status"` // 状态
|
||||
Comment string `field:"comment"` // 回复内容
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
IsReadonly bool `field:"isReadonly"` // 是否为只读
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type UserTicketLogOperator struct {
|
||||
Id interface{} // ID
|
||||
AdminId interface{} // 管理员ID
|
||||
UserId interface{} // 用户ID
|
||||
TicketId interface{} // 工单ID
|
||||
Status interface{} // 状态
|
||||
Comment interface{} // 回复内容
|
||||
CreatedAt interface{} // 创建时间
|
||||
IsReadonly interface{} // 是否为只读
|
||||
State interface{} // 状态
|
||||
}
|
||||
|
||||
func NewUserTicketLogOperator() *UserTicketLogOperator {
|
||||
return &UserTicketLogOperator{}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package tickets
|
||||
32
EdgeAPI/internal/db/models/tickets/user_ticket_model.go
Normal file
32
EdgeAPI/internal/db/models/tickets/user_ticket_model.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package tickets
|
||||
|
||||
// UserTicket 工单
|
||||
type UserTicket struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
CategoryId uint64 `field:"categoryId"` // 分类ID
|
||||
ToAdminId uint64 `field:"toAdminId"` // 指派的管理员ID
|
||||
UserId uint64 `field:"userId"` // 用户ID
|
||||
Subject string `field:"subject"` // 标题
|
||||
Body string `field:"body"` // 内容
|
||||
Status string `field:"status"` // 状态
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
LastLogAt uint64 `field:"lastLogAt"` // 最后日志时间
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type UserTicketOperator struct {
|
||||
Id interface{} // ID
|
||||
CategoryId interface{} // 分类ID
|
||||
ToAdminId interface{} // 指派的管理员ID
|
||||
UserId interface{} // 用户ID
|
||||
Subject interface{} // 标题
|
||||
Body interface{} // 内容
|
||||
Status interface{} // 状态
|
||||
CreatedAt interface{} // 创建时间
|
||||
LastLogAt interface{} // 最后日志时间
|
||||
State interface{} // 状态
|
||||
}
|
||||
|
||||
func NewUserTicketOperator() *UserTicketOperator {
|
||||
return &UserTicketOperator{}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package tickets
|
||||
Reference in New Issue
Block a user