base_from_smm.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. func SmmIndexHandle(baseFilePath, renameFilePath, indexName, indexCode, unit, frequency, source string, excelDataMap map[string]string, terminalCode string) (err error, errMsg string) {
  12. messages := make([]string, 0)
  13. defer func() {
  14. if len(messages) > 0 {
  15. errMsg = strings.Join(messages, "\n")
  16. utils.FileLog.Info("SmmIndexHandle ErrMsg: %s", errMsg)
  17. }
  18. if err != nil {
  19. tips := fmt.Sprintf("SMM有色excel数据处理失败, Err: %s, ErrMsg: %s", err.Error(), errMsg)
  20. utils.FileLog.Info(tips)
  21. go alarm_msg.SendAlarmMsg(tips, 3)
  22. }
  23. }()
  24. indexObj := new(models.BaseFromSmmIndex)
  25. var indexId int64
  26. addDataList := make([]*models.BaseFromSmmData, 0)
  27. updateDataList := make([]*models.BaseFromSmmData, 0)
  28. exitDataMap := make(map[string]*models.BaseFromSmmData)
  29. // 修改指标信息
  30. if indexName == "" {
  31. utils.FileLog.Info("未刷新到指标数据:indexName:" + indexName)
  32. return
  33. }
  34. // 判断指标是否存在
  35. var isAdd int
  36. item, e := indexObj.GetSmmIndexItem(indexCode)
  37. if e != nil {
  38. if e.Error() == utils.ErrNoRow() {
  39. isAdd = 1
  40. } else {
  41. isAdd = -1
  42. err = fmt.Errorf("GetSmmIndexItem Err: %s", e.Error())
  43. return
  44. }
  45. }
  46. if item != nil && item.BaseFromSmmIndexId > 0 {
  47. isAdd = 2
  48. } else {
  49. isAdd = 1
  50. }
  51. if !strings.Contains(frequency, "度") {
  52. frequency = frequency + "度"
  53. }
  54. if isAdd == 1 {
  55. indexObj.IndexCode = indexCode
  56. indexObj.IndexName = indexName
  57. indexObj.Unit = unit
  58. indexObj.Frequency = frequency
  59. indexObj.Interface = source
  60. indexObj.ModifyTime = time.Now()
  61. indexObj.CreateTime = time.Now()
  62. indexObj.BaseFileName = baseFilePath
  63. indexObj.RenameFileName = renameFilePath
  64. indexObj.TerminalCode = terminalCode
  65. lastId, e := indexObj.AddBaseFromSmmIndex()
  66. if e != nil {
  67. err = fmt.Errorf("AddBaseFromSmmIndex err: %s", e.Error())
  68. return
  69. }
  70. indexId = lastId
  71. go models.ModifySmmIndexSort(indexId)
  72. } else if isAdd == 2 {
  73. //获取已存在的所有数据
  74. exitDataList, e := models.GetBaseFromSmmDataBySmmCode(indexCode)
  75. if e != nil {
  76. err = fmt.Errorf("GetBaseFromSmmDataBySmmCode err: %s", e.Error())
  77. return
  78. }
  79. fmt.Println("exitDataListLen:", len(exitDataList))
  80. for _, v := range exitDataList {
  81. //dateStr := v.DataTime.Format(utils.FormatDate)
  82. exitDataMap[v.DataTime] = v
  83. }
  84. indexId = item.BaseFromSmmIndexId
  85. //修改指标信息
  86. updateParams := make(map[string]interface{})
  87. updateParams["index_name"] = indexName
  88. updateParams["frequency"] = frequency
  89. updateParams["unit"] = unit
  90. updateParams["interface"] = source
  91. updateParams["modify_time"] = time.Now()
  92. if baseFilePath != `` && item.BaseFileName != baseFilePath {
  93. updateParams["base_file_name"] = baseFilePath
  94. }
  95. if renameFilePath != `` && item.RenameFileName != renameFilePath {
  96. updateParams["rename_file_name"] = renameFilePath
  97. }
  98. whereParam := make(map[string]interface{})
  99. whereParam["index_code"] = indexCode
  100. smmIndexObj := new(models.BaseFromSmmIndex)
  101. e = smmIndexObj.Update(updateParams, whereParam)
  102. if e != nil {
  103. err = fmt.Errorf("smm index update err: %s", e.Error())
  104. return
  105. }
  106. }
  107. // 遍历excel数据,然后跟现有的数据做校验,不存在则入库
  108. for date, value := range excelDataMap {
  109. dateTime, e := time.ParseInLocation(utils.FormatDate, date, time.Local)
  110. if e != nil {
  111. messages = append(messages, fmt.Sprintf("data time parse err: %s, date: %s", e.Error(), date))
  112. continue
  113. }
  114. if _, ok := exitDataMap[date]; !ok {
  115. if !strings.Contains(value, "#N/A") {
  116. var saveDataTime time.Time
  117. if strings.Contains(date, "00:00:00") {
  118. saveDataTime, e = time.Parse(utils.FormatDateTime, date)
  119. if e != nil {
  120. messages = append(messages, fmt.Sprintf("save data time parse err: %s, date: %s", e.Error(), date))
  121. continue
  122. }
  123. } else {
  124. saveDataTime, e = time.Parse(utils.FormatDate, date)
  125. if e != nil {
  126. messages = append(messages, fmt.Sprintf("save data time parse err: %s, date: %s", e.Error(), date))
  127. continue
  128. }
  129. }
  130. if saveDataTime.IsZero() {
  131. messages = append(messages, fmt.Sprintf("save data time empty"))
  132. continue
  133. }
  134. timestamp := saveDataTime.UnixNano() / 1e6
  135. dataItem := new(models.BaseFromSmmData)
  136. dataItem.BaseFromSmmIndexId = int(indexId)
  137. dataItem.IndexCode = indexCode
  138. dataItem.DataTime = dateTime.Format(utils.FormatDate)
  139. dataItem.Value = value
  140. dataItem.CreateTime = time.Now()
  141. dataItem.ModifyTime = time.Now()
  142. dataItem.DataTimestamp = timestamp
  143. addDataList = append(addDataList, dataItem)
  144. }
  145. } else {
  146. // 更新对应日期数据
  147. if value == "" || strings.Contains(value, "#N/A") {
  148. continue
  149. }
  150. // 若数值相同则忽略更新
  151. existData := exitDataMap[date]
  152. if existData != nil && existData.Value == value {
  153. continue
  154. }
  155. dataItem := new(models.BaseFromSmmData)
  156. dataItem.IndexCode = indexCode
  157. dataItem.DataTime = dateTime.Format(utils.FormatDate)
  158. dataItem.Value = value
  159. dataItem.ModifyTime = time.Now()
  160. updateDataList = append(updateDataList, dataItem)
  161. }
  162. }
  163. //fmt.Println("addDataList len: ", len(addDataList))
  164. if len(addDataList) > 0 {
  165. e = models.AddBaseFromSmmData(addDataList)
  166. if e != nil {
  167. err = fmt.Errorf("AddBaseFromSmmData err: %s", e.Error())
  168. return
  169. }
  170. }
  171. // 已存在的日期数据更新
  172. //fmt.Println("updateDataList len: ", len(updateDataList))
  173. if len(updateDataList) > 0 {
  174. e = models.MultiUpdateBaseFromSmmDataValue(updateDataList)
  175. if e != nil {
  176. err = fmt.Errorf("MultiUpdateBaseFromSmmDataValue err: %s", e.Error())
  177. return
  178. }
  179. }
  180. itemInfo, err := models.GetSmmIndexInfoMaxAndMinInfo(indexCode)
  181. if err == nil && itemInfo != nil {
  182. e = models.ModifySmmIndexMaxAndMinInfo(indexCode, itemInfo)
  183. if e != nil {
  184. err = fmt.Errorf("ModifySmmIndexMaxAndMinInfo err: %s", e.Error())
  185. return
  186. }
  187. }
  188. // 同步刷新ETA图库有色的指标
  189. {
  190. // 获取指标详情
  191. edbInfo, e := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_YS, indexCode)
  192. if e != nil && e.Error() != utils.ErrNoRow() {
  193. messages = append(messages, fmt.Sprintf("刷新ETA指标异常, indexCode: %s, err: %s", indexCode, e.Error()))
  194. }
  195. // 已经加入到指标库的话,那么就去更新ETA指标库吧
  196. if edbInfo != nil {
  197. go logic.RefreshBaseEdbInfo(edbInfo, ``)
  198. }
  199. }
  200. return
  201. }
  202. func GetEdbDataFromSmm(edbCode string) (smmBaseDataAll []models.BaseFromSmmDataList, err error) {
  203. if utils.BusinessCode == "E2023110300" {
  204. return GetSmmNewIndexFromBridge(edbCode)
  205. }
  206. return getEdbDataFromSmmData(edbCode)
  207. }
  208. func getEdbDataFromSmmData(edbCode string) (smmBaseDataList []models.BaseFromSmmDataList, err error) {
  209. var condition string
  210. var pars []interface{}
  211. if edbCode != "" {
  212. condition += " AND index_code=? "
  213. pars = append(pars, edbCode)
  214. }
  215. smmBaseDataAll, err := models.GetBaseFromSmmDataByCondition(condition, pars)
  216. if err != nil && err.Error() != utils.ErrNoRow() {
  217. return
  218. }
  219. var smmBaseDataListItem models.BaseFromSmmDataList
  220. smmBaseDataListItem.Data = smmBaseDataAll
  221. smmBaseDataListItem.IndexCode = edbCode
  222. smmBaseDataList = append(smmBaseDataList, smmBaseDataListItem)
  223. return
  224. }