174 lines
5.0 KiB
Go
174 lines
5.0 KiB
Go
package service
|
||
|
||
import (
|
||
"encoding/json"
|
||
"psi/database"
|
||
"psi/models"
|
||
systemReq "psi/models/request"
|
||
systemRes "psi/models/response"
|
||
"psi/utils"
|
||
"time"
|
||
|
||
"gorm.io/datatypes"
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
type SplitAccountConfigService struct{}
|
||
|
||
// GetSplitAccountConfigList 获取分账配置列表
|
||
func (s *SplitAccountConfigService) GetSplitAccountConfigList(req systemReq.GetSplitAccountConfigListRequest, db ...*gorm.DB) (*systemRes.SplitAccountConfigListResponse, error) {
|
||
databaseConn := database.OptionalDB(db...)
|
||
|
||
if req.Page < 1 {
|
||
req.Page = 1
|
||
}
|
||
if req.PageSize < 1 || req.PageSize > 100 {
|
||
req.PageSize = 20
|
||
}
|
||
|
||
query := databaseConn.Model(&models.SplitAccountConfig{}).Where("deleted_at = ?", 0)
|
||
|
||
if req.Status != "" {
|
||
query = query.Where("status = ?", req.Status)
|
||
}
|
||
if req.Keyword != "" {
|
||
query = query.Where("rule_name like ?", "%"+req.Keyword+"%")
|
||
}
|
||
|
||
var total int64
|
||
if err := query.Count(&total).Error; err != nil {
|
||
return nil, utils.NewError("查询总数失败")
|
||
}
|
||
|
||
var configs []models.SplitAccountConfig
|
||
offset := (req.Page - 1) * req.PageSize
|
||
if err := query.Order("created_at DESC").Offset(offset).Limit(req.PageSize).Find(&configs).Error; err != nil {
|
||
return nil, utils.NewError("查询分账配置列表失败")
|
||
}
|
||
|
||
var configItems []systemRes.SplitAccountConfigItem
|
||
for _, config := range configs {
|
||
configItems = append(configItems, systemRes.ConvertSplitAccountConfigToItem(config))
|
||
}
|
||
|
||
return &systemRes.SplitAccountConfigListResponse{
|
||
List: configItems,
|
||
Total: total,
|
||
Page: req.Page,
|
||
PageSize: req.PageSize,
|
||
}, nil
|
||
}
|
||
|
||
// GetSplitAccountConfigDetail 获取分账配置详情
|
||
func (s *SplitAccountConfigService) GetSplitAccountConfigDetail(id int64, db ...*gorm.DB) (*models.SplitAccountConfig, error) {
|
||
databaseConn := database.OptionalDB(db...)
|
||
|
||
var config models.SplitAccountConfig
|
||
|
||
result := databaseConn.Where("id = ? AND deleted_at = ?", id, 0).First(&config)
|
||
if result.Error != nil {
|
||
return nil, utils.NewError("分账配置不存在")
|
||
}
|
||
|
||
return &config, nil
|
||
}
|
||
|
||
// CreateSplitAccountConfig 创建分账配置
|
||
func (s *SplitAccountConfigService) CreateSplitAccountConfig(req systemReq.AddSplitAccountConfigRequest, username string, db ...*gorm.DB) (int64, error) {
|
||
databaseConn := database.OptionalDB(db...)
|
||
|
||
var ruleValue datatypes.JSON
|
||
if req.RuleValue != "" {
|
||
// 验证是否为合法JSON
|
||
var temp interface{}
|
||
if err := json.Unmarshal([]byte(req.RuleValue), &temp); err != nil {
|
||
return 0, utils.NewError("分账规则配置格式错误,不是合法的JSON")
|
||
}
|
||
ruleValue, _ = json.Marshal(temp)
|
||
}
|
||
|
||
now := time.Now().Unix()
|
||
config := models.SplitAccountConfig{
|
||
RuleName: req.RuleName,
|
||
RuleValue: ruleValue,
|
||
Status: req.Status,
|
||
Description: req.Description,
|
||
CreatedBy: username,
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
DeletedAt: 0,
|
||
}
|
||
|
||
if config.Status == 0 {
|
||
config.Status = 1
|
||
}
|
||
|
||
if err := databaseConn.Create(&config).Error; err != nil {
|
||
return 0, utils.NewError("创建分账配置失败: " + err.Error())
|
||
}
|
||
|
||
return config.ID, nil
|
||
}
|
||
|
||
// UpdateSplitAccountConfig 更新分账配置
|
||
func (s *SplitAccountConfigService) UpdateSplitAccountConfig(req systemReq.UpdateSplitAccountConfigRequest, username string, db ...*gorm.DB) error {
|
||
databaseConn := database.OptionalDB(db...)
|
||
|
||
var config models.SplitAccountConfig
|
||
result := databaseConn.Where("id = ? AND deleted_at = ?", req.ID, 0).First(&config)
|
||
if result.Error != nil {
|
||
return utils.NewError("分账配置不存在")
|
||
}
|
||
|
||
updateData := make(map[string]interface{})
|
||
updateData["updated_at"] = time.Now().Unix()
|
||
updateData["updated_by"] = username
|
||
|
||
if req.RuleName != "" {
|
||
updateData["rule_name"] = req.RuleName
|
||
}
|
||
if req.RuleValue != "" {
|
||
var temp interface{}
|
||
if err := json.Unmarshal([]byte(req.RuleValue), &temp); err != nil {
|
||
return utils.NewError("分账规则配置格式错误,不是合法的JSON")
|
||
}
|
||
ruleValue, _ := json.Marshal(temp)
|
||
updateData["rule_value"] = ruleValue
|
||
}
|
||
if req.Status >= 0 {
|
||
updateData["status"] = req.Status
|
||
}
|
||
if req.Description != "" {
|
||
updateData["description"] = req.Description
|
||
}
|
||
|
||
if err := databaseConn.Model(&config).Updates(updateData).Error; err != nil {
|
||
return utils.NewError("更新分账配置失败: " + err.Error())
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// DeleteSplitAccountConfig 删除分账配置(软删除)
|
||
func (s *SplitAccountConfigService) DeleteSplitAccountConfig(req systemReq.DeleteSplitAccountConfigRequest, username string, db ...*gorm.DB) error {
|
||
databaseConn := database.OptionalDB(db...)
|
||
|
||
var config models.SplitAccountConfig
|
||
result := databaseConn.Where("id = ? AND deleted_at = ?", req.ID, 0).First(&config)
|
||
if result.Error != nil {
|
||
return utils.NewError("分账配置不存在")
|
||
}
|
||
|
||
updateData := map[string]interface{}{
|
||
"deleted_at": time.Now().Unix(),
|
||
"updated_at": time.Now().Unix(),
|
||
"updated_by": username,
|
||
}
|
||
|
||
if err := databaseConn.Model(&config).Updates(updateData).Error; err != nil {
|
||
return utils.NewError("删除分账配置失败: " + err.Error())
|
||
}
|
||
|
||
return nil
|
||
}
|