作者 yangfu

共创统计服务

package query
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type CooperationStatisticsQuery struct {
//// 页面大小
//PageNumber int64 `cname:"页码" json:"pageNumber,omitempty"`
//// 页面大小
//PageSize int64 `cname:"页面大小" json:"pageSize,omitempty"`
//// 公司ID,通过集成REST上下文获取
//CompanyId int64 `cname:"公司ID" json:"companyId"`
//// 组织机构ID
//OrgId int64 `cname:"组织机构ID" json:"orgId" valid:"Required"`
//// 用户ID,通过集成REST上下文获取,可翻译成发起人、承接人、推荐人、业务员
////UserId int64 `cname:"用户ID" json:"userId,string" valid:"Required"`
//// 用户基础数据id
//UserBaseId int64 `cname:"用户基础数据ID" json:"userBaseId,string"`
Action string `json:"action"`
QueryOptions map[string]interface{} `json:"queryOptions"`
}
func (checkUndertakerQuery *CooperationStatisticsQuery) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (checkUndertakerQuery *CooperationStatisticsQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(checkUndertakerQuery)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(checkUndertakerQuery).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
package service
import (
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/cooperationStatistics/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/domain_service"
)
// ContractDividendStatistics 合约分红统计
type CooperationStatisticsService struct {
}
// CooperationContractStatistics 共创合约统计
func (svr *CooperationStatisticsService) CooperationContractStatistics(contractStatisticsQuery *query.CooperationStatisticsQuery) (interface{}, error) {
if err := contractStatisticsQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
var err error
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
contractDividendsService, err := factory.CreateContractDividendsService(map[string]interface{}{
"transactionContext": transactionContext,
})
var res interface{}
switch contractStatisticsQuery.Action {
case domain_service.ContractDividends:
res, err = contractDividendsService.SearchContractDividends(contractStatisticsQuery.QueryOptions)
}
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return res, nil
}
... ...
package factory
import (
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/domain_service"
)
... ... @@ -20,3 +21,11 @@ func CreateDepartmentService(options map[string]interface{}) (service.Department
func CreateOrganizationService(options map[string]interface{}) (service.OrgService, error) {
return domain_service.NewOrganizationService()
}
func CreateContractDividendsService(options map[string]interface{}) (*domain_service.ContractStatisticsService, error) {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pgTransaction.TransactionContext)
}
return domain_service.NewContractStatisticsService(transactionContext)
}
... ...
package domain_service
import (
"fmt"
"github.com/go-pg/pg/v10"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
"time"
)
const (
ContractDividends = "ContractDividends"
)
// ContractStatisticsService 共创统计服务
type ContractStatisticsService struct {
transactionContext *pgTransaction.TransactionContext
}
/***** 1.合约分红模块 *****/
// 分红合约搜索
//
// queryOptions 查询参数
// - pageNumber
// - pageSize
// 按组织
// - companyId
// - orgId
// 按个人
// - userBaseId
func (svr *ContractStatisticsService) SearchContractDividends(queryOptions map[string]interface{}) (interface{}, error) {
// 1.根据个人、企业查询合约列表
var contracts []*domain.CooperationContract
var err error
if _, ok := queryOptions["userBaseId"]; ok {
contracts, err = svr.getUserContracts(queryOptions)
} else if _, ok := queryOptions["orgId"]; ok {
contracts, err = svr.getCompanyContracts(queryOptions)
}
if err != nil {
return nil, err
}
// 2.根据合约查询分红预算
var numbers []string
var results = make([]*searchContractDividendsResult, 0)
for i := range contracts {
item := contracts[i]
resultItem := &searchContractDividendsResult{
CooperationContractId: item.CooperationContractId,
CooperationContractName: item.CooperationContractName,
CooperationContractNumber: item.CooperationContractNumber,
Status: item.Status,
CreatedAt: item.CreatedAt,
}
results = append(results, resultItem)
numbers = append(numbers, item.CooperationContractNumber)
}
mapEstimate, err := svr.getContractsDividendsEstimate(numbers)
if err != nil {
return nil, err
}
for i := range results {
if v, ok := mapEstimate[results[i].CooperationContractNumber]; ok {
results[i].DividendsAmount = v.DividendsAmount
}
}
// TODO: 3.根据合约查询订单金额
return results, nil
}
// getUserContracts 获取用户的合约列表
//
// p1 p1_desc
func (svr *ContractStatisticsService) getUserContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
undertakerRepository, _ := repository.NewCooperationContractUndertakerRepository(svr.transactionContext)
_, undertakers, err := undertakerRepository.Find(queryOptions)
var numbers []string
for i := range undertakers {
numbers = append(numbers, undertakers[i].CooperationContractNumber)
}
if len(numbers) == 0 {
return []*domain.CooperationContract{}, nil
}
queryOptions["inCooperationContractNumber"] = numbers
contractRepository, _ := repository.NewCooperationContractRepository(svr.transactionContext)
// TODO: 参数查询条件
_, contracts, err := contractRepository.Find(queryOptions)
return contracts, err
}
// getCompanyContracts 获取组织合约列表
//
// p1 p1_desc
func (svr *ContractStatisticsService) getCompanyContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
contractRepository, _ := repository.NewCooperationContractRepository(svr.transactionContext)
// TODO: 参数查询条件
_, contracts, err := contractRepository.Find(queryOptions)
return contracts, err
}
// getContractsDividendsEstimate 合约分红预算
func (svr *ContractStatisticsService) getContractsDividendsEstimate(numbers []string) (map[string]*domain.DividendsEstimate, error) {
var estimates []*domain.DividendsEstimate
var resMap = make(map[string]*domain.DividendsEstimate)
if len(numbers) == 0 {
return resMap, nil
}
_, err := svr.transactionContext.PgDd.Query(estimates, `select cooperation_contract_number,sum(dividends_amount) dividends_amount from dividends_estimates
where cooperation_contract_number in (?)
group by cooperation_contract_number
`, pg.In(numbers))
if err != nil {
return nil, err
}
for i := range estimates {
resMap[estimates[i].CooperationContractNumber] = estimates[i]
}
return resMap, nil
}
type searchContractDividendsResult struct {
// 共创合约ID
CooperationContractId int64 `json:"cooperationContractId,string"`
// 共创合约名称
CooperationContractName string `json:"cooperationContractName"`
// 共创合约编号
CooperationContractNumber string `json:"cooperationContractNumber"`
// 合约状态,1恢复,2暂停
Status int32 `json:"cooperationContractStatus"`
// 分红预算
DividendsAmount float64 `json:"dividendsAmount"`
// 分红订单金额
DividendsOrderAmount float64 `json:"dividendsOrderAmount"`
// 创建时间
CreatedAt time.Time `json:"createdAt"`
}
func NewContractStatisticsService(transactionContext *pgTransaction.TransactionContext) (*ContractStatisticsService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &ContractStatisticsService{
transactionContext: transactionContext,
}, nil
}
}
... ...
package controllers
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/cooperationStatistics/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/cooperationStatistics/service"
)
type CooperationStatisticsController struct {
BaseController
}
func (controller *CooperationStatisticsController) CooperationStatistics() {
cooperationStatisticsService := service.CooperationStatisticsService{}
searchCooperationProjectQuery := &query.CooperationStatisticsQuery{}
controller.Unmarshal(searchCooperationProjectQuery)
data, err := cooperationStatisticsService.CooperationContractStatistics(searchCooperationProjectQuery)
controller.Response(data, err)
}
... ...
package routers
import (
"github.com/beego/beego/v2/server/web"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/port/beego/controllers"
)
func init() {
web.Router("/cooperation-statistics", &controllers.CooperationStatisticsController{}, "Post:CooperationStatistics")
}
... ...