predict_edb_data_calculate_phase_shift.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. package models
  2. import (
  3. "errors"
  4. "eta/eta_index_lib/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // SavePredictCalculatePhaseShift 期数移位
  12. func SavePredictCalculatePhaseShift(req *EdbInfoCalculateBatchSaveReq, fromEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName, lang string) (edbInfo *EdbInfo, latestDateStr string, latestValue float64, err error) {
  13. o := orm.NewOrm()
  14. to, err := o.Begin()
  15. if err != nil {
  16. return
  17. }
  18. defer func() {
  19. if err != nil {
  20. fmt.Println("SavePredictCalculateTimeShift,Err:" + err.Error())
  21. _ = to.Rollback()
  22. } else {
  23. _ = to.Commit()
  24. }
  25. }()
  26. // 前端那边让后端处理。。。
  27. if req.Frequency == "日度" && req.MoveFrequency == "" {
  28. return nil, "", 0, errors.New("日度指标,移动频率不能为空")
  29. }
  30. switch req.Frequency {
  31. case "周度":
  32. req.MoveFrequency = "周"
  33. case "旬度":
  34. req.MoveFrequency = "旬"
  35. case "月度":
  36. req.MoveFrequency = "月"
  37. case "季度":
  38. req.MoveFrequency = "季"
  39. case "半年度":
  40. req.MoveFrequency = "半年"
  41. case "年度":
  42. req.MoveFrequency = "年"
  43. }
  44. if req.EdbInfoId <= 0 {
  45. edbInfo = new(EdbInfo)
  46. edbInfo.EdbInfoType = 1
  47. edbInfo.Source = utils.DATA_SOURCE_PREDICT_CALCULATE_PHASE_SHIFT
  48. edbInfo.SourceName = "预测期数位移"
  49. edbInfo.EdbCode = edbCode
  50. edbInfo.EdbName = req.EdbName
  51. edbInfo.EdbNameSource = req.EdbName
  52. edbInfo.Frequency = req.Frequency
  53. edbInfo.Unit = req.Unit
  54. edbInfo.ClassifyId = req.ClassifyId
  55. edbInfo.SysUserId = sysUserId
  56. edbInfo.SysUserRealName = sysUserRealName
  57. edbInfo.CreateTime = time.Now()
  58. edbInfo.ModifyTime = time.Now()
  59. edbInfo.UniqueCode = uniqueCode
  60. edbInfo.CalculateFormula = req.Formula
  61. edbInfo.EdbType = 2
  62. edbInfo.MoveType = req.MoveType
  63. edbInfo.MoveFrequency = req.MoveFrequency
  64. edbInfo.EdbNameEn = req.EdbName
  65. edbInfo.UnitEn = req.Unit
  66. edbInfo.Sort = GetAddEdbMaxSortByClassifyId(req.ClassifyId, utils.PREDICT_EDB_INFO_TYPE)
  67. newEdbInfoId, tmpErr := to.Insert(edbInfo)
  68. if tmpErr != nil {
  69. err = tmpErr
  70. return
  71. }
  72. edbInfo.EdbInfoId = int(newEdbInfoId)
  73. //关联关系
  74. {
  75. calculateMappingItem := new(EdbInfoCalculateMapping)
  76. calculateMappingItem.CreateTime = time.Now()
  77. calculateMappingItem.ModifyTime = time.Now()
  78. calculateMappingItem.Sort = 1
  79. calculateMappingItem.EdbCode = edbCode
  80. calculateMappingItem.EdbInfoId = edbInfo.EdbInfoId
  81. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  82. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  83. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  84. calculateMappingItem.FromSource = fromEdbInfo.Source
  85. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  86. calculateMappingItem.FromTag = ""
  87. calculateMappingItem.Source = edbInfo.Source
  88. calculateMappingItem.SourceName = edbInfo.SourceName
  89. _, err = to.Insert(calculateMappingItem)
  90. if err != nil {
  91. return
  92. }
  93. }
  94. } else {
  95. edbInfo, err = GetEdbInfoById(req.EdbInfoId)
  96. if err != nil {
  97. return
  98. }
  99. latestDateStr = edbInfo.LatestDate
  100. latestValue = edbInfo.LatestValue
  101. oldEdbInfo := *edbInfo //旧的指标信息
  102. //修改指标信息
  103. switch lang {
  104. case utils.EnLangVersion:
  105. edbInfo.EdbNameEn = req.EdbName
  106. edbInfo.UnitEn = req.Unit
  107. default:
  108. edbInfo.EdbName = req.EdbName
  109. edbInfo.Unit = req.Unit
  110. edbInfo.EdbNameSource = req.EdbName
  111. }
  112. edbInfo.Frequency = req.Frequency
  113. edbInfo.ClassifyId = req.ClassifyId
  114. edbInfo.MoveType = req.MoveType
  115. edbInfo.MoveFrequency = req.MoveFrequency
  116. edbInfo.CalculateFormula = req.Formula
  117. edbInfo.ModifyTime = time.Now()
  118. _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "MoveType", "MoveFrequency", "CalculateFormula", "ModifyTime", "EdbNameEn", "UnitEn")
  119. if err != nil {
  120. return
  121. }
  122. //判断计算指标是否被更换
  123. var existCondition string
  124. var existPars []interface{}
  125. existCondition += " AND edb_info_id=? AND from_edb_info_id=? "
  126. existPars = append(existPars, edbInfo.EdbInfoId, req.FromEdbInfoId)
  127. var count int
  128. count, err = GetEdbInfoCalculateCountByCondition(existCondition, existPars)
  129. if err != nil {
  130. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  131. return
  132. }
  133. if count <= 0 || req.MoveType != oldEdbInfo.MoveType || req.MoveFrequency != oldEdbInfo.MoveFrequency || req.Formula != oldEdbInfo.CalculateFormula {
  134. //如果是依赖指标变更,那么需要删除对应的关联指标,并添加新的关系
  135. if count <= 0 {
  136. //删除,计算指标关联的,基础指标的关联关系
  137. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
  138. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  139. if err != nil {
  140. err = errors.New("删除计算指标关联关系失败,Err:" + err.Error())
  141. return
  142. }
  143. //关联关系
  144. {
  145. calculateMappingItem := &EdbInfoCalculateMapping{
  146. EdbInfoCalculateMappingId: 0,
  147. EdbInfoId: edbInfo.EdbInfoId,
  148. Source: utils.DATA_SOURCE_PREDICT_CALCULATE_PHASE_SHIFT,
  149. SourceName: "预测期数位移",
  150. EdbCode: edbInfo.EdbCode,
  151. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  152. FromEdbCode: fromEdbInfo.EdbCode,
  153. FromEdbName: fromEdbInfo.EdbName,
  154. FromSource: fromEdbInfo.Source,
  155. FromSourceName: fromEdbInfo.SourceName,
  156. FromTag: "",
  157. Sort: 1,
  158. CreateTime: time.Now(),
  159. ModifyTime: time.Now(),
  160. }
  161. _, err = to.Insert(calculateMappingItem)
  162. if err != nil {
  163. return
  164. }
  165. }
  166. }
  167. //清空原有数据
  168. sql := ` DELETE FROM edb_data_predict_calculate_phase_shift WHERE edb_info_id = ? `
  169. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  170. if err != nil {
  171. return
  172. }
  173. } else {
  174. return
  175. }
  176. }
  177. formulaInt, _ := strconv.Atoi(req.Formula)
  178. //计算数据
  179. latestDateStr, latestValue, err = refreshAllPredictCalculatePhaseShift(to, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, formulaInt, edbInfo.MoveType, fromEdbInfo, edbInfo.EdbCode, "", "", edbInfo.MoveFrequency)
  180. return
  181. }
  182. // RefreshAllPredictCalculatePhaseShift 刷新所有时间移位数据
  183. func RefreshAllPredictCalculatePhaseShift(edbInfoId, source, subSource, formulaInt, moveType int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate, moveFrequency string) (latestDateStr string, latestValue float64, err error) {
  184. o := orm.NewOrm()
  185. to, err := o.Begin()
  186. if err != nil {
  187. return
  188. }
  189. defer func() {
  190. if err != nil {
  191. fmt.Println("RefreshAllPredictCalculatePhaseShift,Err:" + err.Error())
  192. _ = to.Rollback()
  193. } else {
  194. _ = to.Commit()
  195. }
  196. }()
  197. //计算数据
  198. latestDateStr, latestValue, err = refreshAllPredictCalculatePhaseShift(to, edbInfoId, source, subSource, formulaInt, moveType, fromEdbInfo, edbCode, startDate, endDate, moveFrequency)
  199. return
  200. }
  201. // refreshAllPredictCalculatePhaseShift 刷新所有时间移位数据
  202. func refreshAllPredictCalculatePhaseShift(to orm.TxOrmer, edbInfoId, source, subSource, formulaInt, moveType int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate, moveFrequency string) (latestDateStr string, latestValue float64, err error) {
  203. edbInfoIdStr := strconv.Itoa(edbInfoId)
  204. dataList, err := GetPredictEdbDataListAllByStartDate(fromEdbInfo, 0, startDate)
  205. if err != nil {
  206. return
  207. }
  208. var dateArr []string
  209. dataMap := make(map[string]*EdbInfoSearchData)
  210. for _, v := range dataList {
  211. dateArr = append(dateArr, v.DataTime)
  212. dataMap[v.DataTime] = v
  213. }
  214. addSql := ` INSERT INTO edb_data_predict_calculate_phase_shift(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  215. var isAdd bool
  216. resultMap := make(map[string]float64)
  217. dataLen := len(dataList)
  218. var moveNum int
  219. for i := 0; i < dataLen; i++ {
  220. // step_1 如果 领先/滞后 之后时间key存在,将该key为目标key,填充
  221. currentIndex := dataList[i]
  222. // 领先
  223. if moveType != 2 {
  224. periods := dataLen - i + formulaInt - 1
  225. if periods < dataLen {
  226. newIndex := dataList[dataLen-periods-1]
  227. resultMap[newIndex.DataTime] = currentIndex.Value
  228. if strings.Contains(currentIndex.DataTime, fromEdbInfo.LatestDate) {
  229. latestDateStr = newIndex.DataTime
  230. }
  231. } else {
  232. moveNum = formulaInt - i
  233. // 新数据须根据频度补充key
  234. currentDate, _ := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  235. shiftDay := CalculateIntervalDays(moveFrequency, moveNum, currentDate, resultMap, moveType)
  236. var newDate time.Time
  237. if moveFrequency == "年" {
  238. newDate = currentDate.AddDate(moveNum, 0, 0)
  239. } else {
  240. newDate = currentDate.AddDate(0, 0, shiftDay)
  241. }
  242. format := newDate.Format(utils.FormatDate)
  243. if strings.Contains(currentIndex.DataTime, fromEdbInfo.LatestDate) {
  244. latestDateStr = format
  245. }
  246. resultMap[format] = currentIndex.Value
  247. }
  248. } else {
  249. // 滞后
  250. periods := dataLen - i - formulaInt
  251. if periods > 0 {
  252. newIndex := dataList[dataLen-periods]
  253. resultMap[newIndex.DataTime] = currentIndex.Value
  254. if strings.Contains(currentIndex.DataTime, fromEdbInfo.LatestDate) {
  255. latestDateStr = newIndex.DataTime
  256. }
  257. } else {
  258. moveNum = formulaInt + 1 - (dataLen - i)
  259. // 新数据须根据频度补充key
  260. currentDate, _ := time.ParseInLocation(utils.FormatDate, dataList[dataLen-1].DataTime, time.Local)
  261. shiftDay := CalculateIntervalDays(moveFrequency, moveNum, currentDate, resultMap, moveType)
  262. var newDate time.Time
  263. if moveFrequency == "年" {
  264. newDate = currentDate.AddDate(-moveNum, 0, 0)
  265. } else {
  266. newDate = currentDate.AddDate(0, 0, -shiftDay)
  267. }
  268. format := newDate.Format(utils.FormatDate)
  269. resultMap[format] = currentIndex.Value
  270. if strings.Contains(currentIndex.DataTime, fromEdbInfo.LatestDate) {
  271. latestDateStr = format
  272. }
  273. }
  274. }
  275. }
  276. for key, value := range resultMap {
  277. currentDate, _ := time.ParseInLocation(utils.FormatDate, key, time.Local)
  278. timestamp := currentDate.UnixNano() / 1e6
  279. timestampStr := fmt.Sprintf("%d", timestamp)
  280. addSql += GetAddSql(edbInfoIdStr, edbCode, key, timestampStr, fmt.Sprintf("%f", value))
  281. isAdd = true
  282. }
  283. if isAdd {
  284. addSql = strings.TrimRight(addSql, ",")
  285. _, err = to.Raw(addSql).Exec()
  286. if err != nil {
  287. return
  288. }
  289. }
  290. return
  291. }