base_calculate.go 54 KB

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