base_from_yongyi.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package services
  2. import (
  3. "eta/eta_index_lib/logic"
  4. "eta/eta_index_lib/models"
  5. "eta/eta_index_lib/services/alarm_msg"
  6. "eta/eta_index_lib/utils"
  7. "fmt"
  8. "strings"
  9. "time"
  10. )
  11. // HandleYongyiIndex 处理涌益咨询的excel数据
  12. func HandleYongyiIndex(req *models.HandleYongyiExcelDataReq) (err error) {
  13. errMsgList := make([]string, 0)
  14. defer func() {
  15. if len(errMsgList) > 0 {
  16. msg := fmt.Sprint("数据源-涌益咨询数据处理失败,err:", strings.Join(errMsgList, "\n"))
  17. utils.FileLog.Info(msg)
  18. go alarm_msg.SendAlarmMsg(msg, 3)
  19. }
  20. }()
  21. for _, v := range req.List {
  22. if v.IndexName == "" || v.IndexCode == "" {
  23. errMsgList = append(errMsgList, fmt.Sprintf("新增指标异常,指标编码%s或者指标ID%s为空:", v.IndexCode, v.IndexName))
  24. continue
  25. }
  26. err = handleYongyiIndex(v, req.TerminalCode)
  27. if err != nil {
  28. errMsgList = append(errMsgList, fmt.Sprintf("新增指标异常,指标编码:%s, Err: %s", v.IndexCode, err))
  29. return
  30. }
  31. }
  32. return
  33. }
  34. func handleYongyiIndex(req *models.HandleYongyiExcelData, terminalCode string) (err error) {
  35. indexName := req.IndexName
  36. indexCode := req.IndexCode
  37. excelDataMap := req.ExcelDataMap
  38. errMsgList := make([]string, 0)
  39. defer func() {
  40. if len(errMsgList) > 0 {
  41. msg := fmt.Sprint("数据源-涌益咨询数据处理失败,err:", strings.Join(errMsgList, "\n"))
  42. utils.FileLog.Info(msg)
  43. go alarm_msg.SendAlarmMsg(msg, 3)
  44. }
  45. }()
  46. indexObj := new(models.BaseFromYongyiIndex)
  47. dataObj := new(models.BaseFromYongyiData)
  48. classifyObj := new(models.BaseFromYongyiClassify)
  49. var indexId int64
  50. addDataList := make([]*models.BaseFromYongyiData, 0)
  51. exitDataMap := make(map[string]*models.BaseFromYongyiData)
  52. // 修改指标信息
  53. if indexName == "" {
  54. utils.FileLog.Info("未刷新到指标数据:indexName:" + indexName)
  55. return
  56. }
  57. // 判断目录是否存在
  58. var classifyId int64
  59. now := time.Now()
  60. if req.ClassifyName != "" {
  61. classifyObj, err = classifyObj.GetByClassifyName(req.ClassifyName)
  62. if err != nil {
  63. if err.Error() == utils.ErrNoRow() {
  64. //新增分类
  65. classifyObj = &models.BaseFromYongyiClassify{
  66. ClassifyName: req.ClassifyName,
  67. ParentId: 0,
  68. SysUserId: 0,
  69. SysUserRealName: "",
  70. Level: 1,
  71. Sort: req.ClassifySort,
  72. ModifyTime: now,
  73. CreateTime: now,
  74. }
  75. classifyId, err = classifyObj.Add()
  76. if err != nil {
  77. err = fmt.Errorf("新增分类失败 Err:%s", err)
  78. return
  79. }
  80. classifyObj.ClassifyId = classifyId
  81. } else {
  82. return
  83. }
  84. } else {
  85. classifyId = classifyObj.ClassifyId
  86. }
  87. }
  88. //判断指标是否存在
  89. var isAdd int
  90. item, err := indexObj.GetByIndexCode(indexCode)
  91. if err != nil {
  92. if err.Error() == utils.ErrNoRow() {
  93. isAdd = 1
  94. err = nil
  95. } else {
  96. isAdd = -1
  97. err = fmt.Errorf("查询数据源指标库失败 GetByIndexCode Err:%s", err)
  98. return
  99. }
  100. }
  101. if item != nil && item.YongyiIndexId > 0 {
  102. fmt.Println("item:", item)
  103. isAdd = 2
  104. } else {
  105. isAdd = 1
  106. }
  107. if isAdd == 1 {
  108. indexObj.IndexCode = indexCode
  109. indexObj.IndexName = indexName
  110. indexObj.Frequency = req.Frequency
  111. indexObj.ClassifyId = classifyId
  112. indexObj.Unit = req.Unit
  113. indexObj.Sort = req.Sort
  114. indexObj.ModifyTime = time.Now()
  115. indexObj.CreateTime = time.Now()
  116. indexObj.TerminalCode = terminalCode
  117. indexId, err = indexObj.Add()
  118. if err != nil {
  119. err = fmt.Errorf("数据源新增涌溢指标失败 Err:%s", err)
  120. return
  121. }
  122. indexObj.YongyiIndexId = indexId
  123. } else if isAdd == 2 {
  124. if item.TerminalCode == `` && terminalCode != `` {
  125. item.TerminalCode = terminalCode
  126. err = item.Update([]string{"TerminalCode"})
  127. if err != nil {
  128. err = fmt.Errorf("数据源更新涌溢指标失败 Err:%s", err)
  129. return
  130. }
  131. }
  132. indexObj.YongyiIndexId = item.YongyiIndexId
  133. indexObj.IndexName = indexName
  134. indexObj.Frequency = req.Frequency
  135. indexObj.ClassifyId = classifyId
  136. indexObj.Unit = req.Unit
  137. indexObj.Sort = req.Sort
  138. indexObj.ModifyTime = time.Now()
  139. //修改数据
  140. updateColsArr := make([]string, 0)
  141. updateColsArr = append(updateColsArr, "index_name")
  142. updateColsArr = append(updateColsArr, "classify_id")
  143. updateColsArr = append(updateColsArr, "unit")
  144. updateColsArr = append(updateColsArr, "frequency")
  145. updateColsArr = append(updateColsArr, "sort")
  146. updateColsArr = append(updateColsArr, "modify_time")
  147. e := indexObj.Update(updateColsArr)
  148. if e != nil {
  149. fmt.Println("Index Update Err:" + e.Error())
  150. return
  151. }
  152. }
  153. //获取已存在的所有数据
  154. var exitDataList []*models.BaseFromYongyiData
  155. exitDataList, err = dataObj.GetByIndexCode(indexCode)
  156. if err != nil {
  157. err = fmt.Errorf("数据源查询涌溢指标数据失败 Err:%s", err)
  158. return
  159. }
  160. fmt.Println("exitDataListLen:", len(exitDataList))
  161. for _, v := range exitDataList {
  162. dateStr := v.DataTime
  163. exitDataMap[dateStr] = v
  164. }
  165. indexId = item.YongyiIndexId
  166. // 遍历excel数据,然后跟现有的数据做校验,不存在则入库
  167. for date, value := range excelDataMap {
  168. if findData, ok := exitDataMap[date]; !ok {
  169. _, err = time.ParseInLocation(utils.FormatDate, date, time.Local)
  170. if err != nil {
  171. err = fmt.Errorf("%s 转换日期格式失败 Err:%s", date, err)
  172. return
  173. }
  174. if !strings.Contains(value, "#N/A") {
  175. var saveDataTime time.Time
  176. if strings.Contains(date, "00:00:00") {
  177. saveDataTime, err = time.Parse(utils.FormatDateTime, date)
  178. } else {
  179. saveDataTime, err = time.Parse(utils.FormatDate, date)
  180. }
  181. if err != nil {
  182. err = fmt.Errorf("%s 转换日期格式失败 Err:%s", date, err)
  183. continue
  184. }
  185. timestamp := saveDataTime.UnixNano() / 1e6
  186. dataItem := new(models.BaseFromYongyiData)
  187. dataItem.YongyiIndexId = int(indexId)
  188. dataItem.IndexCode = indexCode
  189. dataItem.DataTime = date
  190. dataItem.Value = value
  191. dataItem.CreateTime = time.Now()
  192. dataItem.ModifyTime = time.Now()
  193. dataItem.DataTimestamp = timestamp
  194. addDataList = append(addDataList, dataItem)
  195. }
  196. } else {
  197. if findData != nil && findData.Value != value && !strings.Contains(value, "#N/A") { //修改数据
  198. dataObj.YongyiDataId = findData.YongyiDataId
  199. dataObj.Value = value
  200. dataObj.ModifyTime = time.Now()
  201. updateDataColsArr := make([]string, 0)
  202. updateDataColsArr = append(updateDataColsArr, "value")
  203. updateDataColsArr = append(updateDataColsArr, "modify_time")
  204. dataObj.Update(updateDataColsArr)
  205. }
  206. }
  207. }
  208. if len(addDataList) > 0 {
  209. err = dataObj.AddMulti(addDataList)
  210. if err != nil {
  211. err = fmt.Errorf("批量新增指标失败 Err:%s", err)
  212. return
  213. }
  214. }
  215. var dateItem *models.EdbInfoMaxAndMinInfo
  216. dateItem, err = dataObj.GetMaxAndMinDateByIndexCode(indexCode)
  217. if err != nil {
  218. err = fmt.Errorf("查询指标最新日期失败 Err:%s", err)
  219. return
  220. }
  221. go func() {
  222. indexObj.ModifyIndexMaxAndMinDate(indexCode, dateItem)
  223. }()
  224. // 同步刷新ETA指标库的指标
  225. {
  226. // 获取指标详情
  227. baseObj := new(models.BaseFromYongyi)
  228. var edbInfo *models.EdbInfo
  229. edbInfo, err = models.GetEdbInfoByEdbCode(baseObj.GetSource(), indexCode)
  230. if err != nil {
  231. if err.Error() != utils.ErrNoRow() {
  232. errMsgList = append(errMsgList, fmt.Sprint("刷新ETA指标异常,指标编码:", indexCode, err.Error()))
  233. return
  234. } else {
  235. err = nil
  236. }
  237. }
  238. // 已经加入到指标库的话,那么就去更新ETA指标库吧
  239. if edbInfo != nil {
  240. go logic.RefreshBaseEdbInfo(edbInfo, ``)
  241. }
  242. }
  243. return
  244. }