chart.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. package models
  2. import (
  3. "errors"
  4. "eta/eta_chart_lib/models/mgo"
  5. "eta/eta_chart_lib/utils"
  6. "fmt"
  7. "strconv"
  8. "time"
  9. "github.com/beego/beego/v2/client/orm"
  10. "go.mongodb.org/mongo-driver/bson"
  11. "github.com/nosixtools/solarlunar"
  12. )
  13. type ChartInfo struct {
  14. ChartInfoId int `json:"-" orm:"column(chart_info_id);pk"`
  15. ChartName string `description:"来源名称"`
  16. ChartNameEn string `description:"英文图表名称"`
  17. ChartClassifyId int `json:"-" description:"图表分类id"`
  18. SysUserId int `json:"-"`
  19. SysUserRealName string `json:"-"`
  20. UniqueCode string `description:"图表唯一编码"`
  21. CreateTime time.Time `json:"-"`
  22. ModifyTime time.Time `json:"-"`
  23. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
  24. StartDate string `description:"自定义开始日期"`
  25. EndDate string `description:"自定义结束日期"`
  26. IsSetName int `description:"设置名称"`
  27. EdbInfoIds string `description:"指标id"`
  28. ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
  29. Calendar string `description:"公历/农历"`
  30. SeasonStartDate string `description:"季节性图开始日期"`
  31. SeasonEndDate string `description:"季节性图开始日期"`
  32. ChartImage string `description:"图表图片"`
  33. BarConfig string `description:"柱方图的配置,json数据" json:"-"`
  34. XMin string `description:"图表X轴最小值"`
  35. XMax string `description:"图表X轴最大值"`
  36. LeftMin string `description:"图表左侧最小值"`
  37. LeftMax string `description:"图表左侧最大值"`
  38. RightMin string `description:"图表右侧最小值"`
  39. RightMax string `description:"图表右侧最大值"`
  40. Right2Min string `description:"图表右侧2最小值"`
  41. Right2Max string `description:"图表右侧2最大值"`
  42. MinMaxSave int `description:"是否手动保存过上下限:0-否;1-是"`
  43. Source int `description:"1:ETA图库;2:商品价格曲线"`
  44. UnitEn string `description:"英文图表单位"`
  45. Unit string `description:"图表单位"`
  46. ExtraConfig string `description:"图表额外配置,json数据" json:"-"`
  47. ChartSource string `description:"图表来源str"`
  48. ChartSourceEn string `description:"图表来源(英文)"`
  49. SeasonExtraConfig string `description:"季节性图表中的配置,json数据"`
  50. StartYear int `description:"当选择的日期类型为最近N年类型时,即date_type=20, 用start_year表示N"`
  51. ChartThemeId int `description:"图表应用主题ID"`
  52. ChartThemeStyle string `description:"图表应用主题样式"`
  53. SourcesFrom string `description:"图表来源"`
  54. Instructions string `description:"图表说明"`
  55. MarkersLines string `description:"标识线"`
  56. MarkersAreas string `description:"标识区"`
  57. }
  58. func GetChartInfoByUniqueCode(uniqueCode string) (item *ChartInfo, err error) {
  59. o := orm.NewOrmUsingDB("data")
  60. sql := ` SELECT * FROM chart_info WHERE unique_code=? `
  61. err = o.Raw(sql, uniqueCode).QueryRow(&item)
  62. return
  63. }
  64. type ChartEdbInfoMapping struct {
  65. EdbInfoId int `description:"指标id"`
  66. SourceName string `description:"来源名称"`
  67. Source int `description:"来源id"`
  68. EdbCode string `description:"指标编码"`
  69. EdbName string `description:"指标名称"`
  70. EdbAliasName string `description:"指标名称(别名)"`
  71. EdbAliasNameEn string `description:"英文指标名称(别名)"`
  72. EdbNameEn string `description:"英文指标名称"`
  73. Frequency string `description:"频率"`
  74. FrequencyEn string `description:"英文频率"`
  75. Unit string `description:"单位"`
  76. UnitEn string `description:"英文单位"`
  77. StartDate string `description:"起始日期"`
  78. EndDate string `description:"终止日期"`
  79. ModifyTime string `description:"指标最后更新时间"`
  80. ChartEdbMappingId int `description:"图表指标id" json:"-"`
  81. ChartInfoId int `description:"图表id"`
  82. MaxData float64 `description:"上限"`
  83. MinData float64 `description:"下限"`
  84. IsOrder bool `description:"true:正序,false:逆序"`
  85. IsAxis int `description:"1:左轴,0:右轴"`
  86. EdbInfoType int `description:"1:标准指标,0:领先指标"`
  87. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  88. LeadValue int `description:"领先值"`
  89. LeadUnit string `description:"领先单位"`
  90. LeadUnitEn string `description:"领先英文单位"`
  91. ChartStyle string `description:"图表类型"`
  92. ChartColor string `description:"颜色"`
  93. ChartWidth float64 `description:"线条大小"`
  94. DataList interface{}
  95. EdbInfoCategoryType int `description:"0:普通指标,1:预测指标"`
  96. PredictChartColor string `description:"预测数据的颜色"`
  97. ChartType int `description:"生成样式:1:曲线图,2:季节性图,3:面积图,4:柱状图,5:散点图,6:组合图"`
  98. LatestDate string `description:"数据最新日期"`
  99. LatestValue float64 `description:"数据最新值"`
  100. MoveLatestDate string `description:"移动后的数据最新日期"`
  101. UniqueCode string `description:"指标唯一编码"`
  102. MinValue float64 `json:"-" description:"最小值"`
  103. MaxValue float64 `json:"-" description:"最大值"`
  104. IsNullData bool `json:"-" description:"是否空数据"`
  105. MappingSource int `description:"1:ETA图库;2:商品价格曲线"`
  106. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  107. SubSourceName string `description:"子数据来源名称"`
  108. IndicatorCode string `description:"指标代码"`
  109. StockCode string `description:"证券代码"`
  110. IsConvert int `description:"是否数据转换 0不转 1转"`
  111. ConvertType int `description:"数据转换类型 1乘 2除 3对数"`
  112. ConvertValue float64 `description:"数据转换值"`
  113. ConvertUnit string `description:"数据转换单位"`
  114. ConvertEnUnit string `description:"数据转换单位"`
  115. }
  116. func GetChartEdbMappingList(chartInfoId int) (list []*ChartEdbInfoMapping, err error) {
  117. o := orm.NewOrmUsingDB("data")
  118. aField := `a.chart_edb_mapping_id,a.chart_info_id,a.edb_info_id,a.create_time,a.modify_time,a.unique_code,a.max_data,a.min_data,a.is_order,a.is_axis,a.edb_info_type,a.lead_value,a.lead_unit,a.chart_style,a.chart_color,a.predict_chart_color,a.chart_width,a.source as mapping_source,a.edb_alias_name`
  119. sql := ` SELECT ` + aField + `,b.source_name,b.source,b.edb_code,b.edb_name,b.edb_name_en,b.frequency,b.unit,b.unit_en,b.start_date,b.end_date,b.modify_time,b.edb_type,b.latest_date,b.latest_value,b.unique_code,b.edb_info_type AS edb_info_category_type,b.sub_source,
  120. a.is_convert, a.convert_type, a.convert_value, a.convert_unit, a.convert_en_unit
  121. FROM chart_edb_mapping AS a
  122. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  123. WHERE chart_info_id=?
  124. ORDER BY chart_edb_mapping_id ASC `
  125. _, err = o.Raw(sql, chartInfoId).QueryRows(&list)
  126. return
  127. }
  128. type EdbDataList struct {
  129. EdbDataId int `json:"-" description:" 指标数据ID"`
  130. EdbInfoId int `json:"-" description:"指标ID"`
  131. DataTime string `description:"数据日期"`
  132. DataTimestamp int64 `description:"数据日期"`
  133. Value float64 `description:"数据值"`
  134. }
  135. // GetEdbDataList 获取指标的数据(日期正序返回)
  136. func GetEdbDataList(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  137. // 自有数据需要额外处理(从mongo获取)
  138. if source == utils.DATA_SOURCE_BUSINESS {
  139. return getEdbDataListByMongo(source, subSource, edbInfoId, startDate, endDate)
  140. }
  141. return getEdbDataListByMysql(source, subSource, edbInfoId, startDate, endDate)
  142. }
  143. // getEdbDataListByMysql
  144. // @Description: 从mysql中获取指标的数据(日期正序返回)
  145. // @author: Roc
  146. // @datetime 2024-05-08 16:49:48
  147. // @param source int
  148. // @param subSource int
  149. // @param edbInfoId int
  150. // @param startDate string
  151. // @param endDate string
  152. // @return list []*EdbDataList
  153. // @return err error
  154. func getEdbDataListByMysql(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  155. tableName := GetEdbDataTableName(source, subSource)
  156. if tableName == "" {
  157. err = errors.New("无效的渠道:" + strconv.Itoa(source))
  158. list = make([]*EdbDataList, 0)
  159. return list, err
  160. }
  161. var pars []interface{}
  162. var condition string
  163. if startDate != "" {
  164. condition += ` AND data_time>=? `
  165. pars = append(pars, startDate)
  166. }
  167. if endDate != "" {
  168. condition += ` AND data_time<=? `
  169. pars = append(pars, endDate)
  170. }
  171. sql := `SELECT edb_data_id,edb_info_id,data_time,value,data_timestamp FROM %s WHERE edb_info_id=? `
  172. if condition != "" {
  173. sql += condition
  174. }
  175. sql += ` ORDER BY data_time ASC `
  176. sql = fmt.Sprintf(sql, tableName)
  177. o := orm.NewOrmUsingDB("data")
  178. _, err = o.Raw(sql, edbInfoId, pars).QueryRows(&list)
  179. return
  180. }
  181. // getEdbDataListByMongo
  182. // @Description: 从mongo中获取指标的数据(日期正序返回)
  183. // @author: Roc
  184. // @datetime 2024-05-08 16:49:59
  185. // @param source int
  186. // @param subSource int
  187. // @param edbInfoId int
  188. // @param startDate string
  189. // @param endDate string
  190. // @return list []*EdbDataList
  191. // @return err error
  192. func getEdbDataListByMongo(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  193. list = make([]*EdbDataList, 0)
  194. mogDataObj := mgo.EdbDataBusiness{}
  195. // 构建查询条件
  196. queryConditions := bson.M{
  197. "edb_info_id": edbInfoId,
  198. }
  199. // 日期
  200. dateCondition, err := mgo.BuildDateCondition(startDate, endDate)
  201. if err != nil {
  202. return
  203. }
  204. if len(dateCondition) > 0 {
  205. queryConditions["data_time"] = dateCondition
  206. }
  207. // 获取列表数据
  208. tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"data_time"})
  209. if tmpErr != nil {
  210. err = tmpErr
  211. return
  212. }
  213. for k, v := range tmpDataList {
  214. list = append(list, &EdbDataList{
  215. EdbDataId: k + 1,
  216. EdbInfoId: v.EdbInfoId,
  217. DataTime: v.DataTime.Format(utils.FormatDate),
  218. DataTimestamp: v.DataTimestamp,
  219. Value: v.Value,
  220. })
  221. }
  222. return
  223. }
  224. type EdbDataItemList struct {
  225. EdbDataId int `json:"-" description:" 指标数据ID"`
  226. EdbInfoId int `json:"-" description:"指标ID"`
  227. DataTime string `description:"数据日期"`
  228. DataTimestamp int64 `description:"数据日期"`
  229. Value float64 `description:"数据值"`
  230. }
  231. type EdbDataItems struct {
  232. Items []*EdbDataList
  233. Year int
  234. BetweenDay int `json:"-" description:"公历与农历之间相差的天数"`
  235. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  236. }
  237. type EdbDataResult struct {
  238. List []*EdbDataItems
  239. }
  240. type QuarterData struct {
  241. Year string
  242. DataList []*EdbDataList
  243. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  244. ChartLegend string
  245. Years string
  246. }
  247. type QuarterXDateItem struct {
  248. StartDate time.Time
  249. EndDate time.Time
  250. ShowName string
  251. ChartLegend string
  252. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  253. }
  254. type ChartInfoDetailResp struct {
  255. ChartInfo *ChartInfo
  256. EdbInfoList []*ChartEdbInfoMapping
  257. XEdbIdValue []int `description:"柱方图的x轴数据,指标id"`
  258. YDataList []YData `description:"柱方图的y轴数据"`
  259. XDataList []XData `description:"商品价格曲线的X轴数据"`
  260. CorrelationChartInfo *CorrelationInfo `description:"相关性图表信息"`
  261. DataResp interface{} `description:"图表数据,根据图的类型而定的,没有确定的数据格式"`
  262. WaterMark string `description:"水印"`
  263. IsCollect bool `description:"是否收藏"`
  264. }
  265. // XData 商品价格曲线的的x轴数据
  266. type XData struct {
  267. Name string `description:"别名"`
  268. NameEn string `description:"英文别名"`
  269. }
  270. // YData 柱方图的y轴数据
  271. type YData struct {
  272. Date string `description:"数据日期"`
  273. ConfigDate time.Time `description:"配置的日期" json:"-"`
  274. Color string `description:"数据颜色"`
  275. Name string `description:"别名"`
  276. NameEn string `description:"英文别名"`
  277. Value []float64 `description:"每个指标的值"`
  278. NoDataEdbList []int `description:"没有数据的指标列表"`
  279. XEdbInfoIdList []int `description:"对应X轴的指标id列表"`
  280. EdbValMap map[int]float64 `description:"指标与值的对应" json:"-"`
  281. M []int `description:"对应开始日期的间隔值" json:"-"`
  282. NameList []string `description:"每个值对应的名称"`
  283. EnNameList []string `description:"每个值对应的英文名称"`
  284. Unit string `description:"中文单位名称"`
  285. UnitEn string `description:"英文单位名称"`
  286. }
  287. // RollingCorrelationChartDataResp 滚动相关性图
  288. type RollingCorrelationChartDataResp struct {
  289. XDateTimeValue []string `description:"滚动相关性图的x轴数据,日期数据"`
  290. YDataList []YData `description:"滚动相关性图的y轴数据"`
  291. }
  292. // 指标季度数据计算(公历转农历)
  293. func AddCalculateQuarterV5(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  294. var errMsg string
  295. defer func() {
  296. if errMsg != "" {
  297. fmt.Println("errMsg:", errMsg)
  298. }
  299. }()
  300. if len(dataList) <= 0 {
  301. result.List = make([]*EdbDataItems, 0)
  302. return
  303. }
  304. endDate := dataList[len(dataList)-1].DataTime
  305. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  306. if err != nil {
  307. return result, err
  308. }
  309. thisMonth := int(endDateForm.Month())
  310. result = new(EdbDataResult)
  311. var yearArr []int
  312. yearMap := make(map[int]int)
  313. var cureentDate time.Time
  314. if thisMonth < 11 {
  315. for k, v := range dataList {
  316. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  317. if err != nil {
  318. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  319. return result, err
  320. }
  321. if k == len(dataList)-1 {
  322. cureentDate = dateTime
  323. }
  324. year := dateTime.Year()
  325. if _, ok := yearMap[year]; !ok {
  326. yearArr = append(yearArr, year)
  327. }
  328. yearMap[year] = year
  329. }
  330. } else {
  331. for k, v := range dataList {
  332. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  333. if err != nil {
  334. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  335. return result, err
  336. }
  337. if k == len(dataList)-1 {
  338. cureentDate = dateTime
  339. }
  340. year := dateTime.Year() + 1
  341. if _, ok := yearMap[year]; !ok {
  342. yearArr = append(yearArr, year)
  343. }
  344. yearMap[year] = year
  345. }
  346. }
  347. //排序
  348. fmt.Println("yearArr:", yearArr)
  349. thisYear := cureentDate.Year()
  350. //thisMonth := int(cureentDate.Month())
  351. fmt.Println("thisMonth:", thisMonth)
  352. for ky, vy := range yearArr {
  353. fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
  354. if thisMonth < 11 {
  355. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  356. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  357. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  358. if err != nil {
  359. errMsg = "生成当前春节失败,Err:" + err.Error()
  360. return result, err
  361. }
  362. preYear := vy
  363. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  364. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  365. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  366. if err != nil {
  367. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  368. return result, err
  369. }
  370. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  371. items := new(EdbDataItems)
  372. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  373. items.Year = preYear
  374. for _, v := range dataList {
  375. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  376. if err != nil {
  377. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  378. return result, err
  379. }
  380. newDate := dateTime.AddDate(0, 0, int(day))
  381. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  382. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  383. if newDate.Before(selectDate) || newDate == selectDate {
  384. timestamp := newDate.UnixNano() / 1e6
  385. item := new(EdbDataList)
  386. item.DataTime = newDate.Format(utils.FormatDate)
  387. item.EdbInfoId = v.EdbInfoId
  388. item.Value = v.Value
  389. item.EdbDataId = v.EdbDataId
  390. item.DataTimestamp = timestamp
  391. items.Items = append(items.Items, item)
  392. }
  393. }
  394. result.List = append(result.List, items)
  395. } else {
  396. nextYear := thisYear + 1
  397. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  398. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  399. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  400. if err != nil {
  401. errMsg = "生成当前春节失败,Err:" + err.Error()
  402. return result, err
  403. }
  404. preYear := vy
  405. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  406. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  407. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  408. if err != nil {
  409. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  410. return result, err
  411. }
  412. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  413. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  414. items := new(EdbDataItems)
  415. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  416. items.Year = preYear - 1
  417. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  418. if ky+1 < len(yearArr) {
  419. for _, v := range dataList {
  420. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  421. if err != nil {
  422. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  423. return result, err
  424. }
  425. newDate := dateTime.AddDate(0, 0, int(day))
  426. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  427. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  428. if newDate.Before(selectDate) || newDate == selectDate {
  429. timestamp := newDate.UnixNano() / 1e6
  430. item := new(EdbDataList)
  431. item.DataTime = newDate.Format(utils.FormatDate)
  432. item.EdbInfoId = v.EdbInfoId
  433. item.Value = v.Value
  434. item.EdbDataId = v.EdbDataId
  435. item.DataTimestamp = timestamp
  436. items.Items = append(items.Items, item)
  437. }
  438. }
  439. result.List = append(result.List, items)
  440. } else {
  441. for _, v := range dataList {
  442. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  443. if err != nil {
  444. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  445. return result, err
  446. }
  447. timestamp := dateTime.UnixNano() / 1e6
  448. item := new(EdbDataList)
  449. item.DataTime = dateTime.Format(utils.FormatDate)
  450. item.EdbInfoId = v.EdbInfoId
  451. item.Value = v.Value
  452. item.EdbDataId = v.EdbDataId
  453. item.DataTimestamp = timestamp
  454. items.Items = append(items.Items, item)
  455. }
  456. result.List = append(result.List, items)
  457. }
  458. }
  459. }
  460. return
  461. }
  462. // AddCalculateQuarterV6 指标季度数据计算(季节性图表)
  463. func AddCalculateQuarterV6(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  464. var errMsg string
  465. defer func() {
  466. if errMsg != "" {
  467. fmt.Println("errMsg:", errMsg)
  468. }
  469. }()
  470. endDate := dataList[len(dataList)-1].DataTime
  471. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  472. if err != nil {
  473. return result, err
  474. }
  475. thisMonth := int(endDateForm.Month())
  476. result = new(EdbDataResult)
  477. var yearArr []int
  478. yearMap := make(map[int]int)
  479. var cureentDate time.Time
  480. if thisMonth < 11 {
  481. for k, v := range dataList {
  482. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  483. if err != nil {
  484. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  485. return result, err
  486. }
  487. if k == len(dataList)-1 {
  488. cureentDate = dateTime
  489. }
  490. year := dateTime.Year()
  491. if _, ok := yearMap[year]; !ok {
  492. yearArr = append(yearArr, year)
  493. }
  494. yearMap[year] = year
  495. }
  496. } else {
  497. for k, v := range dataList {
  498. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  499. if err != nil {
  500. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  501. return result, err
  502. }
  503. if k == len(dataList)-1 {
  504. cureentDate = dateTime
  505. }
  506. year := dateTime.Year() + 1
  507. if _, ok := yearMap[year]; !ok {
  508. yearArr = append(yearArr, year)
  509. }
  510. yearMap[year] = year
  511. }
  512. }
  513. //排序
  514. fmt.Println("yearArr:", yearArr)
  515. thisYear := cureentDate.Year()
  516. //thisMonth := int(cureentDate.Month())
  517. fmt.Println("thisMonth:", thisMonth)
  518. for ky, vy := range yearArr {
  519. fmt.Printf("line 432:ky:%d, vy:%d, thisYear:%d, thisMonth:%d", ky, vy, thisYear, thisMonth)
  520. fmt.Println("")
  521. if thisMonth < 11 {
  522. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  523. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  524. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  525. if err != nil {
  526. errMsg = "生成当前春节失败,Err:" + err.Error()
  527. return result, err
  528. }
  529. preYear := vy
  530. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  531. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  532. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  533. if err != nil {
  534. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  535. return result, err
  536. }
  537. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  538. fmt.Println("day:", day, currentYearCjglDate, preYearCjglDate)
  539. items := new(EdbDataItems)
  540. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  541. items.Year = preYear
  542. for _, v := range dataList {
  543. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  544. if err != nil {
  545. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  546. return result, err
  547. }
  548. newDate := dateTime.AddDate(0, 0, int(day))
  549. timestamp := newDate.UnixNano() / 1e6
  550. item := new(EdbDataList)
  551. item.DataTime = newDate.Format(utils.FormatDate)
  552. item.EdbInfoId = v.EdbInfoId
  553. item.Value = v.Value
  554. item.EdbDataId = v.EdbDataId
  555. item.DataTimestamp = timestamp
  556. items.Items = append(items.Items, item)
  557. }
  558. result.List = append(result.List, items)
  559. } else {
  560. nextYear := thisYear + 1
  561. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  562. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  563. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  564. if err != nil {
  565. errMsg = "生成当前春节失败,Err:" + err.Error()
  566. return result, err
  567. }
  568. preYear := vy
  569. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  570. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  571. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  572. if err != nil {
  573. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  574. return result, err
  575. }
  576. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  577. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  578. items := new(EdbDataItems)
  579. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  580. items.Year = preYear
  581. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  582. //if ky+1 < len(yearArr) {
  583. for _, v := range dataList {
  584. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  585. if err != nil {
  586. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  587. return result, err
  588. }
  589. newDate := dateTime.AddDate(0, 0, int(day))
  590. timestamp := newDate.UnixNano() / 1e6
  591. item := new(EdbDataList)
  592. item.DataTime = newDate.Format(utils.FormatDate)
  593. item.EdbInfoId = v.EdbInfoId
  594. item.Value = v.Value
  595. item.EdbDataId = v.EdbDataId
  596. item.DataTimestamp = timestamp
  597. items.Items = append(items.Items, item)
  598. }
  599. result.List = append(result.List, items)
  600. /*} else {
  601. for _, v := range dataList {
  602. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  603. if err != nil {
  604. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  605. return result, err
  606. }
  607. timestamp := dateTime.UnixNano() / 1e6
  608. item := new(EdbDataList)
  609. item.DataTime = dateTime.Format(utils.FormatDate)
  610. item.EdbInfoId = v.EdbInfoId
  611. item.Value = v.Value
  612. item.EdbDataId = v.EdbDataId
  613. item.DataTimestamp = timestamp
  614. items.Items = append(items.Items, item)
  615. }
  616. result.List = append(result.List, items)
  617. }*/
  618. }
  619. }
  620. return
  621. }
  622. type CorrelationInfo struct {
  623. LeadValue int `description:"领先值"`
  624. LeadUnit string `description:"领先单位"`
  625. StartDate string `description:"开始日期"`
  626. EndDate string `description:"结束日期"`
  627. EdbInfoIdFirst int `description:"A指标ID"`
  628. EdbInfoIdSecond int `description:"B指标ID"`
  629. PeriodData string `description:"X轴-期数数据"`
  630. CorrelationData string `description:"Y轴-相关性系数"`
  631. }
  632. type SeasonExtraItem struct {
  633. ChartLegend []SeasonChartLegend `description:"自定义的图例名称"`
  634. XStartDate string `description:"横坐标显示的起始日"`
  635. XEndDate string `description:"横坐标显示的截止日"`
  636. JumpYear int `description:"横坐标日期是否跨年,1跨年,0不跨年"`
  637. }
  638. type SeasonChartLegend struct {
  639. Name string
  640. Value string
  641. }
  642. type QuarterDataList []*QuarterData
  643. func (m QuarterDataList) Len() int {
  644. return len(m)
  645. }
  646. func (m QuarterDataList) Less(i, j int) bool {
  647. return m[i].Years < m[j].Years
  648. }
  649. func (m QuarterDataList) Swap(i, j int) {
  650. m[i], m[j] = m[j], m[i]
  651. }
  652. type ChartDwCollectReq struct {
  653. UniqueCode string
  654. Token string
  655. }