117 lines
2.6 KiB
Go
117 lines
2.6 KiB
Go
package services
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
|
|
"server/models"
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
)
|
|
|
|
// ListSuppliers 获取供应商列表(可选租户过滤、关键词搜索、状态筛选、分页)
|
|
func ListSuppliers(tenantId, keyword, status string, page, pageSize int) (list []*models.Supplier, total int64, err error) {
|
|
o := orm.NewOrm()
|
|
qs := o.QueryTable(new(models.Supplier)).Filter("delete_time__isnull", true)
|
|
if tenantId != "" {
|
|
qs = qs.Filter("tenant_id", tenantId)
|
|
}
|
|
if keyword != "" {
|
|
cond := orm.NewCondition()
|
|
cond1 := cond.Or("supplier_name__icontains", keyword).
|
|
Or("contact_person__icontains", keyword).
|
|
Or("contact_phone__icontains", keyword)
|
|
qs = qs.SetCond(cond1)
|
|
}
|
|
if status != "" {
|
|
qs = qs.Filter("status", status)
|
|
}
|
|
|
|
total, err = qs.Count()
|
|
if err != nil {
|
|
return
|
|
}
|
|
if page <= 0 {
|
|
page = 1
|
|
}
|
|
if pageSize <= 0 {
|
|
pageSize = 10
|
|
}
|
|
offset := (page - 1) * pageSize
|
|
_, err = qs.OrderBy("-create_time").Limit(pageSize, offset).All(&list)
|
|
return
|
|
}
|
|
|
|
// GetSupplier 通过ID获取供应商
|
|
func GetSupplier(id int64) (*models.Supplier, error) {
|
|
o := orm.NewOrm()
|
|
m := models.Supplier{Id: id}
|
|
if err := o.Read(&m); err != nil {
|
|
return nil, err
|
|
}
|
|
return &m, nil
|
|
}
|
|
|
|
// CreateSupplier 新增供应商
|
|
func CreateSupplier(m *models.Supplier) error {
|
|
o := orm.NewOrm()
|
|
_, err := o.Insert(m)
|
|
return err
|
|
}
|
|
|
|
// UpdateSupplier 更新供应商(全量更新)
|
|
func UpdateSupplier(m *models.Supplier, cols ...string) error {
|
|
o := orm.NewOrm()
|
|
if len(cols) == 0 {
|
|
_, err := o.Update(m)
|
|
return err
|
|
}
|
|
_, err := o.Update(m, cols...)
|
|
return err
|
|
}
|
|
|
|
// SoftDeleteSupplier 软删除供应商
|
|
func SoftDeleteSupplier(id int64) error {
|
|
o := orm.NewOrm()
|
|
now := time.Now()
|
|
m := models.Supplier{Id: id, DeleteTime: &now}
|
|
_, err := o.Update(&m, "delete_time")
|
|
return err
|
|
}
|
|
|
|
// 更新供应商开票信息
|
|
func UpdateSupplierInvoice(params orm.Params) error {
|
|
o := orm.NewOrm()
|
|
// Extract id from params
|
|
idVal, ok := params["id"]
|
|
if !ok || idVal == "" {
|
|
return fmt.Errorf("id is required")
|
|
}
|
|
var id int64
|
|
switch v := idVal.(type) {
|
|
case int64:
|
|
id = v
|
|
case int:
|
|
id = int64(v)
|
|
case float64:
|
|
id = int64(v)
|
|
case string:
|
|
if v == "" {
|
|
return fmt.Errorf("id is required")
|
|
}
|
|
n, err := strconv.ParseInt(v, 10, 64)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid id: %v", err)
|
|
}
|
|
id = n
|
|
default:
|
|
return fmt.Errorf("invalid id type")
|
|
}
|
|
delete(params, "id") // remove id from update fields
|
|
if _, err := o.QueryTable(new(models.Supplier)).Filter("id", id).Update(params); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|