base_from_fenwei.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. // HandleFenweiIndex 处理汾渭数据的excel数据
  12. func HandleFenweiIndex(req *models.HandleFenweiExcelDataReq) (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 = handleFenweiIndex(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 handleFenweiIndex(req *models.HandleFenweiExcelData, 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.BaseFromFenweiIndex)
  47. dataObj := new(models.BaseFromFenweiData)
  48. //classifyObj := new(models.BaseFromFenweiClassify)
  49. var indexId int64
  50. addDataList := make([]*models.BaseFromFenweiData, 0)
  51. updateDataList := make([]*models.BaseFromFenweiData, 0)
  52. exitDataMap := make(map[string]*models.BaseFromFenweiData)
  53. // 修改指标信息
  54. if indexName == "" {
  55. utils.FileLog.Info("未刷新到指标数据:indexName:" + indexName)
  56. return
  57. }
  58. // 判断目录是否存在
  59. //var classifyId int64
  60. //now := time.Now()
  61. //if req.ClassifyName != "" {
  62. // classifyObj, err = classifyObj.GetByClassifyName(req.ClassifyName)
  63. // if err != nil {
  64. // if err.Error() == utils.ErrNoRow() {
  65. // //新增分类
  66. // classifyObj = &models.BaseFromFenweiClassify{
  67. // ClassifyName: req.ClassifyName,
  68. // ParentId: 0,
  69. // SysUserId: 0,
  70. // SysUserRealName: "",
  71. // Level: 1,
  72. // Sort: req.ClassifySort,
  73. // ModifyTime: now,
  74. // CreateTime: now,
  75. // }
  76. //
  77. // classifyId, err = classifyObj.Add()
  78. // if err != nil {
  79. // err = fmt.Errorf("新增分类失败 Err:%s", err)
  80. // return
  81. // }
  82. // classifyObj.ClassifyId = classifyId
  83. // } else {
  84. // return
  85. // }
  86. // } else {
  87. // classifyId = classifyObj.ClassifyId
  88. // }
  89. //}
  90. //判断指标是否存在
  91. var isAdd int
  92. item, err := indexObj.GetByIndexCode(indexCode)
  93. if err != nil {
  94. if err.Error() == utils.ErrNoRow() {
  95. isAdd = 1
  96. err = nil
  97. } else {
  98. isAdd = -1
  99. err = fmt.Errorf("查询数据源指标库失败 GetByIndexCode Err:%s", err)
  100. return
  101. }
  102. }
  103. if item != nil && item.FenweiIndexId > 0 {
  104. fmt.Println("item:", item)
  105. isAdd = 2
  106. } else {
  107. isAdd = 1
  108. }
  109. if isAdd == 1 {
  110. indexObj.IndexCode = indexCode
  111. indexObj.IndexName = indexName
  112. indexObj.Frequency = req.Frequency
  113. indexObj.ClassifyId = req.ClassifyId
  114. indexObj.Unit = req.Unit
  115. indexObj.Sort = req.Sort
  116. indexObj.ModifyTime = time.Now()
  117. indexObj.CreateTime = time.Now()
  118. indexObj.TerminalCode = terminalCode
  119. indexId, err = indexObj.Add()
  120. if err != nil {
  121. err = fmt.Errorf("数据源新增涌溢指标失败 Err:%s", err)
  122. return
  123. }
  124. indexObj.FenweiIndexId = indexId
  125. } else if isAdd == 2 {
  126. if item.TerminalCode == `` && terminalCode != `` {
  127. item.TerminalCode = terminalCode
  128. err = item.Update([]string{"TerminalCode"})
  129. if err != nil {
  130. err = fmt.Errorf("数据源更新涌溢指标失败 Err:%s", err)
  131. return
  132. }
  133. }
  134. //获取已存在的所有数据
  135. var exitDataList []*models.BaseFromFenweiData
  136. exitDataList, err = dataObj.GetByIndexCode(indexCode)
  137. if err != nil {
  138. err = fmt.Errorf("数据源查询涌溢指标数据失败 Err:%s", err)
  139. return
  140. }
  141. fmt.Println("exitDataListLen:", len(exitDataList))
  142. for _, v := range exitDataList {
  143. dateStr := v.DataTime
  144. exitDataMap[dateStr] = v
  145. }
  146. indexId = item.FenweiIndexId
  147. }
  148. // 遍历excel数据,然后跟现有的数据做校验,不存在则入库
  149. for date, value := range excelDataMap {
  150. if findData, ok := exitDataMap[date]; !ok {
  151. _, err = time.ParseInLocation(utils.FormatDate, date, time.Local)
  152. if err != nil {
  153. err = fmt.Errorf("%s 转换日期格式失败 Err:%s", date, err)
  154. return
  155. }
  156. if !strings.Contains(value, "#N/A") {
  157. var saveDataTime time.Time
  158. if strings.Contains(date, "00:00:00") {
  159. saveDataTime, err = time.Parse(utils.FormatDateTime, date)
  160. } else {
  161. saveDataTime, err = time.Parse(utils.FormatDate, date)
  162. }
  163. if err != nil {
  164. err = fmt.Errorf("%s 转换日期格式失败 Err:%s", date, err)
  165. continue
  166. }
  167. timestamp := saveDataTime.UnixNano() / 1e6
  168. dataItem := new(models.BaseFromFenweiData)
  169. dataItem.FenweiIndexId = int(indexId)
  170. dataItem.IndexCode = indexCode
  171. dataItem.DataTime = date
  172. dataItem.Value = value
  173. dataItem.CreateTime = time.Now()
  174. dataItem.ModifyTime = time.Now()
  175. dataItem.DataTimestamp = timestamp
  176. addDataList = append(addDataList, dataItem)
  177. }
  178. } else {
  179. if findData != nil && findData.Value != value && !strings.Contains(value, "#N/A") { //修改数据
  180. findData.Value = value
  181. findData.ModifyTime = time.Now().Local()
  182. updateDataList = append(updateDataList, findData)
  183. }
  184. }
  185. }
  186. if len(addDataList) > 0 {
  187. err = dataObj.AddMulti(addDataList)
  188. if err != nil {
  189. err = fmt.Errorf("批量新增指标失败 Err:%s", err)
  190. return
  191. }
  192. var dateItem *models.EdbInfoMaxAndMinInfo
  193. dateItem, err = dataObj.GetMaxAndMinDateByIndexCode(indexCode)
  194. if err != nil {
  195. err = fmt.Errorf("查询指标最新日期失败 Err:%s", err)
  196. return
  197. }
  198. go func() {
  199. indexObj.ModifyIndexMaxAndMinDate(indexCode, dateItem)
  200. }()
  201. }
  202. // 批量更新数据
  203. if len(updateDataList) > 0 {
  204. e := models.MultiUpdateBaseFromFenweiDataValue(updateDataList)
  205. if e != nil {
  206. err = fmt.Errorf("MultiUpdateBaseFromSmmDataValue err: %s", e.Error())
  207. return
  208. }
  209. }
  210. // 同步刷新ETA指标库的指标
  211. {
  212. // 获取指标详情
  213. baseObj := new(models.BaseFromFenwei)
  214. var edbInfo *models.EdbInfo
  215. edbInfo, err = models.GetEdbInfoByEdbCode(baseObj.GetSource(), indexCode)
  216. if err != nil {
  217. if err.Error() != utils.ErrNoRow() {
  218. errMsgList = append(errMsgList, fmt.Sprint("刷新ETA指标异常,指标编码:", indexCode, err.Error()))
  219. return
  220. } else {
  221. err = nil
  222. }
  223. }
  224. // 已经加入到指标库的话,那么就去更新ETA指标库吧
  225. if edbInfo != nil {
  226. go logic.RefreshBaseEdbInfo(edbInfo, ``)
  227. }
  228. }
  229. return
  230. }