123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908 |
- package data_manage
- import (
- "encoding/json"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "hongze/hz_crm_api/utils"
- "sort"
- "strconv"
- "strings"
- "time"
- "github.com/nosixtools/solarlunar"
- )
- func GetEdbDataQuarterCount(edbInfoId int) (count int, err error) {
- o := orm.NewOrmUsingDB("data")
- hsql := `SELECT COUNT(1) AS count FROM edb_data_quarter WHERE edb_info_id=? `
- err = o.Raw(hsql, edbInfoId).QueryRow(&count)
- return
- }
- // 指标季度数据计算
- func AddCalculateQuarter(edbInfoId, source int, edbCode string) (err error) {
- o := orm.NewOrmUsingDB("data")
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- fmt.Println("Err:", err)
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- edbInfoIdStr := strconv.Itoa(edbInfoId)
- fmt.Println(edbInfoIdStr)
- //计算数据
- var condition string
- var pars []interface{}
- condition += " AND edb_info_id=? "
- pars = append(pars, edbInfoId)
- dataList, err := GetEdbDataListAll(condition, pars, source, 0)
- if err != nil {
- return err
- }
- var yearArr []int
- yearMap := make(map[int]int)
- dataMap := make(map[string]*EdbInfoSearchData)
- for _, v := range dataList {
- //日其中获取年
- itemDate, err := time.Parse(utils.FormatDate, v.DataTime)
- if err != nil {
- return err
- }
- year := itemDate.Year()
- if _, ok := yearMap[year]; !ok {
- yearArr = append(yearArr, year)
- yearMap[year] = year
- }
- dataMap[v.DataTime] = v
- }
- sort.Sort(sort.Reverse(sort.IntSlice(yearArr)))
- addSql := ` INSERT INTO edb_data_quarter(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
- var isAdd bool
- //yearLen := len(yearArr)
- fmt.Println(yearArr)
- for _, yv := range yearArr {
- currentYear := yv
- hsql := `SELECT * FROM holiday WHERE year=? `
- currentHolidayObj := new(Holiday)
- err = to.Raw(hsql, currentYear).QueryRow(¤tHolidayObj)
- if err != nil {
- fmt.Println("get holiday err:" + err.Error())
- return err
- }
- cjDate := currentHolidayObj.HolidayDate
- fmt.Println("year:", yv, "cjDate:", cjDate)
- //utils.FileLog.Info("cjDate:", cjDate)
- existMap := make(map[string]string)
- for i := 1; i <= 70; i++ {
- preDate := cjDate.Format(utils.FormatDate)
- fmt.Println("year:", yv, "preDate:", preDate, "i:", i)
- if findItem, ok := dataMap[preDate]; ok {
- timestamp := currentHolidayObj.HolidayDate.UnixNano() / 1e6
- timestampStr := fmt.Sprintf("%d", timestamp)
- if _, exist := existMap[preDate]; !exist {
- valStr := utils.SubFloatToString(findItem.Value, 4)
- addSql += GetAddSql(edbInfoIdStr, edbCode, preDate, timestampStr, valStr)
- isAdd = true
- }
- }
- cjDate = cjDate.AddDate(0, 0, -1)
- }
- cjDate = currentHolidayObj.HolidayDate
- for i := 1; i <= 70; i++ {
- nextDate := cjDate.Format(utils.FormatDate)
- fmt.Println("year:", yv, "nextDate:", nextDate, "i:", i)
- if findItem, ok := dataMap[nextDate]; ok {
- timestamp := currentHolidayObj.HolidayDate.UnixNano() / 1e6
- timestampStr := fmt.Sprintf("%d", timestamp)
- if _, exist := existMap[nextDate]; !exist {
- valStr := utils.SubFloatToString(findItem.Value, 4)
- addSql += GetAddSql(edbInfoIdStr, edbCode, nextDate, timestampStr, valStr)
- isAdd = true
- }
- }
- cjDate = cjDate.AddDate(0, 0, +1)
- }
- //nextIndex := yk + 1
- //if nextIndex < yearLen {
- // nextYear := yearArr[nextIndex]
- // fmt.Println("year:", currentYear, nextYear)
- // nextHolidayObj := new(Holiday)
- // hsql := `SELECT * FROM holiday WHERE year=? `
- // err = to.Raw(hsql, nextYear).QueryRow(&nextHolidayObj)
- // if err != nil {
- // return err
- // }
- // nextHolidayStr := nextHolidayObj.HolidayDate.Format(utils.FormatDate)
- // currentHolidayStr := currentHolidayObj.HolidayDate.Format(utils.FormatDate)
- //
- // if findItem, ok := dataMap[nextHolidayStr]; ok {
- // timestamp := currentHolidayObj.HolidayDate.UnixNano() / 1e6
- // timestampStr := fmt.Sprintf("%d", timestamp)
- // addSql += GetAddSql(edbInfoIdStr, edbCode, currentHolidayStr, timestampStr, findItem.Value)
- // isAdd = true
- // }
- //
- // nextMinDate := strconv.Itoa(nextYear) + "-01-01"
- // nextMaxDate := strconv.Itoa(nextYear) + "-12-31"
- // currentMinDate := strconv.Itoa(currentYear) + "-01-01"
- // currentMaxDate := strconv.Itoa(currentYear) + "-12-31"
- //
- // i := 0
- // for {
- // i--
- // nextHolidayFormat := nextHolidayObj.HolidayDate.AddDate(0, 0, i)
- // currentHolidayFormat := currentHolidayObj.HolidayDate.AddDate(0, 0, i)
- //
- // nextHolidayStr := nextHolidayFormat.Format(utils.FormatDate)
- // currentHolidayStr := currentHolidayFormat.Format(utils.FormatDate)
- //
- // fmt.Println("currentHolidayStr:", currentHolidayStr, nextHolidayStr)
- // if findItem, ok := dataMap[nextHolidayStr]; ok {
- // timestamp := currentHolidayFormat.UnixNano() / 1e6
- // timestampStr := fmt.Sprintf("%d", timestamp)
- // addSql += GetAddSql(edbInfoIdStr, edbCode, currentHolidayStr, timestampStr, findItem.Value)
- // isAdd = true
- // }
- // //fmt.Println("nextHolidayStr:", nextHolidayStr,nextMinDate)
- // if nextHolidayStr == nextMinDate || currentHolidayStr == currentMinDate {
- // fmt.Println("min跳出循环:" + nextMinDate)
- // break
- // }
- // }
- //
- // n := 0
- // for {
- // n++
- // nextHolidayFormat := nextHolidayObj.HolidayDate.AddDate(0, 0, n)
- // currentHolidayFormat := currentHolidayObj.HolidayDate.AddDate(0, 0, n)
- //
- // nextHolidayStr := nextHolidayFormat.Format(utils.FormatDate)
- // currentHolidayStr := currentHolidayFormat.Format(utils.FormatDate)
- //
- // fmt.Println("currentHolidayStr:", currentHolidayStr, nextHolidayStr)
- //
- // if findItem, ok := dataMap[nextHolidayStr]; ok {
- // timestamp := currentHolidayFormat.UnixNano() / 1e6
- // timestampStr := fmt.Sprintf("%d", timestamp)
- //
- // addSql += GetAddSql(edbInfoIdStr, edbCode, currentHolidayStr, timestampStr, findItem.Value)
- // isAdd = true
- // }
- //
- // //fmt.Println("nextHolidayStr:", nextHolidayStr,nextMaxDate)
- // if nextHolidayStr == nextMaxDate || currentHolidayStr == currentMaxDate {
- // fmt.Println("max跳出循环:" + nextMaxDate)
- // break
- // }
- // }
- //}
- }
- fmt.Println("isAdd", isAdd)
- if isAdd {
- addSql = strings.TrimRight(addSql, ",")
- utils.FileLog.Info(addSql)
- _, err = to.Raw(addSql).Exec()
- fmt.Println("err:", err)
- if err != nil {
- return err
- }
- }
- return
- }
- // 指标季度数据计算(公历转农历)
- func AddCalculateQuarterV2(edbInfoId, source int, edbCode string) (err error) {
- o := orm.NewOrmUsingDB("data")
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- fmt.Println("Err:", err)
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- edbInfoIdStr := strconv.Itoa(edbInfoId)
- fmt.Println(edbInfoIdStr)
- //计算数据
- var condition string
- var pars []interface{}
- condition += " AND edb_info_id=? "
- pars = append(pars, edbInfoId)
- dataList, err := GetEdbDataListAll(condition, pars, source, 0)
- if err != nil {
- return err
- }
- var yearArr []int
- yearMap := make(map[int]int)
- dataMap := make(map[string]*EdbInfoSearchData)
- for _, v := range dataList {
- //日其中获取年
- itemDate, err := time.Parse(utils.FormatDate, v.DataTime)
- if err != nil {
- return err
- }
- year := itemDate.Year()
- if _, ok := yearMap[year]; !ok {
- yearArr = append(yearArr, year)
- yearMap[year] = year
- }
- dataMap[v.DataTime] = v
- }
- sort.Sort(sort.Reverse(sort.IntSlice(yearArr)))
- addSql := ` INSERT INTO edb_data_quarter(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
- var isAdd bool
- //yearLen := len(yearArr)
- fmt.Println(yearArr)
- for _, yv := range yearArr {
- fmt.Println(yv)
- }
- fmt.Println("isAdd", isAdd)
- if isAdd {
- addSql = strings.TrimRight(addSql, ",")
- utils.FileLog.Info(addSql)
- _, err = to.Raw(addSql).Exec()
- fmt.Println("err:", err)
- if err != nil {
- return err
- }
- }
- return
- }
- // 指标季度数据计算(公历转农历)
- func AddCalculateQuarterV3(edbInfoId, source int, edbCode string, list []*EdbDataList) (err error) {
- var errMsg string
- defer func() {
- if errMsg != "" {
- fmt.Println("errMsg:", errMsg)
- }
- }()
- lunarDate := "2017-01-01" //农历
- fmt.Println(solarlunar.LunarToSolar(lunarDate, false))
- dataList, err := GetHzTestEdbdata()
- var yearArr []int
- yearMap := make(map[int]int)
- var cureentDate time.Time
- for k, v := range dataList {
- if k == 0 {
- cureentDate = v.Dt
- }
- year := v.Dt.Year()
- if _, ok := yearMap[year]; !ok {
- yearArr = append(yearArr, year)
- }
- yearMap[year] = year
- }
- //排序
- fmt.Println(yearArr)
- thisYear := cureentDate.Year()
- thisMonth := int(cureentDate.Month())
- sort.Ints(yearArr)
- fmt.Println("thisYear:", thisYear)
- result := new(HzTestEdbdataResult)
- for ky, vy := range yearArr {
- 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 err
- }
- fmt.Println(ky, vy)
- 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 err
- }
- day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
- fmt.Println("day:", day)
- items := new(HzTestEdbdataItems)
- dataLen := len(dataList)
- for i := dataLen - 1; i >= 0; i-- {
- v := dataList[i]
- newDate := v.Dt.AddDate(0, 0, int(day))
- selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
- selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
- if newDate.Before(selectDate) {
- item := new(HzTestEdbdata)
- item.TtradeCode = v.TtradeCode
- item.Dt = newDate
- item.Close = v.Close
- timestamp := item.Dt.UnixNano() / 1e6
- item.DataTimestamp = timestamp
- items.Items = append(items.Items, item)
- }
- }
- result.List = append(result.List, items)
- } else {
- fmt.Println(ky, vy)
- 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 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 err
- }
- day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
- fmt.Println("day:", day)
- items := new(HzTestEdbdataItems)
- dataLen := len(dataList)
- for i := dataLen - 1; i >= 0; i-- {
- v := dataList[i]
- newDate := v.Dt.AddDate(0, 0, int(day))
- selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
- selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
- if newDate.Before(selectDate) {
- item := new(HzTestEdbdata)
- item.TtradeCode = v.TtradeCode
- item.Dt = newDate
- item.Close = v.Close
- timestamp := item.Dt.UnixNano() / 1e6
- item.DataTimestamp = timestamp
- items.Items = append(items.Items, item)
- }
- }
- result.List = append(result.List, items)
- }
- }
- resultJson, err := json.Marshal(result)
- utils.FileLog.Info(string(resultJson))
- return
- }
- // 指标季度数据计算(公历转农历)
- func AddCalculateQuarterV4(dataList []*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.Println("line 432:", ky, vy, thisYear, thisMonth)
- 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)
- 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))
- selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
- selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
- if newDate.Before(selectDate) || newDate == selectDate {
- timestamp := newDate.UnixNano() / 1e6
- item := new(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 - 1
- 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))
- selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
- selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
- if newDate.Before(selectDate) || newDate == selectDate {
- timestamp := newDate.UnixNano() / 1e6
- item := new(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
- }
- // AddCalculateQuarterV4ByUniqueCode 指标季度数据计算(公历转农历)
- func AddCalculateQuarterV4ByUniqueCode(dataList []*EdbDataListByUniqueCode) (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.Println("line 432:", ky, vy, thisYear, thisMonth)
- 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)
- items := new(EdbDataItems)
- 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))
- selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
- selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
- if newDate.Before(selectDate) || newDate == selectDate {
- timestamp := newDate.UnixNano() / 1e6
- item := new(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.Year = preYear - 1
- 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))
- selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
- selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
- if newDate.Before(selectDate) || newDate == selectDate {
- timestamp := newDate.UnixNano() / 1e6
- item := new(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
- }
- type HzTestEdbdataResult struct {
- List []*HzTestEdbdataItems
- }
- type HzTestEdbdataItems struct {
- Items []*HzTestEdbdata
- }
- type HzTestEdbdata struct {
- TtradeCode string `orm:"column(TRADE_CODE)"`
- Dt time.Time `orm:"column(DT)"`
- Close float64 `orm:"column(CLOSE)"`
- ModifyTime time.Time `orm:"column(modify_time)"`
- DataTimestamp int64
- }
- func GetHzTestEdbdata() (list []*HzTestEdbdata, err error) {
- o := orm.NewOrm()
- sql := ` SELECT * FROM hz_test_edbdata WHERE TRADE_CODE=? ORDER BY DT DESC `
- _, err = o.Raw(sql, "ST_0000577845").QueryRows(&list)
- return
- }
- type EdbDataItems struct {
- Items []*EdbDataList
- Year int
- BetweenDay int `json:"-" description:"公历与农历之间相差的天数"`
- CuttingDataTimestamp int64 `description:"切割的时间戳"`
- }
- type EdbDataResult struct {
- List []*EdbDataItems
- }
- func AddCalculateQuarterV5(dataList []*EdbDataList) (result *EdbDataResult, err error) {
- var errMsg string
- defer func() {
- if errMsg != "" {
- fmt.Println("errMsg:", errMsg)
- }
- }()
- result = new(EdbDataResult)
- thisYear := time.Now().Year()
- endDate := dataList[len(dataList)-1].DataTime
- endDateForm, err := time.Parse(utils.FormatDate, endDate)
- if err != nil {
- return result, err
- }
- thisMonth := int(endDateForm.Month())
- fmt.Println(thisMonth)
- for i := 4; i > 0; i-- {
- if thisMonth < 11 {
- nextYear := thisYear
- 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
- }
- preYearCjnl := strconv.Itoa(nextYear-i) + "-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)
- items := new(EdbDataItems)
- for _, v := range dataList {
- dataDate := v.DataTime
- dataDateForm, err := time.Parse(utils.FormatDate, dataDate)
- if err != nil {
- return result, err
- }
- newDate := dataDateForm.AddDate(0, 0, int(day))
- selectDateStr := strconv.Itoa(nextYear) + "-11" + "-30"
- selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
- if newDate.Before(selectDate) {
- timestamp := newDate.UnixNano() / 1e6
- item := new(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)
- }
- }
- fmt.Println(nextYear, "年春节阳历", nextYearCjgl)
- fmt.Println(i, "年前春节", preYearCjnl)
- fmt.Println(i, "年前春节阳历", preYearCjgl)
- fmt.Println("与", nextYear, "年春节相差天数", day, "____________")
- 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
- }
- preYearCjnl := strconv.Itoa(nextYear-i) + "-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)
- items := new(EdbDataItems)
- for _, v := range dataList {
- dataDate := v.DataTime
- dataDateForm, err := time.Parse(utils.FormatDate, dataDate)
- if err != nil {
- return result, err
- }
- newDate := dataDateForm.AddDate(0, 0, int(day))
- selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
- selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
- if newDate.Before(selectDate) {
- timestamp := newDate.UnixNano() / 1e6
- item := new(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)
- }
- }
- fmt.Println(nextYear, "年春节阳历", nextYearCjgl)
- fmt.Println(i, "年前春节", preYearCjnl)
- fmt.Println(i, "年前春节阳历", preYearCjgl)
- fmt.Println("与", nextYear, "年春节相差天数", day, "____________")
- result.List = append(result.List, items)
- }
- }
- //合并第五组数据
- items := new(EdbDataItems)
- for _, v := range dataList {
- dataDate := v.DataTime
- dataDateForm, err := time.Parse(utils.FormatDate, dataDate)
- if err != nil {
- return result, err
- }
- timestamp := dataDateForm.UnixNano() / 1e6
- item := new(EdbDataList)
- item.DataTime = v.DataTime
- 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
- }
|