123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- package data
- import (
- "eta/eta_forum_admin/models"
- "eta/eta_forum_admin/utils"
- "fmt"
- "github.com/nosixtools/solarlunar"
- "strconv"
- "time"
- )
- type EdbDataItems struct {
- Items []*models.EdbDataList
- Year int
- BetweenDay int `json:"-" description:"公历与农历之间相差的天数"`
- CuttingDataTimestamp int64 `description:"切割的时间戳"`
- }
- type EdbDataResult struct {
- List []*EdbDataItems
- }
- // AddCalculateQuarterV6 指标季度数据计算(季节性图表)
- func AddCalculateQuarterV6(dataList []*models.EdbDataList) (result *EdbDataResult, err error) {
- var errMsg string
- defer func() {
- if errMsg != "" {
- fmt.Println("errMsg:", errMsg)
- }
- }()
- endDate := dataList[len(dataList)-1].DataTime
- endDateForm, err := time.Parse(utils.FormatDate, endDate)
- if err != nil {
- return result, err
- }
- thisMonth := int(endDateForm.Month())
- result = new(EdbDataResult)
- var yearArr []int
- yearMap := make(map[int]int)
- var cureentDate time.Time
- if thisMonth < 11 {
- for k, v := range dataList {
- dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
- if err != nil {
- errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
- return result, err
- }
- if k == len(dataList)-1 {
- cureentDate = dateTime
- }
- year := dateTime.Year()
- if _, ok := yearMap[year]; !ok {
- yearArr = append(yearArr, year)
- }
- yearMap[year] = year
- }
- } else {
- for k, v := range dataList {
- dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
- if err != nil {
- errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
- return result, err
- }
- if k == len(dataList)-1 {
- cureentDate = dateTime
- }
- year := dateTime.Year() + 1
- if _, ok := yearMap[year]; !ok {
- yearArr = append(yearArr, year)
- }
- yearMap[year] = year
- }
- }
- //排序
- fmt.Println("yearArr:", yearArr)
- thisYear := cureentDate.Year()
- //thisMonth := int(cureentDate.Month())
- fmt.Println("thisMonth:", thisMonth)
- for ky, vy := range yearArr {
- fmt.Printf("line 432:ky:%d, vy:%d, thisYear:%d, thisMonth:%d", ky, vy, thisYear, thisMonth)
- fmt.Println("")
- if thisMonth < 11 {
- currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
- currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
- currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
- if err != nil {
- errMsg = "生成当前春节失败,Err:" + err.Error()
- return result, err
- }
- preYear := vy
- preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
- preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
- preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
- if err != nil {
- errMsg = "生成历史年份春节失败,Err:" + err.Error()
- return result, err
- }
- day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
- fmt.Println("day:", day, currentYearCjglDate, preYearCjglDate)
- items := new(EdbDataItems)
- items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
- items.Year = preYear
- for _, v := range dataList {
- dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
- if err != nil {
- errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
- return result, err
- }
- newDate := dateTime.AddDate(0, 0, int(day))
- timestamp := newDate.UnixNano() / 1e6
- item := new(models.EdbDataList)
- item.DataTime = newDate.Format(utils.FormatDate)
- item.EdbInfoId = v.EdbInfoId
- item.Value = v.Value
- item.EdbDataId = v.EdbDataId
- item.DataTimestamp = timestamp
- items.Items = append(items.Items, item)
- }
- result.List = append(result.List, items)
- } else {
- nextYear := thisYear + 1
- nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
- nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
- nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
- if err != nil {
- errMsg = "生成当前春节失败,Err:" + err.Error()
- return result, err
- }
- preYear := vy
- preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
- preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
- preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
- if err != nil {
- errMsg = "生成历史年份春节失败,Err:" + err.Error()
- return result, err
- }
- day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
- fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
- items := new(EdbDataItems)
- items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
- items.Year = preYear
- fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
- //if ky+1 < len(yearArr) {
- for _, v := range dataList {
- dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
- if err != nil {
- errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
- return result, err
- }
- newDate := dateTime.AddDate(0, 0, int(day))
- timestamp := newDate.UnixNano() / 1e6
- item := new(models.EdbDataList)
- item.DataTime = newDate.Format(utils.FormatDate)
- item.EdbInfoId = v.EdbInfoId
- item.Value = v.Value
- item.EdbDataId = v.EdbDataId
- item.DataTimestamp = timestamp
- items.Items = append(items.Items, item)
- }
- result.List = append(result.List, items)
- /*} else {
- for _, v := range dataList {
- dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
- if err != nil {
- errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
- return result, err
- }
- timestamp := dateTime.UnixNano() / 1e6
- item := new(EdbDataList)
- item.DataTime = dateTime.Format(utils.FormatDate)
- item.EdbInfoId = v.EdbInfoId
- item.Value = v.Value
- item.EdbDataId = v.EdbDataId
- item.DataTimestamp = timestamp
- items.Items = append(items.Items, item)
- }
- result.List = append(result.List, items)
- }*/
- }
- }
- return
- }
|