jiayue_index.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package data
  2. import (
  3. "encoding/json"
  4. "eta/eta_mobile/models/data_manage"
  5. "eta/eta_mobile/services/alarm_msg"
  6. "eta/eta_mobile/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. return mapping[origin]
  85. }
  86. // GetJiaYueIndexFromBridge 从桥接服务获取指标信息
  87. func GetJiaYueIndexFromBridge(param data_manage.BridgeJiaYueIndexParams) (indexData data_manage.BridgeJiaYueIndexAndData, err error) {
  88. defer func() {
  89. if err != nil {
  90. b, _ := json.Marshal(param)
  91. tips := fmt.Sprintf("桥接服务-获取嘉悦指标信息, err: %s, params: %s", err.Error(), string(b))
  92. go alarm_msg.SendAlarmMsg(tips, 3)
  93. }
  94. }()
  95. url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiJiaYueIndexUrl)
  96. data, e := json.Marshal(param)
  97. if e != nil {
  98. err = fmt.Errorf("data json marshal err: %s", e.Error())
  99. return
  100. }
  101. body := ioutil.NopCloser(strings.NewReader(string(data)))
  102. client := &http.Client{}
  103. req, e := http.NewRequest("POST", url, body)
  104. if e != nil {
  105. err = fmt.Errorf("http create request err: %s", e.Error())
  106. return
  107. }
  108. checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  109. contentType := "application/json;charset=utf-8"
  110. req.Header.Set("Content-Type", contentType)
  111. req.Header.Set("Authorization", checkToken)
  112. resp, e := client.Do(req)
  113. if e != nil {
  114. err = fmt.Errorf("http client do err: %s", e.Error())
  115. return
  116. }
  117. defer func() {
  118. _ = resp.Body.Close()
  119. }()
  120. b, e := ioutil.ReadAll(resp.Body)
  121. if e != nil {
  122. err = fmt.Errorf("resp body read err: %s", e.Error())
  123. return
  124. }
  125. if len(b) == 0 {
  126. err = fmt.Errorf("resp body is empty")
  127. return
  128. }
  129. // 生产环境解密
  130. if utils.RunMode == "release" {
  131. str := string(b)
  132. str = strings.Trim(str, `"`)
  133. b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  134. }
  135. result := new(data_manage.BridgeJiaYueResultIndexData)
  136. if e = json.Unmarshal(b, &result); e != nil {
  137. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  138. return
  139. }
  140. if result.Code != 200 {
  141. err = fmt.Errorf("result: %s", string(b))
  142. return
  143. }
  144. indexData = result.Data
  145. return
  146. }
  147. // GetJiaYueIndexDataFromBridge 从桥接服务获取指标数据
  148. //func GetJiaYueIndexDataFromBridge(param data_manage.BridgeJiaYueIndexDataParams) (indexData data_manage.BridgeJiaYueIndexAndData, err error) {
  149. // defer func() {
  150. // if err != nil {
  151. // b, _ := json.Marshal(param)
  152. // tips := fmt.Sprintf("桥接服务-获取嘉悦指标数据, err: %s, params: %s", err.Error(), string(b))
  153. // go alarm_msg.SendAlarmMsg(tips, 3)
  154. // }
  155. // }()
  156. //
  157. // url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiJiaYueIndexDataUrl)
  158. // data, e := json.Marshal(param)
  159. // if e != nil {
  160. // err = fmt.Errorf("data json marshal err: %s", e.Error())
  161. // return
  162. // }
  163. // body := ioutil.NopCloser(strings.NewReader(string(data)))
  164. // client := &http.Client{}
  165. // req, e := http.NewRequest("POST", url, body)
  166. // if e != nil {
  167. // err = fmt.Errorf("http create request err: %s", e.Error())
  168. // return
  169. // }
  170. //
  171. // checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  172. // contentType := "application/json;charset=utf-8"
  173. // req.Header.Set("Content-Type", contentType)
  174. // req.Header.Set("Authorization", checkToken)
  175. // resp, e := client.Do(req)
  176. // if e != nil {
  177. // err = fmt.Errorf("http client do err: %s", e.Error())
  178. // return
  179. // }
  180. // defer func() {
  181. // _ = resp.Body.Close()
  182. // }()
  183. // b, e := ioutil.ReadAll(resp.Body)
  184. // if e != nil {
  185. // err = fmt.Errorf("resp body read err: %s", e.Error())
  186. // return
  187. // }
  188. // if len(b) == 0 {
  189. // err = fmt.Errorf("resp body is empty")
  190. // return
  191. // }
  192. // // 生产环境解密
  193. // if utils.RunMode == "release" {
  194. // str := string(b)
  195. // str = strings.Trim(str, `"`)
  196. // b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  197. // }
  198. //
  199. // result := new(data_manage.BridgeJiaYueResultIndexData)
  200. // if e = json.Unmarshal(b, &result); e != nil {
  201. // err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  202. // return
  203. // }
  204. // if result.Code != 200 {
  205. // err = fmt.Errorf("result: %s", string(b))
  206. // return
  207. // }
  208. // indexData = result.Data
  209. // return
  210. //}
  211. // GetJiaYueFrequencyListFromBridge 获取指标频度列表
  212. func GetJiaYueFrequencyListFromBridge() (frequencies []string, err error) {
  213. url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiJiaYueFrequencyListUrl)
  214. body := ioutil.NopCloser(strings.NewReader(""))
  215. client := &http.Client{}
  216. req, e := http.NewRequest("POST", url, body)
  217. if e != nil {
  218. err = fmt.Errorf("http create request err: %s", e.Error())
  219. return
  220. }
  221. checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  222. contentType := "application/json;charset=utf-8"
  223. req.Header.Set("Content-Type", contentType)
  224. req.Header.Set("Authorization", checkToken)
  225. resp, e := client.Do(req)
  226. if e != nil {
  227. err = fmt.Errorf("http client do err: %s", e.Error())
  228. return
  229. }
  230. defer func() {
  231. _ = resp.Body.Close()
  232. }()
  233. b, e := ioutil.ReadAll(resp.Body)
  234. if e != nil {
  235. err = fmt.Errorf("resp body read err: %s", e.Error())
  236. return
  237. }
  238. if len(b) == 0 {
  239. err = fmt.Errorf("resp body is empty")
  240. return
  241. }
  242. // 生产环境解密
  243. if utils.RunMode == "release" {
  244. str := string(b)
  245. str = strings.Trim(str, `"`)
  246. b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  247. }
  248. result := new(data_manage.BridgeJiaYueResultFrequencyList)
  249. if e = json.Unmarshal(b, &result); e != nil {
  250. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  251. return
  252. }
  253. if result.Code != 200 {
  254. err = fmt.Errorf("result: %s", string(b))
  255. return
  256. }
  257. frequencies = result.Data
  258. return
  259. }
  260. // GetJiaYueIndexPageListFromBridge 获取指标分页列表
  261. func GetJiaYueIndexPageListFromBridge(param data_manage.BridgeJiaYuePageIndexReq) (total int, items []data_manage.DictIndex, err error) {
  262. url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiJiaYuePageIndexUrl)
  263. data, e := json.Marshal(param)
  264. if e != nil {
  265. err = fmt.Errorf("data json marshal err: %s", e.Error())
  266. return
  267. }
  268. body := ioutil.NopCloser(strings.NewReader(string(data)))
  269. client := &http.Client{}
  270. req, e := http.NewRequest("POST", url, body)
  271. if e != nil {
  272. err = fmt.Errorf("http create request err: %s", e.Error())
  273. return
  274. }
  275. checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  276. contentType := "application/json;charset=utf-8"
  277. req.Header.Set("Content-Type", contentType)
  278. req.Header.Set("Authorization", checkToken)
  279. resp, e := client.Do(req)
  280. if e != nil {
  281. err = fmt.Errorf("http client do err: %s", e.Error())
  282. return
  283. }
  284. defer func() {
  285. _ = resp.Body.Close()
  286. }()
  287. b, e := ioutil.ReadAll(resp.Body)
  288. if e != nil {
  289. err = fmt.Errorf("resp body read err: %s", e.Error())
  290. return
  291. }
  292. if len(b) == 0 {
  293. err = fmt.Errorf("resp body is empty")
  294. return
  295. }
  296. // 生产环境解密
  297. if utils.RunMode == "release" {
  298. str := string(b)
  299. str = strings.Trim(str, `"`)
  300. b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  301. }
  302. result := new(data_manage.BridgeJiaYueResultIndexPageList)
  303. if e = json.Unmarshal(b, &result); e != nil {
  304. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  305. return
  306. }
  307. if result.Code != 200 {
  308. err = fmt.Errorf("result: %s", string(b))
  309. return
  310. }
  311. total = result.Data.Total
  312. items = result.Data.List
  313. return
  314. }