173 lines
4.6 KiB
Go
173 lines
4.6 KiB
Go
package service
|
|
|
|
import (
|
|
"assets/consts/procurement"
|
|
"assets/dao/procurement"
|
|
"assets/model/dto/procurement"
|
|
"assets/model/entity/procurement"
|
|
"context"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
)
|
|
|
|
type supplier struct{}
|
|
|
|
// Supplier 供应商服务
|
|
var Supplier = new(supplier)
|
|
|
|
// CreateSupplier 创建供应商
|
|
func (s *supplier) CreateSupplier(ctx context.Context, req *dto.CreateSupplierReq) (*dto.CreateSupplierRes, error) {
|
|
// 转换为实体
|
|
_ = &entity.Supplier{
|
|
Name: req.Name,
|
|
Code: req.Code,
|
|
Phone: req.Phone,
|
|
Mobile: req.Mobile,
|
|
Email: req.Email,
|
|
Address: req.Address,
|
|
Status: consts.SupplierStatus(1), // 默认活跃状态
|
|
}
|
|
|
|
// 保存到数据库
|
|
ids, err := dao.Supplier.Insert(ctx, req)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "创建供应商失败")
|
|
}
|
|
|
|
var id *bson.ObjectID
|
|
if len(ids) > 0 {
|
|
if objectID, ok := ids[0].(bson.ObjectID); ok {
|
|
id = &objectID
|
|
}
|
|
}
|
|
|
|
return &dto.CreateSupplierRes{ID: id}, nil
|
|
}
|
|
|
|
// BatchCreateSuppliers 批量创建供应商
|
|
func (s *supplier) BatchCreateSuppliers(ctx context.Context, req *dto.BatchCreateSuppliersReq) (*dto.BatchCreateSuppliersRes, error) {
|
|
// 保存到数据库
|
|
ids, err := dao.Supplier.BatchInsert(ctx, req)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "批量创建供应商失败")
|
|
}
|
|
|
|
// 转换ID列表
|
|
idList := make([]*bson.ObjectID, 0, len(ids))
|
|
for _, id := range ids {
|
|
if objectID, ok := id.(bson.ObjectID); ok {
|
|
idList = append(idList, &objectID)
|
|
}
|
|
}
|
|
|
|
return &dto.BatchCreateSuppliersRes{IDs: idList}, nil
|
|
}
|
|
|
|
// UpdateSupplier 更新供应商
|
|
func (s *supplier) UpdateSupplier(ctx context.Context, req *dto.UpdateSupplierReq) error {
|
|
// 更新到数据库
|
|
err := dao.Supplier.Update(ctx, req)
|
|
if err != nil {
|
|
return gerror.Wrap(err, "更新供应商失败")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// DeleteSupplier 删除供应商
|
|
func (s *supplier) DeleteSupplier(ctx context.Context, id *bson.ObjectID) error {
|
|
return dao.Supplier.DeleteFake(ctx, id)
|
|
}
|
|
|
|
// GetSupplier 获取供应商详情
|
|
func (s *supplier) GetSupplier(ctx context.Context, id *bson.ObjectID) (*dto.GetSupplierRes, error) {
|
|
supplier, err := dao.Supplier.GetOne(ctx, id)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "获取供应商失败")
|
|
}
|
|
|
|
// 转换为响应
|
|
res := &dto.GetSupplierRes{
|
|
ID: supplier.Id,
|
|
Name: supplier.Name,
|
|
Code: supplier.Code,
|
|
Phone: supplier.Phone,
|
|
Mobile: supplier.Mobile,
|
|
Email: supplier.Email,
|
|
Website: supplier.Website,
|
|
Address: supplier.Address,
|
|
Status: supplier.Status,
|
|
StatusText: consts.GetSupplierStatusText(supplier.Status),
|
|
Rating: supplier.Rating,
|
|
CreatedAt: supplier.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
UpdatedAt: supplier.UpdatedAt.Format("2006-01-02 15:04:05"),
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// ListSuppliers 获取供应商列表
|
|
func (s *supplier) ListSuppliers(ctx context.Context, req *dto.ListSuppliersReq) (*dto.ListSuppliersRes, error) {
|
|
// 获取数据
|
|
suppliers, total, err := dao.Supplier.List(ctx, req)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "获取供应商列表失败")
|
|
}
|
|
|
|
// 转换为响应
|
|
listItems := make([]*dto.SupplierListItem, 0, len(suppliers))
|
|
for _, supplier := range suppliers {
|
|
listItems = append(listItems, &dto.SupplierListItem{
|
|
ID: supplier.Id,
|
|
Name: supplier.Name,
|
|
Code: supplier.Code,
|
|
Phone: supplier.Phone,
|
|
Mobile: supplier.Mobile,
|
|
Email: supplier.Email,
|
|
Address: supplier.Address,
|
|
Status: supplier.Status,
|
|
StatusText: consts.GetSupplierStatusText(supplier.Status),
|
|
Rating: supplier.Rating,
|
|
CreatedAt: supplier.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
UpdatedAt: supplier.UpdatedAt.Format("2006-01-02 15:04:05"),
|
|
})
|
|
}
|
|
|
|
return &dto.ListSuppliersRes{
|
|
List: listItems,
|
|
Total: total,
|
|
}, nil
|
|
}
|
|
|
|
// GetSupplierOptions 获取供应商选项(用于下拉选择)
|
|
func (s *supplier) GetSupplierOptions(ctx context.Context) ([]*dto.SupplierListItem, error) {
|
|
suppliers, err := dao.Supplier.FindActiveSuppliers(ctx)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "获取供应商选项失败")
|
|
}
|
|
|
|
options := make([]*dto.SupplierListItem, 0, len(suppliers))
|
|
for _, supplier := range suppliers {
|
|
options = append(options, &dto.SupplierListItem{
|
|
ID: supplier.Id,
|
|
Name: supplier.Name,
|
|
Code: supplier.Code,
|
|
})
|
|
}
|
|
|
|
return options, nil
|
|
}
|
|
|
|
// GenerateTestData 生成测试数据
|
|
func (s *supplier) GenerateTestData(ctx context.Context) error {
|
|
testData := &dto.BatchCreateSuppliersReq{}
|
|
|
|
_, err := s.BatchCreateSuppliers(ctx, testData)
|
|
if err != nil {
|
|
return gerror.Wrap(err, "生成测试数据失败")
|
|
}
|
|
|
|
return nil
|
|
}
|