edb_data_base.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. package data_manage
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/models/mgo"
  5. "eta_gn/eta_api/utils"
  6. "fmt"
  7. "strconv"
  8. "time"
  9. "go.mongodb.org/mongo-driver/bson"
  10. )
  11. // GetEdbDataTableName 指标数据->存储表
  12. func GetEdbDataTableName(source, subSource int) (tableName string) {
  13. switch source {
  14. case utils.DATA_SOURCE_CALCULATE:
  15. tableName = "edb_data_calculate"
  16. case utils.DATA_SOURCE_CALCULATE_LJZZY:
  17. tableName = "edb_data_calculate_ljzzy"
  18. case utils.DATA_SOURCE_CALCULATE_TBZ:
  19. tableName = "edb_data_calculate_tbz"
  20. case utils.DATA_SOURCE_CALCULATE_TCZ:
  21. tableName = "edb_data_calculate_tcz"
  22. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS:
  23. tableName = "edb_data_calculate_nszydpjjs"
  24. case utils.DATA_SOURCE_MANUAL:
  25. tableName = "edb_data_manual"
  26. case utils.DATA_SOURCE_CALCULATE_HBZ:
  27. tableName = "edb_data_calculate_hbz"
  28. case utils.DATA_SOURCE_CALCULATE_HCZ:
  29. tableName = "edb_data_calculate_hcz"
  30. case utils.DATA_SOURCE_CALCULATE_BP:
  31. tableName = "edb_data_calculate_bp"
  32. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  33. tableName = "edb_data_calculate_zjpj"
  34. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT:
  35. tableName = "edb_data_calculate_time_shift"
  36. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ:
  37. tableName = "edb_data_calculate_ljztbpj"
  38. case utils.DATA_SOURCE_PYTHON:
  39. tableName = "edb_data_python"
  40. case utils.DATA_SOURCE_PREDICT_CALCULATE:
  41. tableName = "edb_data_predict_calculate"
  42. case utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ:
  43. tableName = "edb_data_predict_calculate_tbz"
  44. case utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ:
  45. tableName = "edb_data_predict_calculate_tcz"
  46. case utils.DATA_SOURCE_CALCULATE_CJJX:
  47. tableName = "edb_data_calculate_cjjx"
  48. case utils.DATA_SOURCE_CALCULATE_NHCC:
  49. tableName = "edb_data_calculate_nhcc"
  50. case utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS:
  51. tableName = "edb_data_predict_calculate_nszydpjjs"
  52. case utils.DATA_SOURCE_CALCULATE_ADJUST:
  53. tableName = "edb_data_calculate_adjust"
  54. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZY:
  55. tableName = "edb_data_predict_calculate_ljzzy"
  56. case utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT:
  57. tableName = "edb_data_predict_calculate_time_shift"
  58. case utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ:
  59. tableName = "edb_data_predict_calculate_zjpj"
  60. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ:
  61. tableName = "edb_data_predict_calculate_ljztbpj"
  62. case utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC:
  63. tableName = "edb_data_predict_calculate_nhcc"
  64. case utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX:
  65. tableName = "edb_data_predict_calculate_cjjx"
  66. case utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ:
  67. tableName = "edb_data_predict_calculate_hbz"
  68. case utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ:
  69. tableName = "edb_data_predict_calculate_hcz"
  70. case utils.DATA_SOURCE_PREDICT_CALCULATE_BP:
  71. tableName = "edb_data_predict_calculate_bp"
  72. case utils.DATA_SOURCE_CALCULATE_JP:
  73. tableName = "edb_data_calculate_jp"
  74. case utils.DATA_SOURCE_CALCULATE_NH:
  75. tableName = "edb_data_calculate_nh"
  76. case utils.DATA_SOURCE_CALCULATE_KSZS:
  77. tableName = "edb_data_calculate_kszs"
  78. case utils.DATA_SOURCE_PREDICT_CALCULATE_JP:
  79. tableName = "edb_data_predict_calculate_jp"
  80. case utils.DATA_SOURCE_PREDICT_CALCULATE_NH:
  81. tableName = "edb_data_predict_calculate_nh"
  82. case utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS:
  83. tableName = "edb_data_predict_calculate_kszs"
  84. case utils.DATA_SOURCE_STOCK_PLANT:
  85. tableName = "edb_data_stock_plant"
  86. case utils.DATA_SOURCE_CALCULATE_CORRELATION:
  87. tableName = "edb_data_calculate_correlation"
  88. case utils.DATA_SOURCE_NATIONAL_STATISTICS:
  89. tableName = "edb_data_national_statistics"
  90. case utils.DATA_SOURCE_CALCULATE_LJZZJ: //累计值转季 -> 61
  91. tableName = "edb_data_calculate_ljzzj"
  92. case utils.DATA_SOURCE_CALCULATE_LJZ: //累计值 -> 62
  93. tableName = "edb_data_calculate_ljz"
  94. case utils.DATA_SOURCE_CALCULATE_LJZNCZJ: //累计值(年初至今) -> 63
  95. tableName = "edb_data_calculate_ljznczj"
  96. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZJ: // 预测指标 - 累计值 -> 65
  97. tableName = "edb_data_predict_calculate_ljzzj"
  98. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZ: //预测指标 - 累计值转季->64
  99. tableName = "edb_data_predict_calculate_ljz"
  100. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZNCZJ: //预测指标 - 累计值(年初至今) -> 66
  101. tableName = "edb_data_predict_calculate_ljznczj"
  102. case utils.DATA_SOURCE_CALCULATE_STANDARD_DEVIATION: //标准差->67
  103. tableName = "edb_data_calculate_standard_deviation"
  104. case utils.DATA_SOURCE_CALCULATE_PERCENTILE: //百分位->68
  105. tableName = "edb_data_calculate_percentile"
  106. case utils.DATA_SOURCE_PREDICT_CALCULATE_STANDARD_DEVIATION: //预测标准差->69
  107. tableName = "edb_data_predict_ccalculate_standard_deviation"
  108. case utils.DATA_SOURCE_PREDICT_CALCULATE_PERCENTILE: //预测百分位->70
  109. tableName = "edb_data_predict_ccalculate_percentile"
  110. case utils.DATA_SOURCE_CALCULATE_ZSXY:
  111. tableName = "edb_data_calculate_zsxy" // 指数修匀->72
  112. case utils.DATA_SOURCE_PREDICT_CALCULATE_ZSXY:
  113. tableName = "edb_data_predict_calculate_zsxy" // 预测指数修匀->73
  114. case utils.DATA_SOURCE_CALCULATE_ZDYFX:
  115. tableName = "edb_data_calculate_zdyfx" // 自定义分析->74
  116. case utils.DATA_SOURCE_CALCULATE_RJZ: //日均值->75
  117. tableName = "edb_data_calculate_rjz"
  118. default:
  119. edbSource := GetEdbSourceBySourceId(source)
  120. if edbSource != nil {
  121. tableName = edbSource.TableName
  122. }
  123. }
  124. return
  125. }
  126. type EdbDataBase struct {
  127. EdbDataId int `orm:"column(edb_data_id);pk" gorm:"primaryKey" `
  128. EdbInfoId int
  129. EdbCode string
  130. DataTime string
  131. Value string
  132. Status int
  133. CreateTime time.Time
  134. ModifyTime time.Time
  135. DataTimestamp int64
  136. }
  137. func GetEdbDataAllByEdbCode(edbCode string, source, subSource, limit int) (items []*EdbInfoSearchData, err error) {
  138. // 自有数据需要额外处理(从mongo获取)
  139. if source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
  140. return GetEdbDataAllByEdbCodeByMongo(edbCode, source, subSource, limit)
  141. }
  142. if source == utils.DATA_SOURCE_THS && subSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && utils.UseMongo {
  143. return GetThsHfEdbDataAllByEdbCodeByMongo(edbCode, source, subSource, limit)
  144. }
  145. return GetEdbDataAllByEdbCodeByMysql(edbCode, source, subSource, limit)
  146. }
  147. func GetEdbDataAllByEdbCodeByMysql(edbCode string, source, subSource, limit int) (items []*EdbInfoSearchData, err error) {
  148. var pars []interface{}
  149. pars = append(pars, edbCode)
  150. tableName := GetEdbDataTableName(source, subSource)
  151. sql := ` SELECT * FROM %s WHERE edb_code=? ORDER BY data_time DESC`
  152. if limit > 0 {
  153. sql += ` LIMIT ? `
  154. pars = append(pars, limit)
  155. }
  156. sql = fmt.Sprintf(sql, tableName)
  157. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&items).Error
  158. return
  159. }
  160. func GetEdbDataBaseByEdbInfoId(edbInfoId int, source, subSource int) (items []*EdbDataBase, err error) {
  161. tableName := GetEdbDataTableName(source, subSource)
  162. if source == utils.DATA_SOURCE_PREDICT {
  163. tableName = "edb_data_predict_base"
  164. }
  165. sql := ` SELECT * FROM %s WHERE edb_info_id=? ORDER BY data_time DESC`
  166. sql = fmt.Sprintf(sql, tableName)
  167. err = global.DmSQL["data"].Raw(sql, edbInfoId).Scan(&items).Error
  168. return
  169. }
  170. func GetEdbDataBaseMongoByEdbInfoId(edbInfoId int, source, subSource int) (list []*EdbDataBase, err error) {
  171. list = make([]*EdbDataBase, 0)
  172. mogDataObj := mgo.EdbDataBusiness{}
  173. // 构建查询条件
  174. queryConditions := bson.M{
  175. "edb_info_id": edbInfoId,
  176. }
  177. // 获取列表数据
  178. tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"data_time"})
  179. if tmpErr != nil {
  180. err = tmpErr
  181. return
  182. }
  183. for k, v := range tmpDataList {
  184. list = append(list, &EdbDataBase{
  185. EdbDataId: k + 1,
  186. EdbInfoId: v.EdbInfoId,
  187. EdbCode: v.EdbCode,
  188. DataTime: v.DataTime.Format(utils.FormatDate),
  189. DataTimestamp: v.DataTimestamp,
  190. Value: strconv.FormatFloat(v.Value, 'f', -1, 64),
  191. })
  192. }
  193. return
  194. }
  195. func GetEdbDataAllByEdbCodeByMongo(edbCode string, source, subSource, limit int) (list []*EdbInfoSearchData, err error) {
  196. list = make([]*EdbInfoSearchData, 0)
  197. mogDataObj := mgo.EdbDataBusiness{}
  198. // 构建查询条件
  199. queryConditions := bson.M{
  200. "edb_code": edbCode,
  201. }
  202. var tmpDataList []*mgo.EdbDataBusiness
  203. // 获取列表数据
  204. if limit > 0 {
  205. tmpDataList, err = mogDataObj.GetLimitDataList(queryConditions, int64(limit), []string{"-data_time"})
  206. } else {
  207. tmpDataList, err = mogDataObj.GetLimitDataList(queryConditions, int64(limit), []string{"-data_time"})
  208. }
  209. if err != nil {
  210. return
  211. }
  212. for _, v := range tmpDataList {
  213. list = append(list, &EdbInfoSearchData{
  214. DataTime: v.DataTime.Format(utils.FormatDate),
  215. Value: v.Value,
  216. })
  217. }
  218. return
  219. }
  220. func GetBaseIndexInfoByEdbCode(edbCode string, source int) (item *BaseIndexInfo, err error) {
  221. var pars []interface{}
  222. pars = append(pars, edbCode)
  223. tableName := GetBaseIndexTableName(source)
  224. if tableName == "" {
  225. err = fmt.Errorf("未找到对应的表")
  226. return
  227. }
  228. sql := ` SELECT * FROM %s WHERE index_code=? `
  229. sql = fmt.Sprintf(sql, tableName)
  230. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&item).Error
  231. return
  232. }
  233. func GetBaseIndexTableName(source int) (tableName string) {
  234. edbSource := GetEdbSourceBySourceId(source)
  235. if edbSource != nil {
  236. tableName = edbSource.IndexTableName
  237. }
  238. return
  239. }
  240. func GetEdbDataAllByEdbCodeAndSubSource(edbCode string, source, subSource, limit int) (items []*EdbInfoSearchData, err error) {
  241. var pars []interface{}
  242. pars = append(pars, edbCode)
  243. tableName := GetEdbDataTableNameAndSubSource(source, subSource)
  244. sql := ` SELECT * FROM %s WHERE edb_code=? ORDER BY data_time DESC`
  245. if limit > 0 {
  246. sql += ` LIMIT ? `
  247. pars = append(pars, limit)
  248. }
  249. sql = fmt.Sprintf(sql, tableName)
  250. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&items).Error
  251. return
  252. }
  253. // GetEdbDataTableNameAndSubSource 指标数据->存储表
  254. func GetEdbDataTableNameAndSubSource(source, subSource int) (tableName string) {
  255. switch source {
  256. default:
  257. edbSource := GetEdbSourceBySourceId(source)
  258. if edbSource != nil {
  259. tableName = edbSource.TableName
  260. }
  261. }
  262. return
  263. }
  264. func GetAddSql(edbInfoId, edbCode, dataTime, timestampStr string, value string) (addSql string) {
  265. nowStr := time.Now().Format(utils.FormatDateTime)
  266. addSql += "("
  267. addSql += edbInfoId + "," + "'" + edbCode + "'" + "," + "'" + dataTime + "'" + "," + value + "," + "'" + nowStr + "'" +
  268. "," + "'" + nowStr + "'" + "," + "1"
  269. addSql += "," + "'" + timestampStr + "'"
  270. addSql += "),"
  271. return
  272. }
  273. func GetThsHfEdbDataAllByEdbCodeByMongo(edbCode string, source, subSource, limit int) (list []*EdbInfoSearchData, err error) {
  274. list = make([]*EdbInfoSearchData, 0)
  275. mogDataObj := mgo.EdbDataThsHf{}
  276. // 构建查询条件
  277. queryConditions := bson.M{
  278. "edb_code": edbCode,
  279. }
  280. var tmpDataList []*mgo.EdbDataThsHf
  281. // 获取列表数据
  282. if limit > 0 {
  283. tmpDataList, err = mogDataObj.GetLimitDataList(queryConditions, int64(limit), []string{"-data_time"})
  284. } else {
  285. tmpDataList, err = mogDataObj.GetLimitDataList(queryConditions, int64(limit), []string{"-data_time"})
  286. }
  287. if err != nil {
  288. return
  289. }
  290. for _, v := range tmpDataList {
  291. list = append(list, &EdbInfoSearchData{
  292. DataTime: v.DataTime.Format(utils.FormatDate),
  293. Value: v.Value,
  294. })
  295. }
  296. return
  297. }
  298. func GetThsHfEdbDataBaseMongoByEdbInfoId(edbInfoId int, source, subSource int) (list []*EdbDataBase, err error) {
  299. list = make([]*EdbDataBase, 0)
  300. mogDataObj := mgo.EdbDataThsHf{}
  301. // 构建查询条件
  302. queryConditions := bson.M{
  303. "edb_info_id": edbInfoId,
  304. }
  305. // 获取列表数据
  306. tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"data_time"})
  307. if tmpErr != nil {
  308. err = tmpErr
  309. return
  310. }
  311. for k, v := range tmpDataList {
  312. list = append(list, &EdbDataBase{
  313. EdbDataId: k + 1,
  314. EdbInfoId: v.EdbInfoId,
  315. EdbCode: v.EdbCode,
  316. DataTime: v.DataTime.Format(utils.FormatDate),
  317. DataTimestamp: v.DataTimestamp,
  318. Value: strconv.FormatFloat(v.Value, 'f', -1, 64),
  319. })
  320. }
  321. return
  322. }