作者 陈志颖

feat:完善账期结算服务

... ... @@ -9,6 +9,8 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
"strconv"
"time"
)
... ... @@ -17,7 +19,7 @@ import (
type CreditAccountService struct {
}
// CreateCreditAccount 创建账期结算单服务
// CreateCreditAccount 创建账期结算单服务(账期结算)
func (creditAccountService *CreditAccountService) CreateCreditAccount(createCreditAccountCommand *command.CreateCreditAccountCommand) (interface{}, error) {
if err := createCreditAccountCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ... @@ -81,15 +83,72 @@ func (creditAccountService *CreditAccountService) CreateCreditAccount(createCred
operator = data
}
// 账期结算单仓储初始化
var creditAccountRepository domain.CreditAccountRepository
if value, err := factory.CreateCreditAccountRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
creditAccountRepository = value
}
// 账期结算单DAO初始化
var creditAccountDao *dao.CreditAccountDao
if value, err := factory.CreateCreditAccountDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
creditAccountDao = value
}
// 分红预算单仓储初始化
var dividendsEstimateRepository domain.DividendsEstimateRepository
if value, err := factory.CreateDividendsEstimateRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
dividendsEstimateRepository = value
}
dividendsEstimateIds, _ := utils.SliceAtoi(createCreditAccountCommand.DividendsEstimateIds)
// 获取所选分红预算单
if _, dividendsEstimates, err := dividendsEstimateRepository.Find(map[string]interface{}{
"dividendsEstimateIds": dividendsEstimateIds,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
var creditAccounts []*domain.CreditAccount
for _, dividendsEstimate := range dividendsEstimates {
// 生成账期结算单号
creditAccountNumber, err := creditAccountDao.GenerateCreditAccountNumber()
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
// 生成账期结算单
newCreditAccount := &domain.CreditAccount{
ActuallyPaidAmount: 0,
CreditAccountOrderNum: "",
PaymentStatus: 0,
CreditAccountOrderNum: creditAccountNumber,
PaymentStatus: 1,
PaymentTime: time.Time{},
SettlementAmount: 0,
SettlementTime: time.Time{},
CooperationContractNumber: "",
Participator: nil,
SettlementAmount: dividendsEstimate.DividendsAmount,
SettlementTime: time.Now(),
CooperationContractNumber: dividendsEstimate.CooperationContractNumber,
Participator: &domain.Participator{
UserId: dividendsEstimate.DividendsUser.UserId,
UserBaseId: dividendsEstimate.DividendsUser.UserBaseId,
Org: dividendsEstimate.DividendsUser.Org,
Orgs: dividendsEstimate.DividendsUser.Orgs,
Department: dividendsEstimate.DividendsUser.Department,
Roles: dividendsEstimate.DividendsUser.Roles,
UserInfo: dividendsEstimate.DividendsUser.UserInfo,
UserName: dividendsEstimate.DividendsUser.UserName,
UserPhone: dividendsEstimate.DividendsUser.UserPhone,
UserType: dividendsEstimate.DividendsUser.UserType,
Status: dividendsEstimate.DividendsUser.Status,
Company: dividendsEstimate.DividendsUser.Company,
},
PaymentDocumentAttachment: nil,
Org: organization,
Company: company,
... ... @@ -99,22 +158,16 @@ func (creditAccountService *CreditAccountService) CreateCreditAccount(createCred
DeletedAt: time.Time{},
UpdatedAt: time.Time{},
}
var creditAccountRepository domain.CreditAccountRepository
if value, err := factory.CreateCreditAccountRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
creditAccountRepository = value
}
if creditAccount, err := creditAccountRepository.Save(newCreditAccount); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
creditAccounts = append(creditAccounts, creditAccount)
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return creditAccount, nil
return creditAccounts, nil
}
}
... ... @@ -139,7 +192,7 @@ func (creditAccountService *CreditAccountService) CreditAccountRanking(creditAcc
return nil, nil
}
// GetCreditAccount 返回账期结算单服务
// GetCreditAccount 返回账期结算单详情
func (creditAccountService *CreditAccountService) GetCreditAccount(getCreditAccountQuery *query.GetCreditAccountQuery) (interface{}, error) {
if err := getCreditAccountQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ... @@ -176,7 +229,7 @@ func (creditAccountService *CreditAccountService) GetCreditAccount(getCreditAcco
}
}
// ListCreditAccount 返回账期结算单服务列表
// ListCreditAccount 返回账期结算单列表
func (creditAccountService *CreditAccountService) ListCreditAccount(listCreditAccountQuery *query.ListCreditAccountQuery) (interface{}, error) {
if err := listCreditAccountQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ... @@ -259,7 +312,7 @@ func (creditAccountService *CreditAccountService) PayCreditAccount(payCreditAcco
}
}
// RemoveCreditAccount 移除账期结算单服务
// RemoveCreditAccount 移除账期结算单
func (creditAccountService *CreditAccountService) RemoveCreditAccount(removeCreditAccountCommand *command.RemoveCreditAccountCommand) (interface{}, error) {
if err := removeCreditAccountCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ... @@ -337,7 +390,7 @@ func (creditAccountService *CreditAccountService) SearchCreditAccount(searchCred
}
}
// UpdateCreditAccount 更新账期结算单服务
// UpdateCreditAccount 更新账期结算单
func (creditAccountService *CreditAccountService) UpdateCreditAccount(updateCreditAccountCommand *command.UpdateCreditAccountCommand) (interface{}, error) {
if err := updateCreditAccountCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ...
... ... @@ -82,7 +82,7 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) CreateDivide
operator = data
}
// 分红退货单DAO初始化
// 分红退货单DAO初始化
var dividendsReturnedOrderDao *dao.DividendsReturnedOrderDao
if value, err := factory.CreateDividendsReturnedOrderDao(map[string]interface{}{
"transactionContext": transactionContext,
... ...
... ... @@ -44,3 +44,11 @@ func CreateDividendsReturnedOrderDao(options map[string]interface{}) (*dao.Divid
}
return dao.NewDividendsReturnedOrderDao(transactionContext)
}
func CreateCreditAccountDao(options map[string]interface{}) (*dao.CreditAccountDao, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return dao.NewCreditAccountDao(transactionContext)
}
... ...
package dao
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
"time"
)
type CreditAccountDao struct {
transactionContext *pgTransaction.TransactionContext
}
// GenerateCreditAccountNumber 生成账期结算单号
func (dao *CreditAccountDao) GenerateCreditAccountNumber() (string, error) {
tx := dao.transactionContext.PgTx
var creditAccountModels []*models.CreditAccount
query := tx.Model(&creditAccountModels)
currentTime := time.Now()
todayZeroTime := utils.GetZeroTime(currentTime)
nextDayZeroTime := utils.GetNextDayZeroTime(currentTime)
query.Where("credit_account.created_at >= ?", todayZeroTime)
query.Where("credit_account.created_at < ?", nextDayZeroTime)
if count, err := query.AllWithDeleted().SelectAndCount(); err != nil {
return "", err
} else {
countStr := fmt.Sprintf("%03d", count+1)
timestamp := currentTime.Unix()
timeNow := time.Unix(timestamp, 0)
timeString := timeNow.Format("20060102")
timeString = timeString[2:len(timeString)]
creditAccountNumber := "JS" + timeString + "#" + countStr
return creditAccountNumber, nil
}
}
// CheckCreditAccountNumberAvailable 校验账期结算单号是否唯一
func (dao *CreditAccountDao) CheckCreditAccountNumberAvailable(queryOptions map[string]interface{}) (bool, error) {
tx := dao.transactionContext.PgTx
var creditAccountModels []*models.CreditAccount
query := tx.Model(&creditAccountModels)
if creditAccountNumber, ok := queryOptions["creditAccountNumber"]; ok && creditAccountNumber != "" {
query = query.Where("credit_account_number = ?", creditAccountNumber)
}
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query = query.Where(`credit_account.company @> '{"companyId":"?"}'`, companyId)
}
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query = query.Where(`credit_account.org @> '{"orgId":"?"}'`, orgId)
}
ok, err := query.Exists()
return !ok, err
}
func NewCreditAccountDao(transactionContext *pgTransaction.TransactionContext) (*CreditAccountDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为空")
} else {
return &CreditAccountDao{
transactionContext: transactionContext,
}, nil
}
}
... ...
... ... @@ -24,6 +24,7 @@ func (repository *CooperationContractRelevantRepository) nextIdentify() (int64,
id, err := IdWorker.NextId()
return id, err
}
func (repository *CooperationContractRelevantRepository) Save(cooperationContractRelevant *domain.CooperationContractRelevant) (*domain.CooperationContractRelevant, error) {
sqlBuildFields := []string{
"cooperation_contract_relevant_id",
... ... @@ -134,6 +135,7 @@ func (repository *CooperationContractRelevantRepository) Save(cooperationContrac
}
return cooperationContractRelevant, nil
}
func (repository *CooperationContractRelevantRepository) Remove(cooperationContractRelevant *domain.CooperationContractRelevant) (*domain.CooperationContractRelevant, error) {
tx := repository.transactionContext.PgTx
cooperationContractRelevantModel := new(models.CooperationContractRelevant)
... ... @@ -143,6 +145,7 @@ func (repository *CooperationContractRelevantRepository) Remove(cooperationContr
}
return cooperationContractRelevant, nil
}
func (repository *CooperationContractRelevantRepository) FindOne(queryOptions map[string]interface{}) (*domain.CooperationContractRelevant, error) {
tx := repository.transactionContext.PgTx
cooperationContractRelevantModel := new(models.CooperationContractRelevant)
... ... @@ -161,6 +164,7 @@ func (repository *CooperationContractRelevantRepository) FindOne(queryOptions ma
return transform.TransformToCooperationContractRelevantDomainModelFromPgModels(cooperationContractRelevantModel)
}
}
func (repository *CooperationContractRelevantRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.CooperationContractRelevant, error) {
tx := repository.transactionContext.PgTx
var cooperationContractRelevantModels []*models.CooperationContractRelevant
... ... @@ -187,6 +191,7 @@ func (repository *CooperationContractRelevantRepository) Find(queryOptions map[s
return int64(count), cooperationContractRelevants, nil
}
}
func NewCooperationContractRelevantRepository(transactionContext *pgTransaction.TransactionContext) (*CooperationContractRelevantRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
... ...
... ... @@ -229,6 +229,9 @@ func (repository *DividendsEstimateRepository) Find(queryOptions map[string]inte
if dividendsEstimateOrderNumber, ok := queryOptions["dividendsEstimateOrderNumber"]; ok && dividendsEstimateOrderNumber != "" {
query.Where("dividends_estimate_order_number ilike ?", fmt.Sprintf("%%%s%%", dividendsEstimateOrderNumber))
}
if dividendsEstimateIds, ok := queryOptions["dividendsEstimateIds"]; ok && len(dividendsEstimateIds.([]int64)) > 0 {
query.Where("dividends_estimate_id IN (?)", pg.In(dividendsEstimateIds))
}
offsetLimitFlag := true
if offsetLimit, ok := queryOptions["offsetLimit"]; ok {
offsetLimitFlag = offsetLimit.(bool)
... ...