作者 yangfu

分红统计

@@ -43,6 +43,10 @@ func (svr *CooperationStatisticsService) CooperationContractStatistics(contractS @@ -43,6 +43,10 @@ func (svr *CooperationStatisticsService) CooperationContractStatistics(contractS
43 res, err = statisticsService.CooperationModeStatistics(contractStatisticsQuery.QueryOptions) 43 res, err = statisticsService.CooperationModeStatistics(contractStatisticsQuery.QueryOptions)
44 case domain_service.CompanyDividendsStatistics: 44 case domain_service.CompanyDividendsStatistics:
45 res, err = statisticsService.CompanyDividendsStatistics(contractStatisticsQuery.QueryOptions) 45 res, err = statisticsService.CompanyDividendsStatistics(contractStatisticsQuery.QueryOptions)
  46 + case domain_service.DividendsStatistics:
  47 + res, err = statisticsService.DividendsStatistics(contractStatisticsQuery.QueryOptions)
  48 + case domain_service.SearchDividendsEstimates:
  49 + res, err = statisticsService.SearchDividendsEstimates(contractStatisticsQuery.QueryOptions)
46 } 50 }
47 if err != nil { 51 if err != nil {
48 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) 52 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
@@ -53,6 +53,26 @@ func (dao *CreditAccountDao) CheckCreditAccountNumberAvailable(queryOptions map[ @@ -53,6 +53,26 @@ func (dao *CreditAccountDao) CheckCreditAccountNumberAvailable(queryOptions map[
53 return !ok, err 53 return !ok, err
54 } 54 }
55 55
  56 +// 分红统计(分红明细)
  57 +func (dao *CreditAccountDao) DividendsStatistics(queryOptions map[string]interface{}, v interface{}) error {
  58 + sql := `
  59 +select
  60 +sum(settlement_amount) total,
  61 +sum((case when payment_status = 1 then actually_paid_amount else 0 end)) paid,
  62 +sum((case when settlement_time is not null then settlement_amount else 0 end)) accounted
  63 +from credit_accounts
  64 +where created_at>? and created_at<?
  65 +`
  66 + if v, ok := queryOptions["userBaseId"]; ok {
  67 + sql += fmt.Sprintf(`and participator->>'userBaseId'='%v'`, v)
  68 + }
  69 + if v, ok := queryOptions["orgId"]; ok {
  70 + sql += fmt.Sprintf(`and org->>'orgId'= '%v'`, v)
  71 + }
  72 + _, err := dao.transactionContext.PgDd.Query(v, sql, queryOptions["beginTime"], queryOptions["endTime"])
  73 + return err
  74 +}
  75 +
56 func NewCreditAccountDao(transactionContext *pgTransaction.TransactionContext) (*CreditAccountDao, error) { 76 func NewCreditAccountDao(transactionContext *pgTransaction.TransactionContext) (*CreditAccountDao, error) {
57 if transactionContext == nil { 77 if transactionContext == nil {
58 return nil, fmt.Errorf("transactionContext参数不能为空") 78 return nil, fmt.Errorf("transactionContext参数不能为空")
@@ -7,7 +7,9 @@ import ( @@ -7,7 +7,9 @@ import (
7 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 7 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
8 "github.com/linmadan/egglib-go/utils/tool_funs" 8 "github.com/linmadan/egglib-go/utils/tool_funs"
9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
  10 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
  12 + "time"
11 ) 13 )
12 14
13 const ( 15 const (
@@ -24,6 +26,8 @@ const ( @@ -24,6 +26,8 @@ const (
24 CooperationModeStatistics = "CooperationModeStatistics" 26 CooperationModeStatistics = "CooperationModeStatistics"
25 // 企业-分红统计 27 // 企业-分红统计
26 CompanyDividendsStatistics = "CompanyDividendsStatistics" 28 CompanyDividendsStatistics = "CompanyDividendsStatistics"
  29 + // 企业、个人 - 分红预算列表
  30 + SearchDividendsEstimates = "SearchDividendsEstimates"
27 ) 31 )
28 32
29 // CooperationStatisticsService 共创统计服务 33 // CooperationStatisticsService 共创统计服务
@@ -196,7 +200,78 @@ func (ptr *CooperationStatisticsService) GetContractDividends(queryOptions map[s @@ -196,7 +200,78 @@ func (ptr *CooperationStatisticsService) GetContractDividends(queryOptions map[s
196 200
197 // 分红统计(分红明细) 201 // 分红统计(分红明细)
198 func (ptr *CooperationStatisticsService) DividendsStatistics(queryOptions map[string]interface{}) (interface{}, error) { 202 func (ptr *CooperationStatisticsService) DividendsStatistics(queryOptions map[string]interface{}) (interface{}, error) {
199 - return nil, nil 203 + // 参数验证
  204 + var request = struct {
  205 + //企业
  206 + CompanyId int64 `json:"companyId"`
  207 + OrgId int64 `json:"orgId"`
  208 + //个人
  209 + UserBaseId int64 `json:"userBaseId"`
  210 + }{}
  211 + if err := LoadQueryObject(queryOptions, &request); err != nil {
  212 + return nil, err
  213 + }
  214 + queryOptions = tool_funs.SimpleStructToMap(&request)
  215 +
  216 + type response struct {
  217 + Total float64 `json:"total"`
  218 + Accounting float64 `json:"accounting"`
  219 + Accounted float64 `json:"accounted"`
  220 + Paid float64 `json:"paid"`
  221 + }
  222 + creditAccountDao, _ := dao.NewCreditAccountDao(ptr.transactionContext)
  223 +
  224 + var allDividends = &response{}
  225 + if err := creditAccountDao.DividendsStatistics(queryOptions, allDividends); err != nil {
  226 + return nil, err
  227 + }
  228 + allDividends.Accounting = allDividends.Total - allDividends.Accounted
  229 +
  230 + var annualDividends = &response{}
  231 + queryOptions["beginTime"] = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Local)
  232 + queryOptions["endTime"] = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Local).AddDate(1, 0, 0)
  233 + if err := creditAccountDao.DividendsStatistics(queryOptions, annualDividends); err != nil {
  234 + return nil, err
  235 + }
  236 + annualDividends.Accounting = annualDividends.Total - annualDividends.Accounted
  237 +
  238 + var quarterDividends = &response{}
  239 + queryOptions["beginTime"], queryOptions["endTime"] = quarterBeginEnd()
  240 + if err := creditAccountDao.DividendsStatistics(queryOptions, quarterDividends); err != nil {
  241 + return nil, err
  242 + }
  243 + quarterDividends.Accounting = quarterDividends.Total - quarterDividends.Accounted
  244 +
  245 + ret := map[string]interface{}{
  246 + "allDividends": allDividends,
  247 + "annualDividends": annualDividends,
  248 + "quarterDividends": quarterDividends,
  249 + }
  250 + return ret, nil
  251 +}
  252 +
  253 +func quarterBeginEnd() (time.Time, time.Time) {
  254 + y := time.Now().Year()
  255 + m := time.Now().Month()
  256 + var mBegin, mEnd int
  257 + var begin, end time.Time
  258 + switch m {
  259 + case 4, 5, 6:
  260 + mBegin = 4
  261 + mEnd = 6
  262 + case 7, 8, 9:
  263 + mBegin = 7
  264 + mEnd = 9
  265 + case 10, 11, 12:
  266 + mBegin = 10
  267 + mEnd = 12
  268 + case 1, 2, 3:
  269 + mBegin = 1
  270 + mEnd = 3
  271 + }
  272 + begin = time.Date(y, time.Month(mBegin), 1, 0, 0, 0, 0, time.Local)
  273 + end = time.Date(y, time.Month(mEnd), 1, 0, 0, 0, 0, time.Local)
  274 + return begin, end
200 } 275 }
201 276
202 // 分红预算列表 277 // 分红预算列表