func (self *ReportDaySupply) mergeList(publishedLists ...[]*esentity.ServiceReportDayEs) report_dto.MapRet { //[]*esentity.ServiceReportDayEs { var publishedMap = make(report_dto.MapRet) //聚合统计 for _, publishedList := range publishedLists { for _, publishedLine := range publishedList { if data, ok := publishedMap[publishedLine.ShopMemberId]; ok { switch data.LineType { case Linetype_avab_line: data.AvabCount += publishedLine.AvabCount case Linetype_rfq_line: data.RfqCount += publishedLine.RfqCount case Linetype_PurchaseOrderLine: data.PurchaseLineCount += publishedLine.PurchaseLineCount case Linetype_SalesOrderLine: data.SaleLineCount += publishedLine.SaleLineCount } } else { publishedMap[publishedLine.ShopMemberId] = publishedLine } } } return publishedMap }
指标统计结果是一张宽表
统计过程的数据合并
package report_day import ( "git.ichub.com/general/webcli120/goconfig/base/basedto" "git.ichub.com/general/webcli120/goconfig/ichublog/golog" "github.com/gogf/gf/v2/util/gconv" "github.com/micro/go-micro/v2/logger" "icd/general-srv/esentity" "icd/general-srv/handler/report_base" "icd/general-srv/handler/report_base/reportkey" "icd/general-srv/handler/report_dto" "icd/plugins/db" "time" ) const ( Linetype_rfq_line = "rfq_line" //询盘 Linetype_avab_line = "avab_line" //e供 Linetype_SalesOrderLine = "sales_order_line" // 买盘销售 Linetype_PurchaseOrderLine = "purchase_order_line" // 卖盘采购 ) // 发布项之供盘 // leijianming lei1234 -- candao type ReportDaySupply struct { basedto.BaseEntitySingle *report_base.ReportDayBase } func NewServiceReportDaySupply() *ReportDaySupply { return &ReportDaySupply{ ReportDayBase: report_base.FindBeanReportDayBase(), } } func (self *ReportDaySupply) BuildId(id any, end time.Time) string { return end.Format(time.DateOnly) + "|" + gconv.String(id) + "|0|0" } // 从发布项统计供盘数指标 db func (self *ReportDaySupply) MigrationAvabLineDayReport(start, end time.Time) ([]*esentity.ServiceReportDayEs, error) { golog.Info("ReportDayMatch [MigrationAvabLineDayReport]开始统计所有供盘数:", end) var startShanghai, endShanghai = self.ToShanghai(start, end) //yesterday start = startShanghai.AddDate(0, 0, -1) end = endShanghai.AddDate(0, 0, -1) golog.Info("ReportDaySupply [MigrationAvabLineDayReport] start, end = ", start, end) var statAvabLine, err = self.statAvabLine(start, end) if err != nil { golog.Error("ReportDaySupply [MigrationAvabLineDayReport] err =", err) return nil, err } //statRfqLine //var statRfqLine, err1 = self.statRfqLine(start, end) //if err1 != nil { // golog.Error(err1) //} var statPurchaseLine, err2 = self.statPurchaseLine(start, end) if err2 != nil { golog.Error(err2) } var statSaleLine, err3 = self.statSaleLine(start, end) if err3 != nil { golog.Error(err3) } //去重 golog.Info("ReportDaySupply [MigrationAvabLineDayReport]统计所有供盘数结束:") var mapRet = self.mergeList(statAvabLine, statPurchaseLine, statSaleLine) return self.map2List(mapRet, start), nil } func (self *ReportDaySupply) statRfqLine(start, end time.Time) ([]*esentity.ServiceReportDayEs, error) { const sql = `with P as( select bpl.line_id , bpl.line_type from biz_published_line bpl where bpl.line_type in 'rfq_line' and bpl.active =true and bpl.created_at >= ? and bpl.created_at < ?) select bal.shop_member_id,P.line_type,count(1) as rfq_count from P join biz_rfq_line bal on P.line_id=bal.id group by bal.shop_member_id,P.line_type` reportDayEs := make([]*esentity.ServiceReportDayEs, 0) if err := db.GetDB().Raw(sql, start, end).Scan(&reportDayEs).Error; err != nil { logger.Errorf("MigrationAvabLineDayReport 查询新增或更新的供盘数报错:", err) return reportDayEs, err } golog.Info("ReportDaySupply [makePublishedLine] reportDayEs count= , ", len(reportDayEs)) return reportDayEs, nil } func (self *ReportDaySupply) statPurchaseLine(start, end time.Time) ([]*esentity.ServiceReportDayEs, error) { const sql = `with P as( select bpl.line_id , bpl.line_type from biz_published_line bpl where bpl.line_type in 'purchase_order_line' and bpl.active =true and bpl.created_at >= ? and bpl.created_at < ?) select bal.shop_member_id,P.line_type,count(1) as purchase_line_count from P join biz_purchase_order_line bal on P.line_id=bal.id group by bal.shop_member_id,P.line_type` reportDayEs := make([]*esentity.ServiceReportDayEs, 0) if err := db.GetDB().Raw(sql, start, end).Scan(&reportDayEs).Error; err != nil { logger.Errorf("MigrationAvabLineDayReport 查询新增或更新的供盘数报错:", err) return reportDayEs, err } golog.Info("ReportDaySupply [statPurchaseLine] reportDayEs count= , ", len(reportDayEs)) return reportDayEs, nil } func (self *ReportDaySupply) statSaleLine(start, end time.Time) ([]*esentity.ServiceReportDayEs, error) { const sql = `with P as( select bpl.line_id , bpl.line_type from biz_published_line bpl where bpl.line_type in 'sale_order_line' and bpl.active =true and bpl.created_at >= ? and bpl.created_at < ?) select bal.shop_member_id,,P.line_type,count(1) as sale_line_count from P join biz_sales_order_line bal on P.line_id=bal.id group by bal.shop_member_id,P.line_type` reportDayEs := make([]*esentity.ServiceReportDayEs, 0) if err := db.GetDB().Raw(sql, start, end).Scan(&reportDayEs).Error; err != nil { logger.Errorf("MigrationAvabLineDayReport 查询新增或更新的供盘数报错:", err) return reportDayEs, err } golog.Info("ReportDaySupply [statSaleLine] reportDayEs count= , ", len(reportDayEs)) return reportDayEs, nil } func (self *ReportDaySupply) statAvabLine(start, end time.Time) ([]*esentity.ServiceReportDayEs, error) { // select bpl.line_id from biz_published_line bpl where bpl.line_type ='avab_line' and bpl.active =true // and bpl.updated_at >= ? and bpl.updated_at < ? union const srd_addsql = `with P as( select bpl.line_id,bpl.line_type from biz_published_line bpl where bpl.line_type ='avab_line' and bpl.active =true and bpl.created_at >= ? and bpl.created_at < ? ) select bal.shop_member_id,P.line_type,count(1) as avab_count from P join biz_avab_line bal on P.line_id=bal.id and bal.source_type in (0, 1, 2) group by bal.shop_member_id,P.line_type` reportDayEs := make([]*esentity.ServiceReportDayEs, 0) //昨天新增或者更新激活的供盘发布项 created active=true / if err := db.GetDB().Raw(srd_addsql, start, end).Scan(&reportDayEs).Error; err != nil { logger.Errorf("MigrationAvabLineDayReport 查询新增或更新的供盘数报错:", err) return reportDayEs, err } golog.Info("ReportDaySupply [statAvabLine] reportDayEs coun t= , ", len(reportDayEs)) return reportDayEs, nil } func (self *ReportDaySupply) mergeList(publishedLists ...[]*esentity.ServiceReportDayEs) report_dto.MapRet { //[]*esentity.ServiceReportDayEs { var publishedMap = make(report_dto.MapRet) //聚合统计 for _, publishedList := range publishedLists { for _, publishedLine := range publishedList { if data, ok := publishedMap[publishedLine.ShopMemberId]; ok { switch data.LineType { case Linetype_avab_line: data.AvabCount += publishedLine.AvabCount case Linetype_rfq_line: data.RfqCount += publishedLine.RfqCount case Linetype_PurchaseOrderLine: data.PurchaseLineCount += publishedLine.PurchaseLineCount case Linetype_SalesOrderLine: data.SaleLineCount += publishedLine.SaleLineCount } } else { publishedMap[publishedLine.ShopMemberId] = publishedLine } } } return publishedMap } func (self *ReportDaySupply) ToIdByMemberId(start time.Time, id int64) string { return start.Format(time.DateOnly) + "|" + gconv.String(id) + "|0|0" } func (self *ReportDaySupply) map2List(publishedMap report_dto.MapRet, start time.Time) []*esentity.ServiceReportDayEs { publishedList := make([]*esentity.ServiceReportDayEs, 0) for _, publishedLine := range publishedMap { publishedLine.Id = self.ToIdByMemberId(start.In(reportkey.ZoneShanghai), publishedLine.ShopMemberId) publishedLine.CreatedAt = start.UTC().UnixNano() / 1e6 publishedLine.CreatedDate = start.In(reportkey.ZoneShanghai) publishedLine.StatAt = time.Now().In(reportkey.ZoneShanghai) publishedList = append(publishedList, publishedLine) } return publishedList } //esservice //指标系统的测试用例 预置 //时间维度,0点前,0点后 : 供货项,。。。 //时间维度,8点前,8点后 : 供货项,。。。 //时间维度,16点前,16点后 : 供货项,。。。 //观察点:,统计总数, 时间有没有落到正确的日期 // func (self *ReportDaySupply) makeCancel(start, end time.Time) ([]*esentity.ServiceReportDayEs, error) { // // const srd_deletesql = ` // select bal.shop_member_id,count(1) as cancel_avab_count from biz_published_line bpl // inner join biz_avab_line bal on bpl.line_id = bal.id // where bpl.updated_at > ? and bpl.updated_at < ? // and bpl.active = false and bal.source_type in (0,1,2) // group by bal.shop_member_id; // //昨天归档的供盘发布项 // reportDayEs := make([]*esentity.ServiceReportDayEs, 0) // if err := db.GetDB().Raw(srd_deletesql, start, end).Scan(&reportDayEs).Error; err != nil { // golog.Error("MigrationAvabLineDayReport 查询删除的供盘数报错:", err) // return reportDayEs, err // } // return reportDayEs, nil // }标签:err,供盘,bpl,reportDayEs,start,GO,line,询盘,id From: https://blog.csdn.net/leijmdas/article/details/144520969