edb_info_stat.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package services
  2. import (
  3. "eta/eta_index_lib/models"
  4. "eta/eta_index_lib/models/data_stat"
  5. "eta/eta_index_lib/utils"
  6. "fmt"
  7. "time"
  8. )
  9. // AddEdbInfoUpdateLog 添加指标编辑/刷新日志
  10. func AddEdbInfoUpdateLog(edbInfoId int, updateResult int, updateFailedReason string, sourceUpdateResult int, sourceUpdateFailedReason string, sourceUpdateTime string) (err error) {
  11. var edbInfo *models.EdbInfo
  12. if edbInfoId > 0 {
  13. // 获取指标详情
  14. edbInfo, err = models.GetEdbInfoById(edbInfoId)
  15. if err != nil {
  16. err = fmt.Errorf("指标不存在")
  17. return
  18. }
  19. log := new(data_stat.EdbInfoUpdateLog)
  20. log.EdbInfoId = edbInfo.EdbInfoId
  21. log.SourceName = edbInfo.SourceName
  22. log.Source = edbInfo.Source
  23. log.EdbCode = edbInfo.EdbCode
  24. log.EdbName = edbInfo.EdbName
  25. log.EdbNameSource = edbInfo.EdbNameSource
  26. log.Frequency = edbInfo.Frequency
  27. log.Unit = edbInfo.Unit
  28. log.StartDate = edbInfo.StartDate
  29. log.EndDate = edbInfo.EndDate
  30. log.SysUserId = edbInfo.SysUserId
  31. log.SysUserRealName = edbInfo.SysUserRealName
  32. log.UniqueCode = edbInfo.UniqueCode
  33. log.EdbCreateTime = edbInfo.CreateTime
  34. log.EdbModifyTime = edbInfo.ModifyTime
  35. log.CreateTime = time.Now()
  36. log.LatestDate = edbInfo.LatestDate
  37. log.LatestValue = edbInfo.LatestValue
  38. log.TerminalCode = edbInfo.TerminalCode
  39. log.UpdateResult = updateResult
  40. log.UpdateFailedReason = updateFailedReason
  41. log.DataUpdateTime = edbInfo.DataUpdateTime
  42. log.ErDataUpdateDate = edbInfo.ErDataUpdateDate
  43. log.SourceUpdateResult = sourceUpdateResult
  44. log.SourceUpdateTime = sourceUpdateTime
  45. log.SourceUpdateFailedReason = sourceUpdateFailedReason
  46. _, err = data_stat.AddEdbUpdateLog(log)
  47. if err != nil {
  48. err = fmt.Errorf("新增指标更新日志失败,Err: %s", err)
  49. return
  50. }
  51. }
  52. return
  53. }
  54. // SetMysteelChemicalEdbInfoUpdateStat 定时统计钢联化工的数据源明细表
  55. func SetMysteelChemicalEdbInfoUpdateStat() (err error) {
  56. //查询钢联的所有指标信息
  57. condition := " and source = ? and edb_info_id=101838"
  58. var pars []interface{}
  59. pars = append(pars, utils.DATA_SOURCE_MYSTEEL_CHEMICAL)
  60. edbList, err := models.GetEdbInfoByCondition(condition, pars, 0)
  61. if err != nil {
  62. err = fmt.Errorf("查询钢联化工指标信息出错,err: %s", err)
  63. return
  64. }
  65. nowTime := time.Now()
  66. today := time.Now().Format(utils.FormatDate)
  67. todayT, _ := time.ParseInLocation(utils.FormatDate, today, time.Local)
  68. nextDay := time.Now().AddDate(0, 0, 1).Format(utils.FormatDate)
  69. //查询当日所有钢联指标的终端更新记录
  70. updateLogList, err := data_stat.GetEdbUpdateSourceLogByCreateDate(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, today, nextDay)
  71. if err != nil {
  72. err = fmt.Errorf("查询钢联化工指标终端更新日志报错,err: %s", err)
  73. return
  74. }
  75. updateLogMap := make(map[int]*data_stat.EdbInfoUpdateLog)
  76. if len(updateLogList) > 0 {
  77. for _, v := range updateLogList {
  78. if _, ok := updateLogMap[v.EdbInfoId]; !ok {
  79. updateLogMap[v.EdbInfoId] = v
  80. }
  81. }
  82. }
  83. statCond := " and source = ? and create_time >= ? and create_time < ?"
  84. var statPars []interface{}
  85. statPars = append(statPars, utils.DATA_SOURCE_MYSTEEL_CHEMICAL, today, nextDay)
  86. //查询当日钢联所有的统计数据
  87. updateStatList, err := data_stat.GetEdbUpdateStatByCondition(statCond, statPars)
  88. if err != nil {
  89. err = fmt.Errorf("查询钢联化工数据源明细记录统计报错,err: %s", err)
  90. return
  91. }
  92. updateStatMap := make(map[int]*data_stat.EdbInfoUpdateStat)
  93. if len(updateStatList) > 0 {
  94. for _, v := range updateStatList {
  95. updateStatMap[v.EdbInfoId] = v
  96. }
  97. }
  98. logStat := new(data_stat.EdbInfoUpdateStat)
  99. //组装新增数据
  100. addList := make([]*data_stat.EdbInfoUpdateStat, 0)
  101. modifyList := make([]*data_stat.EdbInfoUpdateStat, 0)
  102. if len(edbList) > 0 {
  103. for _, v := range edbList {
  104. tmp := &data_stat.EdbInfoUpdateStat{
  105. EdbInfoId: v.EdbInfoId,
  106. SourceName: v.SourceName,
  107. Source: v.Source,
  108. EdbCode: v.EdbCode,
  109. EdbName: v.EdbName,
  110. EdbNameSource: v.EdbNameSource,
  111. Frequency: v.Frequency,
  112. Unit: v.Unit,
  113. StartDate: v.StartDate,
  114. EndDate: v.EndDate,
  115. SysUserId: v.SysUserId,
  116. SysUserRealName: v.SysUserRealName,
  117. UniqueCode: v.UniqueCode,
  118. EdbCreateTime: v.CreateTime,
  119. EdbModifyTime: v.ModifyTime,
  120. //CreateTime: v.CreateTime,
  121. LatestDate: v.LatestDate,
  122. LatestValue: v.LatestValue,
  123. TerminalCode: v.TerminalCode,
  124. DataUpdateTime: v.DataUpdateTime,
  125. ErDataUpdateDate: v.ErDataUpdateDate,
  126. /*SourceUpdateResult: v.SourceUpdateResult,
  127. SourceUpdateFailedReason: v.SourceUpdateFailedReason,
  128. SourceUpdateTime: v.SourceUpdateTime,*/
  129. ModifyTime: nowTime,
  130. //IsAdd: v.IsAdd,
  131. //NeedRefresh: v.NeedRefresh,
  132. //HasRefresh: v.HasRefresh,
  133. }
  134. // todo 判断是否需要当日更新
  135. needRefresh, _ := checkMySteelEdbInfoNeedRefresh(v.Frequency)
  136. tmp.NeedRefresh = needRefresh
  137. // 判断是否当日新增
  138. if v.CreateTime.After(todayT) || v.CreateTime == todayT {
  139. tmp.IsAdd = 1
  140. } else {
  141. tmp.IsAdd = 2
  142. }
  143. if up, ok := updateLogMap[v.EdbInfoId]; ok {
  144. tmp.SourceUpdateTime = up.SourceUpdateTime
  145. tmp.SourceUpdateResult = up.SourceUpdateResult
  146. tmp.SourceUpdateFailedReason = up.SourceUpdateFailedReason
  147. if up.SourceUpdateFailedReason != "" && up.SourceUpdateFailedReason != "未刷新到数据" {
  148. tmp.SourceUpdateFailedReason = "服务异常"
  149. }
  150. tmp.HasRefresh = 1
  151. } else if needRefresh == 1 {
  152. tmp.HasRefresh = 0
  153. tmp.SourceUpdateResult = 2
  154. tmp.SourceUpdateFailedReason = "服务异常"
  155. }
  156. // 判断是否需要新增还是更新
  157. if exist, ok := updateStatMap[v.EdbInfoId]; ok {
  158. tmp.Id = exist.Id
  159. modifyList = append(modifyList, tmp)
  160. } else {
  161. tmp.CreateTime = nowTime
  162. addList = append(addList, tmp)
  163. }
  164. }
  165. }
  166. //判断当日指标统计数据是否存在,如果存在则更新,不存在则新增
  167. if len(addList) > 0 {
  168. err = logStat.Add(addList)
  169. }
  170. if len(modifyList) > 0 {
  171. err = data_stat.UpdateEdbUpdateStatMulti(modifyList)
  172. }
  173. return
  174. }
  175. func checkMySteelEdbInfoNeedRefresh(frequency string) (needRefresh int, err error) {
  176. now := time.Now()
  177. week := int(now.Weekday())
  178. //日度
  179. if week >= 1 && week <= 6 {
  180. if frequency == "日度" {
  181. needRefresh = 1
  182. return
  183. }
  184. }
  185. time.Sleep(1 * time.Minute)
  186. //周度
  187. if week >= 3 && week <= 6 {
  188. if frequency == "周度" {
  189. needRefresh = 1
  190. return
  191. }
  192. }
  193. day := now.Day() //季度,月度,年度都是每个月1号刷新
  194. if day == 1 {
  195. needRefresh = 1
  196. }
  197. return
  198. }
  199. // SetEdbSourceStat 定时统计数据源汇总表
  200. func SetEdbSourceStat() (err error) {
  201. //查询钢联的所有指标信息
  202. nowTime := time.Now()
  203. today := time.Now().Format(utils.FormatDate)
  204. nextDay := time.Now().AddDate(0, 0, 1).Format(utils.FormatDate)
  205. statCond := " and create_time >= ? and create_time < ?"
  206. var statPars []interface{}
  207. statPars = append(statPars, today, nextDay)
  208. //查询当日钢联所有的统计数据
  209. updateStatList, err := data_stat.GetEdbUpdateStatByCondition(statCond, statPars)
  210. if err != nil {
  211. err = fmt.Errorf("查询钢联化工数据源明细记录统计报错,err: %s", err)
  212. return
  213. }
  214. updateStatMap := make(map[string][]*data_stat.EdbInfoUpdateStat)
  215. if len(updateStatList) > 0 {
  216. for _, v := range updateStatList {
  217. updateStatMap[v.TerminalCode] = append(updateStatMap[v.TerminalCode], v)
  218. }
  219. }
  220. cond := " and create_time >= ? and create_time < ?"
  221. var pars []interface{}
  222. pars = append(pars, today, nextDay)
  223. //查询当日钢联所有的统计数据
  224. statList, err := data_stat.GetEdbSourceStatByCondition(cond, pars)
  225. if err != nil {
  226. err = fmt.Errorf("查询钢联化工数据源统计报错,err: %s", err)
  227. return
  228. }
  229. statMap := make(map[string]*data_stat.EdbSourceStat)
  230. if len(statList) > 0 {
  231. for _, v := range statList {
  232. statMap[v.TerminalCode] = v
  233. }
  234. }
  235. // 查询今日被删除的指标数
  236. delList, err := data_stat.GetEdbDeleteLogNumByCreateTime(today, nextDay)
  237. if err != nil {
  238. err = fmt.Errorf("查询今日被删除指标数目报错,err: %s", err)
  239. return
  240. }
  241. delMap := make(map[string]int)
  242. if len(delList) > 0 {
  243. for _, v := range delList {
  244. delMap[v.TerminalCode] = v.Num
  245. }
  246. }
  247. logStat := new(data_stat.EdbSourceStat)
  248. //组装新增数据
  249. addList := make([]*data_stat.EdbSourceStat, 0)
  250. modifyList := make([]*data_stat.EdbSourceStat, 0)
  251. for terminalCode, list := range updateStatMap {
  252. tmp := new(data_stat.EdbSourceStat)
  253. for k, v := range list {
  254. if k == 0 {
  255. tmp.SourceName = v.SourceName
  256. tmp.Source = v.Source
  257. tmp.TerminalCode = v.TerminalCode
  258. tmp.ModifyTime = nowTime
  259. }
  260. tmp.EdbNum = tmp.EdbNum + 1
  261. if v.IsAdd == 1 {
  262. tmp.EdbNewNum = tmp.EdbNewNum + 1
  263. }
  264. if v.NeedRefresh == 1 {
  265. tmp.NeedRefreshNum = tmp.NeedRefreshNum + 1
  266. }
  267. if v.HasRefresh == 1 {
  268. tmp.HasRefreshNum = tmp.HasRefreshNum + 1
  269. }
  270. if v.SourceUpdateResult == 1 {
  271. tmp.UpdateSuccessNum = tmp.UpdateSuccessNum + 1
  272. } else {
  273. tmp.UpdateFailedNum = tmp.UpdateFailedNum + 1
  274. }
  275. // todo 数据更新成功和更新失败,与刷新成功和刷新失败的含义一致
  276. tmp.DataUpdateSuccessNum = tmp.UpdateSuccessNum
  277. }
  278. // 处理今天删除的指标数量
  279. if dn, ok := delMap[terminalCode]; ok {
  280. tmp.EdbDelNum = dn
  281. }
  282. // 判断是否需要新增还是更新
  283. if exist, ok := statMap[terminalCode]; ok {
  284. tmp.Id = exist.Id
  285. modifyList = append(modifyList, tmp)
  286. } else {
  287. tmp.CreateTime = nowTime
  288. addList = append(addList, tmp)
  289. }
  290. }
  291. //判断当日指标统计数据是否存在,如果存在则更新,不存在则新增
  292. if len(addList) > 0 {
  293. err = logStat.Add(addList)
  294. }
  295. if len(modifyList) > 0 {
  296. err = data_stat.UpdateEdbSourceStatMulti(modifyList)
  297. }
  298. return
  299. }