base_from_smm_http.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/models"
  5. "eta/eta_index_lib/services/alarm_msg"
  6. "eta/eta_index_lib/utils"
  7. "fmt"
  8. "io/ioutil"
  9. netHttp "net/http"
  10. "strings"
  11. "time"
  12. )
  13. var (
  14. BridgeApiZhongJiIndexDataUrl = "/api/index_data/zhongji/smm/data" // 获取指标数据API
  15. BridgeApiZhongJiIndexListUrl = "/api/index_data/zhongji/smm/list" // 获取指标列表API
  16. BridgeApiZhongJiIndexLatestUrl = "/api/index_data/zhongji/smm/latest" // 获取指标最新数据API
  17. )
  18. // Data is a struct that represents the data field in the JSON string
  19. type Data struct {
  20. Date string `json:"date"`
  21. Value string `json:"value"`
  22. Create string `json:"create"`
  23. }
  24. // Quota is a struct that represents the quota information in the JSON string
  25. type Quota struct {
  26. QuotaID string `json:"quota_id"`
  27. QuotaName string `json:"quota_name"`
  28. Unit string `json:"unit"`
  29. Frequency string `json:"frequency"`
  30. ValueType string `json:"value_type"`
  31. Precision int `json:"precision"`
  32. PermState int `json:"perm_state"`
  33. Data []Data `json:"data"`
  34. }
  35. // Response is a struct that represents the whole JSON string
  36. type SmmResponse struct {
  37. Code int `json:"code"`
  38. Msg string `json:"msg"`
  39. Quota []Quota `json:"data"`
  40. }
  41. type SmmResp struct {
  42. Code int `json:"code"`
  43. Msg string `json:"msg"`
  44. Data SmmResponse `json:"data"`
  45. }
  46. type GetSmmTokenResp struct {
  47. Code int `json:"code"`
  48. Msg string `json:"msg"`
  49. Data SmmTokenData `json:"data"`
  50. }
  51. type SmmTokenData struct {
  52. Token string `json:"token"`
  53. }
  54. // GetSmmNewIndexFromBridge 从桥接服务获取指标
  55. func GetSmmNewIndexFromBridge(edbCode string) (indexDataList []models.BaseFromSmmDataList, err error) {
  56. defer func() {
  57. if err != nil {
  58. tips := fmt.Sprintf("桥接服务-获取SMM指标失败, err: %s", err.Error())
  59. utils.FileLog.Info(tips)
  60. go alarm_msg.SendAlarmMsg(tips, 3)
  61. }
  62. }()
  63. url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiZhongJiIndexDataUrl)
  64. param := models.BridgeZhongJiIndexDataParams{EdbCode: edbCode}
  65. data, e := json.Marshal(param)
  66. if e != nil {
  67. err = fmt.Errorf("data json marshal err: %s", e.Error())
  68. return
  69. }
  70. body := ioutil.NopCloser(strings.NewReader(string(data)))
  71. client := &netHttp.Client{}
  72. req, e := netHttp.NewRequest("POST", url, body)
  73. if e != nil {
  74. err = fmt.Errorf("http create request err: %s", e.Error())
  75. return
  76. }
  77. checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  78. contentType := "application/json;charset=utf-8"
  79. req.Header.Set("Content-Type", contentType)
  80. req.Header.Set("Authorization", checkToken)
  81. resp, e := client.Do(req)
  82. if e != nil {
  83. err = fmt.Errorf("http client do err: %s", e.Error())
  84. return
  85. }
  86. defer func() {
  87. _ = resp.Body.Close()
  88. }()
  89. b, e := ioutil.ReadAll(resp.Body)
  90. if e != nil {
  91. err = fmt.Errorf("resp body read err: %s", e.Error())
  92. return
  93. }
  94. if len(b) == 0 {
  95. err = fmt.Errorf("resp body is empty")
  96. return
  97. }
  98. // 生产环境解密
  99. if utils.RunMode == "release" {
  100. str := string(b)
  101. str = strings.Trim(str, `"`)
  102. b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  103. }
  104. result := new(SmmResp)
  105. if e = json.Unmarshal(b, &result); e != nil {
  106. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  107. return
  108. }
  109. if result.Code != 200 {
  110. err = fmt.Errorf("result: %s", string(b))
  111. return
  112. }
  113. dataList := make([]models.BaseFromSmmDataList, 0)
  114. for _, quota := range result.Data.Quota {
  115. var dataListItem models.BaseFromSmmDataList
  116. dataListItem.IndexCode = quota.QuotaID
  117. for _, v := range quota.Data {
  118. dataItem := new(models.BaseFromSmmData)
  119. dataItem.IndexCode = quota.QuotaID
  120. dataItem.DataTime = v.Date
  121. dataItem.Value = v.Value
  122. createTime, e := time.Parse(utils.FormatDateTime, v.Create)
  123. if e != nil {
  124. err = fmt.Errorf("time parse err: %s", e.Error())
  125. return
  126. }
  127. dataItem.CreateTime = createTime
  128. dataItem.ModifyTime = createTime
  129. currentDate, _ := time.ParseInLocation(utils.FormatDate, v.Date, time.Local)
  130. timestamp := currentDate.UnixNano() / 1e6
  131. dataItem.DataTimestamp = timestamp
  132. dataListItem.Data = append(dataListItem.Data, dataItem)
  133. }
  134. dataList = append(dataList, dataListItem)
  135. }
  136. indexDataList = dataList
  137. return
  138. }
  139. // GetSmmNewIndexListFromBridge 从桥接服务获取指标列表
  140. func GetSmmNewIndexListFromBridge(params models.BridgeZhongJiIndexListParams) (indexList []*models.BaseFromSmmIndex, err error) {
  141. defer func() {
  142. if err != nil {
  143. tips := fmt.Sprintf("桥接服务-获取SMM指标失败, err: %s", err.Error())
  144. utils.FileLog.Info(tips)
  145. go alarm_msg.SendAlarmMsg(tips, 3)
  146. }
  147. }()
  148. url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiZhongJiIndexListUrl)
  149. data, e := json.Marshal(params)
  150. if e != nil {
  151. err = fmt.Errorf("data json marshal err: %s", e.Error())
  152. return
  153. }
  154. body := ioutil.NopCloser(strings.NewReader(string(data)))
  155. client := &netHttp.Client{}
  156. req, e := netHttp.NewRequest("POST", url, body)
  157. if e != nil {
  158. err = fmt.Errorf("http create request err: %s", e.Error())
  159. return
  160. }
  161. checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  162. contentType := "application/json;charset=utf-8"
  163. req.Header.Set("Content-Type", contentType)
  164. req.Header.Set("Authorization", checkToken)
  165. resp, e := client.Do(req)
  166. if e != nil {
  167. err = fmt.Errorf("http client do err: %s", e.Error())
  168. return
  169. }
  170. defer func() {
  171. _ = resp.Body.Close()
  172. }()
  173. b, e := ioutil.ReadAll(resp.Body)
  174. if e != nil {
  175. err = fmt.Errorf("resp body read err: %s", e.Error())
  176. return
  177. }
  178. if len(b) == 0 {
  179. err = fmt.Errorf("resp body is empty")
  180. return
  181. }
  182. // 生产环境解密
  183. if utils.RunMode == "release" {
  184. str := string(b)
  185. str = strings.Trim(str, `"`)
  186. b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  187. }
  188. result := new(models.SmmListResp)
  189. if e = json.Unmarshal(b, &result); e != nil {
  190. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  191. return
  192. }
  193. if result.Code != 200 {
  194. err = fmt.Errorf("result: %s", string(b))
  195. return
  196. }
  197. for _, v := range result.Data.Data.Data {
  198. typeList := strings.Split(v.TypeAll, ";")
  199. item := new(models.BaseFromSmmIndex)
  200. item.IndexCode = v.QuotaID
  201. item.IndexName = v.QuotaName
  202. item.Frequency = v.Frequency
  203. item.StartDate = v.DataStart
  204. item.EndDate = v.DataEnd
  205. item.Name = v.TypeName
  206. item.CreateTime = time.Now()
  207. item.Unit = v.Unit
  208. item.Frequency = v.Frequency
  209. if v.Frequency != "半年" {
  210. item.Frequency += "度"
  211. }
  212. item.Interface = "SMM"
  213. item.ModifyTime = time.Now()
  214. item.DataState = v.DataState
  215. item.ReleaseTime = v.UpdateRule.ReleaseTime
  216. for i, typeItem := range typeList {
  217. if i == 0 {
  218. item.Type1 = typeItem
  219. } else if i == 1 {
  220. item.Type2 = typeItem
  221. } else if i == 2 {
  222. item.Type3 = typeItem
  223. }
  224. }
  225. indexList = append(indexList, item)
  226. }
  227. return
  228. }
  229. // GetSmmNewIndexListTotalFromBridge 从桥接服务获取指标列表总数
  230. func GetSmmNewIndexListTotalFromBridge(params models.BridgeZhongJiIndexListParams) (total int, err error) {
  231. defer func() {
  232. if err != nil {
  233. tips := fmt.Sprintf("桥接服务-获取SMM指标失败, err: %s", err.Error())
  234. utils.FileLog.Info(tips)
  235. go alarm_msg.SendAlarmMsg(tips, 3)
  236. }
  237. }()
  238. url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiZhongJiIndexListUrl)
  239. data, e := json.Marshal(params)
  240. if e != nil {
  241. err = fmt.Errorf("data json marshal err: %s", e.Error())
  242. return
  243. }
  244. body := ioutil.NopCloser(strings.NewReader(string(data)))
  245. client := &netHttp.Client{}
  246. req, e := netHttp.NewRequest("POST", url, body)
  247. if e != nil {
  248. err = fmt.Errorf("http create request err: %s", e.Error())
  249. return
  250. }
  251. checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  252. contentType := "application/json;charset=utf-8"
  253. req.Header.Set("Content-Type", contentType)
  254. req.Header.Set("Authorization", checkToken)
  255. resp, e := client.Do(req)
  256. if e != nil {
  257. err = fmt.Errorf("http client do err: %s", e.Error())
  258. return
  259. }
  260. defer func() {
  261. _ = resp.Body.Close()
  262. }()
  263. b, e := ioutil.ReadAll(resp.Body)
  264. if e != nil {
  265. err = fmt.Errorf("resp body read err: %s", e.Error())
  266. return
  267. }
  268. if len(b) == 0 {
  269. err = fmt.Errorf("resp body is empty")
  270. return
  271. }
  272. // 生产环境解密
  273. if utils.RunMode == "release" {
  274. str := string(b)
  275. str = strings.Trim(str, `"`)
  276. b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  277. }
  278. result := new(models.SmmListResp)
  279. if e = json.Unmarshal(b, &result); e != nil {
  280. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  281. return
  282. }
  283. if result.Code != 200 {
  284. err = fmt.Errorf("result: %s", string(b))
  285. return
  286. }
  287. total = result.Data.Data.Total
  288. return
  289. }
  290. // GetSmmIndexLatestFromBridge 从桥接服务获取指标更新
  291. func GetSmmIndexLatestFromBridge(edbInfoId int, edbCode, startDate string) (err error) {
  292. defer func() {
  293. if err != nil {
  294. tips := fmt.Sprintf("桥接服务-获取SMM指标失败, err: %s", err.Error())
  295. utils.FileLog.Info(tips)
  296. go alarm_msg.SendAlarmMsg(tips, 3)
  297. }
  298. }()
  299. url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiZhongJiIndexLatestUrl)
  300. startDateTime, err := time.Parse(utils.FormatDate, startDate)
  301. if err != nil {
  302. err = fmt.Errorf("startDateTime parse err: %s", err.Error())
  303. return
  304. }
  305. timeStamp := startDateTime.Unix()
  306. param := models.BridgeZhongJiIndexLatestDataParams{
  307. EdbCode: edbCode,
  308. LastTime: timeStamp,
  309. }
  310. data, e := json.Marshal(param)
  311. if e != nil {
  312. err = fmt.Errorf("data json marshal err: %s", e.Error())
  313. return
  314. }
  315. body := ioutil.NopCloser(strings.NewReader(string(data)))
  316. client := &netHttp.Client{}
  317. req, e := netHttp.NewRequest("POST", url, body)
  318. if e != nil {
  319. err = fmt.Errorf("http create request err: %s", e.Error())
  320. return
  321. }
  322. checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  323. contentType := "application/json;charset=utf-8"
  324. req.Header.Set("Content-Type", contentType)
  325. req.Header.Set("Authorization", checkToken)
  326. resp, e := client.Do(req)
  327. if e != nil {
  328. err = fmt.Errorf("http client do err: %s", e.Error())
  329. return
  330. }
  331. defer func() {
  332. _ = resp.Body.Close()
  333. }()
  334. b, e := ioutil.ReadAll(resp.Body)
  335. if e != nil {
  336. err = fmt.Errorf("resp body read err: %s", e.Error())
  337. return
  338. }
  339. if len(b) == 0 {
  340. err = fmt.Errorf("resp body is empty")
  341. return
  342. }
  343. // 生产环境解密
  344. if utils.RunMode == "release" {
  345. str := string(b)
  346. str = strings.Trim(str, `"`)
  347. b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  348. }
  349. result := new(models.SmmLatestDataResponse)
  350. if e = json.Unmarshal(b, &result); e != nil {
  351. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  352. return
  353. }
  354. if result.Code != 200 {
  355. err = fmt.Errorf("result: %s", string(b))
  356. return
  357. }
  358. smmDataList := make([]*models.BaseFromSmmData, 0)
  359. for _, v := range result.Data.Data.DataList {
  360. item := new(models.BaseFromSmmData)
  361. item.BaseFromSmmIndexId = edbInfoId
  362. item.IndexCode = v.QuotaID
  363. item.DataTime = v.Date
  364. item.Value = v.Value
  365. item.CreateTime = time.Now()
  366. item.ModifyTime = time.Now()
  367. timeDate, _ := time.Parse(utils.FormatDateTime, v.Date)
  368. timestamp := timeDate.UnixNano() / 1e6
  369. item.DataTimestamp = timestamp
  370. smmDataList = append(smmDataList, item)
  371. }
  372. err = models.RefreshEdbDataFromSmmToEdb(edbInfoId, edbCode, startDate, smmDataList)
  373. return
  374. }