base_from_mysteel_chemical.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 HandleMysteelIndex(req *models.HandleMysteelIndexResp) (err error) {
  12. for _, v := range req.List {
  13. if v.IndexName == "" || v.IndexCode == "" {
  14. continue
  15. }
  16. err = handleIndex(v)
  17. if err != nil {
  18. return
  19. }
  20. }
  21. _ = SetMysteelChemicalEdbInfoUpdateStat(false)
  22. _ = SetEdbSourceStat(false)
  23. return
  24. }
  25. func handleIndex(indexItem *models.HandleMysteelIndex) (err error) {
  26. defer func() {
  27. if err != nil {
  28. // 添加刷新失败日志
  29. dataUpdateResult := 2
  30. dataUpdateFailedReason := "服务异常"
  31. edbInfo, e := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, indexItem.IndexCode)
  32. if e == nil {
  33. //查询指标存在,才添加刷新日志
  34. _ = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 2, err.Error(), dataUpdateResult, dataUpdateFailedReason, 1, 0)
  35. }
  36. }
  37. }()
  38. indexObj := new(models.BaseFromMysteelChemicalIndex)
  39. var indexId int64
  40. addDataList := make([]models.BaseFromMysteelChemicalData, 0)
  41. exitDataMap := make(map[string]*models.BaseFromMysteelChemicalData)
  42. //判断指标是否存在
  43. var isAdd int
  44. item, err := indexObj.GetIndexItem(indexItem.IndexCode)
  45. if err != nil {
  46. if err.Error() == utils.ErrNoRow() {
  47. isAdd = 1
  48. } else {
  49. isAdd = -1
  50. return
  51. }
  52. }
  53. nameChange := false
  54. if item != nil && item.BaseFromMysteelChemicalIndexId > 0 {
  55. isAdd = 2
  56. if item.IndexName != indexItem.IndexName {
  57. nameChange = true
  58. }
  59. } else {
  60. isAdd = 1
  61. }
  62. fmt.Println("isAdd:", isAdd)
  63. if !strings.Contains(indexItem.Frequency, "度") {
  64. indexItem.Frequency = indexItem.Frequency + "度"
  65. }
  66. if isAdd == 1 {
  67. indexObj.IndexCode = indexItem.IndexCode
  68. indexObj.IndexName = indexItem.IndexName
  69. indexObj.Unit = indexItem.Unit
  70. indexObj.Source = indexItem.Source
  71. indexObj.Describe = indexItem.Describe
  72. indexObj.StartDate, err = utils.DealExcelDate(indexItem.StartDate)
  73. if err != nil {
  74. fmt.Println("utils.DealExcelDate err:" + indexItem.StartDate + err.Error())
  75. return
  76. }
  77. indexObj.EndDate, err = utils.DealExcelDate(indexItem.EndDate)
  78. if err != nil {
  79. fmt.Println("utils.DealExcelDate err:" + indexItem.EndDate + err.Error())
  80. return
  81. }
  82. indexObj.Frequency = indexItem.Frequency
  83. //indexObj.CreateTime = time.Now().Local()
  84. //indexObj.ModifyTime = time.Now().Local()
  85. err = indexObj.Add()
  86. if err != nil {
  87. fmt.Println("add err:" + err.Error())
  88. return
  89. }
  90. indexId = indexObj.BaseFromMysteelChemicalIndexId
  91. } else if isAdd == 2 {
  92. indexObj.BaseFromMysteelChemicalIndexId = item.BaseFromMysteelChemicalIndexId
  93. indexObj.IndexCode = indexItem.IndexCode
  94. indexObj.IndexName = indexItem.IndexName
  95. indexObj.Unit = indexItem.Unit
  96. indexObj.Source = indexItem.Source
  97. indexObj.Describe = indexItem.Describe
  98. indexObj.StartDate, err = utils.DealExcelDate(indexItem.StartDate)
  99. if err != nil {
  100. fmt.Println("utils.DealExcelDate err:" + indexItem.StartDate + err.Error())
  101. return
  102. }
  103. indexObj.EndDate, err = utils.DealExcelDate(indexItem.EndDate)
  104. if err != nil {
  105. fmt.Println("utils.DealExcelDate err:" + indexItem.EndDate + err.Error())
  106. return
  107. }
  108. indexObj.Frequency = indexItem.Frequency
  109. indexObj.ModifyTime = time.Now()
  110. indexId = item.BaseFromMysteelChemicalIndexId
  111. var isStop int
  112. if strings.Contains(indexItem.IndexName, "停") {
  113. isStop = 1
  114. }
  115. indexObj.IsStop = isStop
  116. //修改数据
  117. updateColsArr := make([]string, 0)
  118. updateColsArr = append(updateColsArr, "index_name")
  119. updateColsArr = append(updateColsArr, "unit")
  120. updateColsArr = append(updateColsArr, "source")
  121. updateColsArr = append(updateColsArr, "frequency")
  122. updateColsArr = append(updateColsArr, "start_date")
  123. updateColsArr = append(updateColsArr, "end_date")
  124. updateColsArr = append(updateColsArr, "describe")
  125. updateColsArr = append(updateColsArr, "end_date")
  126. updateColsArr = append(updateColsArr, "is_stop")
  127. updateColsArr = append(updateColsArr, "modify_time")
  128. e := indexObj.Update(updateColsArr)
  129. if e != nil {
  130. fmt.Println("Index Update Err:" + e.Error())
  131. return
  132. }
  133. dataObj := new(models.BaseFromMysteelChemicalData)
  134. //获取已存在的所有数据
  135. exitDataList, err := dataObj.GetIndexDataList(indexItem.IndexCode)
  136. if err != nil {
  137. fmt.Println("GetIndexDataList Err:" + err.Error())
  138. return err
  139. }
  140. fmt.Println("exitDataListLen:", len(exitDataList))
  141. for _, v := range exitDataList {
  142. dateStr := v.DataTime.Format(utils.FormatDate)
  143. exitDataMap[dateStr] = v
  144. }
  145. }
  146. dataObj := new(models.BaseFromMysteelChemicalData)
  147. var hasUpdate bool
  148. // 遍历excel数据,然后跟现有的数据做校验,不存在则入库
  149. for date, value := range indexItem.ExcelDataMap {
  150. dateTime, err := utils.DealExcelDate(date)
  151. if err != nil {
  152. fmt.Println("time.ParseInLocation Err:" + err.Error())
  153. return err
  154. }
  155. date = dateTime.Format(utils.FormatDate)
  156. if findData, ok := exitDataMap[date]; !ok {
  157. if !strings.Contains(value, "#N/A") {
  158. dataItem := new(models.BaseFromMysteelChemicalData)
  159. dataItem.BaseFromMysteelChemicalIndexId = indexId
  160. dataItem.IndexCode = indexItem.IndexCode
  161. dataItem.DataTime = dateTime
  162. dataItem.Value = value
  163. dataItem.UpdateDate = indexItem.UpdateDate
  164. dataItem.CreateTime = time.Now()
  165. dataItem.ModifyTime = time.Now()
  166. addDataList = append(addDataList, *dataItem)
  167. }
  168. } else {
  169. if findData != nil && findData.Value != value && !strings.Contains(value, "#N/A") { //修改数据
  170. dataObj.BaseFromMysteelChemicalDataId = findData.BaseFromMysteelChemicalDataId
  171. dataObj.Value = value
  172. dataObj.ModifyTime = time.Now()
  173. updateDataColsArr := make([]string, 0)
  174. updateDataColsArr = append(updateDataColsArr, "value")
  175. updateDataColsArr = append(updateDataColsArr, "modify_time")
  176. dataObj.Update(updateDataColsArr)
  177. hasUpdate = true
  178. }
  179. }
  180. }
  181. if len(addDataList) > 0 {
  182. err = dataObj.Add(addDataList)
  183. if err != nil {
  184. fmt.Println("dataObj.Add() Err:" + err.Error())
  185. return
  186. }
  187. hasUpdate = true
  188. }
  189. //修改最大最小日期
  190. mysteelIndexMaxItem, err := dataObj.GetMysteelIndexInfoMaxAndMinInfo(indexItem.IndexCode)
  191. if err == nil && mysteelIndexMaxItem != nil {
  192. e := dataObj.ModifyMysteelIndexMaxAndMinInfo(indexItem.IndexCode, mysteelIndexMaxItem)
  193. if e != nil {
  194. fmt.Println("ModifyMysteelIndexMaxAndMinInfo Err:" + e.Error())
  195. }
  196. }
  197. // 同步刷新图库钢联的指标
  198. //go func() {
  199. var indexErr error
  200. var lErr error
  201. defer func() {
  202. if indexErr != nil {
  203. tips := fmt.Sprintf("钢联数据刷新-ETA指标刷新异常, 指标编码: %s, err: %s", indexObj.IndexCode, indexErr.Error())
  204. alarm_msg.SendAlarmMsg(tips, 3)
  205. }
  206. if lErr != nil {
  207. tips := fmt.Sprintf("钢联数据刷新-ETA指标刷新统计异常, 指标编码: %s, err: %s", indexObj.IndexCode, lErr.Error())
  208. alarm_msg.SendAlarmMsg(tips, 3)
  209. }
  210. }()
  211. edbInfo, e := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, indexObj.IndexCode)
  212. if e != nil && e.Error() != utils.ErrNoRow() {
  213. indexErr = e
  214. return
  215. }
  216. if edbInfo != nil {
  217. dataUpdateResult := 2
  218. dataUpdateFailedReason := "服务异常"
  219. _, logErrMsg, logErr := logic.RefreshBaseEdbInfo(edbInfo, ``)
  220. if logErr != nil {
  221. lErr = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 2, logErrMsg+logErr.Error(), dataUpdateResult, dataUpdateFailedReason, 1, 0)
  222. return
  223. }
  224. if hasUpdate {
  225. dataUpdateResult = 1
  226. dataUpdateFailedReason = ""
  227. } else {
  228. dataUpdateFailedReason = "未刷新到数据"
  229. }
  230. // 添加刷新成功日志
  231. lErr = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", dataUpdateResult, dataUpdateFailedReason, 1, 0)
  232. if lErr != nil {
  233. return
  234. }
  235. //如果变更了指标名称,则添加指标信息变更日志
  236. if nameChange {
  237. edbInfo.SourceIndexName = indexItem.IndexName
  238. lErr = edbInfo.Update([]string{"SourceIndexName"})
  239. if lErr != nil {
  240. return
  241. }
  242. lErr = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", 0, "", 0, 1)
  243. }
  244. }
  245. //}()
  246. return
  247. }