base_from_sci_hq.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/logic"
  5. "eta/eta_index_lib/models"
  6. "eta/eta_index_lib/utils"
  7. "fmt"
  8. "strconv"
  9. "time"
  10. )
  11. type SciHqController struct {
  12. BaseAuthController
  13. }
  14. // Add
  15. // @Title 新增卓创红期指标接口
  16. // @Description 新增卓创红期指标接口
  17. // @Success 200 {object} models.AddEdbInfoReq
  18. // @router /add [post]
  19. func (this *SciHqController) Add() {
  20. br := new(models.BaseResponse).Init()
  21. var cacheKey string
  22. defer func() {
  23. utils.Rc.Delete(cacheKey)
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. var req models.AddEdbInfoReq
  28. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  29. if err != nil {
  30. br.Msg = "参数解析异常!"
  31. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  32. return
  33. }
  34. if req.EdbCode == "" {
  35. br.Msg = "请输入指标编码!"
  36. br.ErrMsg = "请输入指标编码,指标编码为空"
  37. return
  38. }
  39. cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(req.Source) + "_" + req.EdbCode
  40. if !utils.Rc.IsExist(cacheKey) {
  41. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  42. err = models.AddEdbDataFromSciHq(req.EdbCode)
  43. if err != nil {
  44. br.Msg = "获取指标信息失败!"
  45. br.ErrMsg = "获取指标信息失败 AddEdbDataFromSci,Err:" + err.Error()
  46. return
  47. }
  48. br.Ret = 200
  49. br.Success = true
  50. br.Msg = "获取成功"
  51. } else {
  52. br.Ret = 501
  53. br.Success = true
  54. br.Msg = "系统处理中,请稍后重试"
  55. }
  56. }
  57. // Refresh
  58. // @Title 刷新卓创红期指标接口
  59. // @Description 刷新卓创红期指标接口
  60. // @Success 200 {object} models.RefreshEdbInfoReq
  61. // @router /refresh [post]
  62. func (this *SciHqController) Refresh() {
  63. br := new(models.BaseResponse).Init()
  64. defer func() {
  65. this.Data["json"] = br
  66. this.ServeJSON()
  67. }()
  68. var req models.RefreshEdbInfoReq
  69. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  70. if err != nil {
  71. br.Msg = "参数解析异常!"
  72. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  73. return
  74. }
  75. if req.EdbCode == "" {
  76. br.Msg = "请输入指标编码!"
  77. br.ErrMsg = "请输入指标编码,指标编码为空"
  78. return
  79. }
  80. if req.EdbInfoId < 0 {
  81. br.Msg = "请输入指标ID!"
  82. br.ErrMsg = "请输入指标ID"
  83. return
  84. }
  85. // 获取指标详情
  86. edbInfo, err := models.GetEdbInfoByEdbCode(req.Source, req.EdbCode)
  87. if err != nil {
  88. br.Msg = "指标不存在!"
  89. br.ErrMsg = "指标不存在"
  90. return
  91. }
  92. isHandling, errMsg, err := logic.RefreshBaseEdbInfo(edbInfo, req.StartDate)
  93. if isHandling {
  94. br.Ret = 501
  95. br.Success = true
  96. br.Msg = "系统处理中,请稍后重试"
  97. return
  98. }
  99. if err != nil {
  100. br.Msg = errMsg
  101. br.ErrMsg = err.Error()
  102. return
  103. }
  104. br.Ret = 200
  105. br.Success = true
  106. br.Msg = "获取成功"
  107. }
  108. // HandleExcelData
  109. // @Title 处理卓创红期指标的接口
  110. // @Description 处理卓创红期指标的接口
  111. // @Success 200 {object} models.HandleMysteelIndexResp
  112. // @router /handle/excel_data [post]
  113. func (this *SciHqController) HandleExcelData() {
  114. br := new(models.BaseResponse).Init()
  115. defer func() {
  116. this.Data["json"] = br
  117. this.ServeJSON()
  118. }()
  119. var req []*models.HandleSciHqExcelDataReq
  120. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  121. if err != nil {
  122. br.Msg = "参数解析异常!"
  123. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  124. return
  125. }
  126. indexList, err := models.GetAllBaseFromSciHqIndex()
  127. if err != nil {
  128. br.Msg = "获取指标列表失败!"
  129. br.ErrMsg = "获取指标列表失败,Err:" + err.Error()
  130. return
  131. }
  132. allIndexMap := make(map[string]*models.BaseFromSciHqIndex)
  133. for _, v := range indexList {
  134. allIndexMap[v.IndexCode] = v
  135. }
  136. addSciHqIndexList := make([]*models.BaseFromSciHqIndex, 0)
  137. for _, v := range req {
  138. t := new(models.BaseFromSciHqIndex)
  139. if _, ok := allIndexMap[v.ExcelIndexCode]; !ok {
  140. t.ClassifyId = 0
  141. t.IndexName = v.IndexName
  142. t.IndexCode = v.ExcelIndexCode
  143. t.Frequency = v.Frequency
  144. t.Unit = v.Unit
  145. t.TerminalCode = v.TerminalCode
  146. t.FilePath = v.FilePath
  147. t.CreateTime = time.Now()
  148. t.ModifyTime = time.Now()
  149. addSciHqIndexList = append(addSciHqIndexList, t)
  150. }
  151. }
  152. if len(addSciHqIndexList) > 0 {
  153. err = models.BatchAddBaseFromSciHqIndex(addSciHqIndexList)
  154. if err != nil {
  155. br.Msg = "添加指标失败!"
  156. br.ErrMsg = "添加指标失败,Err:" + err.Error()
  157. return
  158. }
  159. }
  160. indexList, err = models.GetAllBaseFromSciHqIndex()
  161. if err != nil {
  162. br.Msg = "获取指标列表失败!"
  163. br.ErrMsg = "获取指标列表失败,Err:" + err.Error()
  164. return
  165. }
  166. allIndexMap = make(map[string]*models.BaseFromSciHqIndex)
  167. for _, v := range indexList {
  168. allIndexMap[v.IndexCode] = v
  169. }
  170. for _, v := range req {
  171. if indexInfo, ok := allIndexMap[v.ExcelIndexCode]; !ok {
  172. utils.FileLog.Info("指标不存在,indexCode:" + v.ExcelIndexCode)
  173. continue
  174. } else {
  175. indexDataList, err := models.GetBaseFromSciHqDataByIndexCode(indexInfo.IndexCode)
  176. if err != nil {
  177. br.Msg = "获取指标数据失败!"
  178. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  179. return
  180. }
  181. indexDataExistMap := make(map[string]*models.BaseFromSciHqData)
  182. for _, indexData := range indexDataList {
  183. indexDataExistMap[indexData.DataTime] = indexData
  184. }
  185. addSciHqDataList := make([]*models.BaseFromSciHqData, 0)
  186. for dataDate, dataVal := range v.Data {
  187. currDataTime, err := time.ParseInLocation(utils.FormatDate, dataDate, time.Local)
  188. if err != nil {
  189. utils.FileLog.Info("时间格式化失败,indexCode:" + v.ExcelIndexCode + "Err:" + err.Error())
  190. continue
  191. }
  192. timestamp := currDataTime.UnixNano() / 1e6
  193. sciHqData, ok := indexDataExistMap[dataDate]
  194. if !ok {
  195. tmpBaseFromSciData := &models.BaseFromSciHqData{
  196. BaseFromSciHqIndexId: indexInfo.BaseFromSciHqIndexId,
  197. IndexCode: indexInfo.IndexCode,
  198. DataTime: dataDate,
  199. Value: dataVal,
  200. CreateTime: time.Now(),
  201. ModifyTime: time.Now(),
  202. DataTimestamp: timestamp,
  203. }
  204. addSciHqDataList = append(addSciHqDataList, tmpBaseFromSciData)
  205. } else {
  206. existValue := sciHqData.Value
  207. if dataVal != "" && existValue != dataVal {
  208. sciHqData.Value = dataVal
  209. sciHqData.ModifyTime = time.Now()
  210. if sciHqData.SciHqDataId > 0 {
  211. err = sciHqData.Update([]string{"value", "modify_time"})
  212. if err != nil {
  213. utils.FileLog.Info("更新指标数据失败,indexCode:" + v.ExcelIndexCode + "Err:" + err.Error())
  214. continue
  215. }
  216. }
  217. }
  218. }
  219. }
  220. if len(addSciHqDataList) > 0 {
  221. err = models.BatchAddBaseFromSciHqData(addSciHqDataList)
  222. if err != nil {
  223. utils.FileLog.Info("添加指标数据失败,indexCode:" + v.ExcelIndexCode + "Err:" + err.Error())
  224. continue
  225. }
  226. }
  227. //修改最大最小日期
  228. sciHqIndexMaxItem, err := models.GetSciHqIndexInfoMaxAndMinInfo(indexInfo.IndexCode)
  229. if err == nil && sciHqIndexMaxItem != nil {
  230. e := models.ModifySciHqIndexMaxAndMinInfo(indexInfo.IndexCode, sciHqIndexMaxItem)
  231. if e != nil {
  232. fmt.Println("ModifySciHqIndexMaxAndMinInfo Err:" + e.Error())
  233. }
  234. }
  235. // 同步刷新ETA图库卓创红期的指标
  236. {
  237. // 获取指标详情
  238. edbInfo, err := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_SCI_HQ, indexInfo.IndexCode)
  239. if err != nil && err.Error() != utils.ErrNoRow() {
  240. utils.FileLog.Info("刷新指标异常,indexCode:" + v.ExcelIndexCode + "Err:" + err.Error())
  241. continue
  242. }
  243. // 已经加入到指标库的话,那么就去更新ETA指标库吧
  244. if edbInfo != nil {
  245. go logic.RefreshBaseEdbInfo(edbInfo, ``)
  246. }
  247. }
  248. }
  249. }
  250. br.Msg = "处理成功"
  251. br.Success = true
  252. br.Ret = 200
  253. }