作者 陈志颖

refactor:修改账期结算生成逻辑

@@ -19,7 +19,7 @@ import ( @@ -19,7 +19,7 @@ import (
19 type CreditAccountService struct { 19 type CreditAccountService struct {
20 } 20 }
21 21
22 -// CreateCreditAccount 创建账期结算单服务(账期结算) 22 +// CreateCreditAccount 账期结算
23 func (creditAccountService *CreditAccountService) CreateCreditAccount(createCreditAccountCommand *command.CreateCreditAccountCommand) (interface{}, error) { 23 func (creditAccountService *CreditAccountService) CreateCreditAccount(createCreditAccountCommand *command.CreateCreditAccountCommand) (interface{}, error) {
24 if err := createCreditAccountCommand.ValidateCommand(); err != nil { 24 if err := createCreditAccountCommand.ValidateCommand(); err != nil {
25 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 25 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
@@ -28,8 +28,8 @@ func (creditAccountService *CreditAccountService) CreateCreditAccount(createCred @@ -28,8 +28,8 @@ func (creditAccountService *CreditAccountService) CreateCreditAccount(createCred
28 if err != nil { 28 if err != nil {
29 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 29 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
30 } 30 }
31 - if err := transactionContext.StartTransaction(); err != nil {  
32 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 31 + if err1 := transactionContext.StartTransaction(); err1 != nil {
  32 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err1.Error())
33 } 33 }
34 defer func() { 34 defer func() {
35 _ = transactionContext.RollbackTransaction() 35 _ = transactionContext.RollbackTransaction()
@@ -37,78 +37,78 @@ func (creditAccountService *CreditAccountService) CreateCreditAccount(createCred @@ -37,78 +37,78 @@ func (creditAccountService *CreditAccountService) CreateCreditAccount(createCred
37 37
38 // 公司REST服务初始化 38 // 公司REST服务初始化
39 var companyService service.CompanyService 39 var companyService service.CompanyService
40 - if value, err := factory.CreateCompanyService(map[string]interface{}{}); err != nil {  
41 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 40 + if value, err2 := factory.CreateCompanyService(map[string]interface{}{}); err2 != nil {
  41 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err2.Error())
42 } else { 42 } else {
43 companyService = value 43 companyService = value
44 } 44 }
45 45
46 // 获取公司信息 46 // 获取公司信息
47 var company *domain.Company 47 var company *domain.Company
48 - if data, err := companyService.CompanyFrom(createCreditAccountCommand.CompanyId); err != nil {  
49 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 48 + if data, err3 := companyService.CompanyFrom(createCreditAccountCommand.CompanyId); err3 != nil {
  49 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取公司信息失败")
50 } else { 50 } else {
51 company = data 51 company = data
52 } 52 }
53 53
54 // 组织机构REST服务初始化 54 // 组织机构REST服务初始化
55 var organizationService service.OrgService 55 var organizationService service.OrgService
56 - if value, err := factory.CreateOrganizationService(map[string]interface{}{}); err != nil {  
57 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 56 + if value, err4 := factory.CreateOrganizationService(map[string]interface{}{}); err4 != nil {
  57 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err4.Error())
58 } else { 58 } else {
59 organizationService = value 59 organizationService = value
60 } 60 }
61 61
62 // 获取组织机构信息 62 // 获取组织机构信息
63 var organization *domain.Org 63 var organization *domain.Org
64 - if data, err := organizationService.OrgFrom(createCreditAccountCommand.CompanyId, createCreditAccountCommand.OrgId); err != nil {  
65 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 64 + if data, err5 := organizationService.OrgFrom(createCreditAccountCommand.CompanyId, createCreditAccountCommand.OrgId); err5 != nil {
  65 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取组织机构失败")
66 } else { 66 } else {
67 organization = data 67 organization = data
68 } 68 }
69 69
70 // 用户REST服务初始化 70 // 用户REST服务初始化
71 var userService service.UserService 71 var userService service.UserService
72 - if value, err := factory.CreateUserService(map[string]interface{}{}); err != nil {  
73 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 72 + if value, err6 := factory.CreateUserService(map[string]interface{}{}); err6 != nil {
  73 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err6.Error())
74 } else { 74 } else {
75 userService = value 75 userService = value
76 } 76 }
77 77
78 // 获取操作人 78 // 获取操作人
79 var operator *domain.User 79 var operator *domain.User
80 - if data, err := userService.OperatorFrom(createCreditAccountCommand.CompanyId, createCreditAccountCommand.OrgId, createCreditAccountCommand.UserId); err != nil {  
81 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 80 + if data, err7 := userService.OperatorFrom(createCreditAccountCommand.CompanyId, createCreditAccountCommand.OrgId, createCreditAccountCommand.UserId); err7 != nil {
  81 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取操作人失败")
82 } else { 82 } else {
83 operator = data 83 operator = data
84 } 84 }
85 85
86 // 账期结算单仓储初始化 86 // 账期结算单仓储初始化
87 var creditAccountRepository domain.CreditAccountRepository 87 var creditAccountRepository domain.CreditAccountRepository
88 - if value, err := factory.CreateCreditAccountRepository(map[string]interface{}{ 88 + if value, err8 := factory.CreateCreditAccountRepository(map[string]interface{}{
89 "transactionContext": transactionContext, 89 "transactionContext": transactionContext,
90 - }); err != nil {  
91 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 90 + }); err8 != nil {
  91 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err8.Error())
92 } else { 92 } else {
93 creditAccountRepository = value 93 creditAccountRepository = value
94 } 94 }
95 95
96 // 账期结算单DAO初始化 96 // 账期结算单DAO初始化
97 var creditAccountDao *dao.CreditAccountDao 97 var creditAccountDao *dao.CreditAccountDao
98 - if value, err := factory.CreateCreditAccountDao(map[string]interface{}{ 98 + if value, err9 := factory.CreateCreditAccountDao(map[string]interface{}{
99 "transactionContext": transactionContext, 99 "transactionContext": transactionContext,
100 - }); err != nil {  
101 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 100 + }); err9 != nil {
  101 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err9.Error())
102 } else { 102 } else {
103 creditAccountDao = value 103 creditAccountDao = value
104 } 104 }
105 105
106 // 分红预算单仓储初始化 106 // 分红预算单仓储初始化
107 var dividendsEstimateRepository domain.DividendsEstimateRepository 107 var dividendsEstimateRepository domain.DividendsEstimateRepository
108 - if value, err := factory.CreateDividendsEstimateRepository(map[string]interface{}{ 108 + if value, err10 := factory.CreateDividendsEstimateRepository(map[string]interface{}{
109 "transactionContext": transactionContext, 109 "transactionContext": transactionContext,
110 - }); err != nil {  
111 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 110 + }); err10 != nil {
  111 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err10.Error())
112 } else { 112 } else {
113 dividendsEstimateRepository = value 113 dividendsEstimateRepository = value
114 } 114 }
@@ -116,13 +116,14 @@ func (creditAccountService *CreditAccountService) CreateCreditAccount(createCred @@ -116,13 +116,14 @@ func (creditAccountService *CreditAccountService) CreateCreditAccount(createCred
116 // 批量转换分红预算单号列表 116 // 批量转换分红预算单号列表
117 dividendsEstimateIds, err := utils.SliceAtoi(createCreditAccountCommand.DividendsEstimateIds) 117 dividendsEstimateIds, err := utils.SliceAtoi(createCreditAccountCommand.DividendsEstimateIds)
118 if err != nil { 118 if err != nil {
119 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "分红预算单号错误") 119 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "分红预算单号类型错误")
120 } 120 }
  121 +
121 // 获取所选分红预算单 122 // 获取所选分红预算单
122 - if _, dividendsEstimates, err := dividendsEstimateRepository.Find(map[string]interface{}{ 123 + if _, dividendsEstimates, err11 := dividendsEstimateRepository.Find(map[string]interface{}{
123 "dividendsEstimateIds": dividendsEstimateIds, 124 "dividendsEstimateIds": dividendsEstimateIds,
124 - }); err != nil {  
125 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 125 + }); err11 != nil {
  126 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err11.Error())
126 } else { 127 } else {
127 // 校验共创用户是否一致 128 // 校验共创用户是否一致
128 if len(dividendsEstimates) > 0 { 129 if len(dividendsEstimates) > 0 {
@@ -143,42 +144,46 @@ func (creditAccountService *CreditAccountService) CreateCreditAccount(createCred @@ -143,42 +144,46 @@ func (creditAccountService *CreditAccountService) CreateCreditAccount(createCred
143 } 144 }
144 } 145 }
145 146
146 - // 预算操作 147 + // 预算明细
147 var creditAccounts []*domain.CreditAccount 148 var creditAccounts []*domain.CreditAccount
  149 + var accountDetail []*domain.AccountDetail
148 for _, dividendsEstimate := range dividendsEstimates { 150 for _, dividendsEstimate := range dividendsEstimates {
  151 + accountDetail = append(accountDetail, &domain.AccountDetail{
  152 + DividendsEstimateOrderNumber: dividendsEstimate.DividendsEstimateOrderNumber,
  153 + DividendsType: dividendsEstimate.DividendsType,
  154 + DividendsAmount: dividendsEstimate.DividendsAmount,
  155 + })
  156 + }
  157 +
149 // 生成账期结算单号 158 // 生成账期结算单号
150 - creditAccountNumber, err := creditAccountDao.GenerateCreditAccountNumber()  
151 - if err != nil {  
152 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 159 + creditAccountNumber, err12 := creditAccountDao.GenerateCreditAccountNumber()
  160 + if err12 != nil {
  161 + return nil, application.ThrowError(application.TRANSACTION_ERROR, "生成账期结算单号错误")
153 } 162 }
  163 +
154 // 生成账期结算单 164 // 生成账期结算单
155 newCreditAccount := &domain.CreditAccount{ 165 newCreditAccount := &domain.CreditAccount{
156 ActuallyPaidAmount: 0, 166 ActuallyPaidAmount: 0,
157 CreditAccountOrderNum: creditAccountNumber, 167 CreditAccountOrderNum: creditAccountNumber,
158 PaymentStatus: 1, 168 PaymentStatus: 1,
159 PaymentTime: time.Time{}, 169 PaymentTime: time.Time{},
160 - SettlementAmount: dividendsEstimate.DividendsAmount, 170 + SettlementAmount: dividendsEstimates[0].DividendsAmount,
161 SettlementTime: time.Now(), 171 SettlementTime: time.Now(),
162 - CooperationContractNumber: dividendsEstimate.CooperationContractNumber,  
163 - Participator: &domain.Participator{  
164 - UserId: dividendsEstimate.DividendsUser.UserId,  
165 - UserBaseId: dividendsEstimate.DividendsUser.UserBaseId,  
166 - Org: dividendsEstimate.DividendsUser.Org,  
167 - Orgs: dividendsEstimate.DividendsUser.Orgs,  
168 - Department: dividendsEstimate.DividendsUser.Department,  
169 - Roles: dividendsEstimate.DividendsUser.Roles,  
170 - UserInfo: dividendsEstimate.DividendsUser.UserInfo,  
171 - UserName: dividendsEstimate.DividendsUser.UserName,  
172 - UserPhone: dividendsEstimate.DividendsUser.UserPhone,  
173 - UserType: dividendsEstimate.DividendsUser.UserType,  
174 - Status: dividendsEstimate.DividendsUser.Status,  
175 - Company: dividendsEstimate.DividendsUser.Company,  
176 - },  
177 - AccountDetail: &domain.AccountDetail{  
178 - DividendsEstimateOrderNumber: dividendsEstimate.DividendsEstimateOrderNumber,  
179 - DividendsType: dividendsEstimate.DividendsType,  
180 - DividendsAmount: dividendsEstimate.DividendsAmount, 172 + Participator: &domain.Participator{ // 共创参与
  173 + UserId: dividendsEstimates[0].DividendsUser.UserId,
  174 + UserBaseId: dividendsEstimates[0].DividendsUser.UserBaseId,
  175 + Org: dividendsEstimates[0].DividendsUser.Org,
  176 + Orgs: dividendsEstimates[0].DividendsUser.Orgs,
  177 + Department: dividendsEstimates[0].DividendsUser.Department,
  178 + Roles: dividendsEstimates[0].DividendsUser.Roles,
  179 + UserInfo: dividendsEstimates[0].DividendsUser.UserInfo,
  180 + UserName: dividendsEstimates[0].DividendsUser.UserName,
  181 + UserPhone: dividendsEstimates[0].DividendsUser.UserPhone,
  182 + UserType: dividendsEstimates[0].DividendsUser.UserType,
  183 + Status: dividendsEstimates[0].DividendsUser.Status,
  184 + Company: dividendsEstimates[0].DividendsUser.Company,
181 }, 185 },
  186 + AccountDetail: accountDetail,
182 PaymentDocumentAttachment: nil, 187 PaymentDocumentAttachment: nil,
183 Org: organization, 188 Org: organization,
184 Company: company, 189 Company: company,
@@ -188,14 +193,15 @@ func (creditAccountService *CreditAccountService) CreateCreditAccount(createCred @@ -188,14 +193,15 @@ func (creditAccountService *CreditAccountService) CreateCreditAccount(createCred
188 DeletedAt: time.Time{}, 193 DeletedAt: time.Time{},
189 UpdatedAt: time.Time{}, 194 UpdatedAt: time.Time{},
190 } 195 }
191 - if creditAccount, err := creditAccountRepository.Save(newCreditAccount); err != nil {  
192 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 196 +
  197 + if creditAccount, err13 := creditAccountRepository.Save(newCreditAccount); err13 != nil {
  198 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err13.Error())
193 } else { 199 } else {
194 creditAccounts = append(creditAccounts, creditAccount) 200 creditAccounts = append(creditAccounts, creditAccount)
195 } 201 }
196 - }  
197 - if err := transactionContext.CommitTransaction(); err != nil {  
198 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 202 +
  203 + if err14 := transactionContext.CommitTransaction(); err14 != nil {
  204 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err14.Error())
199 } 205 }
200 return creditAccounts, nil 206 return creditAccounts, nil
201 } 207 }
@@ -315,13 +321,14 @@ func (creditAccountService *CreditAccountService) PayCreditAccount(payCreditAcco @@ -315,13 +321,14 @@ func (creditAccountService *CreditAccountService) PayCreditAccount(payCreditAcco
315 // 参数类型转换 321 // 参数类型转换
316 creditAccountId, _ := strconv.ParseInt(payCreditAccountCommand.CreditAccountId, 10, 64) 322 creditAccountId, _ := strconv.ParseInt(payCreditAccountCommand.CreditAccountId, 10, 64)
317 var creditAccountRepository domain.CreditAccountRepository 323 var creditAccountRepository domain.CreditAccountRepository
318 - if value, err := factory.CreateCreditAccountRepository(map[string]interface{}{ 324 + if value, err1 := factory.CreateCreditAccountRepository(map[string]interface{}{
319 "transactionContext": transactionContext, 325 "transactionContext": transactionContext,
320 - }); err != nil {  
321 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 326 + }); err1 != nil {
  327 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err1.Error())
322 } else { 328 } else {
323 creditAccountRepository = value 329 creditAccountRepository = value
324 } 330 }
  331 + // 获取待支付的账期结算单
325 creditAccount, err := creditAccountRepository.FindOne(map[string]interface{}{"creditAccountId": creditAccountId}) 332 creditAccount, err := creditAccountRepository.FindOne(map[string]interface{}{"creditAccountId": creditAccountId})
326 if err != nil { 333 if err != nil {
327 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 334 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
@@ -329,16 +336,18 @@ func (creditAccountService *CreditAccountService) PayCreditAccount(payCreditAcco @@ -329,16 +336,18 @@ func (creditAccountService *CreditAccountService) PayCreditAccount(payCreditAcco
329 if creditAccount == nil { 336 if creditAccount == nil {
330 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(creditAccountId, 10))) 337 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(creditAccountId, 10)))
331 } 338 }
332 - if err := creditAccount.Update(tool_funs.SimpleStructToMap(payCreditAccountCommand)); err != nil {  
333 - return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) 339 + if err2 := creditAccount.Update(tool_funs.SimpleStructToMap(payCreditAccountCommand)); err2 != nil {
  340 + return nil, application.ThrowError(application.BUSINESS_ERROR, err2.Error())
334 } 341 }
335 - if creditAccount, err := creditAccountRepository.Save(creditAccount); err != nil {  
336 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 342 + // 支付状态设置
  343 + creditAccount.PaymentStatus = 2
  344 + if creditAccountSaved, err4 := creditAccountRepository.Save(creditAccount); err4 != nil {
  345 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err4.Error())
337 } else { 346 } else {
338 - if err := transactionContext.CommitTransaction(); err != nil {  
339 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 347 + if err3 := transactionContext.CommitTransaction(); err3 != nil {
  348 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err3.Error())
340 } 349 }
341 - return creditAccount, nil 350 + return creditAccountSaved, nil
342 } 351 }
343 } 352 }
344 353
@@ -25,7 +25,7 @@ type CreditAccount struct { @@ -25,7 +25,7 @@ type CreditAccount struct {
25 // 参与类型 25 // 参与类型
26 ParticipateType string `json:"participateType"` 26 ParticipateType string `json:"participateType"`
27 // 结算明细 27 // 结算明细
28 - AccountDetail *AccountDetail `json:"accountDetail"` 28 + AccountDetail []*AccountDetail `json:"accountDetail"`
29 // 支付凭证附件 29 // 支付凭证附件
30 PaymentDocumentAttachment *Attachment `json:"paymentDocumentAttachment"` 30 PaymentDocumentAttachment *Attachment `json:"paymentDocumentAttachment"`
31 // 数据所属组织机构 31 // 数据所属组织机构
@@ -207,9 +207,9 @@ func (ptr *CooperationStatisticsService) GetContractDividends(queryOptions map[s @@ -207,9 +207,9 @@ func (ptr *CooperationStatisticsService) GetContractDividends(queryOptions map[s
207 dividends = append(dividends, map[string]interface{}{ 207 dividends = append(dividends, map[string]interface{}{
208 "creditAccountId": a.CreditAccountId, 208 "creditAccountId": a.CreditAccountId,
209 "orderGoodName": "", 209 "orderGoodName": "",
210 - "dividendsType": a.AccountDetail.DividendsType, 210 + //"dividendsType": a.AccountDetail.DividendsType,
211 "dividendsRatio": 0, 211 "dividendsRatio": 0,
212 - "dividendsAmount": a.AccountDetail.DividendsAmount, 212 + //"dividendsAmount": a.AccountDetail.DividendsAmount,
213 "dividendsUser": map[string]interface{}{ 213 "dividendsUser": map[string]interface{}{
214 "userInfo": a.Participator.UserInfo, 214 "userInfo": a.Participator.UserInfo,
215 "userId": a.Participator.UserId, 215 "userId": a.Participator.UserId,
@@ -28,7 +28,7 @@ type CreditAccount struct { @@ -28,7 +28,7 @@ type CreditAccount struct {
28 // 参与类型 28 // 参与类型
29 ParticipateType string `comment:"参与类型,承接人,推荐人,业务员"` 29 ParticipateType string `comment:"参与类型,承接人,推荐人,业务员"`
30 // 结算明细 30 // 结算明细
31 - AccountDetail *domain.AccountDetail `comment:"结算明细"` 31 + AccountDetail []*domain.AccountDetail `comment:"结算明细"`
32 // 支付凭证附件 32 // 支付凭证附件
33 PaymentDocumentAttachment *domain.Attachment `comment:"支付凭证附件"` 33 PaymentDocumentAttachment *domain.Attachment `comment:"支付凭证附件"`
34 // 数据所属组织机构 34 // 数据所属组织机构