feat:完善项目
This commit is contained in:
156
app/controller/manage/equipment.go
Normal file
156
app/controller/manage/equipment.go
Normal file
@ -0,0 +1,156 @@
|
||||
package manage
|
||||
|
||||
import (
|
||||
model2 "ArmedPolice/app/common/model"
|
||||
"ArmedPolice/app/controller/basic"
|
||||
"ArmedPolice/app/model"
|
||||
"ArmedPolice/app/service"
|
||||
"ArmedPolice/config"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Equipment struct{ *service.Session }
|
||||
|
||||
type EquipmentHandle func(session *service.Session) *Equipment
|
||||
|
||||
type (
|
||||
// EquipmentInfo 装备信息
|
||||
EquipmentInfo struct {
|
||||
basic.CommonIDString
|
||||
Title string `json:"title"`
|
||||
Image string `json:"image"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
// EquipmentDetail 装备详细信息
|
||||
EquipmentDetail struct {
|
||||
basic.CommonIDString
|
||||
*model2.ManageEquipment
|
||||
Image string `json:"image"`
|
||||
}
|
||||
// EquipmentParams 装备参数信息
|
||||
EquipmentParams struct {
|
||||
ID uint64
|
||||
Title, Image, Config, Remark string
|
||||
}
|
||||
)
|
||||
|
||||
func (c *EquipmentParams) isExist(iModel model2.IModel) (bool, error) {
|
||||
var count int64
|
||||
|
||||
if err := model2.Count(iModel, &count, model2.NewWhere("title", c.Title)); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
// List 列表信息
|
||||
func (c *Equipment) List(title string, page, pageSize int) (*basic.PageDataResponse, error) {
|
||||
mManageEquipment := model.NewManageEquipment()
|
||||
|
||||
out := make([]*model2.ManageEquipment, 0)
|
||||
|
||||
where := []*model2.ModelWhereOrder{
|
||||
&model2.ModelWhereOrder{
|
||||
Order: model2.NewOrder("id", model2.OrderModeToDesc),
|
||||
},
|
||||
}
|
||||
if title != "" {
|
||||
where = append(where, &model2.ModelWhereOrder{
|
||||
Where: model2.NewWhereLike("title", title),
|
||||
})
|
||||
}
|
||||
var count int64
|
||||
|
||||
if err := model2.PagesFields(mManageEquipment.ManageEquipment, &out, []string{"id", "title", "image", "created_at"},
|
||||
page, pageSize, &count, where...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
list := make([]*EquipmentInfo, 0)
|
||||
|
||||
for _, v := range out {
|
||||
list = append(list, &EquipmentInfo{
|
||||
CommonIDString: basic.CommonIDString{ID: v.GetEncodeID()},
|
||||
Title: v.Title,
|
||||
Image: v.Image.Analysis(config.SettingInfo.Domain),
|
||||
CreatedAt: v.CreatedAt,
|
||||
})
|
||||
}
|
||||
return &basic.PageDataResponse{Data: list, Count: count}, nil
|
||||
}
|
||||
|
||||
// Detail 详细信息
|
||||
func (c *Equipment) Detail(id uint64) (*EquipmentDetail, error) {
|
||||
mManageEquipment := model.NewManageEquipment()
|
||||
mManageEquipment.ID = id
|
||||
|
||||
if isExist, err := model2.FirstWhere(mManageEquipment.ManageEquipment); err != nil {
|
||||
return nil, err
|
||||
} else if !isExist {
|
||||
return nil, errors.New("")
|
||||
}
|
||||
return &EquipmentDetail{
|
||||
CommonIDString: basic.CommonIDString{ID: mManageEquipment.GetEncodeID()},
|
||||
ManageEquipment: mManageEquipment.ManageEquipment,
|
||||
Image: mManageEquipment.Analysis(config.SettingInfo.Domain),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Form 数据处理
|
||||
func (c *Equipment) Form(params *EquipmentParams) error {
|
||||
mManageEquipment := model.NewManageEquipment()
|
||||
|
||||
if params.ID > 0 {
|
||||
mManageEquipment.ID = params.ID
|
||||
|
||||
isExist, err := model2.FirstWhere(mManageEquipment.ManageEquipment)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !isExist {
|
||||
return errors.New("操作错误,装备信息不存在")
|
||||
}
|
||||
|
||||
if params.Title != mManageEquipment.Title {
|
||||
if isExist, err = params.isExist(mManageEquipment.ManageEquipment); err != nil {
|
||||
return err
|
||||
} else if isExist {
|
||||
return errors.New("操作错误,已存在相应的装备信息")
|
||||
}
|
||||
}
|
||||
}
|
||||
mManageEquipment.Title = params.Title
|
||||
mManageEquipment.Image.Image = params.Image
|
||||
mManageEquipment.Config = params.Config
|
||||
mManageEquipment.Remark = params.Remark
|
||||
|
||||
if mManageEquipment.ID > 0 {
|
||||
mManageEquipment.UpdatedAt = time.Now()
|
||||
return model2.Updates(mManageEquipment.ManageEquipment, mManageEquipment.ManageEquipment)
|
||||
}
|
||||
// 查询装备信息是否存在
|
||||
if isExist, err := params.isExist(mManageEquipment.ManageEquipment); err != nil {
|
||||
return err
|
||||
} else if isExist {
|
||||
return errors.New("操作错误,已存在相应的装备信息")
|
||||
}
|
||||
mManageEquipment.TenantID = c.TenantID
|
||||
return model2.Create(mManageEquipment.ManageEquipment)
|
||||
}
|
||||
|
||||
// Delete 删除操作
|
||||
func (c *Equipment) Delete(id uint64) error {
|
||||
mManageEquipment := model.NewManageEquipment()
|
||||
mManageEquipment.ID = id
|
||||
|
||||
if err := model2.Delete(mManageEquipment.ManageEquipment); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewEquipment() EquipmentHandle {
|
||||
return func(session *service.Session) *Equipment {
|
||||
return &Equipment{session}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user