base_from_ccf.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. "html"
  9. "strings"
  10. "time"
  11. )
  12. // HandleCCFIndex 处理CCF的excel数据
  13. func HandleCCFIndex(req *models.HandleCCFEdbDataReq) (err error) {
  14. errMsgList := make([]string, 0)
  15. defer func() {
  16. if len(errMsgList) > 0 {
  17. msg := fmt.Sprint("数据源-CCF数据处理失败, err:", strings.Join(errMsgList, "\n"))
  18. utils.FileLog.Info(msg)
  19. go alarm_msg.SendAlarmMsg(msg, 3)
  20. }
  21. }()
  22. for _, v := range req.List {
  23. if v.IndexName == "" || v.IndexCode == "" {
  24. errMsgList = append(errMsgList, fmt.Sprintf("新增指标异常,指标编码%s或者指标ID%s为空:", v.IndexCode, v.IndexName))
  25. continue
  26. }
  27. err = handleCCFIndex(v, req.TerminalCode)
  28. if err != nil {
  29. errMsgList = append(errMsgList, fmt.Sprintf("新增指标异常,指标编码:%s, Err: %s", v.IndexCode, err))
  30. return
  31. }
  32. }
  33. return
  34. }
  35. func handleCCFIndex(req *models.HandleCCFEdbData, terminalCode string) (err error) {
  36. indexName := req.IndexName
  37. indexCode := req.IndexCode
  38. excelDataMap := req.DateData
  39. errMsgList := make([]string, 0)
  40. defer func() {
  41. if len(errMsgList) > 0 {
  42. msg := fmt.Sprint("数据源-CCF数据处理失败,err:", strings.Join(errMsgList, "\n"))
  43. utils.FileLog.Info(msg)
  44. go alarm_msg.SendAlarmMsg(msg, 3)
  45. }
  46. }()
  47. indexObj := new(models.BaseFromCCFIndex)
  48. dataObj := new(models.BaseFromCCFData)
  49. //classifyObj := new(models.BaseFromCCFClassify)
  50. var indexId int64
  51. addDataList := make([]*models.BaseFromCCFData, 0)
  52. updateDataList := make([]*models.BaseFromCCFData, 0)
  53. exitDataMap := make(map[string]*models.BaseFromCCFData)
  54. // 修改指标信息
  55. if indexName == "" {
  56. utils.FileLog.Info("未刷新到指标数据:indexName:" + indexName)
  57. return
  58. }
  59. //判断指标是否存在
  60. var isAdd int
  61. item, err := indexObj.GetByIndexCode(indexCode)
  62. if err != nil {
  63. if err.Error() == utils.ErrNoRow() {
  64. isAdd = 1
  65. err = nil
  66. } else {
  67. isAdd = -1
  68. err = fmt.Errorf("查询数据源指标库失败 GetByIndexCode Err:%s", err)
  69. return
  70. }
  71. }
  72. if item != nil && item.BaseFromCcfIndexId > 0 {
  73. fmt.Println("item:", item)
  74. isAdd = 2
  75. } else {
  76. isAdd = 1
  77. }
  78. if isAdd == 1 {
  79. indexObj.IndexCode = indexCode
  80. indexObj.IndexName = indexName
  81. indexObj.Frequency = req.Frequency
  82. indexObj.ClassifyId = req.ClassifyId
  83. if req.Unit == "" {
  84. req.Unit = "无"
  85. }
  86. indexObj.Unit = req.Unit
  87. indexObj.Sort = req.Sort
  88. indexObj.ModifyTime = time.Now()
  89. indexObj.CreateTime = time.Now()
  90. indexObj.TerminalCode = terminalCode
  91. indexId, err = indexObj.Add()
  92. if err != nil {
  93. err = fmt.Errorf("数据源新增CCF指标失败 Err:%s", err)
  94. return
  95. }
  96. indexObj.BaseFromCcfIndexId = indexId
  97. } else if isAdd == 2 {
  98. if item.TerminalCode == `` && terminalCode != `` {
  99. item.TerminalCode = terminalCode
  100. err = item.Update([]string{"TerminalCode"})
  101. if err != nil {
  102. err = fmt.Errorf("数据源更新CCF指标失败 Err:%s", err)
  103. return
  104. }
  105. }
  106. //获取已存在的所有数据
  107. var exitDataList []*models.BaseFromCCFData
  108. exitDataList, err = dataObj.GetByIndexCode(indexCode)
  109. if err != nil {
  110. err = fmt.Errorf("数据源查询CCF指标数据失败 Err:%s", err)
  111. return
  112. }
  113. fmt.Println("exitDataListLen:", len(exitDataList))
  114. for _, v := range exitDataList {
  115. dateStr := v.DataTime
  116. exitDataMap[dateStr] = v
  117. }
  118. indexId = item.BaseFromCcfIndexId
  119. }
  120. // 遍历excel数据,然后跟现有的数据做校验,不存在则入库
  121. for date, value := range excelDataMap {
  122. if findData, ok := exitDataMap[date]; !ok {
  123. _, err = time.ParseInLocation(utils.FormatDate, date, time.Local)
  124. if err != nil {
  125. err = fmt.Errorf("%s 转换日期格式失败 Err:%s", date, err)
  126. return
  127. }
  128. if !strings.Contains(value, "#N/A") {
  129. var saveDataTime time.Time
  130. if strings.Contains(date, "00:00:00") {
  131. saveDataTime, err = time.Parse(utils.FormatDateTime, date)
  132. } else {
  133. saveDataTime, err = time.Parse(utils.FormatDate, date)
  134. }
  135. if err != nil {
  136. err = fmt.Errorf("%s 转换日期格式失败 Err:%s", date, err)
  137. continue
  138. }
  139. timestamp := saveDataTime.UnixNano() / 1e6
  140. dataItem := new(models.BaseFromCCFData)
  141. dataItem.BaseFromCcfIndexId = int(indexId)
  142. dataItem.IndexCode = indexCode
  143. dataItem.DataTime = date
  144. dataItem.Value = value
  145. dataItem.CreateTime = time.Now()
  146. dataItem.ModifyTime = time.Now()
  147. dataItem.DataTimestamp = timestamp
  148. addDataList = append(addDataList, dataItem)
  149. }
  150. } else {
  151. if findData != nil && findData.Value != value && !strings.Contains(value, "#N/A") { //修改数据
  152. findData.Value = value
  153. findData.ModifyTime = time.Now().Local()
  154. updateDataList = append(updateDataList, findData)
  155. }
  156. }
  157. }
  158. if len(addDataList) > 0 {
  159. err = dataObj.AddMulti(addDataList)
  160. if err != nil {
  161. err = fmt.Errorf("批量新增指标失败 Err:%s", err)
  162. return
  163. }
  164. var dateItem *models.EdbInfoMaxAndMinInfo
  165. dateItem, err = dataObj.GetMaxAndMinDateByIndexCode(indexCode)
  166. if err != nil {
  167. err = fmt.Errorf("查询指标最新日期失败 Err:%s", err)
  168. return
  169. }
  170. go func() {
  171. indexObj.ModifyIndexMaxAndMinDate(indexCode, dateItem)
  172. }()
  173. }
  174. // 批量更新数据
  175. if len(updateDataList) > 0 {
  176. e := models.MultiUpdateBaseFromCCFDataValue(updateDataList)
  177. if e != nil {
  178. err = fmt.Errorf("MultiUpdateBaseFromSmmDataValue err: %s", e.Error())
  179. return
  180. }
  181. }
  182. // 同步刷新ETA指标库的指标
  183. {
  184. // 获取指标详情
  185. baseObj := new(models.BaseFromCCF)
  186. var edbInfo *models.EdbInfo
  187. edbInfo, err = models.GetEdbInfoByEdbCode(baseObj.GetSource(), indexCode)
  188. if err != nil {
  189. if err.Error() != utils.ErrNoRow() {
  190. errMsgList = append(errMsgList, fmt.Sprint("刷新ETA指标异常,指标编码:", indexCode, err.Error()))
  191. return
  192. } else {
  193. err = nil
  194. }
  195. }
  196. // 已经加入到指标库的话,那么就去更新ETA指标库吧
  197. if edbInfo != nil {
  198. go logic.RefreshBaseEdbInfo(edbInfo, ``)
  199. }
  200. }
  201. return
  202. }
  203. // HandleCCFStockTable 处理CCF的表格数据
  204. func HandleCCFStockTable(req *models.HandleCCFStockTableReq) (err error) {
  205. defer func() {
  206. if err != nil {
  207. tips := fmt.Sprintf("数据源-CCF数据装置处理失败, err: %v", err)
  208. utils.FileLog.Info(tips)
  209. go alarm_msg.SendAlarmMsg(tips, 3)
  210. }
  211. }()
  212. if req.Table == nil {
  213. err = fmt.Errorf("表格信息为空")
  214. return
  215. }
  216. // 已存在则更新表格
  217. excelOb := new(models.CCFStockExcel)
  218. cond := fmt.Sprintf(` AND %s = ? AND %s = ?`, excelOb.Cols().ClassifyId, excelOb.Cols().ExcelDate)
  219. pars := make([]interface{}, 0)
  220. pars = append(pars, req.Table.ClassifyId, req.Table.TableDate.Format(utils.FormatDate))
  221. exists, e := excelOb.GetItemByCondition(cond, pars, "")
  222. if e != nil && e.Error() != utils.ErrNoRow() {
  223. err = fmt.Errorf("获取已存在表格失败, err: %v", e)
  224. return
  225. }
  226. if exists != nil {
  227. exists.ExcelContent = html.EscapeString(req.Table.TableContent)
  228. exists.FromPage = req.Table.FromPage
  229. exists.ModifyTime = time.Now().Local()
  230. cols := []string{excelOb.Cols().ExcelContent, excelOb.Cols().FromPage, excelOb.Cols().ModifyTime}
  231. if e = exists.Update(cols); e != nil {
  232. err = fmt.Errorf("更新已存在表格失败, err: %v", e)
  233. return
  234. }
  235. return
  236. }
  237. // 新增表格
  238. excelOb.ClassifyId = req.Table.ClassifyId
  239. excelOb.ExcelDate = req.Table.TableDate
  240. excelOb.ExcelContent = html.EscapeString(req.Table.TableContent)
  241. excelOb.FromPage = req.Table.FromPage
  242. excelOb.CreateTime = time.Now().Local()
  243. excelOb.ModifyTime = time.Now().Local()
  244. if e = excelOb.Create(); e != nil {
  245. err = fmt.Errorf("新增表格失败, err: %v", e)
  246. return
  247. }
  248. return
  249. }