edb_info.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package services
  2. import (
  3. "errors"
  4. "eta/eta_forum_hub/models"
  5. "eta/eta_forum_hub/models/mgodb"
  6. "eta/eta_forum_hub/services/elastic"
  7. "eta/eta_forum_hub/utils"
  8. "fmt"
  9. "strconv"
  10. "time"
  11. )
  12. // BatchAddOrUpdateEdbInfo 添加批量添加指标到指标库
  13. func BatchAddOrUpdateEdbInfo(edbList []*models.EdbInfo, edbMapping []*models.EdbInfoCalculateMapping, sysUserId int, sysUserRealName string) (newList []*models.EdbInfo, err error, errMsg string, isSendEmail bool) {
  14. isSendEmail = true
  15. edbCodeMap := make(map[string]*models.EdbInfo)
  16. for _, v := range edbList {
  17. tmp, e, msg, _ := AddOrUpdateEdbInfo(v, sysUserId, sysUserRealName)
  18. if e != nil {
  19. err = fmt.Errorf("添加指标失败:%s,%s", e.Error(), msg)
  20. errMsg = "添加指标失败"
  21. return
  22. }
  23. newList = append(newList, tmp)
  24. edbCodeMap[tmp.EdbCode] = tmp
  25. }
  26. //批量添加指标的mapping信息
  27. calculateMappingItemList := make([]*models.EdbInfoCalculateMapping, 0)
  28. for _, v := range edbMapping {
  29. edbInfo, ok := edbCodeMap[v.EdbCode]
  30. if !ok {
  31. continue
  32. }
  33. fromEdbInfo, ok := edbCodeMap[v.FromEdbCode]
  34. if !ok {
  35. continue
  36. }
  37. calculateMappingItem := new(models.EdbInfoCalculateMapping)
  38. calculateMappingItem.CreateTime = time.Now()
  39. calculateMappingItem.ModifyTime = time.Now()
  40. calculateMappingItem.Sort = v.Sort
  41. calculateMappingItem.EdbCode = edbInfo.EdbCode
  42. calculateMappingItem.EdbInfoId = edbInfo.EdbInfoId
  43. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  44. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  45. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  46. calculateMappingItem.FromSource = fromEdbInfo.Source
  47. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  48. calculateMappingItem.FromTag = v.FromTag
  49. calculateMappingItem.Source = edbInfo.Source
  50. calculateMappingItem.SourceName = edbInfo.SourceName
  51. calculateMappingItem.FromSubSource = edbInfo.SubSource
  52. calculateMappingItemList = append(calculateMappingItemList, calculateMappingItem)
  53. }
  54. if len(calculateMappingItemList) > 0 {
  55. err = models.AddEdbInfoCalculateMappingMulti(calculateMappingItemList)
  56. if err != nil {
  57. errMsg = "指标映射关系添加失败"
  58. err = fmt.Errorf("指标映射关系添加失败, %s", err.Error())
  59. return
  60. }
  61. }
  62. return
  63. }
  64. // AddOrUpdateEdbInfo 添加指标到指标库
  65. func AddOrUpdateEdbInfo(edbItem *models.EdbInfo, sysUserId int, sysUserRealName string) (edbInfo *models.EdbInfo, err error, errMsg string, isSendEmail bool) {
  66. isSendEmail = true
  67. //判断指标是否存在
  68. edbInfo, err = models.GetEdbInfoByEdbCode(edbItem.Source, edbItem.EdbCode)
  69. if err != nil {
  70. if err.Error() == utils.ErrNoRow() {
  71. err = nil
  72. } else {
  73. errMsg = "判断指标名称是否存在失败"
  74. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  75. return
  76. }
  77. }
  78. var edbInfoId int64
  79. // 指标已存在
  80. if edbInfo != nil && edbInfo.EdbInfoId > 0 {
  81. edbInfoId = int64(edbInfo.EdbInfoId)
  82. // 更新指标信息
  83. edbInfo.Source = edbItem.Source
  84. edbInfo.SourceName = edbItem.SourceName
  85. edbInfo.EdbType = edbItem.EdbType
  86. edbInfo.EdbCode = edbItem.EdbCode
  87. edbInfo.EdbName = edbItem.EdbName
  88. edbInfo.EdbNameSource = edbItem.EdbNameSource
  89. edbInfo.Frequency = edbItem.Frequency
  90. edbInfo.Unit = edbItem.Unit
  91. edbInfo.StartDate = edbItem.StartDate
  92. edbInfo.EndDate = edbItem.EndDate
  93. edbInfo.ClassifyId = edbItem.ClassifyId
  94. edbInfo.SysUserId = sysUserId
  95. edbInfo.SysUserRealName = sysUserRealName
  96. edbInfo.ModifyTime = time.Now()
  97. edbInfo.ServerUrl = edbItem.ServerUrl
  98. edbInfo.Sort = edbItem.Sort
  99. edbInfo.DataDateType = edbItem.DataDateType
  100. edbInfo.TerminalCode = edbItem.TerminalCode
  101. edbInfo.SourceIndexName = edbItem.SourceIndexName
  102. edbInfo.MaxValue = edbItem.MaxValue
  103. edbInfo.MinValue = edbItem.MinValue
  104. edbInfo.EdbInfoType = edbItem.EdbInfoType
  105. edbInfo.LatestDate = edbItem.LatestDate
  106. edbInfo.LatestValue = edbItem.LatestValue
  107. edbInfo.CalculateFormula = edbItem.CalculateFormula
  108. edbInfo.ManualSave = edbItem.ManualSave
  109. edbInfo.MoveType = edbItem.MoveType
  110. edbInfo.MoveFrequency = edbItem.MoveFrequency
  111. edbInfo.NoUpdate = edbItem.NoUpdate
  112. edbInfo.EdbNameEn = edbItem.EdbNameEn
  113. edbInfo.UnitEn = edbItem.UnitEn
  114. edbInfo.ChartImage = edbItem.ChartImage
  115. edbInfo.Calendar = edbItem.Calendar
  116. edbInfo.EmptyType = edbItem.EmptyType
  117. edbInfo.MaxEmptyType = edbItem.MaxEmptyType
  118. edbInfo.DataUpdateTime = edbItem.DataUpdateTime
  119. edbInfo.ErDataUpdateDate = edbItem.ErDataUpdateDate
  120. edbInfo.EndValue = edbItem.EndValue
  121. edbInfo.SubSource = edbItem.SubSource
  122. edbInfo.SubSourceName = edbItem.SubSourceName
  123. edbInfo.IndicatorCode = edbItem.IndicatorCode
  124. edbInfo.StockCode = edbItem.StockCode
  125. edbInfo.Extra = edbItem.Extra
  126. err = edbInfo.Update([]string{})
  127. if err != nil {
  128. errMsg = "保存失败"
  129. err = errors.New("保存失败,Err:" + err.Error())
  130. return
  131. }
  132. //同时删除指标的映射信息
  133. err = models.DeleteEdbInfoMapping(edbInfoId)
  134. if err != nil {
  135. errMsg = "删除指标映射信息失败"
  136. err = errors.New("删除指标映射信息失败,Err:" + err.Error())
  137. return
  138. }
  139. } else {
  140. edbInfo = new(models.EdbInfo)
  141. edbInfo.Source = edbItem.Source
  142. edbInfo.SourceName = edbItem.SourceName
  143. edbInfo.EdbType = edbItem.EdbType
  144. edbInfo.EdbCode = edbItem.EdbCode
  145. edbInfo.EdbName = edbItem.EdbName
  146. edbInfo.EdbNameSource = edbItem.EdbNameSource
  147. edbInfo.Frequency = edbItem.Frequency
  148. edbInfo.Unit = edbItem.Unit
  149. edbInfo.StartDate = edbItem.StartDate
  150. edbInfo.EndDate = edbItem.EndDate
  151. edbInfo.ClassifyId = edbItem.ClassifyId
  152. edbInfo.SysUserId = sysUserId
  153. edbInfo.SysUserRealName = sysUserRealName
  154. edbInfo.CreateTime = time.Now()
  155. edbInfo.ModifyTime = time.Now()
  156. edbInfo.ServerUrl = edbItem.ServerUrl
  157. edbInfo.Sort = edbItem.Sort
  158. edbInfo.DataDateType = edbItem.DataDateType
  159. edbInfo.TerminalCode = edbItem.TerminalCode
  160. edbInfo.SourceIndexName = edbItem.SourceIndexName
  161. //timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  162. //edbInfo.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  163. edbInfo.UniqueCode = edbItem.UniqueCode
  164. edbInfo.MaxValue = edbItem.MaxValue
  165. edbInfo.MinValue = edbItem.MinValue
  166. edbInfo.EdbInfoType = edbItem.EdbInfoType
  167. edbInfo.LatestDate = edbItem.LatestDate
  168. edbInfo.LatestValue = edbItem.LatestValue
  169. edbInfo.CalculateFormula = edbItem.CalculateFormula
  170. edbInfo.ManualSave = edbItem.ManualSave
  171. edbInfo.MoveType = edbItem.MoveType
  172. edbInfo.MoveFrequency = edbItem.MoveFrequency
  173. edbInfo.NoUpdate = edbItem.NoUpdate
  174. edbInfo.EdbNameEn = edbItem.EdbNameEn
  175. edbInfo.UnitEn = edbItem.UnitEn
  176. edbInfo.ChartImage = edbItem.ChartImage
  177. edbInfo.Calendar = edbItem.Calendar
  178. edbInfo.EmptyType = edbItem.EmptyType
  179. edbInfo.MaxEmptyType = edbItem.MaxEmptyType
  180. edbInfo.DataUpdateTime = edbItem.DataUpdateTime
  181. edbInfo.ErDataUpdateDate = edbItem.ErDataUpdateDate
  182. edbInfo.EndValue = edbItem.EndValue
  183. edbInfo.SubSource = edbItem.SubSource
  184. edbInfo.SubSourceName = edbItem.SubSourceName
  185. edbInfo.IndicatorCode = edbItem.IndicatorCode
  186. edbInfo.StockCode = edbItem.StockCode
  187. edbInfo.Extra = edbItem.Extra
  188. edbInfoId, err = models.AddEdbInfo(edbInfo)
  189. if err != nil {
  190. errMsg = "保存失败"
  191. err = errors.New("保存失败,Err:" + err.Error())
  192. return
  193. }
  194. edbInfo.EdbInfoId = int(edbInfoId)
  195. }
  196. if edbInfo.EdbType == 1 {
  197. err = mgodb.ModifyEdbDataEdbInfoId(edbInfoId, edbInfo.EdbCode)
  198. if err != nil {
  199. errMsg = "保存失败"
  200. err = errors.New("更新指标数据失败,Err:" + err.Error())
  201. return
  202. }
  203. } else {
  204. err = mgodb.ModifyEdbCalculateDataEdbInfoId(edbInfoId, edbInfo.EdbCode)
  205. if err != nil {
  206. errMsg = "保存失败"
  207. err = errors.New("更新指标数据失败,Err:" + err.Error())
  208. return
  209. }
  210. }
  211. // 更新es
  212. go AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  213. return
  214. }
  215. // 批量删除指标,
  216. func BatchDeleteEdbInfo(edbInfoIds []int) (err error, errMsg string) {
  217. for _, v := range edbInfoIds {
  218. //查询单个指标是否允许删除
  219. err, errMsg = DeleteEdbInfo(v)
  220. if err != nil {
  221. errMsg = "删除指标失败"
  222. return
  223. }
  224. }
  225. return
  226. }
  227. // 删除单个指标
  228. func DeleteEdbInfo(edbInfoId int) (err error, errMsg string) {
  229. //判断指标是否存在
  230. info, err := models.GetEdbInfoById(edbInfoId)
  231. if err != nil {
  232. if err.Error() == utils.ErrNoRow() {
  233. err = nil
  234. return
  235. } else {
  236. errMsg = "判断指标名称是否存在失败"
  237. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  238. return
  239. }
  240. }
  241. //判断指标是否用于作图,如果用于作图,则不可删除
  242. chartCount, tmpErr := models.GetChartEdbMappingCount(edbInfoId)
  243. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  244. errMsg = "删除失败"
  245. err = errors.New("判断指标是否被用于作图失败,Err:" + tmpErr.Error())
  246. return
  247. }
  248. if chartCount > 0 {
  249. errMsg = "当前指标已用作画图,不可删除"
  250. return
  251. }
  252. //判断指标是否用作其他指标的计算
  253. calculateCount, tmpErr := models.GetEdbInfoCalculateMappingCount(edbInfoId)
  254. if tmpErr != nil {
  255. errMsg = "删除失败"
  256. err = errors.New("判断指标是否被用于计算失败,GetEdbInfoCalculateCount Err:" + tmpErr.Error())
  257. return
  258. }
  259. if calculateCount > 0 {
  260. errMsg = "当前指标已用作,指标运算,不可删除"
  261. return
  262. }
  263. // 删除指标,删除映射关系,删除指标数据
  264. err = models.DeleteEdbInfo(edbInfoId)
  265. if err != nil {
  266. errMsg = "删除失败"
  267. err = errors.New("删除指标失败,Err:" + err.Error())
  268. return
  269. }
  270. if info.EdbType == 1 {
  271. err = mgodb.DeleteEdbInfoDataByEdbInfoId(edbInfoId)
  272. if err != nil {
  273. errMsg = "删除失败"
  274. err = errors.New("删除指标数据失败,Err:" + err.Error())
  275. return
  276. }
  277. } else {
  278. err = mgodb.DeleteEdbInfoCalculateDataByEdbInfoId(edbInfoId)
  279. if err != nil {
  280. errMsg = "删除失败"
  281. err = errors.New("删除指标数据失败,Err:" + err.Error())
  282. return
  283. }
  284. }
  285. // 删除es中的数据
  286. go DeleteEdbInfoToEs(edbInfoId)
  287. return
  288. }
  289. // AddOrEditEdbInfoToEs 添加/修改ES中的指标
  290. func AddOrEditEdbInfoToEs(edbInfoId int) {
  291. //添加es
  292. itemInfo, _ := models.GetEdbInfoByCondition("AND edb_info_id=?", []interface{}{edbInfoId})
  293. go elastic.EsAddOrEditEdbInfoData(utils.DATA_INDEX_NAME, strconv.Itoa(itemInfo.EdbInfoId), itemInfo)
  294. }