作者 yangfu

共创统计服务

  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type CooperationStatisticsQuery struct {
  12 + //// 页面大小
  13 + //PageNumber int64 `cname:"页码" json:"pageNumber,omitempty"`
  14 + //// 页面大小
  15 + //PageSize int64 `cname:"页面大小" json:"pageSize,omitempty"`
  16 + //// 公司ID,通过集成REST上下文获取
  17 + //CompanyId int64 `cname:"公司ID" json:"companyId"`
  18 + //// 组织机构ID
  19 + //OrgId int64 `cname:"组织机构ID" json:"orgId" valid:"Required"`
  20 + //// 用户ID,通过集成REST上下文获取,可翻译成发起人、承接人、推荐人、业务员
  21 + ////UserId int64 `cname:"用户ID" json:"userId,string" valid:"Required"`
  22 + //// 用户基础数据id
  23 + //UserBaseId int64 `cname:"用户基础数据ID" json:"userBaseId,string"`
  24 + Action string `json:"action"`
  25 + QueryOptions map[string]interface{} `json:"queryOptions"`
  26 +}
  27 +
  28 +func (checkUndertakerQuery *CooperationStatisticsQuery) Valid(validation *validation.Validation) {
  29 + //validation.SetError("CustomValid", "未实现的自定义认证")
  30 +}
  31 +
  32 +func (checkUndertakerQuery *CooperationStatisticsQuery) ValidateQuery() error {
  33 + valid := validation.Validation{}
  34 + b, err := valid.Valid(checkUndertakerQuery)
  35 + if err != nil {
  36 + return err
  37 + }
  38 + if !b {
  39 + elem := reflect.TypeOf(checkUndertakerQuery).Elem()
  40 + for _, validErr := range valid.Errors {
  41 + field, isExist := elem.FieldByName(validErr.Field)
  42 + if isExist {
  43 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  44 + } else {
  45 + return fmt.Errorf(validErr.Message)
  46 + }
  47 + }
  48 + }
  49 + return nil
  50 +}
  1 +package service
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/core/application"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/cooperationStatistics/query"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/factory"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/domain_service"
  8 +)
  9 +
  10 +// ContractDividendStatistics 合约分红统计
  11 +type CooperationStatisticsService struct {
  12 +}
  13 +
  14 +// CooperationContractStatistics 共创合约统计
  15 +func (svr *CooperationStatisticsService) CooperationContractStatistics(contractStatisticsQuery *query.CooperationStatisticsQuery) (interface{}, error) {
  16 + if err := contractStatisticsQuery.ValidateQuery(); err != nil {
  17 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  18 + }
  19 + var err error
  20 + transactionContext, err := factory.CreateTransactionContext(nil)
  21 + if err != nil {
  22 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  23 + }
  24 + if err := transactionContext.StartTransaction(); err != nil {
  25 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  26 + }
  27 + defer func() {
  28 + _ = transactionContext.RollbackTransaction()
  29 + }()
  30 +
  31 + contractDividendsService, err := factory.CreateContractDividendsService(map[string]interface{}{
  32 + "transactionContext": transactionContext,
  33 + })
  34 + var res interface{}
  35 + switch contractStatisticsQuery.Action {
  36 + case domain_service.ContractDividends:
  37 + res, err = contractDividendsService.SearchContractDividends(contractStatisticsQuery.QueryOptions)
  38 + }
  39 + if err != nil {
  40 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  41 + }
  42 + if err := transactionContext.CommitTransaction(); err != nil {
  43 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  44 + }
  45 + return res, nil
  46 +}
