edb_data_base.go 11 KB

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