package system import ( "errors" "sync" "git.echol.cn/loser/lckt/global" "git.echol.cn/loser/lckt/model/system" "git.echol.cn/loser/lckt/model/system/request" "gorm.io/gorm" ) type PayConfigService struct{} // 缓存支付配置 var ( payConfigCache = make(map[string]*system.PayConfig) payConfigCacheLock sync.RWMutex ) // GetPayConfigList 获取支付配置列表 func (s *PayConfigService) GetPayConfigList(req request.PayConfigSearch) (list []system.PayConfig, total int64, err error) { db := global.GVA_DB.Model(&system.PayConfig{}) if req.Name != "" { db = db.Where("name LIKE ?", "%"+req.Name+"%") } if req.Code != "" { db = db.Where("code LIKE ?", "%"+req.Code+"%") } if req.Type != "" { db = db.Where("type = ?", req.Type) } if req.Enable != nil { db = db.Where("enable = ?", *req.Enable) } err = db.Count(&total).Error if err != nil { return } limit := req.PageSize offset := req.PageSize * (req.Page - 1) err = db.Order("sort ASC, id ASC").Limit(limit).Offset(offset).Find(&list).Error return } // GetPayConfigByID 根据ID获取支付配置 func (s *PayConfigService) GetPayConfigByID(id uint) (config system.PayConfig, err error) { err = global.GVA_DB.Where("id = ?", id).First(&config).Error return } // GetPayConfigByCode 根据编码获取支付配置 func (s *PayConfigService) GetPayConfigByCode(code string) (config system.PayConfig, err error) { // 先从缓存获取 payConfigCacheLock.RLock() if cached, ok := payConfigCache[code]; ok { payConfigCacheLock.RUnlock() return *cached, nil } payConfigCacheLock.RUnlock() // 从数据库获取 err = global.GVA_DB.Where("code = ? AND enable = ?", code, true).First(&config).Error if err != nil { return } // 写入缓存 payConfigCacheLock.Lock() payConfigCache[code] = &config payConfigCacheLock.Unlock() return } // GetEnabledPayConfigs 获取所有启用的支付配置 func (s *PayConfigService) GetEnabledPayConfigs() (list []system.PayConfig, err error) { err = global.GVA_DB.Where("enable = ?", true).Order("sort ASC").Find(&list).Error return } // GetPayConfigsByType 根据类型获取启用的支付配置 func (s *PayConfigService) GetPayConfigsByType(payType string) (list []system.PayConfig, err error) { err = global.GVA_DB.Where("type = ? AND enable = ?", payType, true).Order("sort ASC").Find(&list).Error return } // CreatePayConfig 创建支付配置 func (s *PayConfigService) CreatePayConfig(req request.PayConfigCreate) error { // 验证配置完整性 if err := req.Validate(); err != nil { return err } // 检查编码是否已存在 var count int64 if err := global.GVA_DB.Model(&system.PayConfig{}).Where("code = ?", req.Code).Count(&count).Error; err != nil { return err } if count > 0 { return errors.New("支付编码已存在") } config := system.PayConfig{ Name: req.Name, Code: req.Code, Type: req.Type, Enable: req.Enable, EnabledModes: system.StringArray(req.EnabledModes), Sort: req.Sort, Remark: req.Remark, } // 根据类型设置配置字段 switch req.Type { case "wechat": config.WechatAppID = req.WechatAppID config.WechatMchID = req.WechatMchID config.WechatMchApiV3Key = req.WechatMchApiV3Key config.WechatPrivateKey = req.WechatPrivateKey config.WechatSerialNo = req.WechatSerialNo config.WechatNotifyURL = req.WechatNotifyURL case "shenqi": config.ShenqiPID = req.ShenqiPID config.ShenqiPrivateKey = req.ShenqiPrivateKey config.ShenqiPlatformPubKey = req.ShenqiPlatformPubKey config.ShenqiNotifyURL = req.ShenqiNotifyURL config.ShenqiReturnURL = req.ShenqiReturnURL config.ShenqiBaseURL = req.ShenqiBaseURL } if err := global.GVA_DB.Create(&config).Error; err != nil { return err } // 清除缓存 s.clearCache() return nil } // UpdatePayConfig 更新支付配置 func (s *PayConfigService) UpdatePayConfig(req request.PayConfigUpdate) error { // 检查是否存在 var config system.PayConfig if err := global.GVA_DB.Where("id = ?", req.ID).First(&config).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return errors.New("支付配置不存在") } return err } // 如果修改了编码,检查新编码是否已存在 if req.Code != "" && req.Code != config.Code { var count int64 if err := global.GVA_DB.Model(&system.PayConfig{}).Where("code = ? AND id != ?", req.Code, req.ID).Count(&count).Error; err != nil { return err } if count > 0 { return errors.New("支付编码已存在") } } // 构建更新数据 updates := make(map[string]interface{}) if req.Name != "" { updates["name"] = req.Name } if req.Code != "" { updates["code"] = req.Code } if req.Type != "" { updates["type"] = req.Type } if req.Enable != nil { updates["enable"] = *req.Enable } // 处理 EnabledModes:使用指针类型来区分"不更新"和"更新为空数组" if req.EnabledModes != nil { // 将 *[]string 转换为 StringArray 类型 sa := system.StringArray(*req.EnabledModes) updates["enabled_modes"] = sa } if req.Sort != nil { updates["sort"] = *req.Sort } if req.Remark != "" { updates["remark"] = req.Remark } // 微信支付配置 if req.WechatAppID != nil { updates["wechat_app_id"] = *req.WechatAppID } if req.WechatMchID != nil { updates["wechat_mch_id"] = *req.WechatMchID } if req.WechatMchApiV3Key != nil { updates["wechat_mch_api_v3_key"] = *req.WechatMchApiV3Key } if req.WechatPrivateKey != nil { updates["wechat_private_key"] = *req.WechatPrivateKey } if req.WechatSerialNo != nil { updates["wechat_serial_no"] = *req.WechatSerialNo } if req.WechatNotifyURL != nil { updates["wechat_notify_url"] = *req.WechatNotifyURL } // 神奇支付配置 if req.ShenqiPID != nil { updates["shenqi_pid"] = *req.ShenqiPID } if req.ShenqiPrivateKey != nil { updates["shenqi_private_key"] = *req.ShenqiPrivateKey } if req.ShenqiPlatformPubKey != nil { updates["shenqi_platform_pub_key"] = *req.ShenqiPlatformPubKey } if req.ShenqiNotifyURL != nil { updates["shenqi_notify_url"] = *req.ShenqiNotifyURL } if req.ShenqiReturnURL != nil { updates["shenqi_return_url"] = *req.ShenqiReturnURL } if req.ShenqiBaseURL != nil { updates["shenqi_base_url"] = *req.ShenqiBaseURL } if err := global.GVA_DB.Model(&config).Updates(updates).Error; err != nil { return err } // 清除缓存 s.clearCache() return nil } // DeletePayConfig 删除支付配置 func (s *PayConfigService) DeletePayConfig(id uint) error { if err := global.GVA_DB.Delete(&system.PayConfig{}, id).Error; err != nil { return err } // 清除缓存 s.clearCache() return nil } // TogglePayConfig 切换支付配置状态 func (s *PayConfigService) TogglePayConfig(req request.PayConfigToggle) error { if err := global.GVA_DB.Model(&system.PayConfig{}).Where("id = ?", req.ID).Update("enable", req.Enable).Error; err != nil { return err } // 清除缓存 s.clearCache() return nil } // clearCache 清除支付配置缓存 func (s *PayConfigService) clearCache() { payConfigCacheLock.Lock() payConfigCache = make(map[string]*system.PayConfig) payConfigCacheLock.Unlock() } // GetShenqiPayConfig 获取神奇支付配置 func (s *PayConfigService) GetShenqiPayConfig(code string) (*system.ShenqiPayConfig, error) { config, err := s.GetPayConfigByCode(code) if err != nil { return nil, err } if config.Type != "shenqi" { return nil, errors.New("该配置不是神奇支付类型") } return config.GetShenqiConfig(), nil } // GetWechatPayConfig 获取微信支付配置 func (s *PayConfigService) GetWechatPayConfig(code string) (*system.WechatPayConfig, error) { config, err := s.GetPayConfigByCode(code) if err != nil { return nil, err } if config.Type != "wechat" { return nil, errors.New("该配置不是微信支付类型") } return config.GetWechatConfig(), nil }