gn_platform.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package index_data
  2. import (
  3. "eta_gn/eta_bridge/global"
  4. "eta_gn/eta_bridge/models/gn"
  5. "eta_gn/eta_bridge/models/index"
  6. "eta_gn/eta_bridge/services/alarm_msg"
  7. "eta_gn/eta_bridge/utils"
  8. "fmt"
  9. "github.com/rdlucklib/rdluck_tools/paging"
  10. "github.com/shopspring/decimal"
  11. "strings"
  12. "time"
  13. )
  14. // TODO 过滤前后空格
  15. // GetPageIndexesFromGn 分页获取指标数据
  16. func GetPageIndexesFromGn(pageIndex, pageSize int, modifyTime, indexId string) (list []gn.IndexInfo, page *paging.PagingItem, err error) {
  17. defer func() {
  18. if err != nil {
  19. global.LOG.Info("GetPageIndexesFromGn Err: " + err.Error())
  20. go alarm_msg.SendAlarmMsg("GetPageIndexesFromGn Err: "+err.Error(), 3)
  21. return
  22. }
  23. }()
  24. req := IndexInfoReq{
  25. UpdateTime: "",
  26. Size: pageSize,
  27. PageNo: pageIndex,
  28. }
  29. if indexId != "" {
  30. req.IndexId = indexId
  31. }
  32. resp, err := GetIndexInfoList(req)
  33. if err != nil {
  34. return
  35. }
  36. page = paging.GetPaging(resp.Data.PageNo, resp.Data.Size, resp.Data.Total)
  37. list = make([]gn.IndexInfo, 0)
  38. for _, v := range resp.Data.Records {
  39. //if v.INDEXLEVELTHREE == `` {
  40. // continue
  41. //}
  42. // 必须保证有一级目录
  43. if v.INDEXLEVELONE == `` {
  44. v.INDEXLEVELONE = `其它指标`
  45. }
  46. //if v.INDEXLEVELTWO == `` {
  47. // v.INDEXLEVELTWO = `其它指标`
  48. //}
  49. //if v.INDEXLEVELTHREE == `` {
  50. // v.INDEXLEVELTHREE = `其它指标`
  51. //}
  52. list = append(list, gn.IndexInfo{
  53. ClassifyNameOne: strings.TrimSpace(v.INDEXLEVELONE),
  54. ClassifyNameTwo: strings.TrimSpace(v.INDEXLEVELTWO),
  55. ClassifyNameThree: strings.TrimSpace(v.INDEXLEVELTHREE),
  56. DataIndexCode: fmt.Sprint(v.UNIQUE_ID),
  57. SourceEdbCode: strings.TrimSpace(v.INDEXORIGINCODE),
  58. EdbName: strings.TrimSpace(v.INDEXNAME),
  59. Frequency: strings.TrimSpace(v.DATAFREQ),
  60. Unit: strings.TrimSpace(v.DATAUNIT),
  61. SourceName: strings.TrimSpace(v.DATASOURCE),
  62. SourceCode: strings.TrimSpace(v.DATASOURCECODE),
  63. })
  64. }
  65. return
  66. }
  67. // GetIndexAndDataFromGn 获取指标和数据
  68. func GetIndexAndDataFromGn(indexCode, startDate, endDate string) (list []gn.IndexData, err error) {
  69. defer func() {
  70. if err != nil {
  71. global.LOG.Info("GetIndexAndDataFromGn Err: " + err.Error())
  72. go alarm_msg.SendAlarmMsg("GetIndexAndDataFromGn Err: "+err.Error(), 3)
  73. return
  74. }
  75. }()
  76. var dataDate string
  77. dateList := make([]string, 0)
  78. // 没有指定开始日期的话,那就默认当前日期
  79. if startDate == `` {
  80. startDate = `1901-01-01`
  81. }
  82. dateList = append(dateList, startDate)
  83. // 没有的话,就默认往后找一个月
  84. if endDate == `` {
  85. endDate = time.Now().AddDate(0, 1, 0).Format(utils.FormatDate)
  86. }
  87. dateList = append(dateList, endDate)
  88. if len(dateList) > 0 {
  89. dataDate = strings.Join(dateList, ",")
  90. }
  91. req := IndexValueReq{
  92. UniqueId: indexCode,
  93. DataDate: dataDate,
  94. Size: 500,
  95. PageNo: 1,
  96. }
  97. list = make([]gn.IndexData, 0)
  98. isEnd := false
  99. for i := 1; !isEnd; i++ {
  100. req.PageNo = i
  101. tmpList, page, tmpErr := getIndexAndDataFromGn(req)
  102. if tmpErr != nil {
  103. err = tmpErr
  104. return
  105. }
  106. list = append(list, tmpList...)
  107. isEnd = page.IsEnd
  108. }
  109. return
  110. }
  111. // getIndexAndDataFromGn
  112. // @Description: 获取数据
  113. // @param req
  114. // @return list
  115. // @return page
  116. // @return err
  117. func getIndexAndDataFromGn(req IndexValueReq) (list []gn.IndexData, page *paging.PagingItem, err error) {
  118. defer func() {
  119. if err != nil {
  120. global.LOG.Info("GetIndexAndDataFromGn Err: " + err.Error())
  121. go alarm_msg.SendAlarmMsg("GetIndexAndDataFromGn Err: "+err.Error(), 3)
  122. return
  123. }
  124. }()
  125. resp, err := GetIndexValueList(req)
  126. if err != nil {
  127. return
  128. }
  129. page = paging.GetPaging(resp.Data.PageNo, resp.Data.Size, resp.Data.Total)
  130. list = make([]gn.IndexData, 0)
  131. for _, v := range resp.Data.Records {
  132. // 非数值数据,过滤
  133. //脏数据:
  134. // 停产
  135. // TODO、
  136. // 1-2
  137. // 空字符串
  138. // 空数据,过滤
  139. if v.VALUE == `` {
  140. continue
  141. }
  142. valF, tmpErr := decimal.NewFromString(strings.TrimSpace(v.VALUE))
  143. if tmpErr != nil {
  144. //err = tmpErr
  145. continue
  146. }
  147. val, _ := valF.Float64()
  148. dataTime, tmpErr := time.ParseInLocation(utils.FormatDateTime, v.DATADATE, time.Local)
  149. if tmpErr != nil {
  150. continue
  151. }
  152. list = append(list, gn.IndexData{
  153. Val: val,
  154. UpdateTime: v.UPDATETIME,
  155. DataTime: dataTime.Format(utils.FormatDate),
  156. })
  157. }
  158. return
  159. }
  160. func FixData() {
  161. edbInfoList, err := index.GetAllBaseEdbInfo()
  162. if err != nil {
  163. fmt.Println("获取指标列表失败:", err)
  164. return
  165. }
  166. sourceMap := map[int]string{
  167. 96: `S02`,
  168. 97: `S04`,
  169. 98: `S03`,
  170. 99: `S05`,
  171. 100: `S08`,
  172. 101: `S01`,
  173. }
  174. count := len(edbInfoList)
  175. for k, v := range edbInfoList {
  176. fmt.Println("剩余:", count-k, "条数据")
  177. list, _, err := getData(1, 100, v.EdbCode, sourceMap[v.Source])
  178. if err != nil {
  179. fmt.Println("获取指标数据失败:", err)
  180. continue
  181. }
  182. if len(list) > 0 {
  183. uniqueCode := list[0].DataIndexCode
  184. fmt.Println(v.EdbCode, " === ", uniqueCode)
  185. v.OriginalEdbCode = uniqueCode
  186. err = v.Update([]string{"OriginalEdbCode"})
  187. if err != nil {
  188. fmt.Println("修改指标原始编码失败:", err)
  189. continue
  190. }
  191. }
  192. }
  193. }
  194. func getData(pageIndex, pageSize int, edbCode, dataSource string) (list []gn.IndexInfo, page *paging.PagingItem, err error) {
  195. defer func() {
  196. if err != nil {
  197. global.LOG.Info("GetPageIndexesFromGn Err: " + err.Error())
  198. go alarm_msg.SendAlarmMsg("GetPageIndexesFromGn Err: "+err.Error(), 3)
  199. return
  200. }
  201. }()
  202. req := IndexInfoV2Req{
  203. UpdateTime: "",
  204. Size: pageSize,
  205. PageNo: pageIndex,
  206. EdbCode: edbCode,
  207. SourceCode: dataSource,
  208. }
  209. resp, err := GetIndexInfoListV2(req)
  210. if err != nil {
  211. return
  212. }
  213. page = paging.GetPaging(resp.Data.PageNo, resp.Data.Size, resp.Data.Total)
  214. list = make([]gn.IndexInfo, 0)
  215. for _, v := range resp.Data.Records {
  216. //if v.INDEXLEVELTHREE == `` {
  217. // continue
  218. //}
  219. // 必须保证有一级目录
  220. if v.INDEXLEVELONE == `` {
  221. v.INDEXLEVELONE = `其它指标`
  222. }
  223. //if v.INDEXLEVELTWO == `` {
  224. // v.INDEXLEVELTWO = `其它指标`
  225. //}
  226. //if v.INDEXLEVELTHREE == `` {
  227. // v.INDEXLEVELTHREE = `其它指标`
  228. //}
  229. list = append(list, gn.IndexInfo{
  230. ClassifyNameOne: strings.TrimSpace(v.INDEXLEVELONE),
  231. ClassifyNameTwo: strings.TrimSpace(v.INDEXLEVELTWO),
  232. ClassifyNameThree: strings.TrimSpace(v.INDEXLEVELTHREE),
  233. DataIndexCode: fmt.Sprint(v.UNIQUE_ID),
  234. SourceEdbCode: strings.TrimSpace(v.INDEXORIGINCODE),
  235. EdbName: strings.TrimSpace(v.INDEXORIGINNAME),
  236. Frequency: strings.TrimSpace(v.DATAFREQ),
  237. Unit: strings.TrimSpace(v.DATAUNIT),
  238. SourceName: strings.TrimSpace(v.DATASOURCE),
  239. SourceCode: strings.TrimSpace(v.DATASOURCECODE),
  240. })
  241. }
  242. return
  243. }