edb_data.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package services
  2. import (
  3. "eta/eta_forum_hub/models"
  4. "eta/eta_forum_hub/models/mgodb"
  5. "eta/eta_forum_hub/utils"
  6. "fmt"
  7. "strconv"
  8. "time"
  9. )
  10. func BatchAddOrUpdateEdbData(req []*models.AddEdbDataReq) (err error) {
  11. for _, reqData := range req {
  12. if reqData.EdbType == 1 {
  13. err = AddOrUpdateEdbData(reqData.EdbCode, reqData.DataList)
  14. if err != nil {
  15. return
  16. }
  17. } else {
  18. err = AddOrUpdateEdbDataCalculate(reqData.EdbCode, reqData.DataList)
  19. if err != nil {
  20. return
  21. }
  22. }
  23. }
  24. return
  25. }
  26. func AddOrUpdateEdbData(edbCode string, dataList []*models.EdbDataBase) (err error) {
  27. addList := make([]interface{}, 0)
  28. existList, err := mgodb.GetEdbDataBaseByEdbCode(edbCode)
  29. if err != nil {
  30. err = fmt.Errorf("查询指标数据出错 error, %v", err)
  31. return
  32. }
  33. existMap := make(map[string]*mgodb.EdbDataBase, len(existList))
  34. deleteMap := make(map[string]struct{}, len(existList))
  35. for _, exist := range existList {
  36. tmp := exist.DataTime.Format(utils.FormatDate)
  37. existMap[tmp] = exist
  38. deleteMap[tmp] = struct{}{}
  39. }
  40. for _, v := range dataList {
  41. dataTime, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  42. if e != nil {
  43. err = fmt.Errorf("时间格式化出错 error, %v", e)
  44. return
  45. }
  46. val, e := strconv.ParseFloat(v.Value, 64)
  47. if e != nil {
  48. err = fmt.Errorf("指标数据格式化出错 error, %v", e)
  49. return
  50. }
  51. if oldObj, ok := existMap[v.DataTime]; !ok {
  52. tmp := &mgodb.EdbDataBase{
  53. EdbInfoId: 0,
  54. EdbCode: v.EdbCode,
  55. DataTime: dataTime,
  56. Value: val,
  57. Status: v.Status,
  58. CreateTime: time.Now(),
  59. ModifyTime: time.Now(),
  60. DataTimestamp: v.DataTimestamp,
  61. }
  62. addList = append(addList, tmp)
  63. if len(addList) >= 500 {
  64. err = mgodb.InsertEdbDataBatch(addList)
  65. if err != nil {
  66. err = fmt.Errorf("批量新增指标数据失败 error, %v", e)
  67. return
  68. }
  69. addList = make([]interface{}, 0)
  70. }
  71. } else {
  72. delete(deleteMap, v.DataTime)
  73. if val != oldObj.Value {
  74. err = mgodb.ModifyValueEdbDataValue(oldObj.EdbDataId, val)
  75. if err != nil {
  76. err = fmt.Errorf("更新指标数据出错 error, %v", err)
  77. return
  78. }
  79. }
  80. }
  81. }
  82. //遍历deletemap,找出需要删除的日期
  83. {
  84. removeDateList := make([]time.Time, 0)
  85. for dateTime := range deleteMap {
  86. dateT, e := time.ParseInLocation(utils.FormatDate, dateTime, time.Local)
  87. if e != nil {
  88. err = fmt.Errorf("日期格式错误 error, %v", e)
  89. return
  90. }
  91. removeDateList = append(removeDateList, dateT)
  92. }
  93. removeNum := len(removeDateList)
  94. if removeNum > 0 {
  95. err = mgodb.DeleteEdbInfoDataByEdbInfoIdAndDate(edbCode, removeDateList)
  96. if err != nil {
  97. err = fmt.Errorf("删除多余日期失败,error, %v", err)
  98. return
  99. }
  100. }
  101. }
  102. if len(addList) > 0 {
  103. err = mgodb.InsertEdbDataBatch(addList)
  104. }
  105. return
  106. }
  107. func AddOrUpdateEdbDataCalculate(edbCode string, dataList []*models.EdbDataBase) (err error) {
  108. addList := make([]interface{}, 0)
  109. existList, err := mgodb.GetEdbDataCalculateByEdbCode(edbCode)
  110. if err != nil {
  111. err = fmt.Errorf("查询指标数据出错 error, %v", err)
  112. return
  113. }
  114. existMap := make(map[string]*mgodb.EdbDataBase, len(existList))
  115. deleteMap := make(map[string]struct{}, len(existList))
  116. for _, exist := range existList {
  117. tmp := exist.DataTime.Format(utils.FormatDate)
  118. existMap[tmp] = exist
  119. deleteMap[tmp] = struct{}{}
  120. }
  121. for _, v := range dataList {
  122. dataTime, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  123. if e != nil {
  124. err = fmt.Errorf("时间格式化出错 error, %v", e)
  125. return
  126. }
  127. val, e := strconv.ParseFloat(v.Value, 64)
  128. if e != nil {
  129. err = fmt.Errorf("指标数据格式化出错 error, %v", e)
  130. return
  131. }
  132. if oldObj, ok := existMap[v.DataTime]; !ok {
  133. tmp := &mgodb.EdbDataBase{
  134. EdbInfoId: 0,
  135. EdbCode: v.EdbCode,
  136. DataTime: dataTime,
  137. Value: val,
  138. Status: v.Status,
  139. CreateTime: time.Now(),
  140. ModifyTime: time.Now(),
  141. DataTimestamp: v.DataTimestamp,
  142. }
  143. addList = append(addList, tmp)
  144. if len(addList) >= 500 {
  145. err = mgodb.InsertEdbCalculateDataBatch(addList)
  146. if err != nil {
  147. err = fmt.Errorf("批量新增指标数据失败 error, %v", e)
  148. return
  149. }
  150. addList = make([]interface{}, 0)
  151. }
  152. } else {
  153. delete(deleteMap, v.DataTime)
  154. if val != oldObj.Value {
  155. err = mgodb.ModifyValueEdbCalculateDataValue(oldObj.EdbDataId, val)
  156. if err != nil {
  157. err = fmt.Errorf("更新指标数据出错 error, %v", err)
  158. return
  159. }
  160. }
  161. }
  162. }
  163. //遍历deleteMap,找出需要删除的日期
  164. {
  165. removeDateList := make([]time.Time, 0)
  166. for dateTime := range deleteMap {
  167. dateT, e := time.ParseInLocation(utils.FormatDate, dateTime, time.Local)
  168. if e != nil {
  169. err = fmt.Errorf("日期格式错误 error, %v", e)
  170. return
  171. }
  172. removeDateList = append(removeDateList, dateT)
  173. }
  174. removeNum := len(removeDateList)
  175. if removeNum > 0 {
  176. err = mgodb.DeleteEdbCalculateDataByEdbInfoIdAndDate(edbCode, removeDateList)
  177. if err != nil {
  178. err = fmt.Errorf("删除多余日期失败,error, %v", err)
  179. return
  180. }
  181. }
  182. }
  183. if len(addList) > 0 {
  184. err = mgodb.InsertEdbCalculateDataBatch(addList)
  185. }
  186. return
  187. }