chart.go 13 KB

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