package data_manage import ( "eta_gn/eta_api/global" "eta_gn/eta_api/models/mgo" "eta_gn/eta_api/utils" "fmt" "strconv" "time" "go.mongodb.org/mongo-driver/bson" ) // GetEdbDataTableName 指标数据->存储表 func GetEdbDataTableName(source, subSource int) (tableName string) { switch source { case utils.DATA_SOURCE_CALCULATE: tableName = "edb_data_calculate" case utils.DATA_SOURCE_CALCULATE_LJZZY: tableName = "edb_data_calculate_ljzzy" case utils.DATA_SOURCE_CALCULATE_TBZ: tableName = "edb_data_calculate_tbz" case utils.DATA_SOURCE_CALCULATE_TCZ: tableName = "edb_data_calculate_tcz" case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS: tableName = "edb_data_calculate_nszydpjjs" case utils.DATA_SOURCE_MANUAL: tableName = "edb_data_manual" case utils.DATA_SOURCE_CALCULATE_HBZ: tableName = "edb_data_calculate_hbz" case utils.DATA_SOURCE_CALCULATE_HCZ: tableName = "edb_data_calculate_hcz" case utils.DATA_SOURCE_CALCULATE_BP: tableName = "edb_data_calculate_bp" case utils.DATA_SOURCE_CALCULATE_ZJPJ: tableName = "edb_data_calculate_zjpj" case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT: tableName = "edb_data_calculate_time_shift" case utils.DATA_SOURCE_CALCULATE_LJZTBPJ: tableName = "edb_data_calculate_ljztbpj" case utils.DATA_SOURCE_PYTHON: tableName = "edb_data_python" case utils.DATA_SOURCE_PREDICT_CALCULATE: tableName = "edb_data_predict_calculate" case utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ: tableName = "edb_data_predict_calculate_tbz" case utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ: tableName = "edb_data_predict_calculate_tcz" case utils.DATA_SOURCE_CALCULATE_CJJX: tableName = "edb_data_calculate_cjjx" case utils.DATA_SOURCE_CALCULATE_NHCC: tableName = "edb_data_calculate_nhcc" case utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS: tableName = "edb_data_predict_calculate_nszydpjjs" case utils.DATA_SOURCE_CALCULATE_ADJUST: tableName = "edb_data_calculate_adjust" case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZY: tableName = "edb_data_predict_calculate_ljzzy" case utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT: tableName = "edb_data_predict_calculate_time_shift" case utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ: tableName = "edb_data_predict_calculate_zjpj" case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: tableName = "edb_data_predict_calculate_ljztbpj" case utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: tableName = "edb_data_predict_calculate_nhcc" case utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX: tableName = "edb_data_predict_calculate_cjjx" case utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ: tableName = "edb_data_predict_calculate_hbz" case utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ: tableName = "edb_data_predict_calculate_hcz" case utils.DATA_SOURCE_PREDICT_CALCULATE_BP: tableName = "edb_data_predict_calculate_bp" case utils.DATA_SOURCE_CALCULATE_JP: tableName = "edb_data_calculate_jp" case utils.DATA_SOURCE_CALCULATE_NH: tableName = "edb_data_calculate_nh" case utils.DATA_SOURCE_CALCULATE_KSZS: tableName = "edb_data_calculate_kszs" case utils.DATA_SOURCE_PREDICT_CALCULATE_JP: tableName = "edb_data_predict_calculate_jp" case utils.DATA_SOURCE_PREDICT_CALCULATE_NH: tableName = "edb_data_predict_calculate_nh" case utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS: tableName = "edb_data_predict_calculate_kszs" case utils.DATA_SOURCE_STOCK_PLANT: tableName = "edb_data_stock_plant" case utils.DATA_SOURCE_CALCULATE_CORRELATION: tableName = "edb_data_calculate_correlation" case utils.DATA_SOURCE_NATIONAL_STATISTICS: tableName = "edb_data_national_statistics" case utils.DATA_SOURCE_CALCULATE_LJZZJ: //累计值转季 -> 61 tableName = "edb_data_calculate_ljzzj" case utils.DATA_SOURCE_CALCULATE_LJZ: //累计值 -> 62 tableName = "edb_data_calculate_ljz" case utils.DATA_SOURCE_CALCULATE_LJZNCZJ: //累计值(年初至今) -> 63 tableName = "edb_data_calculate_ljznczj" case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZJ: // 预测指标 - 累计值 -> 65 tableName = "edb_data_predict_calculate_ljzzj" case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZ: //预测指标 - 累计值转季->64 tableName = "edb_data_predict_calculate_ljz" case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZNCZJ: //预测指标 - 累计值(年初至今) -> 66 tableName = "edb_data_predict_calculate_ljznczj" case utils.DATA_SOURCE_CALCULATE_STANDARD_DEVIATION: //标准差->67 tableName = "edb_data_calculate_standard_deviation" case utils.DATA_SOURCE_CALCULATE_PERCENTILE: //百分位->68 tableName = "edb_data_calculate_percentile" case utils.DATA_SOURCE_PREDICT_CALCULATE_STANDARD_DEVIATION: //预测标准差->69 tableName = "edb_data_predict_ccalculate_standard_deviation" case utils.DATA_SOURCE_PREDICT_CALCULATE_PERCENTILE: //预测百分位->70 tableName = "edb_data_predict_ccalculate_percentile" case utils.DATA_SOURCE_CALCULATE_ZSXY: tableName = "edb_data_calculate_zsxy" // 指数修匀->72 case utils.DATA_SOURCE_PREDICT_CALCULATE_ZSXY: tableName = "edb_data_predict_calculate_zsxy" // 预测指数修匀->73 case utils.DATA_SOURCE_CALCULATE_ZDYFX: tableName = "edb_data_calculate_zdyfx" // 自定义分析->74 case utils.DATA_SOURCE_CALCULATE_RJZ: //日均值->75 tableName = "edb_data_calculate_rjz" default: edbSource := GetEdbSourceBySourceId(source) if edbSource != nil { tableName = edbSource.TableName } } return } type EdbDataBase struct { EdbDataId int `orm:"column(edb_data_id);pk" gorm:"primaryKey" ` EdbInfoId int EdbCode string DataTime string Value string Status int CreateTime time.Time ModifyTime time.Time DataTimestamp int64 } func GetEdbDataAllByEdbCode(edbCode string, source, subSource, limit int) (items []*EdbInfoSearchData, err error) { // 自有数据需要额外处理(从mongo获取) if source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo { return GetEdbDataAllByEdbCodeByMongo(edbCode, source, subSource, limit) } if source == utils.DATA_SOURCE_THS && subSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && utils.UseMongo { return GetThsHfEdbDataAllByEdbCodeByMongo(edbCode, source, subSource, limit) } return GetEdbDataAllByEdbCodeByMysql(edbCode, source, subSource, limit) } func GetEdbDataAllByEdbCodeByMysql(edbCode string, source, subSource, limit int) (items []*EdbInfoSearchData, err error) { var pars []interface{} pars = append(pars, edbCode) tableName := GetEdbDataTableName(source, subSource) sql := ` SELECT * FROM %s WHERE edb_code=? ORDER BY data_time DESC` if limit > 0 { sql += ` LIMIT ? ` pars = append(pars, limit) } sql = fmt.Sprintf(sql, tableName) err = global.DmSQL["data"].Raw(sql, pars...).Scan(&items).Error return } func GetEdbDataBaseByEdbInfoId(edbInfoId int, source, subSource int) (items []*EdbDataBase, err error) { tableName := GetEdbDataTableName(source, subSource) if source == utils.DATA_SOURCE_PREDICT { tableName = "edb_data_predict_base" } sql := ` SELECT * FROM %s WHERE edb_info_id=? ORDER BY data_time DESC` sql = fmt.Sprintf(sql, tableName) err = global.DmSQL["data"].Raw(sql, edbInfoId).Scan(&items).Error return } func GetEdbDataBaseMongoByEdbInfoId(edbInfoId int, source, subSource int) (list []*EdbDataBase, err error) { list = make([]*EdbDataBase, 0) mogDataObj := mgo.EdbDataBusiness{} // 构建查询条件 queryConditions := bson.M{ "edb_info_id": edbInfoId, } // 获取列表数据 tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"data_time"}) if tmpErr != nil { err = tmpErr return } for k, v := range tmpDataList { list = append(list, &EdbDataBase{ EdbDataId: k + 1, EdbInfoId: v.EdbInfoId, EdbCode: v.EdbCode, DataTime: v.DataTime.Format(utils.FormatDate), DataTimestamp: v.DataTimestamp, Value: strconv.FormatFloat(v.Value, 'f', -1, 64), }) } return } func GetEdbDataAllByEdbCodeByMongo(edbCode string, source, subSource, limit int) (list []*EdbInfoSearchData, err error) { list = make([]*EdbInfoSearchData, 0) mogDataObj := mgo.EdbDataBusiness{} // 构建查询条件 queryConditions := bson.M{ "edb_code": edbCode, } var tmpDataList []*mgo.EdbDataBusiness // 获取列表数据 if limit > 0 { tmpDataList, err = mogDataObj.GetLimitDataList(queryConditions, int64(limit), []string{"-data_time"}) } else { tmpDataList, err = mogDataObj.GetLimitDataList(queryConditions, int64(limit), []string{"-data_time"}) } if err != nil { return } for _, v := range tmpDataList { list = append(list, &EdbInfoSearchData{ DataTime: v.DataTime.Format(utils.FormatDate), Value: v.Value, }) } return } func GetBaseIndexInfoByEdbCode(edbCode string, source int) (item *BaseIndexInfo, err error) { var pars []interface{} pars = append(pars, edbCode) tableName := GetBaseIndexTableName(source) if tableName == "" { err = fmt.Errorf("未找到对应的表") return } sql := ` SELECT * FROM %s WHERE index_code=? ` sql = fmt.Sprintf(sql, tableName) err = global.DmSQL["data"].Raw(sql, pars...).Scan(&item).Error return } func GetBaseIndexTableName(source int) (tableName string) { edbSource := GetEdbSourceBySourceId(source) if edbSource != nil { tableName = edbSource.IndexTableName } return } func GetEdbDataAllByEdbCodeAndSubSource(edbCode string, source, subSource, limit int) (items []*EdbInfoSearchData, err error) { var pars []interface{} pars = append(pars, edbCode) tableName := GetEdbDataTableNameAndSubSource(source, subSource) sql := ` SELECT * FROM %s WHERE edb_code=? ORDER BY data_time DESC` if limit > 0 { sql += ` LIMIT ? ` pars = append(pars, limit) } sql = fmt.Sprintf(sql, tableName) err = global.DmSQL["data"].Raw(sql, pars...).Scan(&items).Error return } // GetEdbDataTableNameAndSubSource 指标数据->存储表 func GetEdbDataTableNameAndSubSource(source, subSource int) (tableName string) { switch source { default: edbSource := GetEdbSourceBySourceId(source) if edbSource != nil { tableName = edbSource.TableName } } return } func GetAddSql(edbInfoId, edbCode, dataTime, timestampStr string, value string) (addSql string) { nowStr := time.Now().Format(utils.FormatDateTime) addSql += "(" addSql += edbInfoId + "," + "'" + edbCode + "'" + "," + "'" + dataTime + "'" + "," + value + "," + "'" + nowStr + "'" + "," + "'" + nowStr + "'" + "," + "1" addSql += "," + "'" + timestampStr + "'" addSql += ")," return } func GetThsHfEdbDataAllByEdbCodeByMongo(edbCode string, source, subSource, limit int) (list []*EdbInfoSearchData, err error) { list = make([]*EdbInfoSearchData, 0) mogDataObj := mgo.EdbDataThsHf{} // 构建查询条件 queryConditions := bson.M{ "edb_code": edbCode, } var tmpDataList []*mgo.EdbDataThsHf // 获取列表数据 if limit > 0 { tmpDataList, err = mogDataObj.GetLimitDataList(queryConditions, int64(limit), []string{"-data_time"}) } else { tmpDataList, err = mogDataObj.GetLimitDataList(queryConditions, int64(limit), []string{"-data_time"}) } if err != nil { return } for _, v := range tmpDataList { list = append(list, &EdbInfoSearchData{ DataTime: v.DataTime.Format(utils.FormatDate), Value: v.Value, }) } return } func GetThsHfEdbDataBaseMongoByEdbInfoId(edbInfoId int, source, subSource int) (list []*EdbDataBase, err error) { list = make([]*EdbDataBase, 0) mogDataObj := mgo.EdbDataThsHf{} // 构建查询条件 queryConditions := bson.M{ "edb_info_id": edbInfoId, } // 获取列表数据 tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"data_time"}) if tmpErr != nil { err = tmpErr return } for k, v := range tmpDataList { list = append(list, &EdbDataBase{ EdbDataId: k + 1, EdbInfoId: v.EdbInfoId, EdbCode: v.EdbCode, DataTime: v.DataTime.Format(utils.FormatDate), DataTimestamp: v.DataTimestamp, Value: strconv.FormatFloat(v.Value, 'f', -1, 64), }) } return }