base_calculate.go 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924
  1. package models
  2. import (
  3. "errors"
  4. "eta_gn/eta_index_lib/utils"
  5. "fmt"
  6. "github.com/shopspring/decimal"
  7. "sort"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // EdbInfoData
  13. // @Description: 指标数据
  14. type EdbInfoData struct {
  15. EdbDataId int `description:"数据ID"`
  16. DataTime time.Time `description:"数据日期"`
  17. Value float64 `description:"数据"`
  18. }
  19. // BaseCalculateBySearchData
  20. // @Description: 基础的计算公式
  21. type BaseCalculateBySearchData struct {
  22. DataList []*EdbInfoSearchData
  23. Frequency string `description:"需要转换的频度"`
  24. Formula interface{}
  25. Calendar string `description:"公历/农历"`
  26. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  27. MoveFrequency string `description:"移动频度"`
  28. FromFrequency string `description:"来源的频度"`
  29. Source int `description:"1:累计值转月;2:累计值转季;3:同比值;4:同差值;5:N数值移动平均数计算;6:环比值;7:环差值;8:升频;9:降频;10:时间移位;11:超季节性;12:年化;13:累计值;14:累计值年初至今;15:指数修匀;16:日均值"`
  30. }
  31. // BaseCalculate
  32. // @Description: 基础的计算公式
  33. type BaseCalculate struct {
  34. DataList []*EdbInfoData
  35. Frequency string `description:"需要转换的频度"`
  36. Formula interface{}
  37. Calendar string `description:"公历/农历"`
  38. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  39. MoveFrequency string `description:"移动频度"`
  40. FromFrequency string `description:"来源的频度"`
  41. Source int `description:"1:累计值转月;2:累计值转季;3:同比值;4:同差值;5:N数值移动平均数计算;6:环比值;7:环差值;8:升频;9:降频;10:时间移位;11:超季节性;12:年化;13:累计值;14:累计值年初至今;15:指数修匀;16:日均值"`
  42. }
  43. type BaseCalculateResp struct {
  44. DataMap map[string]float64
  45. DateList []string
  46. }
  47. // Ljzzy
  48. // @Description: 累计值转月
  49. // @author: Roc
  50. // @receiver obj
  51. // @datetime2023-11-02 18:05:19
  52. // @return dateDataMap map[time.Time]float64
  53. // @return err error
  54. func (obj BaseCalculate) Ljzzy() (dateDataMap map[time.Time]float64, err error, errMsg string) {
  55. dateDataMap = make(map[time.Time]float64)
  56. dataList := obj.DataList // 升序
  57. // 数据处理
  58. yearMap := make(map[int]map[int]*EdbInfoData)
  59. dataLen := len(dataList)
  60. for i := 0; i < dataLen; i++ {
  61. item := dataList[i]
  62. //日其中获取年
  63. itemDate := item.DataTime
  64. year := itemDate.Year()
  65. month := int(itemDate.Month())
  66. if monthMap, yok := yearMap[year]; yok {
  67. monthMap[month] = item
  68. yearMap[year] = monthMap
  69. } else {
  70. monthMap = make(map[int]*EdbInfoData)
  71. monthMap[month] = item
  72. yearMap[year] = monthMap
  73. }
  74. }
  75. // 开始计算
  76. for yk, yv := range yearMap {
  77. _, oneMonthOk := yv[1]
  78. _, twoMonthOk := yv[2]
  79. if !oneMonthOk && !twoMonthOk {
  80. continue
  81. }
  82. for i := 1; i <= 12; i++ {
  83. //fmt.Println(yk, i, yv[i])
  84. dataCurrentItem := yv[i]
  85. if dataCurrentItem == nil {
  86. continue
  87. }
  88. var date time.Time
  89. var val float64
  90. // 一二月单独处理
  91. if i == 1 || i == 2 {
  92. if _, mok := yv[1]; mok { //1月有值
  93. if i == 1 {
  94. date = dataCurrentItem.DataTime
  95. val, _ = decimal.NewFromFloat(dataCurrentItem.Value).Float64() //a.Div(b).Float64()
  96. }
  97. if i == 2 {
  98. dataOneItem := yv[1]
  99. date = dataCurrentItem.DataTime
  100. twoMonth := decimal.NewFromFloat(dataCurrentItem.Value)
  101. oneMonth := decimal.NewFromFloat(dataOneItem.Value)
  102. val, _ = twoMonth.Sub(oneMonth).Float64()
  103. }
  104. } else { //1月无值
  105. dataTwoItem := yv[2]
  106. if i == 1 {
  107. dateStr := strconv.Itoa(yk) + "-01-31"
  108. a := decimal.NewFromFloat(dataTwoItem.Value)
  109. b := decimal.NewFromFloat(2.0)
  110. val, _ = a.Div(b).Float64()
  111. date, err = time.ParseInLocation(utils.FormatDate, dateStr, time.Local)
  112. if err != nil {
  113. return
  114. }
  115. }
  116. if i == 2 {
  117. //1月无值:1月=2月/2 (不管怎样,都要给1月赋值)
  118. {
  119. dateStr := strconv.Itoa(yk) + "-01-31"
  120. a := decimal.NewFromFloat(dataTwoItem.Value)
  121. b := decimal.NewFromFloat(2.0)
  122. val, _ = a.Div(b).Float64()
  123. date, err = time.ParseInLocation(utils.FormatDate, dateStr, time.Local)
  124. if err != nil {
  125. return
  126. }
  127. dateDataMap[date] = val
  128. }
  129. //end 1月无值
  130. // 这是正常二月份的值
  131. date = dataCurrentItem.DataTime
  132. a := decimal.NewFromFloat(dataTwoItem.Value)
  133. b := decimal.NewFromFloat(2.0)
  134. val, _ = a.Div(b).Float64()
  135. }
  136. }
  137. } else {
  138. dataPreItem := yv[i-1]
  139. if dataCurrentItem != nil && dataPreItem != nil {
  140. date = dataCurrentItem.DataTime
  141. //val = dataCurrentItem.Value - dataPreItem.Value
  142. a := decimal.NewFromFloat(dataCurrentItem.Value)
  143. b := decimal.NewFromFloat(dataPreItem.Value)
  144. val, _ = a.Sub(b).Float64()
  145. }
  146. }
  147. // 如果没有日期,那么就退出当前循环,进入下一个循环
  148. if date.IsZero() {
  149. continue
  150. }
  151. dateDataMap[date] = val
  152. }
  153. }
  154. return
  155. }
  156. // Ljzzj
  157. // @Description: 累计值转季度
  158. // @author: Roc
  159. // @receiver obj
  160. // @datetime2023-11-02 18:05:05
  161. // @return dateDataMap map[time.Time]float64
  162. // @return err error
  163. func (obj BaseCalculate) Ljzzj() (dateDataMap map[time.Time]float64, err error, errMsg string) {
  164. dateDataMap = make(map[time.Time]float64)
  165. dataList := obj.DataList // 升序
  166. // 数据处理
  167. yearMap := make(map[int]map[int]*EdbInfoData)
  168. dataLen := len(dataList)
  169. for i := 0; i < dataLen; i++ {
  170. item := dataList[i]
  171. //日其中获取年
  172. itemDate := item.DataTime
  173. year := itemDate.Year()
  174. quarter := utils.MonthQuarterMap[int(itemDate.Month())]
  175. if quarterMap, yok := yearMap[year]; yok {
  176. quarterMap[quarter] = item
  177. yearMap[year] = quarterMap
  178. } else {
  179. quarterMap = make(map[int]*EdbInfoData)
  180. quarterMap[quarter] = item
  181. yearMap[year] = quarterMap
  182. }
  183. }
  184. // 开始计算
  185. for yk, yv := range yearMap {
  186. _, oneQuarterOk := yv[1]
  187. _, twoQuarterOk := yv[2]
  188. if !oneQuarterOk && !twoQuarterOk {
  189. continue
  190. }
  191. for i := 1; i <= 4; i++ {
  192. //fmt.Println(yk, i, yv[i])
  193. dataCurrentItem := yv[i]
  194. if dataCurrentItem == nil {
  195. continue
  196. }
  197. var date time.Time
  198. var val float64
  199. // 一二季度单独处理
  200. if i == 1 || i == 2 {
  201. if _, mok := yv[1]; mok { //第1个季度有值
  202. if i == 1 {
  203. date = dataCurrentItem.DataTime
  204. val, _ = decimal.NewFromFloat(dataCurrentItem.Value).Float64() //a.Div(b).Float64()
  205. }
  206. if i == 2 {
  207. dataOneItem := yv[1]
  208. date = dataCurrentItem.DataTime
  209. twoQuarter := decimal.NewFromFloat(dataCurrentItem.Value)
  210. oneQuarter := decimal.NewFromFloat(dataOneItem.Value)
  211. val, _ = twoQuarter.Sub(oneQuarter).Float64()
  212. }
  213. } else { //第1个季度无值
  214. dataTwoItem := yv[2]
  215. if i == 1 {
  216. dateStr := strconv.Itoa(yk) + "-03-31"
  217. a := decimal.NewFromFloat(dataTwoItem.Value)
  218. b := decimal.NewFromFloat(2.0)
  219. val, _ = a.Div(b).Float64()
  220. date, err = time.ParseInLocation(utils.FormatDate, dateStr, time.Local)
  221. if err != nil {
  222. return
  223. }
  224. }
  225. if i == 2 {
  226. //第1个季度无值:第1个季度=第2个季度/2 (不管怎样,都要给1季度赋值)
  227. {
  228. dateStr := strconv.Itoa(yk) + "-03-31"
  229. a := decimal.NewFromFloat(dataTwoItem.Value)
  230. b := decimal.NewFromFloat(2.0)
  231. val, _ = a.Div(b).Float64()
  232. date, err = time.ParseInLocation(utils.FormatDate, dateStr, time.Local)
  233. if err != nil {
  234. return
  235. }
  236. dateDataMap[date] = val
  237. }
  238. //end 第1个季度无值
  239. date = dataCurrentItem.DataTime
  240. a := decimal.NewFromFloat(dataTwoItem.Value)
  241. b := decimal.NewFromFloat(2.0)
  242. val, _ = a.Div(b).Float64()
  243. }
  244. }
  245. } else {
  246. dataPreItem := yv[i-1]
  247. if dataCurrentItem != nil && dataPreItem != nil {
  248. date = dataCurrentItem.DataTime
  249. //val = dataCurrentItem.Value - dataPreItem.Value
  250. a := decimal.NewFromFloat(dataCurrentItem.Value)
  251. b := decimal.NewFromFloat(dataPreItem.Value)
  252. val, _ = a.Sub(b).Float64()
  253. }
  254. }
  255. // 如果没有日期,那么就退出当前循环,进入下一个循环
  256. if date.IsZero() {
  257. continue
  258. }
  259. dateDataMap[date] = val
  260. }
  261. }
  262. return
  263. }
  264. // Tbz
  265. // @Description: 同比值计算
  266. // @author: Roc
  267. // @receiver obj
  268. // @datetime2023-11-02 18:04:59
  269. // @return dateDataMap map[time.Time]float64
  270. // @return err error
  271. func (obj BaseCalculate) Tbz() (dateDataMap map[time.Time]float64, err error, errMsg string) {
  272. dateDataMap = make(map[time.Time]float64)
  273. dataList := obj.DataList
  274. frequency := obj.FromFrequency
  275. //数据处理
  276. // 数据降序
  277. dataList = reverseSliceByDesc(dataList)
  278. var dateArr []time.Time
  279. dataMap := make(map[string]*EdbInfoData) // 避免因为时间戳导致的日期不对,还是用string来表示比较合适
  280. var firstDataTime time.Time // 第一条数据的日期
  281. for _, v := range dataList {
  282. v.DataTime = time.Date(v.DataTime.Year(), v.DataTime.Month(), v.DataTime.Day(), 0, 0, 0, 0, v.DataTime.Location())
  283. dateArr = append(dateArr, v.DataTime)
  284. dataMap[v.DataTime.Format(utils.FormatDate)] = v
  285. firstDataTime = v.DataTime
  286. }
  287. if firstDataTime.IsZero() {
  288. errMsg = "无数据"
  289. err = errors.New(errMsg)
  290. return
  291. }
  292. firstDataTime = firstDataTime.AddDate(1, 0, 0)
  293. // 开始计算
  294. for _, currentDate := range dateArr {
  295. // 如果当前日期的日期早于数据最早的日期,那么就不往下执行了
  296. if currentDate.Before(firstDataTime) {
  297. break
  298. }
  299. //当前日期
  300. currentDateStr := currentDate.Format(utils.FormatDate)
  301. currentItem, ok := dataMap[currentDateStr]
  302. if !ok {
  303. continue
  304. }
  305. // 找到的数据
  306. var findItem *EdbInfoData
  307. //上一年的日期
  308. preDate := currentDate.AddDate(-1, 0, 0)
  309. preDateStr := preDate.Format(utils.FormatDate)
  310. if findItem, ok = dataMap[preDateStr]; !ok { //上一年同期没找到
  311. if frequency == "月度" { //向上和向下,各找一个月
  312. for i := 0; i <= 35; i++ {
  313. nextDateDay := preDate.AddDate(0, 0, i)
  314. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  315. if findItem, ok = dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  316. break
  317. } else {
  318. preDateDay := preDate.AddDate(0, 0, -i)
  319. preDateDayStr := preDateDay.Format(utils.FormatDate)
  320. if findItem, ok = dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  321. break
  322. }
  323. }
  324. }
  325. } else if frequency == "季度" || frequency == "年度" {
  326. if findItem, ok = dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  327. break
  328. }
  329. } else {
  330. nextDateDay := preDate.AddDate(0, 0, 1)
  331. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  332. preDateDay := preDate.AddDate(0, 0, -1)
  333. preDateDayStr := preDateDay.Format(utils.FormatDate)
  334. for i := 0; i < 35; i++ {
  335. if i >= 1 {
  336. nextDateDay = nextDateDay.AddDate(0, 0, i)
  337. nextDateDayStr = nextDateDay.Format(utils.FormatDate)
  338. }
  339. if findItem, ok = dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  340. break
  341. } else {
  342. if i >= 1 {
  343. preDateDay = preDateDay.AddDate(0, 0, -i)
  344. preDateDayStr = preDateDay.Format(utils.FormatDate)
  345. }
  346. if findItem, ok = dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  347. break
  348. }
  349. }
  350. }
  351. }
  352. }
  353. // 如果没找到数据
  354. if findItem == nil {
  355. continue
  356. }
  357. dateDataMap[currentDate] = tbzDiv(currentItem.Value, findItem.Value)
  358. }
  359. return
  360. }
  361. // tbzDiv
  362. // @Description: 同比值计算
  363. // @author: Roc
  364. // @datetime2023-11-02 16:29:14
  365. // @param a float64
  366. // @param b float64
  367. // @return float64
  368. func tbzDiv(a, b float64) float64 {
  369. var val float64
  370. if b != 0 {
  371. af := decimal.NewFromFloat(a)
  372. bf := decimal.NewFromFloat(b)
  373. val, _ = af.Div(bf).Sub(decimal.NewFromFloat(1)).Round(4).Float64()
  374. }
  375. return val
  376. }
  377. // Tcz
  378. // @Description: 计算同差值
  379. // @author: Roc
  380. // @receiver obj
  381. // @datetime2023-11-02 18:04:51
  382. // @return dateDataMap map[time.Time]float64
  383. // @return err error
  384. func (obj BaseCalculate) Tcz() (dateDataMap map[time.Time]float64, err error, errMsg string) {
  385. dateDataMap = make(map[time.Time]float64)
  386. dataList := obj.DataList // 降序
  387. frequency := obj.FromFrequency
  388. // 数据处理
  389. // 数据降序
  390. dataList = reverseSliceByDesc(dataList)
  391. var dateArr []time.Time
  392. dataMap := make(map[string]*EdbInfoData)
  393. for _, v := range dataList {
  394. v.DataTime = time.Date(v.DataTime.Year(), v.DataTime.Month(), v.DataTime.Day(), 0, 0, 0, 0, v.DataTime.Location())
  395. dateArr = append(dateArr, v.DataTime)
  396. dataMap[v.DataTime.Format(utils.FormatDate)] = v
  397. }
  398. // 开始计算
  399. for _, currentDate := range dateArr {
  400. //当前日期
  401. currentItem, ok := dataMap[currentDate.Format(utils.FormatDate)]
  402. if !ok {
  403. continue
  404. }
  405. // 找到的数据
  406. var findItem *EdbInfoData
  407. //上一年的日期
  408. preDate := currentDate.AddDate(-1, 0, 0)
  409. preDateStr := preDate.Format(utils.FormatDate)
  410. if findItem, ok = dataMap[preDateStr]; !ok {
  411. //上一年同期没找到
  412. if frequency == "月度" { //向上和向下,各找一个月
  413. for i := 0; i <= 35; i++ {
  414. nextDateDay := preDate.AddDate(0, 0, i)
  415. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  416. if findItem, ok = dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  417. break
  418. } else {
  419. preDateDay := preDate.AddDate(0, 0, -i)
  420. preDateDayStr := preDateDay.Format(utils.FormatDate)
  421. if findItem, ok = dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  422. break
  423. }
  424. }
  425. }
  426. } else if frequency == "季度" || frequency == "年度" {
  427. if findItem, ok = dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  428. break
  429. }
  430. } else {
  431. for i := 0; i < 35; i++ {
  432. nextDateDay := preDate.AddDate(0, 0, i)
  433. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  434. if findItem, ok = dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  435. break
  436. } else {
  437. preDateDay := preDate.AddDate(0, 0, -i)
  438. preDateDayStr := preDateDay.Format(utils.FormatDate)
  439. if findItem, ok = dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  440. break
  441. }
  442. }
  443. }
  444. }
  445. }
  446. // 如果没找到数据
  447. if findItem == nil {
  448. continue
  449. }
  450. dateDataMap[currentDate] = tczSub(currentItem.Value, findItem.Value)
  451. }
  452. return
  453. }
  454. // tczSub
  455. // @Description: 计算同差值
  456. // @author: Roc
  457. // @datetime2023-11-02 18:01:46
  458. // @param a float64
  459. // @param b float64
  460. // @return float64
  461. func tczSub(a, b float64) float64 {
  462. af := decimal.NewFromFloat(a)
  463. bf := decimal.NewFromFloat(b)
  464. val, _ := af.Sub(bf).Round(4).Float64()
  465. return val
  466. }
  467. // Nszydpjjs
  468. // @Description: N数值移动平均数计算
  469. // @author: Roc
  470. // @receiver obj
  471. // @datetime2023-11-02 18:17:38
  472. // @return dateDataMap map[time.Time]float64
  473. // @return err error
  474. func (obj BaseCalculate) Nszydpjjs() (dateDataMap map[time.Time]float64, err error, errMsg string) {
  475. dateDataMap = make(map[time.Time]float64)
  476. fromDataList := obj.DataList
  477. nVal := fmt.Sprint(obj.Formula)
  478. formulaInt, err := strconv.Atoi(nVal)
  479. if err != nil {
  480. return
  481. }
  482. // 数据处理
  483. // 数据降序
  484. fromDataList = reverseSliceByDesc(fromDataList)
  485. var fromDateArr []time.Time
  486. fromDataMap := make(map[time.Time]*EdbInfoData)
  487. for _, v := range fromDataList {
  488. fromDateArr = append(fromDateArr, v.DataTime)
  489. fromDataMap[v.DataTime] = v
  490. }
  491. arrLen := len(fromDateArr)
  492. for ak, currentDate := range fromDateArr {
  493. //处理第一个值
  494. var valArr []float64
  495. if findItem, ok := fromDataMap[currentDate]; ok {
  496. valArr = append(valArr, findItem.Value)
  497. } else {
  498. continue
  499. }
  500. if ak+1 != arrLen {
  501. //处理除第一个值之外的N-1个值
  502. for i := 1; i < formulaInt; i++ {
  503. arrIndex := ak + i
  504. if arrIndex >= arrLen {
  505. break
  506. }
  507. arrVal := fromDateArr[arrIndex]
  508. if findItem, ok := fromDataMap[arrVal]; ok {
  509. valArr = append(valArr, findItem.Value)
  510. } else {
  511. continue
  512. }
  513. }
  514. }
  515. valArrLen := len(valArr)
  516. //var totalVal float64
  517. totalVal := decimal.NewFromFloat(0.00)
  518. for _, v := range valArr {
  519. newDecimal := decimal.NewFromFloat(v)
  520. totalVal = totalVal.Add(newDecimal)
  521. }
  522. af := totalVal //decimal.NewFromFloat(totalVal)
  523. bf := decimal.NewFromFloat(float64(valArrLen))
  524. val, _ := af.Div(bf).Round(4).Float64()
  525. dateDataMap[currentDate] = val
  526. }
  527. return
  528. }
  529. // Hbz
  530. // @Description: 环比值计算
  531. // @author: Roc
  532. // @receiver obj
  533. // @datetime2023-11-02 18:28:24
  534. // @return dateDataMap map[time.Time]float64
  535. // @return err error
  536. func (obj BaseCalculate) Hbz() (dateDataMap map[time.Time]float64, err error, errMsg string) {
  537. dateDataMap = make(map[time.Time]float64)
  538. dataList := obj.DataList
  539. nVal := fmt.Sprint(obj.Formula)
  540. formulaInt, err := strconv.Atoi(nVal)
  541. if err != nil {
  542. return
  543. }
  544. // 数据处理
  545. // 数据降序
  546. dataList = reverseSliceByDesc(dataList)
  547. var dateArr []time.Time
  548. for _, v := range dataList {
  549. v.DataTime = time.Date(v.DataTime.Year(), v.DataTime.Month(), v.DataTime.Day(), 0, 0, 0, 0, v.DataTime.Location())
  550. dateArr = append(dateArr, v.DataTime)
  551. }
  552. // 数据计算
  553. dataLen := len(dataList)
  554. //fmt.Println("dataLen:", dataLen)
  555. for i := 0; i < dataLen; i++ {
  556. j := i + formulaInt
  557. if j < dataLen {
  558. //当期
  559. currentItem := dataList[i]
  560. preItem := dataList[j]
  561. // 当期和上期没有数据
  562. if currentItem == nil || preItem == nil {
  563. continue
  564. }
  565. // 开始计算
  566. val, ok := hbzDiv(currentItem.Value, preItem.Value)
  567. // 计算失败
  568. if !ok {
  569. continue
  570. }
  571. dateDataMap[currentItem.DataTime] = val
  572. }
  573. }
  574. return
  575. }
  576. // hbzDiv
  577. // @Description: 环比值计算,current:当期,pre:上期 公式: (当期-上期)/上期
  578. // @author: Roc
  579. // @datetime2023-11-02 18:20:11
  580. // @param current float64
  581. // @param pre float64
  582. // @return val float64
  583. // @return ok bool
  584. func hbzDiv(current, pre float64) (val float64, ok bool) {
  585. if pre == 0 {
  586. return
  587. }
  588. currentVal := decimal.NewFromFloat(current)
  589. preVal := decimal.NewFromFloat(pre)
  590. val, _ = currentVal.Sub(preVal).Div(preVal).Round(4).Float64()
  591. //valStr := decimal.NewFromFloat(val).RoundCeil(4).String() //utils.SubFloatToString(val, 4)
  592. ok = true
  593. return
  594. }
  595. // Hcz
  596. // @Description: 计算环差值数据
  597. // @author: Roc
  598. // @receiver obj
  599. // @datetime2023-11-02 18:33:20
  600. // @return dateDataMap map[time.Time]float64
  601. // @return err error
  602. func (obj BaseCalculate) Hcz() (dateDataMap map[time.Time]float64, err error, errMsg string) {
  603. dateDataMap = make(map[time.Time]float64)
  604. dataList := obj.DataList
  605. nVal := fmt.Sprint(obj.Formula)
  606. formulaInt, err := strconv.Atoi(nVal)
  607. if err != nil {
  608. return
  609. }
  610. // 数据处理
  611. // 数据降序
  612. dataList = reverseSliceByDesc(dataList)
  613. var dateArr []time.Time
  614. for _, v := range dataList {
  615. v.DataTime = time.Date(v.DataTime.Year(), v.DataTime.Month(), v.DataTime.Day(), 0, 0, 0, 0, v.DataTime.Location())
  616. dateArr = append(dateArr, v.DataTime)
  617. }
  618. dataLen := len(dataList)
  619. //fmt.Println("dataLen:", dataLen)
  620. for i := 0; i < dataLen; i++ {
  621. j := i + formulaInt
  622. if j < dataLen {
  623. //当期
  624. currentItem := dataList[i]
  625. preItem := dataList[j]
  626. // 当期和上期没有数据
  627. if currentItem == nil || preItem == nil {
  628. continue
  629. }
  630. // 开始计算
  631. val := hczDiv(currentItem.Value, preItem.Value)
  632. dateDataMap[currentItem.DataTime] = val
  633. }
  634. }
  635. return
  636. }
  637. // hczDiv
  638. // @Description: 环差值计算,current:当期,pre:上期 公式:当期-上期
  639. // @author: Roc
  640. // @datetime2023-11-02 18:33:07
  641. // @param current float64
  642. // @param pre float64
  643. // @return float64
  644. func hczDiv(current, pre float64) float64 {
  645. currentVal := decimal.NewFromFloat(current)
  646. preVal := decimal.NewFromFloat(pre)
  647. val, _ := currentVal.Sub(preVal).Round(4).Float64()
  648. //valStr := decimal.NewFromFloat(val).RoundCeil(4).String() //utils.SubFloatToString(val, 4)
  649. return val
  650. }
  651. // UpFrequency
  652. // @Description: 升频计算
  653. // @author: Roc
  654. // @receiver obj
  655. // @datetime2023-11-02 18:43:03
  656. // @return dateDataMap map[time.Time]float64
  657. // @return err error
  658. func (obj BaseCalculate) UpFrequency() (dateDataMap map[time.Time]float64, err error, errMsg string) {
  659. dateDataMap = make(map[time.Time]float64)
  660. dataList := obj.DataList
  661. // 数据处理
  662. var dateArr []time.Time
  663. dataMap := make(map[time.Time]*EdbInfoData)
  664. fromDataMap := make(map[time.Time]float64)
  665. //来源指指标数据
  666. for _, v := range dataList {
  667. v.DataTime = time.Date(v.DataTime.Year(), v.DataTime.Month(), v.DataTime.Day(), 0, 0, 0, 0, v.DataTime.Location())
  668. dateArr = append(dateArr, v.DataTime)
  669. dataMap[v.DataTime] = v
  670. fromDataMap[v.DataTime] = v.Value
  671. }
  672. if obj.FromFrequency == `日度` {
  673. errMsg = `不能选择日度指标`
  674. err = errors.New(errMsg)
  675. return
  676. }
  677. nowDay := time.Now()
  678. nowDay = time.Date(nowDay.Year(), nowDay.Month(), nowDay.Day(), 0, 0, 0, 0, nowDay.Location())
  679. // 数据计算
  680. dataLen := len(dataList)
  681. lastI := dataLen - 1
  682. for i := 0; i < dataLen; i++ {
  683. //当期
  684. currentItem := dataList[i]
  685. currentDate := currentItem.DataTime
  686. var day int
  687. var preItem *EdbInfoData
  688. var preDate time.Time
  689. // 如果是第一期的数据,那么直接赋值
  690. if i == 0 {
  691. dateDataMap[currentItem.DataTime] = currentItem.Value
  692. continue
  693. }
  694. // 上一期
  695. j := i - 1
  696. preItem = dataList[j]
  697. preDate = preItem.DataTime
  698. day = int(currentDate.Sub(preItem.DataTime).Hours() / float64(24))
  699. //utils.FileLog.Info("preItem.DataTime:" + preItem.DataTime.Format(utils.FormatDate) + ";currentItem.DataTime" + currentItem.DataTime.Format(utils.FormatDate))
  700. //fmt.Println("preItem.DataTime:" + preItem.DataTime.Format(utils.FormatDate) + ";currentItem.DataTime:" + currentItem.DataTime.Format(utils.FormatDate))
  701. for k := 1; k < day; k++ {
  702. needDay := preDate.AddDate(0, 0, k)
  703. dateDataMap[needDay] = preItem.Value
  704. }
  705. dateDataMap[currentItem.DataTime] = currentItem.Value
  706. // 如果是最后一期的数据
  707. if i == lastI {
  708. day = int(nowDay.Sub(currentDate).Hours() / float64(24))
  709. //utils.FileLog.Info("preItem.DataTime:" + currentDate.Format(utils.FormatDate) + ";currentItem.DataTime" + nowDay.Format(utils.FormatDate))
  710. for k := 1; k <= day; k++ {
  711. needDay := currentDate.AddDate(0, 0, k)
  712. dateDataMap[needDay] = currentItem.Value
  713. }
  714. }
  715. }
  716. return
  717. }
  718. // DownFrequency
  719. // @Description: 降频计算
  720. // @author: Roc
  721. // @receiver obj
  722. // @datetime2023-11-02 18:51:26
  723. // @return dateDataMap map[time.Time]float64
  724. // @return err error
  725. func (obj BaseCalculate) DownFrequency() (dateDataMap map[time.Time]float64, err error, errMsg string) {
  726. dateDataMap = make(map[time.Time]float64)
  727. dataList := obj.DataList
  728. edbFrequency := obj.Frequency
  729. formula := obj.Formula.(string)
  730. if !CheckFrequency(obj.FromFrequency, obj.Frequency) {
  731. errMsg = "频度异常,不允许低频降频到高频"
  732. err = errors.New(errMsg)
  733. return
  734. }
  735. // 数据处理
  736. var dateArr []time.Time
  737. dataMap := make(map[time.Time]*EdbInfoData)
  738. fromDataMap := make(map[time.Time]float64)
  739. //来源指指标数据
  740. for _, v := range dataList {
  741. v.DataTime = time.Date(v.DataTime.Year(), v.DataTime.Month(), v.DataTime.Day(), 0, 0, 0, 0, v.DataTime.Location())
  742. dateArr = append(dateArr, v.DataTime)
  743. dataMap[v.DataTime] = v
  744. fromDataMap[v.DataTime] = v.Value
  745. }
  746. // 数据计算
  747. dataLen := len(dataList)
  748. if dataLen <= 0 {
  749. return
  750. }
  751. startDataTime := dataList[0].DataTime
  752. endDataTime := dataList[dataLen-1].DataTime
  753. var lastValue float64 // 最近的值
  754. var nextEndDate time.Time // 下一个节点的日期
  755. weekDayDataList := make([]float64, 0)
  756. for tmpStartDataTime := startDataTime; !tmpStartDataTime.After(endDataTime); tmpStartDataTime = tmpStartDataTime.AddDate(0, 0, 1) {
  757. // 将当前数据加入到 weekDayDataList
  758. if tmpData, ok := dataMap[tmpStartDataTime]; ok {
  759. tmpValue := decimal.NewFromFloat(tmpData.Value)
  760. tmpValueFloat, _ := tmpValue.Round(4).Float64()
  761. weekDayDataList = append(weekDayDataList, tmpValueFloat)
  762. }
  763. // 如果下个节点的日期不存在,那么就先给赋值(兼容时间区间内只有一组数据的情况)
  764. if nextEndDate.IsZero() {
  765. nextEndDate = utils.GetFrequencyEndDay(tmpStartDataTime, edbFrequency)
  766. }
  767. // 日期处理过滤
  768. switch edbFrequency {
  769. case "周度":
  770. if tmpStartDataTime.Weekday() != 5 {
  771. //不是周五,代表需要进入下一个循环获取数据并计算
  772. continue
  773. } else {
  774. //记录下一个结束节点的日期
  775. nextEndDate = tmpStartDataTime.AddDate(0, 0, 7)
  776. }
  777. case "旬度":
  778. nextDay := tmpStartDataTime.AddDate(0, 0, 1)
  779. if nextDay.Day() != 1 && nextDay.Day() != 11 && nextDay.Day() != 21 {
  780. //不是每月10、20、最后一天,代表需要进入下一个循环获取数据并计算
  781. continue
  782. } else {
  783. //记录下一个结束节点的日期
  784. if nextDay.Day() == 1 || nextDay.Day() == 11 {
  785. //月初或者月末的时候,加10天就好了
  786. nextEndDate = nextDay.AddDate(0, 0, 9)
  787. } else {
  788. tmpNextMonth := nextDay.AddDate(0, 1, 0)
  789. nextEndDate = time.Date(tmpNextMonth.Year(), tmpNextMonth.Month(), 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  790. }
  791. }
  792. case "月度":
  793. nextDay := tmpStartDataTime.AddDate(0, 0, 1)
  794. if nextDay.Day() != 1 {
  795. //不是每月最后一天,代表需要进入下一个循环获取数据并计算
  796. continue
  797. } else {
  798. //记录下一个结束节点的日期
  799. nextEndDate = nextDay.AddDate(0, 1, -1)
  800. }
  801. case "季度":
  802. nextDay := tmpStartDataTime.AddDate(0, 0, 1)
  803. if (nextDay.Month() == 1 || nextDay.Month() == 4 || nextDay.Month() == 7 || nextDay.Month() == 10) && nextDay.Day() == 1 {
  804. //记录下一个结束节点的日期
  805. nextEndDate = nextDay.AddDate(0, 3, -1)
  806. } else {
  807. //不是3,6,9,12 月份的最后一天,代表需要进入下一个循环获取数据并计算
  808. continue
  809. }
  810. case "年度":
  811. if tmpStartDataTime.Month() == 12 && tmpStartDataTime.Day() == 31 {
  812. //记录下一个结束节点的日期
  813. nextEndDate = tmpStartDataTime.AddDate(1, 0, 0)
  814. } else {
  815. //不是每年的12-31日,代表需要进入下一个循环获取数据并计算
  816. continue
  817. }
  818. default:
  819. err = errors.New("错误的频度:" + edbFrequency)
  820. return
  821. }
  822. // 当前时间段内的数据计算,得出实际值
  823. var currVal float64
  824. lenWeekDayDataList := len(weekDayDataList)
  825. // 如果这个时间区间内没有数据,那么就采用上一个时间区间的值
  826. if len(weekDayDataList) <= 0 {
  827. currVal = lastValue
  828. } else {
  829. if formula == "期末值" {
  830. currVal = weekDayDataList[lenWeekDayDataList-1]
  831. } else {
  832. // 平均值
  833. sumValDeci := decimal.NewFromFloat(0)
  834. for _, v := range weekDayDataList {
  835. tmpValDeci := decimal.NewFromFloat(v)
  836. sumValDeci = sumValDeci.Add(tmpValDeci)
  837. }
  838. lenDeci := decimal.NewFromInt(int64(lenWeekDayDataList))
  839. currVal, _ = sumValDeci.Div(lenDeci).Round(4).Float64()
  840. }
  841. }
  842. // 赋值
  843. dateDataMap[tmpStartDataTime] = currVal
  844. // 一轮结束后,数据清空
  845. weekDayDataList = make([]float64, 0)
  846. }
  847. // 最后已有的日期处理完成后,需要对剩余不在时间段内的数据做处理
  848. if len(weekDayDataList) > 0 {
  849. // 当前时间段内的数据计算,得出实际值
  850. var currVal float64
  851. lenWeekDayDataList := len(weekDayDataList)
  852. // 如果这个时间区间内没有数据,那么就采用上一个时间区间的值
  853. if len(weekDayDataList) < 0 {
  854. currVal = lastValue
  855. } else {
  856. if formula == "期末值" {
  857. currVal = weekDayDataList[lenWeekDayDataList-1]
  858. } else {
  859. // 平均值
  860. sumValDeci := decimal.NewFromFloat(0)
  861. for _, v := range weekDayDataList {
  862. tmpValDeci := decimal.NewFromFloat(v)
  863. sumValDeci = sumValDeci.Add(tmpValDeci)
  864. }
  865. lenDeci := decimal.NewFromInt(int64(lenWeekDayDataList))
  866. currVal, _ = sumValDeci.Div(lenDeci).Round(4).Float64()
  867. }
  868. }
  869. // 赋值
  870. dateDataMap[nextEndDate] = currVal
  871. }
  872. return
  873. }
  874. // TimeShift
  875. // @Description: 时间移位计算
  876. // @author: Roc
  877. // @receiver obj
  878. // @datetime2023-11-03 13:19:07
  879. // @return dateDataMap map[time.Time]float64
  880. // @return err error
  881. func (obj BaseCalculate) TimeShift() (dateDataMap map[time.Time]float64, err error, errMsg string) {
  882. dateDataMap = make(map[time.Time]float64)
  883. dataList := obj.DataList
  884. nVal := fmt.Sprint(obj.Formula)
  885. formulaInt, err := strconv.Atoi(nVal)
  886. if err != nil {
  887. return
  888. }
  889. moveType := obj.MoveType
  890. moveFrequency := obj.MoveFrequency
  891. // 数据处理
  892. var dateArr []time.Time
  893. dataMap := make(map[time.Time]*EdbInfoData)
  894. for _, v := range dataList {
  895. v.DataTime = time.Date(v.DataTime.Year(), v.DataTime.Month(), v.DataTime.Day(), 0, 0, 0, 0, v.DataTime.Location())
  896. dateArr = append(dateArr, v.DataTime)
  897. dataMap[v.DataTime] = v
  898. }
  899. var shiftDay int
  900. switch moveFrequency {
  901. case "天":
  902. shiftDay = formulaInt
  903. case "周":
  904. shiftDay = formulaInt * 7
  905. case "月":
  906. shiftDay = formulaInt * 30
  907. case "季":
  908. shiftDay = formulaInt * 90
  909. case "年":
  910. shiftDay = formulaInt * 365
  911. default:
  912. shiftDay = formulaInt
  913. }
  914. if moveType == 2 {
  915. shiftDay = -shiftDay
  916. }
  917. dataLen := len(dataList)
  918. for i := 0; i < dataLen; i++ {
  919. //当期
  920. currentItem := dataList[i]
  921. currentDate := currentItem.DataTime
  922. newDate := currentDate.AddDate(0, 0, shiftDay)
  923. val, _ := decimal.NewFromFloat(currentItem.Value).Round(4).Float64()
  924. dateDataMap[newDate] = val
  925. }
  926. return
  927. }
  928. // Cjjx
  929. // @Description: 超季节性计算
  930. // @author: Roc
  931. // @receiver obj
  932. // @datetime2023-11-03 13:28:23
  933. // @return dateDataMap map[time.Time]float64
  934. // @return err error
  935. func (obj BaseCalculate) Cjjx() (dateDataMap map[time.Time]float64, err error, errMsg string) {
  936. dateDataMap = make(map[time.Time]float64)
  937. dataList := obj.DataList
  938. nVal := fmt.Sprint(obj.Formula)
  939. formulaInt, err := strconv.Atoi(nVal)
  940. if err != nil {
  941. return
  942. }
  943. calendar := obj.Calendar
  944. // 数据处理
  945. var dateArr []time.Time
  946. dataMap := make(map[time.Time]*EdbInfoData)
  947. for _, v := range dataList {
  948. v.DataTime = time.Date(v.DataTime.Year(), v.DataTime.Month(), v.DataTime.Day(), 0, 0, 0, 0, v.DataTime.Location())
  949. dateArr = append(dateArr, v.DataTime)
  950. dataMap[v.DataTime] = v
  951. }
  952. // 通过插值法补全所有数据(包含周末)
  953. handleDataMap := make(map[time.Time]float64)
  954. _, err = HandleDataByLinearRegressionByTime(dataList, handleDataMap)
  955. if err != nil {
  956. return
  957. }
  958. // 每个年份的日期数据需要平移的天数
  959. moveDayMap := make(map[int]int, 0) // 每个年份的春节公历
  960. var lastDataDay time.Time
  961. if len(dataList) > 0 {
  962. lastDataDay = dataList[0].DataTime
  963. }
  964. // 数据计算
  965. for _, currentDate := range dateArr {
  966. // 如果遇到闰二月,如2.29,去掉该天数据
  967. if strings.Contains(currentDate.Format(utils.FormatDate), "02-29") {
  968. continue
  969. }
  970. //农历的超季节性运算,只计算11月--次年5月,分段计算,与数据区间和N数值有关
  971. if calendar == "农历" && currentDate.Month() > 5 && currentDate.Month() < 11 {
  972. continue
  973. }
  974. currentItem, ok := dataMap[currentDate]
  975. // 找不到数据就退出当前循环,进入下一循环
  976. if !ok {
  977. continue
  978. }
  979. pastValueList := make([]float64, 0) // 过去几期的数据
  980. pastValueList = append(pastValueList, currentItem.Value) //当前日期
  981. for i := 1; i < formulaInt; i++ {
  982. //前几年当天公历的日期
  983. historyPreDate := currentDate.AddDate(-i, 0, 0)
  984. moveDay := 0
  985. if calendar == "农历" {
  986. if tmpMoveDay, ok := moveDayMap[historyPreDate.Year()]; !ok {
  987. moveDay, err = getMoveDay(lastDataDay, historyPreDate)
  988. if err != nil {
  989. return
  990. }
  991. } else {
  992. moveDay = tmpMoveDay
  993. }
  994. // 移动天数到对应农历 的 公历 日期
  995. historyPreDate = historyPreDate.AddDate(0, 0, moveDay)
  996. }
  997. if tmpValue, ok := handleDataMap[historyPreDate]; ok { //上一年同期找到
  998. pastValueList = append(pastValueList, tmpValue)
  999. }
  1000. }
  1001. if len(pastValueList) == formulaInt {
  1002. val, ok := cjjxSub(currentItem.Value, pastValueList)
  1003. if !ok {
  1004. continue
  1005. }
  1006. dateDataMap[currentDate] = val
  1007. }
  1008. }
  1009. return
  1010. }
  1011. // cjjxSub
  1012. // @Description: 超季节性计算
  1013. // @author: Roc
  1014. // @datetime2023-11-03 13:25:49
  1015. // @param currValue float64
  1016. // @param pastValue []float64
  1017. // @return value float64
  1018. // @return ok bool
  1019. func cjjxSub(currValue float64, pastValue []float64) (value float64, ok bool) {
  1020. num := len(pastValue)
  1021. if num == 0 {
  1022. return
  1023. }
  1024. numDecimal := decimal.NewFromInt(int64(num))
  1025. af := decimal.NewFromFloat(currValue)
  1026. bf := decimal.NewFromFloat(pastValue[0])
  1027. for k := 1; k < num; k++ {
  1028. tmpVal := decimal.NewFromFloat(pastValue[k])
  1029. bf = bf.Add(tmpVal)
  1030. }
  1031. value, _ = af.Sub(bf.Div(numDecimal)).Round(4).Float64()
  1032. ok = true
  1033. return
  1034. }
  1035. // Annualized
  1036. // @Description: 年化计算
  1037. // @author: Roc
  1038. // @receiver obj
  1039. // @datetime2023-11-03 13:34:32
  1040. // @param to orm.TxOrmer
  1041. // @param edbInfoId int
  1042. // @param source int
  1043. // @param fromEdbInfo *EdbInfo
  1044. // @param edbCode string
  1045. // @return dateDataMap map[time.Time]float64
  1046. // @return err error
  1047. func (obj BaseCalculate) Annualized() (dateDataMap map[time.Time]float64, err error, errMsg string) {
  1048. dateDataMap = make(map[time.Time]float64)
  1049. fromDataList := obj.DataList
  1050. lenFromDataList := len(fromDataList)
  1051. // 如果来源指标没有数据,那么就直接返回得了
  1052. if lenFromDataList <= 0 {
  1053. return
  1054. }
  1055. // 数据处理
  1056. // 插值法数据处理
  1057. handleDataMap := make(map[time.Time]float64)
  1058. _, err = HandleDataByLinearRegressionByTime(fromDataList, handleDataMap)
  1059. if err != nil {
  1060. return
  1061. }
  1062. // 每年的最后一天的数据值
  1063. yearLastValMap := make(map[int]float64)
  1064. startDataTime := fromDataList[0].DataTime
  1065. endDataTime := fromDataList[lenFromDataList-1].DataTime
  1066. for i := startDataTime.Year(); i <= endDataTime.Year(); i++ {
  1067. tmpDate := time.Date(i, 12, 31, startDataTime.Hour(), startDataTime.Minute(), startDataTime.Second(), startDataTime.Nanosecond(), time.Local)
  1068. if tmpVal, ok := handleDataMap[tmpDate]; ok {
  1069. yearLastValMap[i] = tmpVal
  1070. }
  1071. }
  1072. // 数据计算
  1073. // 遍历来源指标数据
  1074. for _, v := range fromDataList {
  1075. currDate := v.DataTime
  1076. perValMap := make(map[time.Time]float64)
  1077. //前3年当日的数据
  1078. for i := 1; i <= 3; i++ {
  1079. tmpDateTime := currDate.AddDate(-i, 0, 0)
  1080. if tmpVal, ok := handleDataMap[tmpDateTime]; ok {
  1081. perValMap[tmpDateTime] = tmpVal
  1082. }
  1083. }
  1084. lenPerValMap := len(perValMap)
  1085. // 如果数据少于2年,那么就不参与计算,结束当前循环,进入下一个循环
  1086. if lenPerValMap < 2 {
  1087. continue
  1088. }
  1089. // N年 当前值占全年比重 的值列表
  1090. divValList := make([]decimal.Decimal, 0)
  1091. for tmpDateTime, tmpVal := range perValMap {
  1092. yearLastVal, ok2 := yearLastValMap[tmpDateTime.Year()]
  1093. // 如果当年最后一天没有数据
  1094. if !ok2 {
  1095. continue
  1096. }
  1097. // 当前值占全年比重
  1098. tmpYearLastVal := decimal.NewFromFloat(yearLastVal)
  1099. if tmpYearLastVal.IsZero() { //如果是0,那么就退出当前循环,进入下一个循环
  1100. continue
  1101. }
  1102. divVal := decimal.NewFromFloat(tmpVal).Div(tmpYearLastVal)
  1103. divValList = append(divValList, divVal)
  1104. }
  1105. lenDivValList := len(divValList)
  1106. // 如果 N年 当前值占全年比重 的值 小于 2个,那么就不参与计算,结束当前循环,进入下一个循环
  1107. if lenDivValList < 2 {
  1108. continue
  1109. }
  1110. divValSum := decimal.NewFromFloat(0)
  1111. for _, divVal := range divValList {
  1112. divValSum = divValSum.Add(divVal)
  1113. }
  1114. // 当前计算出来的结果
  1115. tmpDivVal := divValSum.Div(decimal.NewFromInt(int64(lenDivValList)))
  1116. if tmpDivVal.IsZero() { //如果是0,那么就退出当前循环,进入下一个循环
  1117. continue
  1118. }
  1119. currVal, _ := decimal.NewFromFloat(v.Value).Div(tmpDivVal).Round(4).Float64()
  1120. dateDataMap[currDate] = currVal
  1121. }
  1122. return
  1123. }
  1124. // Ljz
  1125. // @Description: 累计值
  1126. // @author: Roc
  1127. // @receiver obj
  1128. // @datetime2023-11-03 13:49:17
  1129. // @return dateDataMap map[time.Time]float64
  1130. // @return err error
  1131. func (obj BaseCalculate) Ljz() (dateDataMap map[time.Time]float64, err error, errMsg string) {
  1132. dateDataMap = make(map[time.Time]float64)
  1133. dataList := obj.DataList
  1134. frequency := obj.Frequency //需要变更的频度
  1135. fromFrequency := obj.FromFrequency //来源的频度
  1136. // 数据处理
  1137. var isWeekData bool // 是否周度数据,如果是周度数据的话,是需要变频的,最后结果还需要除以7
  1138. // 周度数据需要先变成日度的
  1139. if fromFrequency == `周度` {
  1140. isWeekData = true
  1141. }
  1142. fromEdbDataMap := make(map[time.Time]float64)
  1143. if isWeekData {
  1144. dataList, err = HandleDataByLinearRegressionByTime(dataList, fromEdbDataMap)
  1145. if err != nil {
  1146. return
  1147. }
  1148. }
  1149. //日度转周度:日期选周五,计算上周六到本周五的日度值的加总,最新日期为最新值对应的周五。
  1150. //日度转月度:日期选每个月最后一天,计算当月所有日度值的加总,最新日期为最新值对应当月最后一天。
  1151. //日度转季度、年度:方法类似转月度。
  1152. //周度转月度/季度/年度:将周度值转成日度,空值用插值法插值,计算当月/当季/当年所有值的加总,然后除以7。
  1153. //月度转季度/年度: 当季/当年月度值相加。
  1154. dateList := make([]time.Time, 0)
  1155. valueMap := make(map[time.Time]float64)
  1156. switch frequency {
  1157. case "年度":
  1158. yearMap := make(map[int]float64)
  1159. yearList := make([]int, 0)
  1160. for _, item := range dataList {
  1161. item.DataTime = time.Date(item.DataTime.Year(), item.DataTime.Month(), item.DataTime.Day(), 0, 0, 0, 0, item.DataTime.Location())
  1162. year := item.DataTime.Year()
  1163. yearVal, ok := yearMap[year]
  1164. if ok {
  1165. yearMap[year] = item.Value + yearVal
  1166. } else {
  1167. yearList = append(yearList, year)
  1168. yearMap[year] = item.Value
  1169. }
  1170. }
  1171. for _, v := range yearList {
  1172. currTime := time.Date(v, 12, 31, 0, 0, 0, 0, time.Local)
  1173. dateList = append(dateList, currTime)
  1174. valueMap[currTime] = yearMap[v]
  1175. }
  1176. case "半年度":
  1177. yearMonthMap := make(map[string]float64)
  1178. yearMonthList := make([]string, 0)
  1179. for _, item := range dataList {
  1180. item.DataTime = time.Date(item.DataTime.Year(), item.DataTime.Month(), item.DataTime.Day(), 0, 0, 0, 0, item.DataTime.Location())
  1181. itemDate := item.DataTime
  1182. year := itemDate.Year()
  1183. var tmpK string
  1184. if itemDate.Month() <= 6 {
  1185. tmpK = fmt.Sprint(year, "06")
  1186. } else {
  1187. tmpK = fmt.Sprint(year, "12")
  1188. }
  1189. yearVal, ok := yearMonthMap[tmpK]
  1190. if ok {
  1191. yearMonthMap[tmpK] = item.Value + yearVal
  1192. } else {
  1193. yearMonthList = append(yearMonthList, tmpK)
  1194. yearMonthMap[tmpK] = item.Value
  1195. }
  1196. }
  1197. for _, v := range yearMonthList {
  1198. currTime, tmpErr := time.ParseInLocation(utils.FormatYearMonthUnSpace, v, time.Local)
  1199. if tmpErr != nil {
  1200. err = tmpErr
  1201. return
  1202. }
  1203. currTime = currTime.AddDate(0, 1, -1)
  1204. dateList = append(dateList, currTime)
  1205. valueMap[currTime] = yearMonthMap[v]
  1206. }
  1207. case "季度":
  1208. yearMonthMap := make(map[string]float64)
  1209. yearMonthList := make([]string, 0)
  1210. for _, item := range dataList {
  1211. item.DataTime = time.Date(item.DataTime.Year(), item.DataTime.Month(), item.DataTime.Day(), 0, 0, 0, 0, item.DataTime.Location())
  1212. itemDate := item.DataTime
  1213. year := itemDate.Year()
  1214. var tmpK string
  1215. if itemDate.Month() <= 3 {
  1216. tmpK = fmt.Sprint(year, "03")
  1217. } else if itemDate.Month() <= 6 {
  1218. tmpK = fmt.Sprint(year, "06")
  1219. } else if itemDate.Month() <= 9 {
  1220. tmpK = fmt.Sprint(year, "09")
  1221. } else {
  1222. tmpK = fmt.Sprint(year, "12")
  1223. }
  1224. yearVal, ok := yearMonthMap[tmpK]
  1225. if ok {
  1226. yearMonthMap[tmpK] = item.Value + yearVal
  1227. } else {
  1228. yearMonthList = append(yearMonthList, tmpK)
  1229. yearMonthMap[tmpK] = item.Value
  1230. }
  1231. }
  1232. for _, v := range yearMonthList {
  1233. currTime, tmpErr := time.ParseInLocation(utils.FormatYearMonthUnSpace, v, time.Local)
  1234. if tmpErr != nil {
  1235. err = tmpErr
  1236. return
  1237. }
  1238. currTime = currTime.AddDate(0, 1, -1)
  1239. dateList = append(dateList, currTime)
  1240. valueMap[currTime] = yearMonthMap[v]
  1241. }
  1242. case "月度":
  1243. yearMonthMap := make(map[string]float64)
  1244. yearMonthList := make([]string, 0)
  1245. for _, item := range dataList {
  1246. item.DataTime = time.Date(item.DataTime.Year(), item.DataTime.Month(), item.DataTime.Day(), 0, 0, 0, 0, item.DataTime.Location())
  1247. itemDate := item.DataTime
  1248. year := itemDate.Year()
  1249. var tmpK string
  1250. tmpK = fmt.Sprint(year*100 + int(itemDate.Month()))
  1251. yearVal, ok := yearMonthMap[tmpK]
  1252. if ok {
  1253. yearMonthMap[tmpK] = item.Value + yearVal
  1254. } else {
  1255. yearMonthList = append(yearMonthList, tmpK)
  1256. yearMonthMap[tmpK] = item.Value
  1257. }
  1258. }
  1259. for _, v := range yearMonthList {
  1260. currTime, tmpErr := time.ParseInLocation(utils.FormatYearMonthUnSpace, v, time.Local)
  1261. if tmpErr != nil {
  1262. err = tmpErr
  1263. return
  1264. }
  1265. currTime = currTime.AddDate(0, 1, -1)
  1266. dateList = append(dateList, currTime)
  1267. valueMap[currTime] = yearMonthMap[v]
  1268. }
  1269. case "旬度":
  1270. tmpDateDataMap := make(map[time.Time]float64)
  1271. tmpDateList := make([]time.Time, 0)
  1272. for _, item := range dataList {
  1273. item.DataTime = time.Date(item.DataTime.Year(), item.DataTime.Month(), item.DataTime.Day(), 0, 0, 0, 0, item.DataTime.Location())
  1274. itemDate := item.DataTime
  1275. dayInt := itemDate.Year()*100 + int(itemDate.Month())
  1276. var currTime time.Time
  1277. if itemDate.Day() <= 10 {
  1278. //本月上旬
  1279. tmpK := fmt.Sprint(dayInt, "10")
  1280. currTime, err = time.ParseInLocation(utils.FormatDateUnSpace, tmpK, time.Local)
  1281. if err != nil {
  1282. return
  1283. }
  1284. } else if itemDate.Day() <= 20 {
  1285. // 本月中旬
  1286. tmpK := fmt.Sprint(dayInt, "20")
  1287. currTime, err = time.ParseInLocation(utils.FormatDateUnSpace, tmpK, time.Local)
  1288. if err != nil {
  1289. return
  1290. }
  1291. } else {
  1292. // 本月下旬
  1293. currTime, err = time.ParseInLocation(utils.FormatYearMonthUnSpace, fmt.Sprint(dayInt), time.Local)
  1294. if err != nil {
  1295. return
  1296. }
  1297. currTime = currTime.AddDate(0, 1, -1)
  1298. }
  1299. yearVal, ok := tmpDateDataMap[currTime]
  1300. if ok {
  1301. tmpDateDataMap[currTime] = item.Value + yearVal
  1302. } else {
  1303. tmpDateList = append(tmpDateList, currTime)
  1304. tmpDateDataMap[currTime] = item.Value
  1305. }
  1306. }
  1307. for _, currTime := range tmpDateList {
  1308. dateList = append(dateList, currTime)
  1309. valueMap[currTime] = tmpDateDataMap[currTime]
  1310. }
  1311. case "周度":
  1312. tmpDateDataMap := make(map[time.Time]float64)
  1313. tmpDateList := make([]time.Time, 0)
  1314. for _, item := range dataList {
  1315. item.DataTime = time.Date(item.DataTime.Year(), item.DataTime.Month(), item.DataTime.Day(), 0, 0, 0, 0, item.DataTime.Location())
  1316. itemDate := item.DataTime
  1317. var currTime time.Time
  1318. // 周六周日,这是下一个周五的数据
  1319. if itemDate.Weekday() == 0 {
  1320. currTime = itemDate.AddDate(0, 0, 5)
  1321. } else if itemDate.Weekday() == 6 {
  1322. currTime = itemDate.AddDate(0, 0, 6)
  1323. } else {
  1324. currTime = itemDate.AddDate(0, 0, 5-int(itemDate.Weekday()))
  1325. }
  1326. yearVal, ok := tmpDateDataMap[currTime]
  1327. if ok {
  1328. tmpDateDataMap[currTime] = item.Value + yearVal
  1329. } else {
  1330. tmpDateList = append(tmpDateList, currTime)
  1331. tmpDateDataMap[currTime] = item.Value
  1332. }
  1333. }
  1334. for _, currTime := range tmpDateList {
  1335. dateList = append(dateList, currTime)
  1336. valueMap[currTime] = tmpDateDataMap[currTime]
  1337. }
  1338. default:
  1339. err = errors.New("错误的频度")
  1340. return
  1341. }
  1342. // 数据计算
  1343. for _, currTime := range dateList {
  1344. tmpVal, ok2 := valueMap[currTime]
  1345. if !ok2 {
  1346. err = errors.New("数据异常,date:" + currTime.Format(utils.FormatDate))
  1347. return
  1348. }
  1349. var saveValue float64
  1350. if isWeekData { //周度指标转的话,最后结果要除以7
  1351. saveValue, _ = decimal.NewFromFloat(tmpVal).Div(decimal.NewFromInt(7)).Round(4).Float64()
  1352. } else {
  1353. saveValue, _ = decimal.NewFromFloat(tmpVal).Round(4).Float64()
  1354. }
  1355. dateDataMap[currTime] = saveValue
  1356. }
  1357. return
  1358. }
  1359. // LjzNczj
  1360. // @Description:年初至今累计值计算
  1361. // @author: Roc
  1362. // @receiver obj
  1363. // @datetime2023-11-03 13:55:44
  1364. // @return dateDataMap map[time.Time]float64
  1365. // @return err error
  1366. func (obj BaseCalculate) LjzNczj() (dateDataMap map[time.Time]float64, err error, errMsg string) {
  1367. dateDataMap = make(map[time.Time]float64)
  1368. dataList := obj.DataList
  1369. frequency := obj.Frequency //需要变更的频度
  1370. fromFrequency := obj.FromFrequency //来源的频度
  1371. // 数据处理
  1372. var isWeekData bool // 是否周度数据,如果是周度数据的话,是需要变频的,最后结果还需要除以7
  1373. // 周度数据需要先变成日度的
  1374. if fromFrequency == `周度` {
  1375. isWeekData = true
  1376. }
  1377. fromEdbDataMap := make(map[time.Time]float64)
  1378. if isWeekData {
  1379. dataList, err = HandleDataByLinearRegressionByTime(dataList, fromEdbDataMap)
  1380. if err != nil {
  1381. return
  1382. }
  1383. }
  1384. //日度数据年初至今:日期同原日度数据。将每年1月1日(含)到日度数据所在日期含间的日度值,进行加总。
  1385. //周度数据年初至今:日期同原周度数据。将周度值转成日度频率,空值用插值法插值,然后算法同日度年度至今,再除以7
  1386. //月度/季度数据年初至今:日期同原月度/季度数据,将每年1月1日(含)到月度数据所在日期(含)之间的月度/季度值,进行加总
  1387. //以此类推
  1388. dateList := make([]time.Time, 0)
  1389. valueMap := make(map[time.Time]float64)
  1390. yearMap := make(map[int]float64)
  1391. switch frequency {
  1392. case "周度":
  1393. tmpDateDataMap := make(map[time.Time]float64)
  1394. tmpDateList := make([]time.Time, 0)
  1395. for _, item := range dataList {
  1396. item.DataTime = time.Date(item.DataTime.Year(), item.DataTime.Month(), item.DataTime.Day(), 0, 0, 0, 0, item.DataTime.Location())
  1397. itemDate := item.DataTime
  1398. var currTime time.Time
  1399. // 周六周日,这是下一个周五的数据
  1400. if itemDate.Weekday() == 0 {
  1401. currTime = itemDate.AddDate(0, 0, 5)
  1402. } else if itemDate.Weekday() == 6 {
  1403. currTime = itemDate.AddDate(0, 0, 6)
  1404. } else {
  1405. currTime = itemDate.AddDate(0, 0, 5-int(itemDate.Weekday()))
  1406. }
  1407. year := itemDate.Year()
  1408. yearVal, ok := yearMap[year]
  1409. if ok {
  1410. yearMap[year] = item.Value + yearVal
  1411. } else {
  1412. yearMap[year] = item.Value
  1413. }
  1414. if itemDate.Equal(currTime) {
  1415. tmpDateDataMap[itemDate] = yearMap[year]
  1416. tmpDateList = append(tmpDateList, itemDate)
  1417. }
  1418. }
  1419. for _, currTime := range tmpDateList {
  1420. dateList = append(dateList, currTime)
  1421. valueMap[currTime] = tmpDateDataMap[currTime]
  1422. }
  1423. default:
  1424. for _, item := range dataList {
  1425. item.DataTime = time.Date(item.DataTime.Year(), item.DataTime.Month(), item.DataTime.Day(), 0, 0, 0, 0, item.DataTime.Location())
  1426. itemDate := item.DataTime
  1427. year := itemDate.Year()
  1428. yearVal, ok := yearMap[year]
  1429. if ok {
  1430. yearMap[year] = item.Value + yearVal
  1431. } else {
  1432. yearMap[year] = item.Value
  1433. }
  1434. valueMap[itemDate] = yearMap[year]
  1435. dateList = append(dateList, itemDate)
  1436. }
  1437. }
  1438. // 数据计算
  1439. for _, currTime := range dateList {
  1440. tmpVal, ok2 := valueMap[currTime]
  1441. if !ok2 {
  1442. err = errors.New("数据异常,date:" + currTime.Format(utils.FormatDate))
  1443. return
  1444. }
  1445. var saveValue float64
  1446. if isWeekData { //周度指标转的话,最后结果要除以7
  1447. saveValue, _ = decimal.NewFromFloat(tmpVal).Div(decimal.NewFromInt(7)).Round(4).Float64()
  1448. } else {
  1449. saveValue, _ = decimal.NewFromFloat(tmpVal).Round(4).Float64()
  1450. }
  1451. dateDataMap[currTime] = saveValue
  1452. }
  1453. return
  1454. }
  1455. // ExponentialSmoothing
  1456. // @Description: 指数修匀计算
  1457. // @author: Roc
  1458. // @receiver obj
  1459. // @datetime2023-11-03 14:07:47
  1460. // @return dateDataMap map[time.Time]float64
  1461. // @return err error
  1462. func (obj BaseCalculate) ExponentialSmoothing() (dateDataMap map[time.Time]float64, err error, errMsg string) {
  1463. dateDataMap = make(map[time.Time]float64)
  1464. dataList := obj.DataList
  1465. formula := obj.Formula.(string) // alpha值
  1466. alpha, _ := strconv.ParseFloat(formula, 64)
  1467. if alpha <= 0 || alpha >= 1 {
  1468. err = fmt.Errorf("alpha值有误: %v", alpha)
  1469. return
  1470. }
  1471. // 获取标准差图表的指标数据
  1472. fromDataList, err := calculateExponentialSmoothingData(dataList, alpha)
  1473. if err != nil {
  1474. return
  1475. }
  1476. // 数据计算
  1477. for _, tmpData := range fromDataList {
  1478. if tmpData.DataTime.IsZero() {
  1479. continue
  1480. }
  1481. // 当前的实际值
  1482. saveValue, _ := decimal.NewFromFloat(tmpData.Value).Round(4).Float64()
  1483. dateDataMap[tmpData.DataTime] = saveValue
  1484. }
  1485. return
  1486. }
  1487. // calculateExponentialSmoothingData
  1488. // @Description: 计算指数修匀
  1489. // @author: Roc
  1490. // @datetime2023-11-03 14:05:41
  1491. // @param dataList []*EdbInfoData 时间基准指标在时间区间内的值
  1492. // @param strAlpha float64
  1493. // @return newDataList []EdbInfoData
  1494. // @return err error
  1495. func calculateExponentialSmoothingData(dataList []*EdbInfoData, alpha float64) (newDataList []EdbInfoData, err error) {
  1496. if alpha <= 0 || alpha >= 1 {
  1497. err = fmt.Errorf("alpha值有误: %v", alpha)
  1498. return
  1499. }
  1500. var preVal float64
  1501. alphaDecimal := decimal.NewFromFloat(alpha)
  1502. subAlpha := decimal.NewFromFloat(1).Sub(alphaDecimal)
  1503. for k, d := range dataList {
  1504. d.DataTime = time.Date(d.DataTime.Year(), d.DataTime.Month(), d.DataTime.Day(), 0, 0, 0, 0, d.DataTime.Location())
  1505. // 首期的值以原始值作为指数修匀的计算值
  1506. if k == 0 {
  1507. newDataList = append(newDataList, EdbInfoData{
  1508. EdbDataId: k,
  1509. DataTime: dataList[k].DataTime,
  1510. Value: d.Value,
  1511. })
  1512. preVal = d.Value
  1513. continue
  1514. }
  1515. // 上一期的值参与计算
  1516. preDecimal := decimal.NewFromFloat(preVal)
  1517. valDecimal := decimal.NewFromFloat(d.Value)
  1518. partA := alphaDecimal.Mul(valDecimal)
  1519. partB := subAlpha.Mul(preDecimal)
  1520. res, _ := (partA.Add(partB)).Float64()
  1521. preVal = res
  1522. newDataList = append(newDataList, EdbInfoData{
  1523. EdbDataId: k,
  1524. DataTime: dataList[k].DataTime,
  1525. Value: res,
  1526. })
  1527. }
  1528. return
  1529. }
  1530. // Rjz
  1531. // @Description: 日均值计算
  1532. // @author: Roc
  1533. // @receiver obj
  1534. // @datetime2023-11-03 14:47:47
  1535. // @return dateDataMap map[time.Time]float64
  1536. // @return err error
  1537. func (obj BaseCalculate) Rjz() (dateDataMap map[time.Time]float64, err error, errMsg string) {
  1538. dateDataMap = make(map[time.Time]float64)
  1539. dataList := obj.DataList
  1540. fromFrequency := obj.FromFrequency
  1541. if fromFrequency == `` {
  1542. errMsg = "原指标的频度不能为空"
  1543. err = errors.New(errMsg)
  1544. return
  1545. }
  1546. if obj.FromFrequency == "日度" {
  1547. errMsg = "日度指标无需进行日均值计算"
  1548. err = errors.New(errMsg)
  1549. return
  1550. }
  1551. // 数据处理
  1552. // 数据降序
  1553. dataList = reverseSliceByDesc(dataList)
  1554. var dateArr []time.Time
  1555. dataMap := make(map[time.Time]*EdbInfoData)
  1556. for _, v := range dataList {
  1557. v.DataTime = time.Date(v.DataTime.Year(), v.DataTime.Month(), v.DataTime.Day(), 0, 0, 0, 0, v.DataTime.Location())
  1558. dateArr = append(dateArr, v.DataTime)
  1559. dataMap[v.DataTime] = v
  1560. }
  1561. // 数据计算
  1562. for _, currentDate := range dateArr {
  1563. //当前日期
  1564. currentItem, ok := dataMap[currentDate]
  1565. if !ok {
  1566. continue
  1567. }
  1568. //根据频度计算需要均分的天数
  1569. days := GetRjzFrequencyDays(currentDate, fromFrequency)
  1570. val, ok := rjzDivV2(currentItem.Value, days)
  1571. if !ok {
  1572. continue
  1573. }
  1574. dateDataMap[currentDate] = val
  1575. }
  1576. return
  1577. }
  1578. // rjzDivV2
  1579. // @Description: 日均值计算
  1580. // @author: Roc
  1581. // @datetime2023-11-03 14:47:36
  1582. // @param a float64
  1583. // @param b int
  1584. // @return val float64
  1585. // @return ok bool
  1586. func rjzDivV2(a float64, b int) (val float64, ok bool) {
  1587. if b == 0 {
  1588. return
  1589. }
  1590. af := decimal.NewFromFloat(a)
  1591. bf := decimal.NewFromFloat(float64(b))
  1592. val, _ = af.Div(bf).Round(4).Float64()
  1593. ok = true
  1594. return
  1595. }
  1596. // reverseSliceByDesc
  1597. // @Description: 倒转切片
  1598. // @author: Roc
  1599. // @datetime2023-11-06 14:08:25
  1600. // @param slice []*EdbInfoData
  1601. // @return []*EdbInfoData
  1602. func reverseSliceByDesc(slice []*EdbInfoData) (newSlice []*EdbInfoData) {
  1603. // 只有一个数据的话,那么就直接返回
  1604. if len(slice) <= 1 {
  1605. newSlice = slice
  1606. return
  1607. }
  1608. if !slice[1].DataTime.After(slice[0].DataTime) {
  1609. newSlice = slice
  1610. return
  1611. }
  1612. newSlice = make([]*EdbInfoData, len(slice))
  1613. for i, j := 0, len(slice)-1; i < len(slice); i, j = i+1, j-1 {
  1614. newSlice[i] = slice[j]
  1615. }
  1616. return
  1617. }
  1618. // EdbInfoSearchDataToData
  1619. // @Description: 将EdbInfoSearchData转成EdbInfoData
  1620. // @author: Roc
  1621. // @datetime2023-11-09 11:11:31
  1622. // @param dataList []*EdbInfoSearchData
  1623. // @return newDateDataList []*EdbInfoData
  1624. // @return err error
  1625. func EdbInfoSearchDataToData(dataList []*EdbInfoSearchData) (newDateDataList []*EdbInfoData, err error) {
  1626. newDateDataList = make([]*EdbInfoData, 0)
  1627. for _, data := range dataList {
  1628. dateTime, tmpErr := time.ParseInLocation(utils.FormatDate, data.DataTime, time.Local)
  1629. if tmpErr != nil {
  1630. err = tmpErr
  1631. return
  1632. }
  1633. newDateDataList = append(newDateDataList, &EdbInfoData{
  1634. EdbDataId: data.EdbDataId,
  1635. DataTime: dateTime,
  1636. Value: data.Value,
  1637. })
  1638. }
  1639. return
  1640. }
  1641. // GetDateDataAndDateList
  1642. // @Description: 通过时间数据map 获取 时间字符串数据map和日期字符串切片
  1643. // @author: Roc
  1644. // @datetime2023-11-09 13:10:38
  1645. // @param dateDataMap map[time.Time]float64
  1646. // @return dateStrDataMap map[string]float64
  1647. // @return dateStrList []string
  1648. func GetDateDataAndDateList(dateDataMap map[time.Time]float64) (dateStrDataMap map[string]float64, dateStrList []string) {
  1649. dateStrDataMap = make(map[string]float64)
  1650. dateStrList = make([]string, 0)
  1651. dateList := make([]time.Time, 0)
  1652. for date, val := range dateDataMap {
  1653. dateStr := date.Format(utils.FormatDate)
  1654. dateStrDataMap[dateStr] = val
  1655. dateList = append(dateList, date)
  1656. }
  1657. // 使用 sort.Slice 对日期切片进行排序
  1658. sort.Slice(dateList, func(i, j int) bool {
  1659. return dateList[i].Before(dateList[j])
  1660. })
  1661. // 将日期排序后返回日期字符串切片
  1662. for _, v := range dateList {
  1663. dateStrList = append(dateStrList, v.Format(utils.FormatDate))
  1664. }
  1665. return
  1666. }
  1667. // StepCalculateBySearchData
  1668. // @Description: 分步骤计算
  1669. type StepCalculateBySearchData struct {
  1670. DataList []*EdbInfoSearchData `description:"基础数据"`
  1671. Calculates []CalculatesReq
  1672. }
  1673. type CalculatesReq struct {
  1674. Formula interface{}
  1675. Calendar string `description:"公历/农历"`
  1676. Frequency string `description:"需要转换的频度"`
  1677. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  1678. MoveFrequency string `description:"移动频度"`
  1679. FromFrequency string `description:"来源的频度"`
  1680. Source int `description:"1:累计值转月;2:累计值转季;3:同比值;4:同差值;5:N数值移动平均数计算;6:环比值;7:环差值;8:升频;9:降频;10:时间移位;11:超季节性;12:年化;13:累计值;14:累计值年初至今;15:指数修匀;16:日均值"`
  1681. Sort int `description:"计算顺序"`
  1682. }
  1683. func TransDateData2EdbData(dateData map[time.Time]float64) (edbData []*EdbInfoData) {
  1684. edbData = make([]*EdbInfoData, 0)
  1685. for d, v := range dateData {
  1686. edbData = append(edbData, &EdbInfoData{
  1687. DataTime: d,
  1688. Value: v,
  1689. })
  1690. }
  1691. sort.Slice(edbData, func(i, j int) bool {
  1692. return edbData[i].DataTime.Before(edbData[j].DataTime)
  1693. })
  1694. return
  1695. }
  1696. // EdbInfoSearchDataToEdbInfoData
  1697. // @Description: 数据格式转换
  1698. // @author: Roc
  1699. // @datetime 2024-08-22 16:30:34
  1700. // @param list []*EdbInfoSearchData
  1701. // @return result []*EdbInfoData
  1702. // @return err error
  1703. func EdbInfoSearchDataToEdbInfoData(list []*EdbInfoSearchData) (result []*EdbInfoData, err error) {
  1704. result = make([]*EdbInfoData, 0)
  1705. for _, v := range list {
  1706. dataTime, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  1707. if tmpErr != nil {
  1708. err = tmpErr
  1709. return
  1710. }
  1711. result = append(result, &EdbInfoData{
  1712. EdbDataId: v.EdbDataId,
  1713. DataTime: dataTime,
  1714. Value: v.Value,
  1715. })
  1716. }
  1717. return
  1718. }