123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- package models
- import (
- "eta_gn/eta_index_lib/global"
- "eta_gn/eta_index_lib/models/mgo"
- "eta_gn/eta_index_lib/utils"
- "fmt"
- "go.mongodb.org/mongo-driver/bson"
- "time"
- )
- // BaseFromBusinessIndex
- // @Description: 外部指标(商家系统)表
- type BaseFromBusinessIndex struct {
- BaseFromBusinessIndexId int64 `gorm:"column:base_from_business_index_id;primaryKey" description:"指标ID"`
- IndexCode string `gorm:"column:index_code" description:"指标编码"`
- IndexName string `gorm:"column:index_name" description:"指标名称"`
- Unit string `gorm:"column:unit" description:"单位"`
- Frequency string `gorm:"column:frequency" description:"频度"`
- Source int `gorm:"column:source" description:"数据来源"`
- SourceName string `gorm:"column:source_name" description:"数据来源名称"`
- StartDate time.Time `gorm:"column:start_date" description:"开始日期"`
- EndDate time.Time `gorm:"column:end_date" description:"结束日期"`
- Remark string `gorm:"column:remark" description:"备注字段"`
- BaseModifyTime time.Time `gorm:"column:base_modify_time" description:"基础信息(名称,单位,频度)变更时间"`
- DataUpdateTime time.Time `gorm:"column:data_update_time" description:"最近一次数据发生变化的时间"`
- CreateTime time.Time `gorm:"column:create_time" description:"创建时间"`
- ModifyTime time.Time `gorm:"column:modify_time" description:"修改时间"`
- }
- func (m *BaseFromBusinessIndex) TableName() string {
- return "base_from_business_index"
- }
- // BaseFromBusinessIndexResp
- // @Description: 外部指标(商家系统)表
- type BaseFromBusinessIndexResp struct {
- IndexCode string `description:"指标编码"`
- IndexName string `description:"指标名称"`
- Unit string `description:"单位"`
- Frequency string `description:"频度"`
- SourceName string `description:"数据来源名称"`
- }
- // EdbBusinessSource
- // @Description: 自有数据(商家)指标来源
- type EdbBusinessSource struct {
- EdbBusinessSourceId int64 `orm:"column(edb_business_source_id);pk"`
- SourceName string `description:"来源名称"` // 来源名称
- CreateTime time.Time `description:"创建时间"` // 创建时间
- }
- // AddBusinessIndexReq
- // @Description: 添加外部指标(商家)请求
- type AddBusinessIndexReq struct {
- IndexCode string `description:"指标编码"`
- IndexName string `description:"指标名称"`
- Unit string `description:"单位"`
- Frequency string `description:"频度"`
- SourceName string `description:"数据来源名称"`
- Remark string `description:"备注字段"`
- DataList []AddBusinessDataReq `description:"指标数据"`
- }
- // AddBusinessDataReq
- // @Description: 外部指标(商家系统)数据
- type AddBusinessDataReq struct {
- Value float64 `description:"值"`
- Date string `description:"日期"`
- }
- // DelBusinessIndexReq
- // @Description: 删除外部指标(商家)请求
- type DelBusinessIndexReq struct {
- IndexCodeList []string `description:"指标编码"`
- }
- // DelBusinessIndexDataReq
- // @Description: 删除外部指标(商家)明细数据请求
- type DelBusinessIndexDataReq struct {
- IndexCode string `description:"指标编码"`
- StartDate string `description:"开始日期"`
- EndDate string `description:"结束日期"`
- }
- // GetIndexItem
- // @Description: 根据指标编码获取自有数据指标
- // @author: Roc
- // @receiver m
- // @datetime 2024-05-31 16:29:30
- // @param indexCode string
- // @return item *BaseFromBusinessIndex
- // @return err error
- func (m *BaseFromBusinessIndex) GetIndexItem(indexCode string) (item *BaseFromBusinessIndex, err error) {
- sql := `SELECT * FROM base_from_business_index WHERE index_code = ? `
- err = global.DEFAULT_DmSQL.Raw(sql, indexCode).First(&item).Error
- return
- }
- // GetIndexItemList
- // @Description: 根据指标编码列表获取自有数据指标列表
- // @author: Roc
- // @receiver m
- // @datetime 2024-05-31 16:29:12
- // @param indexCodeList []string
- // @return items []*BaseFromBusinessIndex
- // @return err error
- func (m *BaseFromBusinessIndex) GetIndexItemList(indexCodeList []string) (items []*BaseFromBusinessIndex, err error) {
- num := len(indexCodeList)
- if num <= 0 {
- return
- }
- sql := `SELECT * FROM base_from_business_index WHERE index_code in (` + utils.GetOrmInReplace(num) + `) `
- err = global.DEFAULT_DmSQL.Raw(sql, indexCodeList).Find(&items).Error
- return
- }
- // DelIndexItemList
- // @Description: 根据指标编码列表删除自有数据指标
- // @author: Roc
- // @receiver m
- // @datetime 2024-05-31 16:36:52
- // @param indexCodeList []string
- // @return err error
- func (m *BaseFromBusinessIndex) DelIndexItemList(indexCodeList []string) (err error) {
- num := len(indexCodeList)
- if num <= 0 {
- return
- }
- sql := `DELETE FROM base_from_business_index WHERE index_code in (` + utils.GetOrmInReplace(num) + `) `
- err = global.DEFAULT_DmSQL.Exec(sql, indexCodeList).Error
- return
- }
- // GetMaxId
- // @Description: 获取自有数据库中的最大id
- // @author: Roc
- // @receiver m
- // @datetime 2024-05-31 13:11:34
- // @return maxId int
- // @return err error
- func (m *BaseFromBusinessIndex) GetMaxId() (maxId int, err error) {
- sql := `SELECT max(base_from_business_index_id) id FROM base_from_business_index limit 1`
- err = global.DEFAULT_DmSQL.Raw(sql).First(&maxId).Error
- return
- }
- // Add 新增
- func (m *BaseFromBusinessIndex) Add() (err error) {
- err = global.DEFAULT_DmSQL.Create(m).Error
- if err != nil {
- return
- }
- return
- }
- func (m *BaseFromBusinessIndex) Update(cols []string) (err error) {
- err = global.DEFAULT_DmSQL.Model(m).Select(cols).Updates(m).Error
- return
- }
- func (m *BaseFromBusinessIndex) Del() (err error) {
- err = global.DEFAULT_DmSQL.Delete(m).Error
- return
- }
- func (m *BaseFromBusinessIndex) UpdateIndex(item *BaseFromBusinessIndex, updateCols []string) (err error) {
- if item == nil {
- return
- }
- if len(updateCols) == 0 {
- return
- }
- err = global.DEFAULT_DmSQL.Model(item).Select(updateCols).Updates(item).Error
- return
- }
- // GetEdbBusinessSourceItem
- // @Description: 根据来源名称获取来源信息
- // @author: Roc
- // @receiver m
- // @datetime 2024-04-25 18:09:03
- // @param sourceName string
- // @return item *EdbBusinessSource
- // @return err error
- func (m *EdbBusinessSource) GetEdbBusinessSourceItem(sourceName string) (item *EdbBusinessSource, err error) {
- sql := `SELECT * FROM edb_business_source WHERE source_name = ? `
- err = global.DEFAULT_DmSQL.Raw(sql, sourceName).First(&item).Error
- return
- }
- // Add 新增
- func (m *EdbBusinessSource) Add() (err error) {
- err = global.DEFAULT_DmSQL.Create(m).Error
- if err != nil {
- return
- }
- return
- }
- // GetEdbInfoMaxAndMinInfo
- // @Description: 获取指标的最新数据记录信息
- // @author: Roc
- // @receiver m
- // @datetime 2024-07-02 14:50:50
- // @param edbCode string
- // @return item *EdbInfoMaxAndMinInfo
- // @return err error
- func (m BaseFromBusinessIndex) GetEdbInfoMaxAndMinInfo(edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
- if utils.UseMongo {
- return m.getEdbInfoMaxAndMinInfoByMongo(edbCode)
- } else {
- return m.getEdbInfoMaxAndMinInfoByMysql(edbCode)
- }
- return
- }
- // getEdbInfoMaxAndMinInfoByMongo
- // @Description: 获取指标的最新数据记录信息(从mongo中获取)
- // @author: Roc
- // @receiver m
- // @datetime 2024-07-02 14:41:20
- // @param edbCode string
- // @return item *EdbInfoMaxAndMinInfo
- // @return err error
- func (m BaseFromBusinessIndex) getEdbInfoMaxAndMinInfoByMongo(edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
- mogDataObj := new(mgo.BaseFromBusinessData)
- pipeline := []bson.M{
- {"$match": bson.M{"index_code": edbCode}},
- {"$group": bson.M{
- "_id": nil,
- "min_date": bson.M{"$min": "$data_time"},
- "max_date": bson.M{"$max": "$data_time"},
- "min_value": bson.M{"$min": "$value"},
- "max_value": bson.M{"$max": "$value"},
- }},
- {"$project": bson.M{"_id": 0}}, // 可选,如果不需要_id字段
- }
- result, err := mogDataObj.GetEdbInfoMaxAndMinInfo(pipeline)
- if err != nil {
- fmt.Println("BaseFromBusinessIndex GetEdbInfoMaxAndMinInfo Err:" + err.Error())
- return
- }
- if !result.MaxDate.IsZero() {
- whereQuery := bson.M{"index_code": edbCode, "data_time": result.MaxDate}
- selectParam := bson.D{{"value", 1}, {"_id", 0}}
- latestValue, tmpErr := mogDataObj.GetLatestValue(whereQuery, selectParam)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- result.LatestValue = latestValue.Value
- result.EndValue = latestValue.Value
- }
- item = &EdbInfoMaxAndMinInfo{
- MinDate: result.MinDate.Format(utils.FormatDate),
- MaxDate: result.MaxDate.Format(utils.FormatDate),
- MinValue: result.MinValue,
- MaxValue: result.MaxValue,
- LatestValue: result.LatestValue,
- LatestDate: result.LatestDate.Format(utils.FormatDate),
- EndValue: result.EndValue,
- }
- return
- }
- // getEdbInfoMaxAndMinInfoByMysql
- // @Description: 获取指标的最新数据记录信息(从mysql中获取)
- // @author: Roc
- // @receiver m
- // @datetime 2024-07-02 14:49:58
- // @param edbCode string
- // @return item *EdbInfoMaxAndMinInfo
- // @return err error
- func (m BaseFromBusinessIndex) getEdbInfoMaxAndMinInfoByMysql(edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
- dataObj := BaseFromBusinessData{}
- result, err := dataObj.GetEdbInfoMaxAndMinInfo(edbCode)
- if err != nil {
- return
- }
- item = &EdbInfoMaxAndMinInfo{
- MinDate: result.MinDate,
- MaxDate: result.MaxDate,
- MinValue: result.MinValue,
- MaxValue: result.MaxValue,
- LatestValue: result.LatestValue,
- LatestDate: result.LatestDate,
- EndValue: result.EndValue,
- }
- item.MinDate = utils.GormDateStrToDateStr(item.MinDate)
- item.MaxDate = utils.GormDateStrToDateStr(item.MaxDate)
- item.LatestDate = utils.GormDateStrToDateStr(item.LatestDate)
- return
- }
- // ModifyIndexMaxAndMinInfo
- // @Description: 修改最大值和最小值信息
- // @author: Roc
- // @receiver m
- // @datetime 2024-05-06 14:07:46
- // @param indexCode string
- // @param item *EdbInfoMaxAndMinInfo
- // @param isIndexUpdateOrAdd bool
- // @return err error
- func (m *BaseFromBusinessIndex) ModifyIndexMaxAndMinInfo(indexCode string, item *EdbInfoMaxAndMinInfo, isIndexUpdateOrAdd bool) (err error) {
- sql := ` UPDATE base_from_business_index SET start_date=?,end_date=?,modify_time=NOW() `
- if isIndexUpdateOrAdd {
- sql += `,data_update_time=NOW() `
- }
- sql += ` WHERE index_code=?`
- err = global.DEFAULT_DmSQL.Exec(sql, item.MinDate, item.MaxDate, indexCode).Error
- return
- }
|