base_from_smm.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. if item.IndexName != indexName {
  107. baseFromSmmRecord := new(models.BaseFromSmmRecord)
  108. baseFromSmmRecord.BaseFromSmmIndexId = item.BaseFromSmmIndexId
  109. baseFromSmmRecord.OldIndexName = item.IndexName
  110. baseFromSmmRecord.NewIndexName = indexName
  111. ctime := time.Now()
  112. baseFromSmmRecord.CreateTime = ctime
  113. baseFromSmmRecord.Timestamp = ctime.Unix()
  114. e = models.AddBaseFromSmmRecord(baseFromSmmRecord)
  115. if e != nil {
  116. err = fmt.Errorf("add smm record err: %s", e.Error())
  117. return
  118. }
  119. }
  120. }
  121. // 遍历excel数据,然后跟现有的数据做校验,不存在则入库
  122. for date, value := range excelDataMap {
  123. dateTime, e := time.ParseInLocation(utils.FormatDate, date, time.Local)
  124. if e != nil {
  125. messages = append(messages, fmt.Sprintf("data time parse err: %s, date: %s", e.Error(), date))
  126. continue
  127. }
  128. if _, ok := exitDataMap[date]; !ok {
  129. if !strings.Contains(value, "#N/A") {
  130. var saveDataTime time.Time
  131. if strings.Contains(date, "00:00:00") {
  132. saveDataTime, e = time.Parse(utils.FormatDateTime, date)
  133. if e != nil {
  134. messages = append(messages, fmt.Sprintf("save data time parse err: %s, date: %s", e.Error(), date))
  135. continue
  136. }
  137. } else {
  138. saveDataTime, e = time.Parse(utils.FormatDate, date)
  139. if e != nil {
  140. messages = append(messages, fmt.Sprintf("save data time parse err: %s, date: %s", e.Error(), date))
  141. continue
  142. }
  143. }
  144. if saveDataTime.IsZero() {
  145. messages = append(messages, fmt.Sprintf("save data time empty"))
  146. continue
  147. }
  148. timestamp := saveDataTime.UnixNano() / 1e6
  149. dataItem := new(models.BaseFromSmmData)
  150. dataItem.BaseFromSmmIndexId = int(indexId)
  151. dataItem.IndexCode = indexCode
  152. dataItem.DataTime = dateTime.Format(utils.FormatDate)
  153. dataItem.Value = value
  154. dataItem.CreateTime = time.Now()
  155. dataItem.ModifyTime = time.Now()
  156. dataItem.DataTimestamp = timestamp
  157. addDataList = append(addDataList, dataItem)
  158. }
  159. } else {
  160. // 更新对应日期数据
  161. if value == "" || strings.Contains(value, "#N/A") {
  162. continue
  163. }
  164. // 若数值相同则忽略更新
  165. existData := exitDataMap[date]
  166. if existData != nil && existData.Value == value {
  167. continue
  168. }
  169. dataItem := new(models.BaseFromSmmData)
  170. dataItem.IndexCode = indexCode
  171. dataItem.DataTime = dateTime.Format(utils.FormatDate)
  172. dataItem.Value = value
  173. dataItem.ModifyTime = time.Now()
  174. updateDataList = append(updateDataList, dataItem)
  175. }
  176. }
  177. //fmt.Println("addDataList len: ", len(addDataList))
  178. if len(addDataList) > 0 {
  179. e = models.AddBaseFromSmmData(addDataList)
  180. if e != nil {
  181. err = fmt.Errorf("AddBaseFromSmmData err: %s", e.Error())
  182. return
  183. }
  184. }
  185. // 已存在的日期数据更新
  186. //fmt.Println("updateDataList len: ", len(updateDataList))
  187. if len(updateDataList) > 0 {
  188. e = models.MultiUpdateBaseFromSmmDataValue(updateDataList)
  189. if e != nil {
  190. err = fmt.Errorf("MultiUpdateBaseFromSmmDataValue err: %s", e.Error())
  191. return
  192. }
  193. }
  194. itemInfo, err := models.GetSmmIndexInfoMaxAndMinInfo(indexCode)
  195. if err == nil && itemInfo != nil {
  196. e = models.ModifySmmIndexMaxAndMinInfo(indexCode, itemInfo)
  197. if e != nil {
  198. err = fmt.Errorf("ModifySmmIndexMaxAndMinInfo err: %s", e.Error())
  199. return
  200. }
  201. }
  202. // 同步刷新ETA图库有色的指标
  203. {
  204. // 获取指标详情
  205. edbInfo, e := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_YS, indexCode)
  206. if e != nil && e.Error() != utils.ErrNoRow() {
  207. messages = append(messages, fmt.Sprintf("刷新ETA指标异常, indexCode: %s, err: %s", indexCode, e.Error()))
  208. }
  209. // 已经加入到指标库的话,那么就去更新ETA指标库吧
  210. if edbInfo != nil {
  211. go logic.RefreshBaseEdbInfo(edbInfo, ``)
  212. }
  213. }
  214. return
  215. }
  216. func GetEdbDataFromSmm(edbCode string) (smmBaseDataAll []models.BaseFromSmmDataList, err error) {
  217. if utils.SmmDataMethod == "api" {
  218. return GetSmmNewIndexFromBridge(edbCode)
  219. }
  220. return getEdbDataFromSmmData(edbCode)
  221. }
  222. func getEdbDataFromSmmData(edbCode string) (smmBaseDataList []models.BaseFromSmmDataList, err error) {
  223. var condition string
  224. var pars []interface{}
  225. if edbCode != "" {
  226. condition += " AND index_code=? "
  227. pars = append(pars, edbCode)
  228. }
  229. smmBaseDataAll, err := models.GetBaseFromSmmDataByCondition(condition, pars)
  230. if err != nil && err.Error() != utils.ErrNoRow() {
  231. return
  232. }
  233. var smmBaseDataListItem models.BaseFromSmmDataList
  234. smmBaseDataListItem.Data = smmBaseDataAll
  235. smmBaseDataListItem.IndexCode = edbCode
  236. smmBaseDataList = append(smmBaseDataList, smmBaseDataListItem)
  237. return
  238. }