1 package factory 1 package factory
2 2
3 import ( 3 import (
  4 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
4 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service" 5 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
5 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/domain_service" 6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/domain_service"
6 ) 7 )
@@ -20,3 +21,11 @@ func CreateDepartmentService(options map[string]interface{}) (service.Department @@ -20,3 +21,11 @@ func CreateDepartmentService(options map[string]interface{}) (service.Department
20 func CreateOrganizationService(options map[string]interface{}) (service.OrgService, error) { 21 func CreateOrganizationService(options map[string]interface{}) (service.OrgService, error) {
21 return domain_service.NewOrganizationService() 22 return domain_service.NewOrganizationService()
22 } 23 }
  24 +
  25 +func CreateContractDividendsService(options map[string]interface{}) (*domain_service.ContractStatisticsService, error) {
  26 + var transactionContext *pgTransaction.TransactionContext
  27 + if value, ok := options["transactionContext"]; ok {
  28 + transactionContext = value.(*pgTransaction.TransactionContext)
  29 + }
  30 + return domain_service.NewContractStatisticsService(transactionContext)
  31 +}
  1 +package domain_service
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/go-pg/pg/v10"
  6 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
  8 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
  9 + "time"
  10 +)
  11 +
  12 +const (
  13 + ContractDividends = "ContractDividends"
  14 +)
  15 +
  16 +// ContractStatisticsService 共创统计服务
  17 +type ContractStatisticsService struct {
  18 + transactionContext *pgTransaction.TransactionContext
  19 +}
  20 +
  21 +/***** 1.合约分红模块 *****/
  22 +
  23 +// 分红合约搜索
  24 +//
  25 +// queryOptions 查询参数
  26 +// - pageNumber
  27 +// - pageSize
  28 +// 按组织
  29 +// - companyId
  30 +// - orgId
  31 +// 按个人
  32 +// - userBaseId
  33 +func (svr *ContractStatisticsService) SearchContractDividends(queryOptions map[string]interface{}) (interface{}, error) {
  34 + // 1.根据个人、企业查询合约列表
  35 + var contracts []*domain.CooperationContract
  36 + var err error
  37 + if _, ok := queryOptions["userBaseId"]; ok {
  38 + contracts, err = svr.getUserContracts(queryOptions)
  39 + } else if _, ok := queryOptions["orgId"]; ok {
  40 + contracts, err = svr.getCompanyContracts(queryOptions)
  41 + }
  42 + if err != nil {
  43 + return nil, err
  44 + }
  45 +
  46 + // 2.根据合约查询分红预算
  47 + var numbers []string
  48 + var results = make([]*searchContractDividendsResult, 0)
  49 + for i := range contracts {
  50 + item := contracts[i]
  51 + resultItem := &searchContractDividendsResult{
  52 + CooperationContractId: item.CooperationContractId,
  53 + CooperationContractName: item.CooperationContractName,
  54 + CooperationContractNumber: item.CooperationContractNumber,
  55 + Status: item.Status,
  56 + CreatedAt: item.CreatedAt,
  57 + }
  58 + results = append(results, resultItem)
  59 + numbers = append(numbers, item.CooperationContractNumber)
  60 + }
  61 + mapEstimate, err := svr.getContractsDividendsEstimate(numbers)
  62 + if err != nil {
  63 + return nil, err
  64 + }
  65 + for i := range results {
  66 + if v, ok := mapEstimate[results[i].CooperationContractNumber]; ok {
  67 + results[i].DividendsAmount = v.DividendsAmount
  68 + }
  69 + }
  70 +
  71 + // TODO: 3.根据合约查询订单金额
  72 + return results, nil
  73 +}
  74 +
  75 +// getUserContracts 获取用户的合约列表
  76 +//
  77 +// p1 p1_desc
  78 +func (svr *ContractStatisticsService) getUserContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
  79 + undertakerRepository, _ := repository.NewCooperationContractUndertakerRepository(svr.transactionContext)
  80 + _, undertakers, err := undertakerRepository.Find(queryOptions)
  81 + var numbers []string
  82 + for i := range undertakers {
  83 + numbers = append(numbers, undertakers[i].CooperationContractNumber)
  84 + }
  85 + if len(numbers) == 0 {
  86 + return []*domain.CooperationContract{}, nil
  87 + }
  88 + queryOptions["inCooperationContractNumber"] = numbers
  89 + contractRepository, _ := repository.NewCooperationContractRepository(svr.transactionContext)
  90 + // TODO: 参数查询条件
  91 + _, contracts, err := contractRepository.Find(queryOptions)
  92 + return contracts, err
  93 +}
  94 +
  95 +// getCompanyContracts 获取组织合约列表
  96 +//
  97 +// p1 p1_desc
  98 +func (svr *ContractStatisticsService) getCompanyContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
  99 + contractRepository, _ := repository.NewCooperationContractRepository(svr.transactionContext)
  100 + // TODO: 参数查询条件
  101 + _, contracts, err := contractRepository.Find(queryOptions)
  102 + return contracts, err
  103 +}
  104 +
  105 +// getContractsDividendsEstimate 合约分红预算
  106 +func (svr *ContractStatisticsService) getContractsDividendsEstimate(numbers []string) (map[string]*domain.DividendsEstimate, error) {
  107 + var estimates []*domain.DividendsEstimate
  108 + var resMap = make(map[string]*domain.DividendsEstimate)
  109 + if len(numbers) == 0 {
  110 + return resMap, nil
  111 + }
  112 + _, err := svr.transactionContext.PgDd.Query(estimates, `select cooperation_contract_number,sum(dividends_amount) dividends_amount from dividends_estimates
  113 +where cooperation_contract_number in (?)
  114 +group by cooperation_contract_number
  115 +`, pg.In(numbers))
  116 + if err != nil {
  117 + return nil, err
  118 + }
  119 +
  120 + for i := range estimates {
  121 + resMap[estimates[i].CooperationContractNumber] = estimates[i]
  122 + }
  123 + return resMap, nil
  124 +}
  125 +
  126 +type searchContractDividendsResult struct {
  127 + // 共创合约ID
  128 + CooperationContractId int64 `json:"cooperationContractId,string"`
  129 + // 共创合约名称
  130 + CooperationContractName string `json:"cooperationContractName"`
  131 + // 共创合约编号
  132 + CooperationContractNumber string `json:"cooperationContractNumber"`
  133 + // 合约状态,1恢复,2暂停
  134 + Status int32 `json:"cooperationContractStatus"`
  135 + // 分红预算
  136 + DividendsAmount float64 `json:"dividendsAmount"`
  137 + // 分红订单金额
  138 + DividendsOrderAmount float64 `json:"dividendsOrderAmount"`
  139 + // 创建时间
  140 + CreatedAt time.Time `json:"createdAt"`
  141 +}
  142 +
  143 +func NewContractStatisticsService(transactionContext *pgTransaction.TransactionContext) (*ContractStatisticsService, error) {
  144 + if transactionContext == nil {
  145 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  146 + } else {
  147 + return &ContractStatisticsService{
  148 + transactionContext: transactionContext,
  149 + }, nil
  150 + }
  151 +}
  1 +package controllers
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/cooperationStatistics/query"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/cooperationStatistics/service"
  6 +)
  7 +
  8 +type CooperationStatisticsController struct {
  9 + BaseController
  10 +}
  11 +
  12 +func (controller *CooperationStatisticsController) CooperationStatistics() {
  13 + cooperationStatisticsService := service.CooperationStatisticsService{}
  14 + searchCooperationProjectQuery := &query.CooperationStatisticsQuery{}
  15 + controller.Unmarshal(searchCooperationProjectQuery)
  16 + data, err := cooperationStatisticsService.CooperationContractStatistics(searchCooperationProjectQuery)
  17 + controller.Response(data, err)
  18 +}
  1 +package routers
  2 +
  3 +import (
  4 + "github.com/beego/beego/v2/server/web"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/port/beego/controllers"
  6 +)
  7 +
  8 +func init() {
  9 + web.Router("/cooperation-statistics", &controllers.CooperationStatisticsController{}, "Post:CooperationStatistics")
  10 +}