jiayue_index.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package data
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models/data_manage"
  5. "eta/eta_api/services/alarm_msg"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. )
  12. // 嘉悦桥接服务接口
  13. var (
  14. BridgeApiJiaYueIndexUrl = "/api/index_data/jiayue/index" // 获取指标信息
  15. BridgeApiJiaYueIndexDataUrl = "/api/index_data/jiayue/index_data" // 获取指标数据
  16. BridgeApiJiaYuePageIndexUrl = "/api/index_data/jiayue/page_index" // 获取指标列表-分页
  17. BridgeApiJiaYueFrequencyListUrl = "/api/index_data/jiayue/frequency_list" // 获取指标频度列表
  18. )
  19. type EdbBridgeJiaYue struct{}
  20. // GetIndex 获取指标信息
  21. func (brg *EdbBridgeJiaYue) GetIndex(req GetIndexFromBridgeReq) (item BridgeIndexItem, err error) {
  22. var params data_manage.BridgeJiaYueIndexParams
  23. params.IndexCode = req.IndexCode
  24. params.SourceExtend = req.SourceExtend
  25. params.IndexCodeRequired = req.IndexCodeRequired
  26. indexData, e := GetJiaYueIndexFromBridge(params)
  27. if e != nil {
  28. err = fmt.Errorf("GetJiaYueIndexDataFromBridge err: %s", e.Error())
  29. return
  30. }
  31. if indexData.Id <= 0 {
  32. return
  33. }
  34. item.Id = indexData.Id
  35. item.IndexCode = indexData.IndexCode
  36. item.IndexName = indexData.IndexName
  37. item.Unit = indexData.Unit
  38. item.Frequency = brg.TransFrequency(indexData.Frequency)
  39. return
  40. }
  41. // GetIndexAndData 获取指标和数据
  42. //func (brg *EdbBridgeJiaYue) GetIndexAndData(req GetIndexAndDataFromBridgeReq) (item BridgeIndexItem, err error) {
  43. // var params data_manage.BridgeJiaYueIndexDataParams
  44. // params.IndexCode = req.IndexCode
  45. // params.SourceExtend = req.SourceExtend
  46. // params.StartDate = req.StartDate
  47. // params.EndDate = req.EndDate
  48. // indexData, e := GetJiaYueIndexDataFromBridge(params)
  49. // if e != nil {
  50. // err = fmt.Errorf("GetJiaYueIndexDataFromBridge err: %s", e.Error())
  51. // return
  52. // }
  53. // if indexData.Id <= 0 {
  54. // return
  55. // }
  56. // item.Id = indexData.Id
  57. // item.IndexCode = indexData.IndexCode
  58. // item.IndexName = indexData.IndexName
  59. // item.Unit = indexData.Unit
  60. // item.Frequency = brg.TransFrequency(indexData.Frequency)
  61. // var dataList []BridgeIndexDataItem
  62. // for _, v := range indexData.IndexData {
  63. // dataList = append(dataList, BridgeIndexDataItem{
  64. // Val: v.Val,
  65. // DataTime: v.DataTime,
  66. // UpdateTime: v.UpdateTime,
  67. // })
  68. // }
  69. // item.Data = dataList
  70. // return
  71. //}
  72. // TransFrequency 嘉悦指标频度转换
  73. func (brg *EdbBridgeJiaYue) TransFrequency(origin string) string {
  74. mapping := map[string]string{
  75. "日": "日度",
  76. "周": "周度",
  77. "旬": "旬度",
  78. "半月": "旬度",
  79. "月": "月度",
  80. "季": "季度",
  81. "半年": "半年度",
  82. "年": "年度",
  83. "日度": "日度",
  84. "周度": "周度",
  85. "旬度": "旬度",
  86. "月度": "月度",
  87. "季度": "季度",
  88. "半年度": "半年度",
  89. "年度": "年度",
  90. }
  91. return mapping[origin]
  92. }
  93. // GetJiaYueIndexFromBridge 从桥接服务获取指标信息
  94. func GetJiaYueIndexFromBridge(param data_manage.BridgeJiaYueIndexParams) (indexData data_manage.BridgeJiaYueIndexAndData, err error) {
  95. defer func() {
  96. if err != nil {
  97. b, _ := json.Marshal(param)
  98. tips := fmt.Sprintf("桥接服务-获取嘉悦指标信息, err: %s, params: %s", err.Error(), string(b))
  99. go alarm_msg.SendAlarmMsg(tips, 3)
  100. }
  101. }()
  102. url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiJiaYueIndexUrl)
  103. data, e := json.Marshal(param)
  104. if e != nil {
  105. err = fmt.Errorf("data json marshal err: %s", e.Error())
  106. return
  107. }
  108. body := ioutil.NopCloser(strings.NewReader(string(data)))
  109. client := &http.Client{}
  110. req, e := http.NewRequest("POST", url, body)
  111. if e != nil {
  112. err = fmt.Errorf("http create request err: %s", e.Error())
  113. return
  114. }
  115. checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  116. contentType := "application/json;charset=utf-8"
  117. req.Header.Set("Content-Type", contentType)
  118. req.Header.Set("Authorization", checkToken)
  119. resp, e := client.Do(req)
  120. if e != nil {
  121. err = fmt.Errorf("http client do err: %s", e.Error())
  122. return
  123. }
  124. defer func() {
  125. _ = resp.Body.Close()
  126. }()
  127. b, e := ioutil.ReadAll(resp.Body)
  128. if e != nil {
  129. err = fmt.Errorf("resp body read err: %s", e.Error())
  130. return
  131. }
  132. if len(b) == 0 {
  133. err = fmt.Errorf("resp body is empty")
  134. return
  135. }
  136. // 生产环境解密
  137. if utils.RunMode == "release" {
  138. str := string(b)
  139. str = strings.Trim(str, `"`)
  140. b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  141. }
  142. result := new(data_manage.BridgeJiaYueResultIndexData)
  143. if e = json.Unmarshal(b, &result); e != nil {
  144. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  145. return
  146. }
  147. if result.Code != 200 {
  148. err = fmt.Errorf("result: %s", string(b))
  149. return
  150. }
  151. indexData = result.Data
  152. return
  153. }
  154. // GetJiaYueIndexDataFromBridge 从桥接服务获取指标数据
  155. //func GetJiaYueIndexDataFromBridge(param data_manage.BridgeJiaYueIndexDataParams) (indexData data_manage.BridgeJiaYueIndexAndData, err error) {
  156. // defer func() {
  157. // if err != nil {
  158. // b, _ := json.Marshal(param)
  159. // tips := fmt.Sprintf("桥接服务-获取嘉悦指标数据, err: %s, params: %s", err.Error(), string(b))
  160. // go alarm_msg.SendAlarmMsg(tips, 3)
  161. // }
  162. // }()
  163. //
  164. // url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiJiaYueIndexDataUrl)
  165. // data, e := json.Marshal(param)
  166. // if e != nil {
  167. // err = fmt.Errorf("data json marshal err: %s", e.Error())
  168. // return
  169. // }
  170. // body := ioutil.NopCloser(strings.NewReader(string(data)))
  171. // client := &http.Client{}
  172. // req, e := http.NewRequest("POST", url, body)
  173. // if e != nil {
  174. // err = fmt.Errorf("http create request err: %s", e.Error())
  175. // return
  176. // }
  177. //
  178. // checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  179. // contentType := "application/json;charset=utf-8"
  180. // req.Header.Set("Content-Type", contentType)
  181. // req.Header.Set("Authorization", checkToken)
  182. // resp, e := client.Do(req)
  183. // if e != nil {
  184. // err = fmt.Errorf("http client do err: %s", e.Error())
  185. // return
  186. // }
  187. // defer func() {
  188. // _ = resp.Body.Close()
  189. // }()
  190. // b, e := ioutil.ReadAll(resp.Body)
  191. // if e != nil {
  192. // err = fmt.Errorf("resp body read err: %s", e.Error())
  193. // return
  194. // }
  195. // if len(b) == 0 {
  196. // err = fmt.Errorf("resp body is empty")
  197. // return
  198. // }
  199. // // 生产环境解密
  200. // if utils.RunMode == "release" {
  201. // str := string(b)
  202. // str = strings.Trim(str, `"`)
  203. // b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  204. // }
  205. //
  206. // result := new(data_manage.BridgeJiaYueResultIndexData)
  207. // if e = json.Unmarshal(b, &result); e != nil {
  208. // err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  209. // return
  210. // }
  211. // if result.Code != 200 {
  212. // err = fmt.Errorf("result: %s", string(b))
  213. // return
  214. // }
  215. // indexData = result.Data
  216. // return
  217. //}
  218. // GetJiaYueFrequencyListFromBridge 获取指标频度列表
  219. func GetJiaYueFrequencyListFromBridge() (frequencies []string, err error) {
  220. url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiJiaYueFrequencyListUrl)
  221. body := ioutil.NopCloser(strings.NewReader(""))
  222. client := &http.Client{}
  223. req, e := http.NewRequest("POST", url, body)
  224. if e != nil {
  225. err = fmt.Errorf("http create request err: %s", e.Error())
  226. return
  227. }
  228. checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  229. contentType := "application/json;charset=utf-8"
  230. req.Header.Set("Content-Type", contentType)
  231. req.Header.Set("Authorization", checkToken)
  232. resp, e := client.Do(req)
  233. if e != nil {
  234. err = fmt.Errorf("http client do err: %s", e.Error())
  235. return
  236. }
  237. defer func() {
  238. _ = resp.Body.Close()
  239. }()
  240. b, e := ioutil.ReadAll(resp.Body)
  241. if e != nil {
  242. err = fmt.Errorf("resp body read err: %s", e.Error())
  243. return
  244. }
  245. if len(b) == 0 {
  246. err = fmt.Errorf("resp body is empty")
  247. return
  248. }
  249. // 生产环境解密
  250. if utils.RunMode == "release" {
  251. str := string(b)
  252. str = strings.Trim(str, `"`)
  253. b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  254. }
  255. result := new(data_manage.BridgeJiaYueResultFrequencyList)
  256. if e = json.Unmarshal(b, &result); e != nil {
  257. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  258. return
  259. }
  260. if result.Code != 200 {
  261. err = fmt.Errorf("result: %s", string(b))
  262. return
  263. }
  264. frequencies = result.Data
  265. return
  266. }
  267. // GetJiaYueIndexPageListFromBridge 获取指标分页列表
  268. func GetJiaYueIndexPageListFromBridge(param data_manage.BridgeJiaYuePageIndexReq) (total int, items []data_manage.DictIndex, err error) {
  269. url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiJiaYuePageIndexUrl)
  270. data, e := json.Marshal(param)
  271. if e != nil {
  272. err = fmt.Errorf("data json marshal err: %s", e.Error())
  273. return
  274. }
  275. body := ioutil.NopCloser(strings.NewReader(string(data)))
  276. client := &http.Client{}
  277. req, e := http.NewRequest("POST", url, body)
  278. if e != nil {
  279. err = fmt.Errorf("http create request err: %s", e.Error())
  280. return
  281. }
  282. checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  283. contentType := "application/json;charset=utf-8"
  284. req.Header.Set("Content-Type", contentType)
  285. req.Header.Set("Authorization", checkToken)
  286. resp, e := client.Do(req)
  287. if e != nil {
  288. err = fmt.Errorf("http client do err: %s", e.Error())
  289. return
  290. }
  291. defer func() {
  292. _ = resp.Body.Close()
  293. }()
  294. b, e := ioutil.ReadAll(resp.Body)
  295. if e != nil {
  296. err = fmt.Errorf("resp body read err: %s", e.Error())
  297. return
  298. }
  299. if len(b) == 0 {
  300. err = fmt.Errorf("resp body is empty")
  301. return
  302. }
  303. // 生产环境解密
  304. if utils.RunMode == "release" {
  305. str := string(b)
  306. str = strings.Trim(str, `"`)
  307. b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  308. }
  309. result := new(data_manage.BridgeJiaYueResultIndexPageList)
  310. if e = json.Unmarshal(b, &result); e != nil {
  311. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  312. return
  313. }
  314. if result.Code != 200 {
  315. err = fmt.Errorf("result: %s", string(b))
  316. return
  317. }
  318. total = result.Data.Total
  319. items = result.Data.List
  320. return
  321. }