chart.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "hongze/hongze_chart_lib/utils"
  7. "strconv"
  8. "time"
  9. "github.com/nosixtools/solarlunar"
  10. )
  11. type ChartInfo struct {
  12. ChartInfoId int `json:"-" orm:"column(chart_info_id);pk"`
  13. ChartName string `description:"来源名称"`
  14. ChartNameEn string `description:"英文图表名称"`
  15. ChartClassifyId int `json:"-" description:"图表分类id"`
  16. SysUserId int `json:"-"`
  17. SysUserRealName string `json:"-"`
  18. UniqueCode string `description:"图表唯一编码"`
  19. CreateTime time.Time `json:"-"`
  20. ModifyTime time.Time `json:"-"`
  21. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
  22. StartDate string `description:"自定义开始日期"`
  23. EndDate string `description:"自定义结束日期"`
  24. IsSetName int `description:"设置名称"`
  25. EdbInfoIds string `description:"指标id"`
  26. ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
  27. Calendar string `description:"公历/农历"`
  28. SeasonStartDate string `description:"季节性图开始日期"`
  29. SeasonEndDate string `description:"季节性图开始日期"`
  30. ChartImage string `description:"图表图片"`
  31. BarConfig string `description:"柱方图的配置,json数据" json:"-"`
  32. LeftMin string `description:"图表左侧最小值"`
  33. LeftMax string `description:"图表左侧最大值"`
  34. Source int `description:"1:ETA图库;2:商品价格曲线"`
  35. UnitEn string `description:"英文图表名称"`
  36. ExtraConfig string `description:"图表额外配置,json数据" json:"-"`
  37. ChartSource string `description:"图表来源str"`
  38. ChartSourceEn string `description:"图表来源(英文)"`
  39. }
  40. func GetChartInfoByUniqueCode(uniqueCode string) (item *ChartInfo, err error) {
  41. o := orm.NewOrmUsingDB("data")
  42. sql := ` SELECT * FROM chart_info WHERE unique_code=? `
  43. err = o.Raw(sql, uniqueCode).QueryRow(&item)
  44. return
  45. }
  46. type ChartEdbInfoMapping struct {
  47. EdbInfoId int `description:"指标id"`
  48. SourceName string `description:"来源名称"`
  49. Source int `description:"来源id"`
  50. EdbCode string `description:"指标编码"`
  51. EdbName string `description:"指标名称"`
  52. EdbAliasName string `description:"指标名称(别名)"`
  53. EdbAliasNameEn string `description:"英文指标名称(别名)"`
  54. EdbNameEn string `description:"英文指标名称"`
  55. Frequency string `description:"频率"`
  56. FrequencyEn string `description:"英文频率"`
  57. Unit string `description:"单位"`
  58. UnitEn string `description:"英文单位"`
  59. StartDate string `description:"起始日期"`
  60. EndDate string `description:"终止日期"`
  61. ModifyTime string `description:"指标最后更新时间"`
  62. ChartEdbMappingId int `description:"图表指标id" json:"-"`
  63. ChartInfoId int `description:"图表id"`
  64. MaxData float64 `description:"上限"`
  65. MinData float64 `description:"下限"`
  66. IsOrder bool `description:"true:正序,false:逆序"`
  67. IsAxis int `description:"1:左轴,0:右轴"`
  68. EdbInfoType int `description:"1:标准指标,0:领先指标"`
  69. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  70. LeadValue int `description:"领先值"`
  71. LeadUnit string `description:"领先单位"`
  72. LeadUnitEn string `description:"领先英文单位"`
  73. ChartStyle string `description:"图表类型"`
  74. ChartColor string `description:"颜色"`
  75. ChartWidth float64 `description:"线条大小"`
  76. DataList interface{}
  77. EdbInfoCategoryType int `description:"0:普通指标,1:预测指标"`
  78. PredictChartColor string `description:"预测数据的颜色"`
  79. ChartType int `description:"生成样式:1:曲线图,2:季节性图,3:面积图,4:柱状图,5:散点图,6:组合图"`
  80. LatestDate string `description:"数据最新日期"`
  81. LatestValue float64 `description:"数据最新值"`
  82. UniqueCode string `description:"指标唯一编码"`
  83. MinValue float64 `json:"-" description:"最小值"`
  84. MaxValue float64 `json:"-" description:"最大值"`
  85. IsNullData bool `json:"-" description:"是否空数据"`
  86. MappingSource int `description:"1:ETA图库;2:商品价格曲线"`
  87. }
  88. func GetChartEdbMappingList(chartInfoId int) (list []*ChartEdbInfoMapping, err error) {
  89. o := orm.NewOrmUsingDB("data")
  90. 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`
  91. 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
  92. FROM chart_edb_mapping AS a
  93. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  94. WHERE chart_info_id=?
  95. ORDER BY chart_edb_mapping_id ASC `
  96. _, err = o.Raw(sql, chartInfoId).QueryRows(&list)
  97. return
  98. }
  99. type EdbDataList struct {
  100. EdbDataId int `json:"-" description:" 指标数据ID"`
  101. EdbInfoId int `json:"-" description:"指标ID"`
  102. DataTime string `json:"-" description:"数据日期"`
  103. DataTimestamp int64 `description:"数据日期"`
  104. Value float64 `description:"数据值"`
  105. }
  106. func GetEdbDataList(source, endInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  107. tableName := GetEdbDataTableName(source)
  108. if tableName == "" {
  109. err = errors.New("无效的渠道:" + strconv.Itoa(source))
  110. list = make([]*EdbDataList, 0)
  111. return list, err
  112. }
  113. var pars []interface{}
  114. var condition string
  115. if startDate != "" {
  116. condition += ` AND data_time>=? `
  117. pars = append(pars, startDate)
  118. }
  119. if endDate != "" {
  120. condition += ` AND data_time<=? `
  121. pars = append(pars, endDate)
  122. }
  123. sql := `SELECT edb_data_id,edb_info_id,data_time,TRUNCATE(value,4) AS value,data_timestamp FROM %s WHERE edb_info_id=? `
  124. if condition != "" {
  125. sql += condition
  126. }
  127. sql += ` ORDER BY data_time ASC `
  128. sql = fmt.Sprintf(sql, tableName)
  129. o := orm.NewOrmUsingDB("data")
  130. _, err = o.Raw(sql, endInfoId, pars).QueryRows(&list)
  131. return
  132. }
  133. type EdbDataItems struct {
  134. Items []*EdbDataList
  135. Year int
  136. BetweenDay int `json:"-" description:"公历与农历之间相差的天数"`
  137. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  138. }
  139. type EdbDataResult struct {
  140. List []*EdbDataItems
  141. }
  142. type QuarterData struct {
  143. Year int
  144. DataList []*EdbDataList
  145. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  146. }
  147. type ChartInfoDetailResp struct {
  148. ChartInfo *ChartInfo
  149. EdbInfoList []*ChartEdbInfoMapping
  150. XEdbIdValue []int `description:"柱方图的x轴数据,指标id"`
  151. YDataList []YData `description:"柱方图的y轴数据"`
  152. XDataList []XData `description:"商品价格曲线的X轴数据"`
  153. CorrelationChartInfo *CorrelationInfo `description:"相关性图表信息"`
  154. DataResp interface{} `description:"图表数据,根据图的类型而定的,没有确定的数据格式"`
  155. }
  156. // XData 商品价格曲线的的x轴数据
  157. type XData struct {
  158. Name string `description:"别名"`
  159. NameEn string `description:"英文别名"`
  160. }
  161. // YData 柱方图的y轴数据
  162. type YData struct {
  163. Date string `description:"数据日期"`
  164. ConfigDate time.Time `description:"配置的日期" json:"-"`
  165. Color string `description:"数据颜色"`
  166. Name string `description:"别名"`
  167. NameEn string `description:"英文别名"`
  168. Value []float64 `description:"每个指标的值"`
  169. NoDataEdbList []int `description:"没有数据的指标列表"`
  170. XEdbInfoIdList []int `description:"对应X轴的指标id列表"`
  171. EdbValMap map[int]float64 `description:"指标与值的对应" json:"-"`
  172. M []int `description:"对应开始日期的间隔值" json:"-"`
  173. NameList []string `description:"每个值对应的名称"`
  174. EnNameList []string `description:"每个值对应的英文名称"`
  175. }
  176. // RollingCorrelationChartDataResp 滚动相关性图
  177. type RollingCorrelationChartDataResp struct {
  178. XDateTimeValue []string `description:"滚动相关性图的x轴数据,日期数据"`
  179. YDataList []YData `description:"滚动相关性图的y轴数据"`
  180. }
  181. // 指标季度数据计算(公历转农历)
  182. func AddCalculateQuarterV5(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  183. var errMsg string
  184. defer func() {
  185. if errMsg != "" {
  186. fmt.Println("errMsg:", errMsg)
  187. }
  188. }()
  189. if len(dataList) <= 0 {
  190. result.List = make([]*EdbDataItems, 0)
  191. return
  192. }
  193. endDate := dataList[len(dataList)-1].DataTime
  194. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  195. if err != nil {
  196. return result, err
  197. }
  198. thisMonth := int(endDateForm.Month())
  199. result = new(EdbDataResult)
  200. var yearArr []int
  201. yearMap := make(map[int]int)
  202. var cureentDate time.Time
  203. if thisMonth < 11 {
  204. for k, v := range dataList {
  205. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  206. if err != nil {
  207. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  208. return result, err
  209. }
  210. if k == len(dataList)-1 {
  211. cureentDate = dateTime
  212. }
  213. year := dateTime.Year()
  214. if _, ok := yearMap[year]; !ok {
  215. yearArr = append(yearArr, year)
  216. }
  217. yearMap[year] = year
  218. }
  219. } else {
  220. for k, v := range dataList {
  221. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  222. if err != nil {
  223. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  224. return result, err
  225. }
  226. if k == len(dataList)-1 {
  227. cureentDate = dateTime
  228. }
  229. year := dateTime.Year() + 1
  230. if _, ok := yearMap[year]; !ok {
  231. yearArr = append(yearArr, year)
  232. }
  233. yearMap[year] = year
  234. }
  235. }
  236. //排序
  237. fmt.Println("yearArr:", yearArr)
  238. thisYear := cureentDate.Year()
  239. //thisMonth := int(cureentDate.Month())
  240. fmt.Println("thisMonth:", thisMonth)
  241. for ky, vy := range yearArr {
  242. fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
  243. if thisMonth < 11 {
  244. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  245. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  246. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  247. if err != nil {
  248. errMsg = "生成当前春节失败,Err:" + err.Error()
  249. return result, err
  250. }
  251. preYear := vy
  252. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  253. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  254. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  255. if err != nil {
  256. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  257. return result, err
  258. }
  259. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  260. items := new(EdbDataItems)
  261. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  262. items.Year = preYear
  263. for _, v := range dataList {
  264. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  265. if err != nil {
  266. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  267. return result, err
  268. }
  269. newDate := dateTime.AddDate(0, 0, int(day))
  270. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  271. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  272. if newDate.Before(selectDate) || newDate == selectDate {
  273. timestamp := newDate.UnixNano() / 1e6
  274. item := new(EdbDataList)
  275. item.DataTime = newDate.Format(utils.FormatDate)
  276. item.EdbInfoId = v.EdbInfoId
  277. item.Value = v.Value
  278. item.EdbDataId = v.EdbDataId
  279. item.DataTimestamp = timestamp
  280. items.Items = append(items.Items, item)
  281. }
  282. }
  283. result.List = append(result.List, items)
  284. } else {
  285. nextYear := thisYear + 1
  286. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  287. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  288. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  289. if err != nil {
  290. errMsg = "生成当前春节失败,Err:" + err.Error()
  291. return result, err
  292. }
  293. preYear := vy
  294. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  295. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  296. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  297. if err != nil {
  298. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  299. return result, err
  300. }
  301. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  302. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  303. items := new(EdbDataItems)
  304. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  305. items.Year = preYear - 1
  306. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  307. if ky+1 < len(yearArr) {
  308. for _, v := range dataList {
  309. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  310. if err != nil {
  311. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  312. return result, err
  313. }
  314. newDate := dateTime.AddDate(0, 0, int(day))
  315. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  316. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  317. if newDate.Before(selectDate) || newDate == selectDate {
  318. timestamp := newDate.UnixNano() / 1e6
  319. item := new(EdbDataList)
  320. item.DataTime = newDate.Format(utils.FormatDate)
  321. item.EdbInfoId = v.EdbInfoId
  322. item.Value = v.Value
  323. item.EdbDataId = v.EdbDataId
  324. item.DataTimestamp = timestamp
  325. items.Items = append(items.Items, item)
  326. }
  327. }
  328. result.List = append(result.List, items)
  329. } else {
  330. for _, v := range dataList {
  331. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  332. if err != nil {
  333. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  334. return result, err
  335. }
  336. timestamp := dateTime.UnixNano() / 1e6
  337. item := new(EdbDataList)
  338. item.DataTime = dateTime.Format(utils.FormatDate)
  339. item.EdbInfoId = v.EdbInfoId
  340. item.Value = v.Value
  341. item.EdbDataId = v.EdbDataId
  342. item.DataTimestamp = timestamp
  343. items.Items = append(items.Items, item)
  344. }
  345. result.List = append(result.List, items)
  346. }
  347. }
  348. }
  349. return
  350. }
  351. type CorrelationInfo struct {
  352. LeadValue int `description:"领先值"`
  353. LeadUnit string `description:"领先单位"`
  354. StartDate string `description:"开始日期"`
  355. EndDate string `description:"结束日期"`
  356. EdbInfoIdFirst int `description:"A指标ID"`
  357. EdbInfoIdSecond int `description:"B指标ID"`
  358. PeriodData string `description:"X轴-期数数据"`
  359. CorrelationData string `description:"Y轴-相关性系数"`
  360. }