330 lines
9.4 KiB
Go
330 lines
9.4 KiB
Go
|
package bus_service
|
|||
|
|
|||
|
import (
|
|||
|
"context"
|
|||
|
"errors"
|
|||
|
"fmt"
|
|||
|
"github.com/go-admin-team/go-admin-core/sdk/service"
|
|||
|
"go-admin/app/admin/models/bus_models"
|
|||
|
"go-admin/common/global"
|
|||
|
"gorm.io/gorm"
|
|||
|
"math/rand"
|
|||
|
"time"
|
|||
|
)
|
|||
|
|
|||
|
type CooperativeService struct {
|
|||
|
service.Service
|
|||
|
}
|
|||
|
|
|||
|
// GetCooperativeList 获取合作商列表
|
|||
|
func (e *CooperativeService) GetCooperativeList(ctx context.Context, req bus_models.CooperativeListReq) (
|
|||
|
bus_models.CooperativeListResp, error) {
|
|||
|
var cooperativeList []bus_models.BusCooperative
|
|||
|
var resp bus_models.CooperativeListResp
|
|||
|
page := req.Page - 1
|
|||
|
if page < 0 {
|
|||
|
page = 0
|
|||
|
}
|
|||
|
if req.PageSize == 0 {
|
|||
|
req.PageSize = 10
|
|||
|
}
|
|||
|
|
|||
|
// 构建查询条件,假设我们支持根据 product_code 查询
|
|||
|
db := e.Orm.WithContext(ctx)
|
|||
|
|
|||
|
// 如果需要支持分页,可以根据请求条件调整查询
|
|||
|
if req.CooperativeNumber != "" {
|
|||
|
db = db.Where("cooperative_number = ?", req.CooperativeNumber)
|
|||
|
}
|
|||
|
if req.CooperativeName != "" {
|
|||
|
db = db.Where("cooperative_name LIKE ?", "%"+req.CooperativeName+"%")
|
|||
|
}
|
|||
|
if req.Status != 0 {
|
|||
|
db = db.Where("status = ?", req.Status)
|
|||
|
}
|
|||
|
|
|||
|
// 查询数据库
|
|||
|
if err := db.Offset(page * req.Page).Limit(req.PageSize).Find(&cooperativeList).Error; err != nil {
|
|||
|
return resp, errors.New("查询合作商列表失败")
|
|||
|
}
|
|||
|
|
|||
|
resp.List = cooperativeList
|
|||
|
resp.Total = len(cooperativeList)
|
|||
|
resp.Page = page + 1
|
|||
|
resp.PageSize = req.PageSize
|
|||
|
|
|||
|
return resp, nil
|
|||
|
}
|
|||
|
|
|||
|
// CreateCooperative 新建合作商(使用事务)
|
|||
|
func (e *CooperativeService) CreateCooperative(req bus_models.CreateCooperativeReq) (bus_models.CreateCooperativeResp, error) {
|
|||
|
var resp bus_models.CreateCooperativeResp
|
|||
|
|
|||
|
// 开启事务
|
|||
|
tx := e.Orm.Begin()
|
|||
|
defer func() {
|
|||
|
if r := recover(); r != nil {
|
|||
|
tx.Rollback()
|
|||
|
}
|
|||
|
}()
|
|||
|
|
|||
|
// 创建合作商
|
|||
|
cooperative := bus_models.BusCooperative{
|
|||
|
CooperativeName: req.CooperativeName,
|
|||
|
Contact: req.Contact,
|
|||
|
Tel: req.Tel,
|
|||
|
Status: global.CooperativeStatusOnUse,
|
|||
|
Account: req.Account,
|
|||
|
Password: req.Password,
|
|||
|
Balance: req.Balance,
|
|||
|
Free: req.Free,
|
|||
|
Bond: req.Bond,
|
|||
|
CardHolder: req.CardHolder,
|
|||
|
Bank: req.Bank,
|
|||
|
CardID: req.CardID,
|
|||
|
TaxID: req.TaxID,
|
|||
|
}
|
|||
|
var err error
|
|||
|
cooperative.CooperativeNumber, err = GenerateCooperativeNumber(e.Orm)
|
|||
|
if err != nil {
|
|||
|
return resp, err
|
|||
|
}
|
|||
|
|
|||
|
if err := tx.Create(&cooperative).Error; err != nil {
|
|||
|
tx.Rollback()
|
|||
|
return resp, errors.New("创建合作商失败")
|
|||
|
}
|
|||
|
|
|||
|
// 插入合作商产品信息
|
|||
|
var cooperativeProducts []bus_models.BusCooperativeProduct
|
|||
|
for _, product := range req.Products {
|
|||
|
cooperativeProducts = append(cooperativeProducts, bus_models.BusCooperativeProduct{
|
|||
|
CooperativeID: cooperative.ID,
|
|||
|
ProductID: product.ProductID,
|
|||
|
Discount: product.Discount,
|
|||
|
})
|
|||
|
}
|
|||
|
if len(cooperativeProducts) > 0 {
|
|||
|
if err := tx.Create(&cooperativeProducts).Error; err != nil {
|
|||
|
tx.Rollback()
|
|||
|
return resp, errors.New("创建合作商产品失败")
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 提交事务
|
|||
|
if err := tx.Commit().Error; err != nil {
|
|||
|
return resp, errors.New("提交事务失败")
|
|||
|
}
|
|||
|
|
|||
|
resp.CooperativeNumber = cooperative.CooperativeNumber
|
|||
|
return resp, nil
|
|||
|
}
|
|||
|
|
|||
|
func (e *CooperativeService) EditCooperative(req bus_models.EditCooperativeReq) error {
|
|||
|
// 开启事务
|
|||
|
tx := e.Orm.Begin()
|
|||
|
defer func() {
|
|||
|
if r := recover(); r != nil {
|
|||
|
tx.Rollback()
|
|||
|
}
|
|||
|
}()
|
|||
|
|
|||
|
// 查找合作商
|
|||
|
var cooperative bus_models.BusCooperative
|
|||
|
if err := tx.Where("cooperative_number = ?", req.CooperativeNumber).First(&cooperative).Error; err != nil {
|
|||
|
tx.Rollback()
|
|||
|
return errors.New("合作商不存在")
|
|||
|
}
|
|||
|
|
|||
|
// 更新合作商信息
|
|||
|
updateData := map[string]interface{}{
|
|||
|
"cooperative_name": req.CooperativeName,
|
|||
|
"contact": req.Contact,
|
|||
|
"tel": req.Tel,
|
|||
|
"status": req.Status,
|
|||
|
"account": req.Account,
|
|||
|
"balance": req.Balance,
|
|||
|
"free": req.Free,
|
|||
|
"bond": req.Bond,
|
|||
|
"card_holder": req.CardHolder,
|
|||
|
"bank": req.Bank,
|
|||
|
"card_id": req.CardID,
|
|||
|
"tax_id": req.TaxID,
|
|||
|
}
|
|||
|
|
|||
|
if req.Password != "" {
|
|||
|
updateData["password"] = req.Password
|
|||
|
}
|
|||
|
|
|||
|
if err := tx.Model(&bus_models.BusCooperative{}).Where("cooperative_number = ?", req.CooperativeNumber).
|
|||
|
Updates(updateData).Error; err != nil {
|
|||
|
tx.Rollback()
|
|||
|
return errors.New("更新合作商信息失败")
|
|||
|
}
|
|||
|
|
|||
|
// 获取数据库中的旧产品数据
|
|||
|
var existingProducts []bus_models.BusCooperativeProduct
|
|||
|
tx.Where("cooperative_id = ?", cooperative.ID).Find(&existingProducts)
|
|||
|
|
|||
|
// 旧数据转换为 map (ProductID => Discount)
|
|||
|
existingMap := make(map[uint64]float64)
|
|||
|
for _, p := range existingProducts {
|
|||
|
existingMap[p.ProductID] = p.Discount
|
|||
|
}
|
|||
|
|
|||
|
// 计算需要更新、删除、插入的数据
|
|||
|
newProductMap := make(map[uint64]float64) // (ProductID => Discount)
|
|||
|
var newProducts []bus_models.BusCooperativeProduct
|
|||
|
for _, product := range req.Products {
|
|||
|
newProductMap[product.ProductID] = product.Discount
|
|||
|
|
|||
|
// 如果是新产品,或者折扣变化,则需要插入/更新
|
|||
|
if discount, exists := existingMap[product.ProductID]; !exists || discount != product.Discount {
|
|||
|
newProducts = append(newProducts, bus_models.BusCooperativeProduct{
|
|||
|
CooperativeID: cooperative.ID,
|
|||
|
ProductID: product.ProductID,
|
|||
|
Discount: product.Discount,
|
|||
|
})
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 删除不存在于新数据中的旧产品
|
|||
|
var productIDsToDelete []uint64
|
|||
|
for oldProductID := range existingMap {
|
|||
|
if _, exists := newProductMap[oldProductID]; !exists {
|
|||
|
productIDsToDelete = append(productIDsToDelete, oldProductID)
|
|||
|
}
|
|||
|
}
|
|||
|
if len(productIDsToDelete) > 0 {
|
|||
|
tx.Where("cooperative_id = ? AND product_id IN ?", cooperative.ID, productIDsToDelete).Delete(&bus_models.BusCooperativeProduct{})
|
|||
|
}
|
|||
|
|
|||
|
// 插入新的或有折扣变化的产品
|
|||
|
if len(newProducts) > 0 {
|
|||
|
if err := tx.Create(&newProducts).Error; err != nil {
|
|||
|
tx.Rollback()
|
|||
|
return errors.New("添加/更新合作商产品信息失败")
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 提交事务
|
|||
|
if err := tx.Commit().Error; err != nil {
|
|||
|
return errors.New("提交事务失败")
|
|||
|
}
|
|||
|
|
|||
|
return nil
|
|||
|
}
|
|||
|
|
|||
|
// DeleteCooperative 删除合作商
|
|||
|
func (e *CooperativeService) DeleteCooperative(req bus_models.DeleteCooperativeReq) error {
|
|||
|
// 执行删除操作
|
|||
|
if err := e.Orm.Where("cooperative_number = ?", req.CooperativeNumber).
|
|||
|
Delete(&bus_models.BusCooperative{}).Error; err != nil {
|
|||
|
return errors.New("删除产品失败")
|
|||
|
}
|
|||
|
|
|||
|
return nil
|
|||
|
}
|
|||
|
|
|||
|
// GetCooperativeDetail 获取合作商详细信息,包括产品信息
|
|||
|
func (e *CooperativeService) GetCooperativeDetail(ctx context.Context, req bus_models.CooperativeDetailReq) (
|
|||
|
bus_models.CooperativeDetailResp, error) {
|
|||
|
var resp bus_models.CooperativeDetailResp
|
|||
|
var cooperative bus_models.BusCooperative
|
|||
|
|
|||
|
// 查询合作商基本信息
|
|||
|
db := e.Orm.WithContext(ctx).Where("id = ?", req.CooperativeID)
|
|||
|
|
|||
|
if err := db.First(&cooperative).Error; err != nil {
|
|||
|
return resp, errors.New("查询合作商详情失败")
|
|||
|
}
|
|||
|
|
|||
|
// 查询该合作商的产品信息
|
|||
|
var cooperativeProducts []bus_models.BusCooperativeProduct
|
|||
|
err := e.Orm.WithContext(ctx).
|
|||
|
Where("cooperative_id = ?", req.CooperativeID).
|
|||
|
Find(&cooperativeProducts).Error
|
|||
|
|
|||
|
if err != nil {
|
|||
|
return resp, errors.New("查询合作商产品信息失败")
|
|||
|
}
|
|||
|
|
|||
|
// 根据产品ID查询产品名称和编码
|
|||
|
var productIDs []uint64
|
|||
|
for _, cp := range cooperativeProducts {
|
|||
|
productIDs = append(productIDs, cp.ProductID)
|
|||
|
}
|
|||
|
|
|||
|
// 查询产品详细信息
|
|||
|
var products []bus_models.BusProduct
|
|||
|
if len(productIDs) > 0 {
|
|||
|
err = e.Orm.WithContext(ctx).
|
|||
|
Where("id IN (?)", productIDs).
|
|||
|
Find(&products).Error
|
|||
|
|
|||
|
if err != nil {
|
|||
|
return resp, errors.New("查询产品信息失败")
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 将产品信息填充到响应中
|
|||
|
var productDetails []bus_models.ProductDetail
|
|||
|
for _, cp := range cooperativeProducts {
|
|||
|
// 查找对应产品
|
|||
|
for _, p := range products {
|
|||
|
if p.ID == cp.ProductID { // 确保ID匹配
|
|||
|
productDetails = append(productDetails, bus_models.ProductDetail{
|
|||
|
ProductCode: p.ProductCode,
|
|||
|
ProductName: p.ProductName,
|
|||
|
Discount: cp.Discount,
|
|||
|
})
|
|||
|
break
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 填充合作商信息
|
|||
|
resp.CooperativeNumber = cooperative.CooperativeNumber
|
|||
|
resp.CooperativeName = cooperative.CooperativeName
|
|||
|
resp.Contact = cooperative.Contact
|
|||
|
resp.Tel = cooperative.Tel
|
|||
|
resp.Status = cooperative.Status
|
|||
|
resp.Account = cooperative.Account
|
|||
|
resp.Balance = cooperative.Balance
|
|||
|
resp.Free = cooperative.Free
|
|||
|
resp.Bond = cooperative.Bond
|
|||
|
resp.Products = productDetails
|
|||
|
|
|||
|
return resp, nil
|
|||
|
}
|
|||
|
|
|||
|
// GenerateCooperativeNumber 生成唯一的合作商编号,日期 + 10位随机数
|
|||
|
func GenerateCooperativeNumber(db *gorm.DB) (string, error) {
|
|||
|
// 获取当前日期(年月日)
|
|||
|
dateStr := time.Now().Format("20060102") // 格式为:YYYYMMDD
|
|||
|
|
|||
|
// 生成一个10位随机数
|
|||
|
rand.Seed(time.Now().UnixNano()) // 设置随机数种子
|
|||
|
randomNum := rand.Int63n(10000000000) // 生成一个0到9999999999之间的随机数
|
|||
|
|
|||
|
// 将随机数转为固定长度的字符串,确保10位
|
|||
|
randomNumStr := fmt.Sprintf("%010d", randomNum) // 确保为10位,不足补零
|
|||
|
|
|||
|
// 拼接日期和随机数
|
|||
|
cooperativeNumber := fmt.Sprintf("%s%s", dateStr, randomNumStr)
|
|||
|
|
|||
|
// 检查生成的合作商编号是否已存在
|
|||
|
var count int64
|
|||
|
err := db.Model(&bus_models.BusCooperative{}).Where("cooperative_number = ?", cooperativeNumber).Count(&count).Error
|
|||
|
if err != nil {
|
|||
|
return "", err
|
|||
|
}
|
|||
|
|
|||
|
// 如果已存在,重新生成
|
|||
|
if count > 0 {
|
|||
|
return GenerateCooperativeNumber(db) // 递归重新生成
|
|||
|
}
|
|||
|
|
|||
|
return cooperativeNumber, nil
|
|||
|
}
|