base_from_smm.go 7.8 KB

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