180 lines
5.4 KiB
Go
180 lines
5.4 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 SplitAccountDeductionLogService struct{}
|
||
|
||
// GetSplitAccountDeductionLogList 获取分账扣钱日志列表
|
||
func (s *SplitAccountDeductionLogService) GetSplitAccountDeductionLogList(req systemReq.GetSplitAccountDeductionLogListRequest, db ...*gorm.DB) (*systemRes.SplitAccountDeductionLogListResponse, 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.SplitAccountDeductionLog{})
|
||
|
||
if req.BusinessNo != "" {
|
||
query = query.Where("business_no like ?", "%"+req.BusinessNo+"%")
|
||
}
|
||
if req.ConfigID != "" {
|
||
query = query.Where("config_id = ?", req.ConfigID)
|
||
}
|
||
if req.StartCreatedAt != "" {
|
||
query = query.Where("created_at >= ?", req.StartCreatedAt)
|
||
}
|
||
if req.EndCreatedAt != "" {
|
||
query = query.Where("created_at <= ?", req.EndCreatedAt)
|
||
}
|
||
|
||
var total int64
|
||
if err := query.Count(&total).Error; err != nil {
|
||
return nil, utils.NewError("查询总数失败")
|
||
}
|
||
|
||
var logs []models.SplitAccountDeductionLog
|
||
offset := (req.Page - 1) * req.PageSize
|
||
if err := query.Order("created_at DESC").Offset(offset).Limit(req.PageSize).Find(&logs).Error; err != nil {
|
||
return nil, utils.NewError("查询分账扣钱日志列表失败")
|
||
}
|
||
|
||
var logItems []systemRes.SplitAccountDeductionLogItem
|
||
for _, log := range logs {
|
||
logItems = append(logItems, systemRes.ConvertSplitAccountDeductionLogToItem(log))
|
||
}
|
||
|
||
return &systemRes.SplitAccountDeductionLogListResponse{
|
||
List: logItems,
|
||
Total: total,
|
||
Page: req.Page,
|
||
PageSize: req.PageSize,
|
||
}, nil
|
||
}
|
||
|
||
// GetSplitAccountDeductionLogDetail 获取分账扣钱日志详情
|
||
func (s *SplitAccountDeductionLogService) GetSplitAccountDeductionLogDetail(id int64, db ...*gorm.DB) (*models.SplitAccountDeductionLog, error) {
|
||
databaseConn := database.OptionalDB(db...)
|
||
|
||
var log models.SplitAccountDeductionLog
|
||
|
||
result := databaseConn.Where("id = ?", id).First(&log)
|
||
if result.Error != nil {
|
||
return nil, utils.NewError("分账扣钱日志不存在")
|
||
}
|
||
|
||
return &log, nil
|
||
}
|
||
|
||
// CreateSplitAccountDeductionLog 创建分账扣钱日志
|
||
func (s *SplitAccountDeductionLogService) CreateSplitAccountDeductionLog(req systemReq.AddSplitAccountDeductionLogRequest, username string, db ...*gorm.DB) (int64, error) {
|
||
databaseConn := database.OptionalDB(db...)
|
||
|
||
var deductionDetails datatypes.JSON
|
||
if req.DeductionDetails != "" {
|
||
var temp interface{}
|
||
if err := json.Unmarshal([]byte(req.DeductionDetails), &temp); err != nil {
|
||
return 0, utils.NewError("扣款规则格式错误,不是合法的JSON")
|
||
}
|
||
deductionDetails, _ = json.Marshal(temp)
|
||
}
|
||
|
||
now := time.Now().Unix()
|
||
log := models.SplitAccountDeductionLog{
|
||
BusinessNo: req.BusinessNo,
|
||
ConfigID: req.ConfigID,
|
||
ConfigName: req.ConfigName,
|
||
DeductionDetails: deductionDetails,
|
||
TotalAmount: req.TotalAmount,
|
||
DeductionAmount: req.DeductionAmount,
|
||
RemainingAmount: req.RemainingAmount,
|
||
CreatedBy: username,
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
|
||
if err := databaseConn.Create(&log).Error; err != nil {
|
||
return 0, utils.NewError("创建分账扣钱日志失败: " + err.Error())
|
||
}
|
||
|
||
return log.ID, nil
|
||
}
|
||
|
||
// UpdateSplitAccountDeductionLog 更新分账扣钱日志
|
||
func (s *SplitAccountDeductionLogService) UpdateSplitAccountDeductionLog(req systemReq.UpdateSplitAccountDeductionLogRequest, username string, db ...*gorm.DB) error {
|
||
databaseConn := database.OptionalDB(db...)
|
||
|
||
var log models.SplitAccountDeductionLog
|
||
result := databaseConn.Where("id = ?", req.ID).First(&log)
|
||
if result.Error != nil {
|
||
return utils.NewError("分账扣钱日志不存在")
|
||
}
|
||
|
||
updateData := make(map[string]interface{})
|
||
updateData["updated_at"] = time.Now().Unix()
|
||
updateData["updated_by"] = username
|
||
|
||
if req.BusinessNo != "" {
|
||
updateData["business_no"] = req.BusinessNo
|
||
}
|
||
if req.ConfigID > 0 {
|
||
updateData["config_id"] = req.ConfigID
|
||
}
|
||
if req.ConfigName != "" {
|
||
updateData["config_name"] = req.ConfigName
|
||
}
|
||
if req.DeductionDetails != "" {
|
||
var temp interface{}
|
||
if err := json.Unmarshal([]byte(req.DeductionDetails), &temp); err != nil {
|
||
return utils.NewError("扣款规则格式错误,不是合法的JSON")
|
||
}
|
||
deductionDetails, _ := json.Marshal(temp)
|
||
updateData["deduction_details"] = deductionDetails
|
||
}
|
||
if req.TotalAmount > 0 {
|
||
updateData["total_amount"] = req.TotalAmount
|
||
}
|
||
if req.DeductionAmount > 0 {
|
||
updateData["deduction_amount"] = req.DeductionAmount
|
||
}
|
||
if req.RemainingAmount > 0 {
|
||
updateData["remaining_amount"] = req.RemainingAmount
|
||
}
|
||
|
||
if err := databaseConn.Model(&log).Updates(updateData).Error; err != nil {
|
||
return utils.NewError("更新分账扣钱日志失败: " + err.Error())
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// DeleteSplitAccountDeductionLog 删除分账扣钱日志
|
||
func (s *SplitAccountDeductionLogService) DeleteSplitAccountDeductionLog(req systemReq.DeleteSplitAccountDeductionLogRequest, username string, db ...*gorm.DB) error {
|
||
databaseConn := database.OptionalDB(db...)
|
||
|
||
var log models.SplitAccountDeductionLog
|
||
result := databaseConn.Where("id = ?", req.ID).First(&log)
|
||
if result.Error != nil {
|
||
return utils.NewError("分账扣钱日志不存在")
|
||
}
|
||
|
||
if err := databaseConn.Delete(&log).Error; err != nil {
|
||
return utils.NewError("删除分账扣钱日志失败: " + err.Error())
|
||
}
|
||
|
||
return nil
|
||
}
|