163 lines
4.0 KiB
Go
163 lines
4.0 KiB
Go
package manage
|
||
|
||
import (
|
||
model2 "ArmedPolice/app/common/model"
|
||
"ArmedPolice/app/controller/basic"
|
||
"ArmedPolice/app/model"
|
||
"ArmedPolice/app/service"
|
||
"errors"
|
||
"time"
|
||
)
|
||
|
||
type Supplier struct{ *service.Session }
|
||
|
||
type SupplierHandle func(session *service.Session) *Supplier
|
||
|
||
type (
|
||
// SupplierBasic 基本 信息
|
||
SupplierBasic struct {
|
||
ID string `json:"id"`
|
||
Name string `json:"name"`
|
||
}
|
||
// SupplierInfo 基本信息
|
||
SupplierInfo struct {
|
||
ID string `json:"id"`
|
||
*model2.ManageSupplier
|
||
}
|
||
// SupplierParams 参数信息
|
||
SupplierParams struct {
|
||
ID uint64
|
||
Name, Contacts, Mobile, Address, Remark string
|
||
Kind model2.ManageSupplierKind
|
||
}
|
||
)
|
||
|
||
// List 列表信息
|
||
func (c *Supplier) List(name, mobile string, kind model2.ManageSupplierKind, page, pageSize int) (*basic.PageDataResponse, error) {
|
||
mManageSupplier := model.NewManageSupplier()
|
||
|
||
out := make([]*model2.ManageSupplier, 0)
|
||
|
||
where := []*model2.ModelWhereOrder{&model2.ModelWhereOrder{
|
||
Where: nil,
|
||
Order: model2.NewOrder("id", model2.OrderModeToDesc),
|
||
}}
|
||
if c.TenantID > 0 {
|
||
// TODO:因不支持此写法,故删除此写法
|
||
// 根据单位由下往上查看信息
|
||
//tenantInfo, err := model.NewSysTenant().Parent(c.TenantID)
|
||
//
|
||
//if err != nil {
|
||
// return nil, err
|
||
//}
|
||
//tenantIDs := make([]uint64, 0)
|
||
//
|
||
//for _, v := range tenantInfo {
|
||
// tenantIDs = append(tenantIDs, v.ID)
|
||
//}
|
||
//where = append(where, &model2.ModelWhereOrder{
|
||
// Where: model2.NewWhereIn("tenant_id", tenantIDs),
|
||
//})
|
||
}
|
||
if name != "" {
|
||
where = append(where, &model2.ModelWhereOrder{
|
||
Where: model2.NewWhereLike("name", name),
|
||
})
|
||
}
|
||
if mobile != "" {
|
||
where = append(where, &model2.ModelWhereOrder{
|
||
Where: model2.NewWhereLike("mobile", mobile),
|
||
})
|
||
}
|
||
if kind > 0 {
|
||
where = append(where, &model2.ModelWhereOrder{
|
||
Where: model2.NewWhere("kind", kind),
|
||
})
|
||
}
|
||
var count int64
|
||
|
||
if err := model2.Pages(mManageSupplier.ManageSupplier, &out, page, pageSize, &count, where...); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
list := make([]*SupplierInfo, 0)
|
||
|
||
for _, v := range out {
|
||
list = append(list, &SupplierInfo{
|
||
ID: v.GetEncodeID(), ManageSupplier: v,
|
||
})
|
||
}
|
||
return &basic.PageDataResponse{Data: list, Count: count}, nil
|
||
}
|
||
|
||
// Select 筛选信息
|
||
func (c *Supplier) Select(kind int) ([]*SupplierBasic, error) {
|
||
out := make([]*model2.ManageSupplier, 0)
|
||
|
||
if err := model2.ScanFields(model.NewManageSupplier().ManageSupplier, &out, []string{"id", "name"},
|
||
&model2.ModelWhereOrder{
|
||
Where: model2.NewWhere("kind", kind),
|
||
}, &model2.ModelWhereOrder{
|
||
Where: model2.NewWhere("tenant_id", c.TenantID),
|
||
}); err != nil {
|
||
return nil, err
|
||
}
|
||
list := make([]*SupplierBasic, 0)
|
||
|
||
for _, v := range out {
|
||
list = append(list, &SupplierBasic{
|
||
ID: v.GetEncodeID(),
|
||
Name: v.Name,
|
||
})
|
||
}
|
||
return list, nil
|
||
}
|
||
|
||
// Form 数据处理
|
||
func (c *Supplier) Form(params *SupplierParams) error {
|
||
mManageSupplier := model.NewManageSupplier()
|
||
|
||
if params.ID > 0 {
|
||
mManageSupplier.ID = params.ID
|
||
|
||
isExist, err := model2.FirstWhere(mManageSupplier.ManageSupplier)
|
||
|
||
if err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
return errors.New("操作错误,供应商信息不存在")
|
||
}
|
||
}
|
||
mManageSupplier.Name = params.Name
|
||
mManageSupplier.Mobile = params.Mobile
|
||
mManageSupplier.Contacts = params.Contacts
|
||
mManageSupplier.Address = params.Address
|
||
mManageSupplier.Remark = params.Remark
|
||
|
||
if mManageSupplier.ID > 0 {
|
||
mManageSupplier.UpdatedAt = time.Now()
|
||
|
||
return model2.Updates(mManageSupplier.ManageSupplier, mManageSupplier.ManageSupplier)
|
||
}
|
||
mManageSupplier.TenantID = c.TenantID
|
||
mManageSupplier.Kind = params.Kind
|
||
return model2.Create(mManageSupplier.ManageSupplier)
|
||
}
|
||
|
||
// Delete 删除操作
|
||
func (c *Supplier) Delete(id uint64) error {
|
||
mManageSupplier := model.NewManageSupplier()
|
||
mManageSupplier.ID = id
|
||
|
||
if err := model2.Delete(mManageSupplier.ManageSupplier); err != nil {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func NewSupplier() SupplierHandle {
|
||
return func(session *service.Session) *Supplier {
|
||
return &Supplier{session}
|
||
}
|
||
}
|