作者 陈志颖

feat:完善账期结算服务

@@ -9,6 +9,8 @@ import ( @@ -9,6 +9,8 @@ import (
9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/factory" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/factory"
10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
  12 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
  13 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
12 "strconv" 14 "strconv"
13 "time" 15 "time"
14 ) 16 )
@@ -17,7 +19,7 @@ import ( @@ -17,7 +19,7 @@ import (
17 type CreditAccountService struct { 19 type CreditAccountService struct {
18 } 20 }
19 21
20 -// CreateCreditAccount 创建账期结算单服务 22 +// CreateCreditAccount 创建账期结算单服务(账期结算)
21 func (creditAccountService *CreditAccountService) CreateCreditAccount(createCreditAccountCommand *command.CreateCreditAccountCommand) (interface{}, error) { 23 func (creditAccountService *CreditAccountService) CreateCreditAccount(createCreditAccountCommand *command.CreateCreditAccountCommand) (interface{}, error) {
22 if err := createCreditAccountCommand.ValidateCommand(); err != nil { 24 if err := createCreditAccountCommand.ValidateCommand(); err != nil {
23 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 25 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
@@ -81,25 +83,7 @@ func (creditAccountService *CreditAccountService) CreateCreditAccount(createCred @@ -81,25 +83,7 @@ func (creditAccountService *CreditAccountService) CreateCreditAccount(createCred
81 operator = data 83 operator = data
82 } 84 }
83 85
84 - newCreditAccount := &domain.CreditAccount{  
85 - ActuallyPaidAmount: 0,  
86 - CreditAccountOrderNum: "",  
87 - PaymentStatus: 0,  
88 - PaymentTime: time.Time{},  
89 - SettlementAmount: 0,  
90 - SettlementTime: time.Time{},  
91 - CooperationContractNumber: "",  
92 - Participator: nil,  
93 - PaymentDocumentAttachment: nil,  
94 - Org: organization,  
95 - Company: company,  
96 - Operator: operator,  
97 - OperateTime: time.Now(),  
98 - CreatedAt: time.Now(),  
99 - DeletedAt: time.Time{},  
100 - UpdatedAt: time.Time{},  
101 - }  
102 - 86 + // 账期结算单仓储初始化
103 var creditAccountRepository domain.CreditAccountRepository 87 var creditAccountRepository domain.CreditAccountRepository
104 if value, err := factory.CreateCreditAccountRepository(map[string]interface{}{ 88 if value, err := factory.CreateCreditAccountRepository(map[string]interface{}{
105 "transactionContext": transactionContext, 89 "transactionContext": transactionContext,
@@ -108,13 +92,82 @@ func (creditAccountService *CreditAccountService) CreateCreditAccount(createCred @@ -108,13 +92,82 @@ func (creditAccountService *CreditAccountService) CreateCreditAccount(createCred
108 } else { 92 } else {
109 creditAccountRepository = value 93 creditAccountRepository = value
110 } 94 }
111 - if creditAccount, err := creditAccountRepository.Save(newCreditAccount); err != nil { 95 +
  96 + // 账期结算单DAO初始化
  97 + var creditAccountDao *dao.CreditAccountDao
  98 + if value, err := factory.CreateCreditAccountDao(map[string]interface{}{
  99 + "transactionContext": transactionContext,
  100 + }); err != nil {
  101 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  102 + } else {
  103 + creditAccountDao = value
  104 + }
  105 +
  106 + // 分红预算单仓储初始化
  107 + var dividendsEstimateRepository domain.DividendsEstimateRepository
  108 + if value, err := factory.CreateDividendsEstimateRepository(map[string]interface{}{
  109 + "transactionContext": transactionContext,
  110 + }); err != nil {
  111 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  112 + } else {
  113 + dividendsEstimateRepository = value
  114 + }
  115 + dividendsEstimateIds, _ := utils.SliceAtoi(createCreditAccountCommand.DividendsEstimateIds)
  116 + // 获取所选分红预算单
  117 + if _, dividendsEstimates, err := dividendsEstimateRepository.Find(map[string]interface{}{
  118 + "dividendsEstimateIds": dividendsEstimateIds,
  119 + }); err != nil {
112 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 120 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
113 } else { 121 } else {
  122 + var creditAccounts []*domain.CreditAccount
  123 + for _, dividendsEstimate := range dividendsEstimates {
  124 + // 生成账期结算单号
  125 + creditAccountNumber, err := creditAccountDao.GenerateCreditAccountNumber()
  126 + if err != nil {
  127 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  128 + }
  129 + // 生成账期结算单
  130 + newCreditAccount := &domain.CreditAccount{
  131 + ActuallyPaidAmount: 0,
  132 + CreditAccountOrderNum: creditAccountNumber,
  133 + PaymentStatus: 1,
  134 + PaymentTime: time.Time{},
  135 + SettlementAmount: dividendsEstimate.DividendsAmount,
  136 + SettlementTime: time.Now(),
  137 + CooperationContractNumber: dividendsEstimate.CooperationContractNumber,
  138 + Participator: &domain.Participator{
  139 + UserId: dividendsEstimate.DividendsUser.UserId,
  140 + UserBaseId: dividendsEstimate.DividendsUser.UserBaseId,
  141 + Org: dividendsEstimate.DividendsUser.Org,
  142 + Orgs: dividendsEstimate.DividendsUser.Orgs,
  143 + Department: dividendsEstimate.DividendsUser.Department,
  144 + Roles: dividendsEstimate.DividendsUser.Roles,
  145 + UserInfo: dividendsEstimate.DividendsUser.UserInfo,
  146 + UserName: dividendsEstimate.DividendsUser.UserName,
  147 + UserPhone: dividendsEstimate.DividendsUser.UserPhone,
  148 + UserType: dividendsEstimate.DividendsUser.UserType,
  149 + Status: dividendsEstimate.DividendsUser.Status,
  150 + Company: dividendsEstimate.DividendsUser.Company,
  151 + },
  152 + PaymentDocumentAttachment: nil,
  153 + Org: organization,
  154 + Company: company,
  155 + Operator: operator,
  156 + OperateTime: time.Now(),
  157 + CreatedAt: time.Now(),
  158 + DeletedAt: time.Time{},
  159 + UpdatedAt: time.Time{},
  160 + }
  161 + if creditAccount, err := creditAccountRepository.Save(newCreditAccount); err != nil {
  162 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  163 + } else {
  164 + creditAccounts = append(creditAccounts, creditAccount)
  165 + }
  166 + }
114 if err := transactionContext.CommitTransaction(); err != nil { 167 if err := transactionContext.CommitTransaction(); err != nil {
115 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 168 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
116 } 169 }
117 - return creditAccount, nil 170 + return creditAccounts, nil
118 } 171 }
119 } 172 }
120 173
@@ -139,7 +192,7 @@ func (creditAccountService *CreditAccountService) CreditAccountRanking(creditAcc @@ -139,7 +192,7 @@ func (creditAccountService *CreditAccountService) CreditAccountRanking(creditAcc
139 return nil, nil 192 return nil, nil
140 } 193 }
141 194
142 -// GetCreditAccount 返回账期结算单服务 195 +// GetCreditAccount 返回账期结算单详情
143 func (creditAccountService *CreditAccountService) GetCreditAccount(getCreditAccountQuery *query.GetCreditAccountQuery) (interface{}, error) { 196 func (creditAccountService *CreditAccountService) GetCreditAccount(getCreditAccountQuery *query.GetCreditAccountQuery) (interface{}, error) {
144 if err := getCreditAccountQuery.ValidateQuery(); err != nil { 197 if err := getCreditAccountQuery.ValidateQuery(); err != nil {
145 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 198 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
@@ -176,7 +229,7 @@ func (creditAccountService *CreditAccountService) GetCreditAccount(getCreditAcco @@ -176,7 +229,7 @@ func (creditAccountService *CreditAccountService) GetCreditAccount(getCreditAcco
176 } 229 }
177 } 230 }
178 231
179 -// ListCreditAccount 返回账期结算单服务列表 232 +// ListCreditAccount 返回账期结算单列表
180 func (creditAccountService *CreditAccountService) ListCreditAccount(listCreditAccountQuery *query.ListCreditAccountQuery) (interface{}, error) { 233 func (creditAccountService *CreditAccountService) ListCreditAccount(listCreditAccountQuery *query.ListCreditAccountQuery) (interface{}, error) {
181 if err := listCreditAccountQuery.ValidateQuery(); err != nil { 234 if err := listCreditAccountQuery.ValidateQuery(); err != nil {
182 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 235 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
@@ -259,7 +312,7 @@ func (creditAccountService *CreditAccountService) PayCreditAccount(payCreditAcco @@ -259,7 +312,7 @@ func (creditAccountService *CreditAccountService) PayCreditAccount(payCreditAcco
259 } 312 }
260 } 313 }
261 314
262 -// RemoveCreditAccount 移除账期结算单服务 315 +// RemoveCreditAccount 移除账期结算单
263 func (creditAccountService *CreditAccountService) RemoveCreditAccount(removeCreditAccountCommand *command.RemoveCreditAccountCommand) (interface{}, error) { 316 func (creditAccountService *CreditAccountService) RemoveCreditAccount(removeCreditAccountCommand *command.RemoveCreditAccountCommand) (interface{}, error) {
264 if err := removeCreditAccountCommand.ValidateCommand(); err != nil { 317 if err := removeCreditAccountCommand.ValidateCommand(); err != nil {
265 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 318 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
@@ -337,7 +390,7 @@ func (creditAccountService *CreditAccountService) SearchCreditAccount(searchCred @@ -337,7 +390,7 @@ func (creditAccountService *CreditAccountService) SearchCreditAccount(searchCred
337 } 390 }
338 } 391 }
339 392
340 -// UpdateCreditAccount 更新账期结算单服务 393 +// UpdateCreditAccount 更新账期结算单
341 func (creditAccountService *CreditAccountService) UpdateCreditAccount(updateCreditAccountCommand *command.UpdateCreditAccountCommand) (interface{}, error) { 394 func (creditAccountService *CreditAccountService) UpdateCreditAccount(updateCreditAccountCommand *command.UpdateCreditAccountCommand) (interface{}, error) {
342 if err := updateCreditAccountCommand.ValidateCommand(); err != nil { 395 if err := updateCreditAccountCommand.ValidateCommand(); err != nil {
343 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 396 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
@@ -82,7 +82,7 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) CreateDivide @@ -82,7 +82,7 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) CreateDivide
82 operator = data 82 operator = data
83 } 83 }
84 84
85 - // 分红退货单DAO初始化 85 + // 分红退货单DAO初始化
86 var dividendsReturnedOrderDao *dao.DividendsReturnedOrderDao 86 var dividendsReturnedOrderDao *dao.DividendsReturnedOrderDao
87 if value, err := factory.CreateDividendsReturnedOrderDao(map[string]interface{}{ 87 if value, err := factory.CreateDividendsReturnedOrderDao(map[string]interface{}{
88 "transactionContext": transactionContext, 88 "transactionContext": transactionContext,
@@ -44,3 +44,11 @@ func CreateDividendsReturnedOrderDao(options map[string]interface{}) (*dao.Divid @@ -44,3 +44,11 @@ func CreateDividendsReturnedOrderDao(options map[string]interface{}) (*dao.Divid
44 } 44 }
45 return dao.NewDividendsReturnedOrderDao(transactionContext) 45 return dao.NewDividendsReturnedOrderDao(transactionContext)
46 } 46 }
  47 +
  48 +func CreateCreditAccountDao(options map[string]interface{}) (*dao.CreditAccountDao, error) {
  49 + var transactionContext *pg.TransactionContext
  50 + if value, ok := options["transactionContext"]; ok {
  51 + transactionContext = value.(*pg.TransactionContext)
  52 + }
  53 + return dao.NewCreditAccountDao(transactionContext)
  54 +}
  1 +package dao
  2 +
  3 +import (
  4 + "fmt"
  5 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
  8 + "time"
  9 +)
  10 +
  11 +type CreditAccountDao struct {
  12 + transactionContext *pgTransaction.TransactionContext
  13 +}
  14 +
  15 +// GenerateCreditAccountNumber 生成账期结算单号
  16 +func (dao *CreditAccountDao) GenerateCreditAccountNumber() (string, error) {
  17 + tx := dao.transactionContext.PgTx
  18 + var creditAccountModels []*models.CreditAccount
  19 + query := tx.Model(&creditAccountModels)
  20 + currentTime := time.Now()
  21 + todayZeroTime := utils.GetZeroTime(currentTime)
  22 + nextDayZeroTime := utils.GetNextDayZeroTime(currentTime)
  23 + query.Where("credit_account.created_at >= ?", todayZeroTime)
  24 + query.Where("credit_account.created_at < ?", nextDayZeroTime)
  25 + if count, err := query.AllWithDeleted().SelectAndCount(); err != nil {
  26 + return "", err
  27 + } else {
  28 + countStr := fmt.Sprintf("%03d", count+1)
  29 + timestamp := currentTime.Unix()
  30 + timeNow := time.Unix(timestamp, 0)
  31 + timeString := timeNow.Format("20060102")
  32 + timeString = timeString[2:len(timeString)]
  33 + creditAccountNumber := "JS" + timeString + "#" + countStr
  34 + return creditAccountNumber, nil
  35 + }
  36 +}
  37 +
  38 +// CheckCreditAccountNumberAvailable 校验账期结算单号是否唯一
  39 +func (dao *CreditAccountDao) CheckCreditAccountNumberAvailable(queryOptions map[string]interface{}) (bool, error) {
  40 + tx := dao.transactionContext.PgTx
  41 + var creditAccountModels []*models.CreditAccount
  42 + query := tx.Model(&creditAccountModels)
  43 + if creditAccountNumber, ok := queryOptions["creditAccountNumber"]; ok && creditAccountNumber != "" {
  44 + query = query.Where("credit_account_number = ?", creditAccountNumber)
  45 + }
  46 + if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
  47 + query = query.Where(`credit_account.company @> '{"companyId":"?"}'`, companyId)
  48 + }
  49 + if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
  50 + query = query.Where(`credit_account.org @> '{"orgId":"?"}'`, orgId)
  51 + }
  52 + ok, err := query.Exists()
  53 + return !ok, err
  54 +}
  55 +
  56 +func NewCreditAccountDao(transactionContext *pgTransaction.TransactionContext) (*CreditAccountDao, error) {
  57 + if transactionContext == nil {
  58 + return nil, fmt.Errorf("transactionContext参数不能为空")
  59 + } else {
  60 + return &CreditAccountDao{
  61 + transactionContext: transactionContext,
  62 + }, nil
  63 + }
  64 +}
@@ -24,6 +24,7 @@ func (repository *CooperationContractRelevantRepository) nextIdentify() (int64, @@ -24,6 +24,7 @@ func (repository *CooperationContractRelevantRepository) nextIdentify() (int64,
24 id, err := IdWorker.NextId() 24 id, err := IdWorker.NextId()
25 return id, err 25 return id, err
26 } 26 }
  27 +
27 func (repository *CooperationContractRelevantRepository) Save(cooperationContractRelevant *domain.CooperationContractRelevant) (*domain.CooperationContractRelevant, error) { 28 func (repository *CooperationContractRelevantRepository) Save(cooperationContractRelevant *domain.CooperationContractRelevant) (*domain.CooperationContractRelevant, error) {
28 sqlBuildFields := []string{ 29 sqlBuildFields := []string{
29 "cooperation_contract_relevant_id", 30 "cooperation_contract_relevant_id",
@@ -134,6 +135,7 @@ func (repository *CooperationContractRelevantRepository) Save(cooperationContrac @@ -134,6 +135,7 @@ func (repository *CooperationContractRelevantRepository) Save(cooperationContrac
134 } 135 }
135 return cooperationContractRelevant, nil 136 return cooperationContractRelevant, nil
136 } 137 }
  138 +
137 func (repository *CooperationContractRelevantRepository) Remove(cooperationContractRelevant *domain.CooperationContractRelevant) (*domain.CooperationContractRelevant, error) { 139 func (repository *CooperationContractRelevantRepository) Remove(cooperationContractRelevant *domain.CooperationContractRelevant) (*domain.CooperationContractRelevant, error) {
138 tx := repository.transactionContext.PgTx 140 tx := repository.transactionContext.PgTx
139 cooperationContractRelevantModel := new(models.CooperationContractRelevant) 141 cooperationContractRelevantModel := new(models.CooperationContractRelevant)
@@ -143,6 +145,7 @@ func (repository *CooperationContractRelevantRepository) Remove(cooperationContr @@ -143,6 +145,7 @@ func (repository *CooperationContractRelevantRepository) Remove(cooperationContr
143 } 145 }
144 return cooperationContractRelevant, nil 146 return cooperationContractRelevant, nil
145 } 147 }
  148 +
146 func (repository *CooperationContractRelevantRepository) FindOne(queryOptions map[string]interface{}) (*domain.CooperationContractRelevant, error) { 149 func (repository *CooperationContractRelevantRepository) FindOne(queryOptions map[string]interface{}) (*domain.CooperationContractRelevant, error) {
147 tx := repository.transactionContext.PgTx 150 tx := repository.transactionContext.PgTx
148 cooperationContractRelevantModel := new(models.CooperationContractRelevant) 151 cooperationContractRelevantModel := new(models.CooperationContractRelevant)
@@ -161,6 +164,7 @@ func (repository *CooperationContractRelevantRepository) FindOne(queryOptions ma @@ -161,6 +164,7 @@ func (repository *CooperationContractRelevantRepository) FindOne(queryOptions ma
161 return transform.TransformToCooperationContractRelevantDomainModelFromPgModels(cooperationContractRelevantModel) 164 return transform.TransformToCooperationContractRelevantDomainModelFromPgModels(cooperationContractRelevantModel)
162 } 165 }
163 } 166 }
  167 +
164 func (repository *CooperationContractRelevantRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.CooperationContractRelevant, error) { 168 func (repository *CooperationContractRelevantRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.CooperationContractRelevant, error) {
165 tx := repository.transactionContext.PgTx 169 tx := repository.transactionContext.PgTx
166 var cooperationContractRelevantModels []*models.CooperationContractRelevant 170 var cooperationContractRelevantModels []*models.CooperationContractRelevant
@@ -187,6 +191,7 @@ func (repository *CooperationContractRelevantRepository) Find(queryOptions map[s @@ -187,6 +191,7 @@ func (repository *CooperationContractRelevantRepository) Find(queryOptions map[s
187 return int64(count), cooperationContractRelevants, nil 191 return int64(count), cooperationContractRelevants, nil
188 } 192 }
189 } 193 }
  194 +
190 func NewCooperationContractRelevantRepository(transactionContext *pgTransaction.TransactionContext) (*CooperationContractRelevantRepository, error) { 195 func NewCooperationContractRelevantRepository(transactionContext *pgTransaction.TransactionContext) (*CooperationContractRelevantRepository, error) {
191 if transactionContext == nil { 196 if transactionContext == nil {
192 return nil, fmt.Errorf("transactionContext参数不能为nil") 197 return nil, fmt.Errorf("transactionContext参数不能为nil")
@@ -229,6 +229,9 @@ func (repository *DividendsEstimateRepository) Find(queryOptions map[string]inte @@ -229,6 +229,9 @@ func (repository *DividendsEstimateRepository) Find(queryOptions map[string]inte
229 if dividendsEstimateOrderNumber, ok := queryOptions["dividendsEstimateOrderNumber"]; ok && dividendsEstimateOrderNumber != "" { 229 if dividendsEstimateOrderNumber, ok := queryOptions["dividendsEstimateOrderNumber"]; ok && dividendsEstimateOrderNumber != "" {
230 query.Where("dividends_estimate_order_number ilike ?", fmt.Sprintf("%%%s%%", dividendsEstimateOrderNumber)) 230 query.Where("dividends_estimate_order_number ilike ?", fmt.Sprintf("%%%s%%", dividendsEstimateOrderNumber))
231 } 231 }
  232 + if dividendsEstimateIds, ok := queryOptions["dividendsEstimateIds"]; ok && len(dividendsEstimateIds.([]int64)) > 0 {
  233 + query.Where("dividends_estimate_id IN (?)", pg.In(dividendsEstimateIds))
  234 + }
232 offsetLimitFlag := true 235 offsetLimitFlag := true
233 if offsetLimit, ok := queryOptions["offsetLimit"]; ok { 236 if offsetLimit, ok := queryOptions["offsetLimit"]; ok {
234 offsetLimitFlag = offsetLimit.(bool) 237 offsetLimitFlag = offsetLimit.(bool)