作者 yangfu

共创统计修改

... ... @@ -47,6 +47,8 @@ func (svr *CooperationStatisticsService) CooperationContractStatistics(contractS
res, err = statisticsService.DividendsStatistics(contractStatisticsQuery.QueryOptions)
case domain_service.SearchDividendsEstimates:
res, err = statisticsService.SearchDividendsEstimates(contractStatisticsQuery.QueryOptions)
case domain_service.CooperationCompanyStatistics:
res, err = statisticsService.CooperationCompanyStatistics(contractStatisticsQuery.QueryOptions)
}
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
... ...
... ... @@ -55,24 +55,43 @@ func (dao *CreditAccountDao) CheckCreditAccountNumberAvailable(queryOptions map[
// 分红统计(分红明细)
func (dao *CreditAccountDao) DividendsStatistics(queryOptions map[string]interface{}, v interface{}) error {
sql := `
select
sum(settlement_amount) total,
sum((case when payment_status = 1 then actually_paid_amount else 0 end)) paid,
sum((case when settlement_time is not null then settlement_amount else 0 end)) accounted
from credit_accounts
where
`
// sql := `
//select
//sum(settlement_amount) total,
//sum((case when payment_status = 1 then actually_paid_amount else 0 end)) paid,
//sum((case when settlement_time is not null then settlement_amount else 0 end)) accounted
//from credit_accounts
//where
//`
//
//
// if _, ok := queryOptions["beginTime"]; ok {
// sql += `created_at>? and created_at<? and `
// }
// if v, ok := queryOptions["userBaseId"]; ok && v.(int64) > 0 {
// sql += fmt.Sprintf(`participator->>'userBaseId'='%v' `, v)
// }
// if v, ok := queryOptions["orgId"]; ok && v.(int64) > 0 {
// sql += fmt.Sprintf(` org->>'orgId'= '%v'`, v)
// }
// _, err := dao.transactionContext.PgDd.Query(v, sql, queryOptions["beginTime"], queryOptions["endTime"])
creditAccount := new(models.CreditAccount)
query := dao.transactionContext.PgTx.Model(creditAccount)
query.ColumnExpr(`sum(settlement_amount) total`)
query.ColumnExpr(`sum((case when payment_status = 1 then actually_paid_amount else 0 end)) paid`)
query.ColumnExpr(`sum((case when settlement_time is not null then settlement_amount else 0 end)) accounted `)
if _, ok := queryOptions["beginTime"]; ok {
sql += `created_at>? and created_at<? and `
query.Where(`created_at>? `, queryOptions["beginTime"])
query.Where(`created_at<? `, queryOptions["endTime"])
}
if v, ok := queryOptions["userBaseId"]; ok && v.(int64) > 0 {
sql += fmt.Sprintf(`participator->>'userBaseId'='%v'`, v)
query.Where(fmt.Sprintf(`participator->>'userBaseId'='%v' `, v))
}
if v, ok := queryOptions["orgId"]; ok && v.(int64) > 0 {
sql += fmt.Sprintf(`org->>'orgId'= '%v'`, v)
query.Where(fmt.Sprintf(` org->>'orgId'= '%v'`, v))
}
_, err := dao.transactionContext.PgDd.Query(v, sql, queryOptions["beginTime"], queryOptions["endTime"])
err := query.Select(v)
return err
}
... ...
package domain_service
import (
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
)
// 共创公司统计
func (ptr *CooperationStatisticsService) CooperationCompanyStatistics(queryOptions map[string]interface{}) (interface{}, error) {
// 参数验证
var request = struct {
UserBaseId int64 `json:"userBaseId" valid:"Required"`
CompanyList []int64 `json:"companyList" valid:"Required"` //公司列表 待统计的公司列表
}{}
if err := LoadQueryObject(queryOptions, &request); err != nil {
return nil, err
}
queryOptions = tool_funs.SimpleStructToMap(&request)
var response = make([]*cooperationCompanyStatisticsResponse, 0)
var totalDividend float64
for i := range request.CompanyList {
orgId := request.CompanyList[i]
item, err := ptr.cooperationCompanyStatistics(request.UserBaseId, orgId)
if err != nil {
return response, err
}
totalDividend += item.DividendsIncome
response = append(response, item)
}
if totalDividend > 0 {
for i := range response {
response[i].DividendsRatio = utils.Round(response[i].DividendsIncome/totalDividend*100, 2)
}
}
return response, nil
}
func (ptr *CooperationStatisticsService) cooperationCompanyStatistics(userBaseId int64, orgId int64) (*cooperationCompanyStatisticsResponse, error) {
// 1.相关项目统计
cooperationApplicationRepository, _ := repository.NewCooperationApplicationRepository(ptr.transactionContext)
cooperationProjectCount, _, err := cooperationApplicationRepository.Find(map[string]interface{}{"userBaseId": userBaseId, "orgId": orgId,
"limit": 1, "cooperationApplicationStatus": int32(2)})
if err != nil {
return nil, err
}
// 2.相关合约统计
cooperationContractRelevantRepository, _ := repository.NewCooperationContractRelevantRepository(ptr.transactionContext)
cooperationContractCount, _, err := cooperationContractRelevantRepository.Find(map[string]interface{}{"userBaseId": userBaseId,
"limit": 1})
if err != nil {
return nil, err
}
// 3.个人分红统计
type response struct {
Total float64 `json:"total"`
Accounting float64 `json:"accounting"`
Accounted float64 `json:"accounted"`
Paid float64 `json:"paid"`
}
creditAccountDao, _ := dao.NewCreditAccountDao(ptr.transactionContext)
var allDividends = &response{}
if err := creditAccountDao.DividendsStatistics(map[string]interface{}{"userBaseId": userBaseId, "orgId": orgId}, allDividends); err != nil {
return nil, err
}
allDividends.Accounting = allDividends.Total - allDividends.Accounted
var ret = &cooperationCompanyStatisticsResponse{
OrgId: orgId,
CooperationProjectCount: cooperationProjectCount,
CooperationContractCount: cooperationContractCount,
DividendsIncome: utils.Round(allDividends.Total, 2),
}
return ret, nil
}
type cooperationCompanyStatisticsResponse struct {
// 当天统计的企业id
OrgId int64 `json:"orgId"`
// 共创项目数
CooperationProjectCount int64 `json:"cooperationProjectCount"`
// 共创合约数
CooperationContractCount int64 `json:"cooperationContractCount"`
// 分红占比
DividendsRatio float64 `json:"dividendsRatio"`
// 分红支出
DividendsIncome float64 `json:"dividendsIncome"`
}
... ...
... ... @@ -29,6 +29,9 @@ const (
CompanyDividendsStatistics = "CompanyDividendsStatistics"
// 企业、个人 - 分红预算列表
SearchDividendsEstimates = "SearchDividendsEstimates"
// 个人 - 共创企业统计
CooperationCompanyStatistics = "CooperationCompanyStatistics"
)
// CooperationStatisticsService 共创统计服务
... ... @@ -54,6 +57,20 @@ func NewCooperationStatisticsService(transactionContext *pgTransaction.Transacti
//
// queryOptions 查询参数
func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions map[string]interface{}) (interface{}, error) {
var request = struct {
//企业
CompanyId int64 `json:"companyId"`
OrgId int64 `json:"orgId"`
//个人
UserBaseId int64 `json:"userBaseId"`
Offset int `json:"offset"`
Limit int `json:"limit"`
}{}
if err := LoadQueryObject(queryOptions, &request); err != nil {
return nil, err
}
queryOptions = tool_funs.SimpleStructToMap(&request)
// 1.根据个人、企业查询合约列表
var contracts []*domain.CooperationContract
var err error
... ... @@ -115,7 +132,6 @@ func (ptr *CooperationStatisticsService) getUserContracts(queryOptions map[strin
}
queryOptions["inCooperationContractNumber"] = numbers
contractRepository, _ := repository.NewCooperationContractRepository(ptr.transactionContext)
// TODO: 参数查询条件
_, contracts, err := contractRepository.Find(queryOptions)
return contracts, err
}
... ... @@ -203,22 +219,27 @@ func (ptr *CooperationStatisticsService) GetContractDividends(queryOptions map[s
var dividends = make([]interface{}, 0)
for i := range creditAccounts {
a := creditAccounts[i]
participateType, _ := strconv.Atoi(a.ParticipateType)
dividends = append(dividends, map[string]interface{}{
"creditAccountId": a.CreditAccountId,
"orderGoodName": "",
//"dividendsType": a.AccountDetail.DividendsType,
"dividendsRatio": 0,
//"dividendsAmount": a.AccountDetail.DividendsAmount,
"dividendsUser": map[string]interface{}{
"userInfo": a.Participator.UserInfo,
"userId": a.Participator.UserId,
},
"dividendsParticipateType": participateType,
"dividendsAccountStatus": a.PaymentStatus,
"dividendsEstimateTime": a.CreatedAt.Unix() * 1000,
"orderOrReturnedOrderNum": a.CreditAccountOrderNum,
})
for j := range a.AccountDetail {
participateType, _ := strconv.Atoi(a.ParticipateType)
item := map[string]interface{}{
"creditAccountId": a.CreditAccountId,
"orderGoodName": "",
"dividendsType": 0,
"dividendsRatio": 0,
"dividendsAmount": a.AccountDetail[j].DividendsAmount,
"dividendsUser": map[string]interface{}{
"userInfo": a.Participator.UserInfo,
"userId": a.Participator.UserId,
},
"dividendsParticipateType": participateType,
"dividendsAccountStatus": a.PaymentStatus,
"dividendsEstimateTime": a.CreatedAt.Unix() * 1000,
"orderOrReturnedOrderNum": a.CreditAccountOrderNum,
}
item["dividendsType"] = a.AccountDetail[j].DividendsType
dividends = append(dividends, item)
}
}
res["dividends"] = dividends
return res, nil
... ...
... ... @@ -179,6 +179,9 @@ func (repository *CooperationContractRelevantRepository) Find(queryOptions map[s
if orgIds, ok := queryOptions["orgIds"]; ok && len(orgIds.([]int64)) > 0 {
query.Where("org->>'orgId' in (?)", pg.In(orgIds))
}
if userBaseId, ok := queryOptions["userBaseId"]; ok && userBaseId.(int64) != 0 {
query.Where("user_base_id = ?", userBaseId)
}
offsetLimitFlag := true
if offsetLimit, ok := queryOptions["offsetLimit"]; ok {
offsetLimitFlag = offsetLimit.(bool)
... ...
... ... @@ -190,6 +190,9 @@ func (repository *CooperationContractUndertakerRepository) Find(queryOptions map
if orgIds, ok := queryOptions["orgIds"]; ok && len(orgIds.([]int64)) > 0 {
query.Where("org->>'orgId' in (?)", pg.In(orgIds))
}
if userBaseId, ok := queryOptions["userBaseId"]; ok && userBaseId.(int64) != 0 {
query.Where("user_base_id = ? ", userBaseId)
}
offsetLimitFlag := true
if offsetLimit, ok := queryOptions["offsetLimit"]; ok {
offsetLimitFlag = offsetLimit.(bool)
... ...
... ... @@ -202,7 +202,7 @@ func (repository *CreditAccountRepository) Find(queryOptions map[string]interfac
query.Where(`(credit_account.participator->>'userName')::text LIKE ?`, fmt.Sprintf("%%%s%%", participatorName))
}
if cooperationContractNumber, ok := queryOptions["cooperationContractNumber"]; ok && cooperationContractNumber != "" {
query.Where(`cooperation_contract_number = ?`, fmt.Sprintf("%%%s%%", cooperationContractNumber))
query.Where(`cooperation_contract_number = ?`, fmt.Sprintf("%s", cooperationContractNumber))
}
if paymentStatus, ok := queryOptions["paymentStatus"]; ok && paymentStatus.(int32) != 0 {
query.Where("payment_status = ?", paymentStatus)
... ...