Merge branch 'feat-hjj-packageManage#A116' into dev

This commit is contained in:
jiaji.H 2025-09-12 10:38:21 +08:00
commit 9830f38ffb
3 changed files with 51 additions and 23 deletions

View File

@ -326,12 +326,11 @@ func UpdateBundleToValueAddService(tx *gorm.DB, records []*model.BundleToValueAd
} }
func DeleteBundleToValueAddService(tx *gorm.DB, bundleUuid, valueUid string) error { func DeleteBundleToValueAddService(tx *gorm.DB, bundleUuid, valueUid string) error {
return tx.Where("bundle_uuid = ? AND value_uid = ? AND deleted_at = 0", bundleUuid, valueUid).Delete(&model.BundleToValueAddService{}).Error return tx.Where("bundle_uuid = ? AND value_uid = ? AND deleted_at = 0", bundleUuid, valueUid).Delete(&model.BundleToValueAddService{}).Error
} }
func GetValueAddServiceUuidsByBundleUuid(bundleUuid string) ([]string, error) { func GetValueAddServiceUuidsByBundleUuid(bundleUuid string) ([]*model.ValueUidWithBenefits, error) {
var uuids []string var result []*model.ValueUidWithBenefits
err := app.ModuleClients.BundleDB.Model(&model.BundleToValueAddService{}).Where("bundle_uuid = ? and deleted_at = 0", bundleUuid).Pluck("value_uid", &uuids).Error err := app.ModuleClients.BundleDB.Model(&model.BundleToValueAddService{}).Select("value_uid, benefits_type").Where("bundle_uuid = ? and deleted_at = 0", bundleUuid).Scan(&result).Error
return uuids, err return result, err
} }
// GetBundleToValueAddServiceByBundleUuid 根据套餐UUID获取所有关联的增值服务uuid // GetBundleToValueAddServiceByBundleUuid 根据套餐UUID获取所有关联的增值服务uuid

View File

@ -3,10 +3,12 @@ package logic
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"micro-bundle/internal/dao" "micro-bundle/internal/dao"
"micro-bundle/pb/bundle" "micro-bundle/pb/bundle"
"micro-bundle/pkg/app" "micro-bundle/pkg/app"
"micro-bundle/pkg/msg" "micro-bundle/pkg/msg"
"strings"
"time" "time"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils" "github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils"
@ -450,42 +452,64 @@ func HandleShelf(req *bundle.HandShelfRequest) (*bundle.CommonResponse, error) {
// 差异更新套餐与增值服务中间表 // 差异更新套餐与增值服务中间表
func diffUpdateBundleToValueAddService(tx *gorm.DB, bundleUuid string, selectService []*model.BundleToValueAddService) error { func diffUpdateBundleToValueAddService(tx *gorm.DB, bundleUuid string, selectService []*model.BundleToValueAddService) error {
oldUuids, err := dao.GetValueAddServiceUuidsByBundleUuid(bundleUuid) // 获取现有的关联记录包含value_uid和benefits_type
oldRecords, err := dao.GetValueAddServiceUuidsByBundleUuid(bundleUuid)
if err != nil { if err != nil {
return errors.New("查询套餐与增值服务关联失败") return errors.New("查询套餐与增值服务关联失败")
} }
newUuids := make(map[string]*model.BundleToValueAddService)
// 构建现有记录的复合键映射 (value_uid + benefits_type)
oldMap := make(map[string]struct{})
for _, record := range oldRecords {
key := fmt.Sprintf("%s_%d", record.ValueUid, record.BenefitsType)
oldMap[key] = struct{}{}
}
// 构建新记录的复合键映射
newMap := make(map[string]*model.BundleToValueAddService)
for _, s := range selectService { for _, s := range selectService {
newUuids[s.ValueUid] = s key := fmt.Sprintf("%s_%d", s.ValueUid, s.BenefitsType)
} s.BundleUuid = bundleUuid
oldSet := make(map[string]struct{}) newMap[key] = s
for _, uid := range oldUuids {
oldSet[uid] = struct{}{}
} }
// 需要新增的 // 需要新增的
toAdd := make([]*model.BundleToValueAddService, 0) toAdd := make([]*model.BundleToValueAddService, 0)
toDel := make([]string, 0) toDel := make([]string, 0)
toUpdate := make([]*model.BundleToValueAddService, 0) toUpdate := make([]*model.BundleToValueAddService, 0)
for uid, s := range newUuids {
if _, exist := oldSet[uid]; !exist { for key, newRecord := range newMap {
s.BundleUuid = bundleUuid if _, exists := oldMap[key]; exists {
toAdd = append(toAdd, s) // 记录存在,需要更新
toUpdate = append(toUpdate, newRecord)
} else { } else {
s.BundleUuid = bundleUuid // 记录不存在,需要新增
toUpdate = append(toUpdate, s) toAdd = append(toAdd, newRecord)
} }
} }
// 需要删除的 // 需要删除的
for _, uid := range oldUuids { for key := range oldMap {
if _, exist := newUuids[uid]; !exist { if _, exists := newMap[key]; !exists {
toDel = append(toDel, uid) // 旧记录在新数据中不存在,需要删除
toDel = append(toDel, key)
} }
} }
if len(toDel) > 0 { if len(toDel) > 0 {
if err = tx.Where("bundle_uuid = ? AND value_uid IN ?", bundleUuid, toDel).Delete(&model.BundleToValueAddService{}).Error; err != nil { for _, key := range toDel {
parts := strings.Split(key, "_")
if len(parts) == 2 {
valueUid := parts[0]
benefitsType := parts[1]
if err = tx.Where("bundle_uuid = ? AND value_uid = ? AND benefits_type = ? AND deleted_at = 0",
bundleUuid, valueUid, benefitsType).Delete(&model.BundleToValueAddService{}).Error; err != nil {
return errors.New("删除套餐与增值服务关联失败") return errors.New("删除套餐与增值服务关联失败")
} }
} }
}
}
if len(toAdd) > 0 { if len(toAdd) > 0 {
if err = dao.CreateBundleToValueAddService(tx, toAdd); err != nil { if err = dao.CreateBundleToValueAddService(tx, toAdd); err != nil {
return errors.New("保存套餐与增值服务关联失败") return errors.New("保存套餐与增值服务关联失败")

View File

@ -84,6 +84,11 @@ type PriceOption struct {
TotalPrice float32 `json:"totalPrice"` // 总价-新加 TotalPrice float32 `json:"totalPrice"` // 总价-新加
} }
type ValueUidWithBenefits struct {
ValueUid string `json:"valueUid"` //增值服务UUID
BenefitsType int32 `json:"benefitsType"` //权益类型
}
// 实现 driver.Valuer 接口 // 实现 driver.Valuer 接口
func (o PriceOptions) Value() (driver.Value, error) { func (o PriceOptions) Value() (driver.Value, error) {
return json.Marshal(o) return json.Marshal(o)