111 lines
3.1 KiB
Go
111 lines
3.1 KiB
Go
package dao
|
|
|
|
import (
|
|
"assets/consts/public"
|
|
dto "assets/model/dto/procurement"
|
|
entity "assets/model/entity/procurement"
|
|
"context"
|
|
|
|
"gitea.com/red-future/common/db/mongo"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
)
|
|
|
|
var Supplier = new(supplier)
|
|
|
|
type supplier struct{}
|
|
|
|
// Insert 插入供应商
|
|
func (d *supplier) Insert(ctx context.Context, req *dto.CreateSupplierReq) (ids []any, err error) {
|
|
var result *entity.Supplier
|
|
if err = gconv.Struct(req, &result); err != nil {
|
|
return
|
|
}
|
|
ids, err = mongo.DB().Insert(ctx, []interface{}{&result}, public.SupplierCollection)
|
|
return
|
|
}
|
|
|
|
// BatchInsert 批量插入供应商
|
|
func (d *supplier) BatchInsert(ctx context.Context, req *dto.BatchCreateSuppliersReq) (ids []any, err error) {
|
|
items := make([]*entity.Supplier, 0, len(req.Suppliers))
|
|
for _, item := range req.Suppliers {
|
|
var result *entity.Supplier
|
|
if err = gconv.Struct(item, &result); err != nil {
|
|
return
|
|
}
|
|
items = append(items, result)
|
|
}
|
|
// 转换为 interface{} 切片
|
|
interfaces := make([]interface{}, len(items))
|
|
for i, item := range items {
|
|
interfaces[i] = item
|
|
}
|
|
ids, err = mongo.DB().Insert(ctx, interfaces, public.SupplierCollection)
|
|
return
|
|
}
|
|
|
|
// GetOne 获取单个供应商
|
|
func (d *supplier) GetOne(ctx context.Context, id *bson.ObjectID) (supplier *entity.Supplier, err error) {
|
|
filter := bson.M{"_id": id}
|
|
err = mongo.DB().FindOne(ctx, filter, &supplier, public.SupplierCollection)
|
|
return
|
|
}
|
|
|
|
// Update 更新供应商
|
|
func (d *supplier) Update(ctx context.Context, req *dto.UpdateSupplierReq) (err error) {
|
|
buildUpdateData, err := mongo.BuildUpdateData(ctx, req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
filter := bson.M{"_id": req.ID}
|
|
update := bson.M{"$set": buildUpdateData}
|
|
_, err = mongo.DB().Update(ctx, filter, update, public.SupplierCollection)
|
|
return
|
|
}
|
|
|
|
// DeleteFake 删除供应商-根据id进行假删
|
|
func (d *supplier) DeleteFake(ctx context.Context, id *bson.ObjectID) (err error) {
|
|
filter := bson.M{"_id": id}
|
|
_, err = mongo.DB().DeleteSoft(ctx, filter, public.SupplierCollection)
|
|
return
|
|
}
|
|
|
|
// List 获取供应商列表
|
|
func (d *supplier) List(ctx context.Context, req *dto.ListSuppliersReq) (res []*entity.Supplier, total int64, err error) {
|
|
filter, err := d.buildListFilter(ctx, req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
total, err = mongo.DB().Find(ctx, filter, &res, public.SupplierCollection, nil, nil)
|
|
return
|
|
}
|
|
|
|
// FindActiveSuppliers 获取活跃供应商列表
|
|
func (d *supplier) FindActiveSuppliers(ctx context.Context) (res []*entity.Supplier, err error) {
|
|
filter := bson.M{
|
|
"status": 1, // consts.SupplierStatusActive
|
|
"isDeleted": false,
|
|
}
|
|
_, err = mongo.DB().Find(ctx, filter, &res, public.SupplierCollection, nil, nil)
|
|
return
|
|
}
|
|
|
|
// buildListFilter 构建列表查询的过滤条件
|
|
func (d *supplier) buildListFilter(ctx context.Context, req *dto.ListSuppliersReq) (filter bson.M, err error) {
|
|
_ = ctx
|
|
filter = bson.M{}
|
|
|
|
if !g.IsEmpty(req.Name) {
|
|
filter["name"] = bson.M{"$regex": req.Name, "$options": "i"}
|
|
}
|
|
if !g.IsEmpty(req.Code) {
|
|
filter["code"] = req.Code
|
|
}
|
|
if req.Status != nil {
|
|
filter["status"] = *req.Status
|
|
}
|
|
return
|
|
}
|