chart.go 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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. Unit string `description:"中文单位名称"`
  45. UnitEn 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 && utils.UseMongo {
  139. return getEdbDataListByMongo(source, subSource, edbInfoId, startDate, endDate)
  140. }
  141. if source == utils.DATA_SOURCE_THS && subSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && utils.UseMongo {
  142. return getThsHfEdbDataListByMongo(source, subSource, edbInfoId, startDate, endDate)
  143. }
  144. return getEdbDataListByMysql(source, subSource, edbInfoId, startDate, endDate)
  145. }
  146. // getEdbDataListByMysql
  147. // @Description: 从mysql中获取指标的数据(日期正序返回)
  148. // @author: Roc
  149. // @datetime 2024-05-08 16:49:48
  150. // @param source int
  151. // @param subSource int
  152. // @param edbInfoId int
  153. // @param startDate string
  154. // @param endDate string
  155. // @return list []*EdbDataList
  156. // @return err error
  157. func getEdbDataListByMysql(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  158. tableName := GetEdbDataTableName(source, subSource)
  159. if tableName == "" {
  160. err = errors.New("无效的渠道:" + strconv.Itoa(source))
  161. list = make([]*EdbDataList, 0)
  162. return list, err
  163. }
  164. var pars []interface{}
  165. var condition string
  166. if startDate != "" {
  167. condition += ` AND data_time>=? `
  168. pars = append(pars, startDate)
  169. }
  170. if endDate != "" {
  171. condition += ` AND data_time<=? `
  172. pars = append(pars, endDate)
  173. }
  174. sql := `SELECT edb_data_id,edb_info_id,data_time,value,data_timestamp FROM %s WHERE edb_info_id=? `
  175. if condition != "" {
  176. sql += condition
  177. }
  178. sql += ` ORDER BY data_time ASC `
  179. sql = fmt.Sprintf(sql, tableName)
  180. o := orm.NewOrmUsingDB("data")
  181. _, err = o.Raw(sql, edbInfoId, pars).QueryRows(&list)
  182. return
  183. }
  184. // getEdbDataListByMongo
  185. // @Description: 从mongo中获取指标的数据(日期正序返回)
  186. // @author: Roc
  187. // @datetime 2024-05-08 16:49:59
  188. // @param source int
  189. // @param subSource int
  190. // @param edbInfoId int
  191. // @param startDate string
  192. // @param endDate string
  193. // @return list []*EdbDataList
  194. // @return err error
  195. func getEdbDataListByMongo(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  196. list = make([]*EdbDataList, 0)
  197. mogDataObj := mgo.EdbDataBusiness{}
  198. // 构建查询条件
  199. queryConditions := bson.M{
  200. "edb_info_id": edbInfoId,
  201. }
  202. // 日期
  203. dateCondition, err := mgo.BuildDateCondition(startDate, endDate)
  204. if err != nil {
  205. return
  206. }
  207. if len(dateCondition) > 0 {
  208. queryConditions["data_time"] = dateCondition
  209. }
  210. // 获取列表数据
  211. tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"data_time"})
  212. if tmpErr != nil {
  213. err = tmpErr
  214. return
  215. }
  216. for k, v := range tmpDataList {
  217. list = append(list, &EdbDataList{
  218. EdbDataId: k + 1,
  219. EdbInfoId: v.EdbInfoId,
  220. DataTime: v.DataTime.Format(utils.FormatDate),
  221. DataTimestamp: v.DataTimestamp,
  222. Value: v.Value,
  223. })
  224. }
  225. return
  226. }
  227. type EdbDataItemList struct {
  228. EdbDataId int `json:"-" description:" 指标数据ID"`
  229. EdbInfoId int `json:"-" description:"指标ID"`
  230. DataTime string `description:"数据日期"`
  231. DataTimestamp int64 `description:"数据日期"`
  232. Value float64 `description:"数据值"`
  233. }
  234. type EdbDataItems struct {
  235. Items []*EdbDataList
  236. Year int
  237. BetweenDay int `json:"-" description:"公历与农历之间相差的天数"`
  238. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  239. }
  240. type EdbDataResult struct {
  241. List []*EdbDataItems
  242. }
  243. type QuarterData struct {
  244. Year string
  245. DataList []*EdbDataList
  246. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  247. ChartLegend string
  248. Years string
  249. }
  250. type QuarterXDateItem struct {
  251. StartDate time.Time
  252. EndDate time.Time
  253. ShowName string
  254. ChartLegend string
  255. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  256. }
  257. type ChartInfoDetailResp struct {
  258. ChartInfo *ChartInfo
  259. EdbInfoList []*ChartEdbInfoMapping
  260. XEdbIdValue []int `description:"柱方图的x轴数据,指标id"`
  261. YDataList []YData `description:"柱方图的y轴数据"`
  262. XDataList []XData `description:"商品价格曲线的X轴数据"`
  263. CorrelationChartInfo *CorrelationInfo `description:"相关性图表信息"`
  264. DataResp interface{} `description:"图表数据,根据图的类型而定的,没有确定的数据格式"`
  265. WaterMark string `description:"水印"`
  266. IsCollect bool `description:"是否收藏"`
  267. }
  268. // XData 商品价格曲线的的x轴数据
  269. type XData struct {
  270. Name string `description:"别名"`
  271. NameEn string `description:"英文别名"`
  272. IsHide int `description:"是否隐藏,0不隐藏,1隐藏"`
  273. }
  274. // YData 柱方图的y轴数据
  275. type YData struct {
  276. Date string `description:"数据日期"`
  277. ConfigDate time.Time `description:"配置的日期" json:"-"`
  278. Color string `description:"数据颜色"`
  279. Name string `description:"别名"`
  280. NameEn string `description:"英文别名"`
  281. Value []float64 `description:"每个指标的值"`
  282. NoDataEdbList []int `description:"没有数据的指标列表"`
  283. XEdbInfoIdList []int `description:"对应X轴的指标id列表"`
  284. EdbValMap map[int]float64 `description:"指标与值的对应" json:"-"`
  285. M []int `description:"对应开始日期的间隔值" json:"-"`
  286. NameList []string `description:"每个值对应的名称"`
  287. EnNameList []string `description:"每个值对应的英文名称"`
  288. SeriesEdb struct {
  289. SeriesId int `description:"因子指标系列ID"`
  290. EdbInfoId int `description:"指标ID"`
  291. } `description:"对应的系列指标"`
  292. }
  293. // RollingCorrelationChartDataResp 滚动相关性图
  294. type RollingCorrelationChartDataResp struct {
  295. XDateTimeValue []string `description:"滚动相关性图的x轴数据,日期数据"`
  296. YDataList []YData `description:"滚动相关性图的y轴数据"`
  297. }
  298. // 指标季度数据计算(公历转农历)
  299. func AddCalculateQuarterV5(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  300. var errMsg string
  301. defer func() {
  302. if errMsg != "" {
  303. fmt.Println("errMsg:", errMsg)
  304. }
  305. }()
  306. if len(dataList) <= 0 {
  307. result.List = make([]*EdbDataItems, 0)
  308. return
  309. }
  310. endDate := dataList[len(dataList)-1].DataTime
  311. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  312. if err != nil {
  313. return result, err
  314. }
  315. thisMonth := int(endDateForm.Month())
  316. result = new(EdbDataResult)
  317. var yearArr []int
  318. yearMap := make(map[int]int)
  319. var cureentDate time.Time
  320. if thisMonth < 11 {
  321. for k, v := range dataList {
  322. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  323. if err != nil {
  324. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  325. return result, err
  326. }
  327. if k == len(dataList)-1 {
  328. cureentDate = dateTime
  329. }
  330. year := dateTime.Year()
  331. if _, ok := yearMap[year]; !ok {
  332. yearArr = append(yearArr, year)
  333. }
  334. yearMap[year] = year
  335. }
  336. } else {
  337. for k, v := range dataList {
  338. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  339. if err != nil {
  340. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  341. return result, err
  342. }
  343. if k == len(dataList)-1 {
  344. cureentDate = dateTime
  345. }
  346. year := dateTime.Year() + 1
  347. if _, ok := yearMap[year]; !ok {
  348. yearArr = append(yearArr, year)
  349. }
  350. yearMap[year] = year
  351. }
  352. }
  353. //排序
  354. fmt.Println("yearArr:", yearArr)
  355. thisYear := cureentDate.Year()
  356. //thisMonth := int(cureentDate.Month())
  357. fmt.Println("thisMonth:", thisMonth)
  358. for ky, vy := range yearArr {
  359. fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
  360. if thisMonth < 11 {
  361. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  362. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  363. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  364. if err != nil {
  365. errMsg = "生成当前春节失败,Err:" + err.Error()
  366. return result, err
  367. }
  368. preYear := vy
  369. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  370. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  371. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  372. if err != nil {
  373. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  374. return result, err
  375. }
  376. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  377. items := new(EdbDataItems)
  378. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  379. items.Year = preYear
  380. for _, v := range dataList {
  381. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  382. if err != nil {
  383. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  384. return result, err
  385. }
  386. newDate := dateTime.AddDate(0, 0, int(day))
  387. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  388. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  389. if newDate.Before(selectDate) || newDate == selectDate {
  390. timestamp := newDate.UnixNano() / 1e6
  391. item := new(EdbDataList)
  392. item.DataTime = newDate.Format(utils.FormatDate)
  393. item.EdbInfoId = v.EdbInfoId
  394. item.Value = v.Value
  395. item.EdbDataId = v.EdbDataId
  396. item.DataTimestamp = timestamp
  397. items.Items = append(items.Items, item)
  398. }
  399. }
  400. result.List = append(result.List, items)
  401. } else {
  402. nextYear := thisYear + 1
  403. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  404. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  405. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  406. if err != nil {
  407. errMsg = "生成当前春节失败,Err:" + err.Error()
  408. return result, err
  409. }
  410. preYear := vy
  411. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  412. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  413. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  414. if err != nil {
  415. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  416. return result, err
  417. }
  418. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  419. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  420. items := new(EdbDataItems)
  421. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  422. items.Year = preYear - 1
  423. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  424. if ky+1 < len(yearArr) {
  425. for _, v := range dataList {
  426. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  427. if err != nil {
  428. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  429. return result, err
  430. }
  431. newDate := dateTime.AddDate(0, 0, int(day))
  432. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  433. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  434. if newDate.Before(selectDate) || newDate == selectDate {
  435. timestamp := newDate.UnixNano() / 1e6
  436. item := new(EdbDataList)
  437. item.DataTime = newDate.Format(utils.FormatDate)
  438. item.EdbInfoId = v.EdbInfoId
  439. item.Value = v.Value
  440. item.EdbDataId = v.EdbDataId
  441. item.DataTimestamp = timestamp
  442. items.Items = append(items.Items, item)
  443. }
  444. }
  445. result.List = append(result.List, items)
  446. } else {
  447. for _, v := range dataList {
  448. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  449. if err != nil {
  450. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  451. return result, err
  452. }
  453. timestamp := dateTime.UnixNano() / 1e6
  454. item := new(EdbDataList)
  455. item.DataTime = dateTime.Format(utils.FormatDate)
  456. item.EdbInfoId = v.EdbInfoId
  457. item.Value = v.Value
  458. item.EdbDataId = v.EdbDataId
  459. item.DataTimestamp = timestamp
  460. items.Items = append(items.Items, item)
  461. }
  462. result.List = append(result.List, items)
  463. }
  464. }
  465. }
  466. return
  467. }
  468. // AddCalculateQuarterV6 指标季度数据计算(季节性图表)
  469. func AddCalculateQuarterV6(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  470. var errMsg string
  471. defer func() {
  472. if errMsg != "" {
  473. fmt.Println("errMsg:", errMsg)
  474. }
  475. }()
  476. endDate := dataList[len(dataList)-1].DataTime
  477. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  478. if err != nil {
  479. return result, err
  480. }
  481. thisMonth := int(endDateForm.Month())
  482. result = new(EdbDataResult)
  483. var yearArr []int
  484. yearMap := make(map[int]int)
  485. var cureentDate time.Time
  486. if thisMonth < 11 {
  487. for k, v := range dataList {
  488. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  489. if err != nil {
  490. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  491. return result, err
  492. }
  493. if k == len(dataList)-1 {
  494. cureentDate = dateTime
  495. }
  496. year := dateTime.Year()
  497. if _, ok := yearMap[year]; !ok {
  498. yearArr = append(yearArr, year)
  499. }
  500. yearMap[year] = year
  501. }
  502. } else {
  503. for k, v := range dataList {
  504. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  505. if err != nil {
  506. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  507. return result, err
  508. }
  509. if k == len(dataList)-1 {
  510. cureentDate = dateTime
  511. }
  512. year := dateTime.Year() + 1
  513. if _, ok := yearMap[year]; !ok {
  514. yearArr = append(yearArr, year)
  515. }
  516. yearMap[year] = year
  517. }
  518. }
  519. //排序
  520. fmt.Println("yearArr:", yearArr)
  521. thisYear := cureentDate.Year()
  522. //thisMonth := int(cureentDate.Month())
  523. fmt.Println("thisMonth:", thisMonth)
  524. for ky, vy := range yearArr {
  525. fmt.Printf("line 432:ky:%d, vy:%d, thisYear:%d, thisMonth:%d", ky, vy, thisYear, thisMonth)
  526. fmt.Println("")
  527. if thisMonth < 11 {
  528. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  529. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  530. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  531. if err != nil {
  532. errMsg = "生成当前春节失败,Err:" + err.Error()
  533. return result, err
  534. }
  535. preYear := vy
  536. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  537. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  538. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  539. if err != nil {
  540. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  541. return result, err
  542. }
  543. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  544. fmt.Println("day:", day, currentYearCjglDate, preYearCjglDate)
  545. items := new(EdbDataItems)
  546. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  547. items.Year = preYear
  548. for _, v := range dataList {
  549. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  550. if err != nil {
  551. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  552. return result, err
  553. }
  554. newDate := dateTime.AddDate(0, 0, int(day))
  555. timestamp := newDate.UnixNano() / 1e6
  556. item := new(EdbDataList)
  557. item.DataTime = newDate.Format(utils.FormatDate)
  558. item.EdbInfoId = v.EdbInfoId
  559. item.Value = v.Value
  560. item.EdbDataId = v.EdbDataId
  561. item.DataTimestamp = timestamp
  562. items.Items = append(items.Items, item)
  563. }
  564. result.List = append(result.List, items)
  565. } else {
  566. nextYear := thisYear + 1
  567. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  568. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  569. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  570. if err != nil {
  571. errMsg = "生成当前春节失败,Err:" + err.Error()
  572. return result, err
  573. }
  574. preYear := vy
  575. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  576. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  577. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  578. if err != nil {
  579. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  580. return result, err
  581. }
  582. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  583. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  584. items := new(EdbDataItems)
  585. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  586. items.Year = preYear
  587. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  588. //if ky+1 < len(yearArr) {
  589. for _, v := range dataList {
  590. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  591. if err != nil {
  592. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  593. return result, err
  594. }
  595. newDate := dateTime.AddDate(0, 0, int(day))
  596. timestamp := newDate.UnixNano() / 1e6
  597. item := new(EdbDataList)
  598. item.DataTime = newDate.Format(utils.FormatDate)
  599. item.EdbInfoId = v.EdbInfoId
  600. item.Value = v.Value
  601. item.EdbDataId = v.EdbDataId
  602. item.DataTimestamp = timestamp
  603. items.Items = append(items.Items, item)
  604. }
  605. result.List = append(result.List, items)
  606. /*} else {
  607. for _, v := range dataList {
  608. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  609. if err != nil {
  610. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  611. return result, err
  612. }
  613. timestamp := dateTime.UnixNano() / 1e6
  614. item := new(EdbDataList)
  615. item.DataTime = dateTime.Format(utils.FormatDate)
  616. item.EdbInfoId = v.EdbInfoId
  617. item.Value = v.Value
  618. item.EdbDataId = v.EdbDataId
  619. item.DataTimestamp = timestamp
  620. items.Items = append(items.Items, item)
  621. }
  622. result.List = append(result.List, items)
  623. }*/
  624. }
  625. }
  626. return
  627. }
  628. type CorrelationInfo struct {
  629. LeadValue int `description:"领先值"`
  630. LeadUnit string `description:"领先单位"`
  631. StartDate string `description:"开始日期"`
  632. EndDate string `description:"结束日期"`
  633. EdbInfoIdFirst int `description:"A指标ID"`
  634. EdbInfoIdSecond int `description:"B指标ID"`
  635. PeriodData string `description:"X轴-期数数据"`
  636. CorrelationData string `description:"Y轴-相关性系数"`
  637. AnalysisMode int `description:"分析模式: 0-单因子; 1-多因子"`
  638. }
  639. type SeasonExtraItem struct {
  640. ChartLegend []SeasonChartLegend `description:"自定义的图例名称"`
  641. XStartDate string `description:"横坐标显示的起始日"`
  642. XEndDate string `description:"横坐标显示的截止日"`
  643. JumpYear int `description:"横坐标日期是否跨年,1跨年,0不跨年"`
  644. RightAxis SeasonRightAxis `description:"自定义右轴指标"`
  645. MaxMinLimits MaxMinLimits `description:"自定义同期上下限"`
  646. SamePeriodAverage SamePeriodAverage `description:"自定义同期均线"`
  647. SamePeriodStandardDeviation SamePeriodStandardDeviation `description:"自定义同期标准差"`
  648. }
  649. // 自定义右轴指标
  650. type SeasonRightAxis struct {
  651. IndicatorType int `description:"右轴指标类型 1:左轴指标同比,2:指标库,3:预测指标 "`
  652. Style string `description:"生成样式"`
  653. Shape string `description:"形状"`
  654. ChartColor string `description:"图表颜色"`
  655. Size int `description:"大小"`
  656. Legend string `description:"图例名称"`
  657. NumFormat int `description:"数值格式 1:百分比 2:小数"`
  658. IsConnected int `description:"是否连接 0不连接 1连接"`
  659. LineColor string `description:"线条颜色"`
  660. LineWidth float64 `description:"线条宽度"`
  661. LineStyle string `description:"线条样式"`
  662. IsShow bool `description:"是否显示"`
  663. IsAdd bool `description:"是否添加"`
  664. }
  665. // 自定义同期上下限
  666. type MaxMinLimits struct {
  667. Color string `description:"颜色"`
  668. Year int `description:"上下限取值范围"`
  669. Legend string `description:"图例名称"`
  670. IsShow bool `description:"是否显示"`
  671. IsAdd bool `description:"是否添加"`
  672. }
  673. // 自定义同期均线
  674. type SamePeriodAverage struct {
  675. Color string `description:"颜色"`
  676. Year int `description:"均线取值范围"`
  677. Legend string `description:"图例名称"`
  678. LineType string `description:"线型"`
  679. LineWidth float64 `description:"线宽"`
  680. IsShow bool `description:"是否显示"`
  681. IsAdd bool `description:"是否添加"`
  682. }
  683. // 自定义同期均线
  684. type SamePeriodAverageResp struct {
  685. Color string `description:"颜色"`
  686. Year int `description:"均线取值范围"`
  687. Legend string `description:"图例名称"`
  688. LineType string `description:"线型"`
  689. LineWidth float64 `description:"线宽"`
  690. IsShow bool `description:"是否显示"`
  691. List []*SamePeriodAverageData `description:"自定义均线列表"`
  692. IsAdd bool `description:"是否添加"`
  693. }
  694. type SamePeriodAverageData struct {
  695. DataTime string `description:"数据日期"`
  696. DataTimestamp int64 `description:"数据日期时间戳"`
  697. Value float64 `description:"均值"`
  698. }
  699. // 自定义同期标准差
  700. type SamePeriodStandardDeviation struct {
  701. Color string `description:"颜色"`
  702. Year int `description:"标准差取值范围"`
  703. Legend string `description:"图例名称"`
  704. Multiple float64 `description:"标准差倍数"`
  705. IsShow bool `description:"是否显示"`
  706. IsAdd bool `description:"是否添加"`
  707. }
  708. type SamePeriodStandardDeviationResp struct {
  709. Color string `description:"颜色"`
  710. Year int `description:"标准差取值范围"`
  711. Legend string `description:"图例名称"`
  712. Multiple float64 `description:"标准差倍数"`
  713. IsShow bool `description:"是否显示"`
  714. List []*MaxMinLimitsData `description:"自定义标准差列表"`
  715. IsAdd bool `description:"是否添加"`
  716. }
  717. type SeasonChartResp struct {
  718. MaxMinLimits MaxMinLimitsResp `description:"自定义上下限"`
  719. SamePeriodAverage SamePeriodAverageResp `description:"自定义同期均线"`
  720. SamePeriodStandardDeviation SamePeriodStandardDeviationResp `description:"自定义同期标准差线"`
  721. RightAxis SeasonRightAxisResp `description:"自定义右轴指标"`
  722. }
  723. // 自定义右轴指标
  724. type SeasonRightAxisResp struct {
  725. SeasonRightAxis
  726. EdbInfoList []*ChartEdbInfoMapping
  727. }
  728. type MaxMinLimitsResp struct {
  729. List []*MaxMinLimitsData `description:"自定义上下限列表"`
  730. Color string `description:"颜色"`
  731. Year int `description:"上下限取值范围"`
  732. Legend string `description:"图例名称"`
  733. IsShow bool `description:"是否显示"`
  734. IsAdd bool `description:"是否添加"`
  735. }
  736. type MaxMinLimitsData struct {
  737. DataTime string `description:"数据日期"`
  738. DataTimestamp int64 `description:"数据日期时间戳"`
  739. MaxValue float64 `description:"最大值"`
  740. MinValue float64 `description:"最小值"`
  741. }
  742. type SeasonChartLegend struct {
  743. Name string
  744. Value string
  745. }
  746. type QuarterDataList []*QuarterData
  747. func (m QuarterDataList) Len() int {
  748. return len(m)
  749. }
  750. func (m QuarterDataList) Less(i, j int) bool {
  751. return m[i].Years < m[j].Years
  752. }
  753. func (m QuarterDataList) Swap(i, j int) {
  754. m[i], m[j] = m[j], m[i]
  755. }
  756. type ChartDwCollectReq struct {
  757. UniqueCode string
  758. Token string
  759. }
  760. // 截面组合图额外配置
  761. type ChartSectionExtraConf struct {
  762. DateConfList []*ChartSectionDateConfItem
  763. IsHeap int `description:"是否堆积(1.堆积,0不堆积)"`
  764. XDataList []XData `description:"横轴名称设置"`
  765. UnitList *ChartSectionCombineUnit `description:"纵轴单位设置"`
  766. BaseChartSeriesName string `description:"基准系列名称"`
  767. SortType int `description:"排序类型,0默认,1升序,2降序"`
  768. }
  769. // 截面组合图额外配置
  770. type ChartSectionAllExtraConf struct {
  771. ChartSectionExtraConf
  772. SeriesList []*ChartSectionSeriesItem
  773. }
  774. type ChartSectionDateConfItem struct {
  775. MoveForward int `description:"前移的期数"`
  776. EdbInfoId int `description:"指标ID"`
  777. EdbName string `description:"指标名称"`
  778. EdbNameEn string `description:"指标名称英文"`
  779. EdbInfoType int `description:"指标类型"`
  780. Frequency string `description:"频度"`
  781. EndDate string `description:"最新日期"`
  782. StaticDate string `description:"固定日期"`
  783. DateType int `description:"日期类型:0 指标日期,1系统日期, 2固定日期"`
  784. DateConfName string `description:"引用日期名称"` // 引用日期名称不能重复
  785. DateConfNameEn string `description:"引用日期英文名称"`
  786. DateChange []*ChartSectionDateChange
  787. }
  788. // 截面组合图引用日期配置
  789. type ChartSectionDateChange struct {
  790. Year int
  791. Month int
  792. Day int
  793. Frequency string `description:"频度变换"`
  794. FrequencyDay string `description:"频度的固定日期"`
  795. ChangeType int `description:"日期变换类型1日期位移,2指定频率"`
  796. }
  797. // 截面组合图系列配置
  798. type ChartSectionSeriesItem struct {
  799. ChartSeriesId int `description:"系列ID"`
  800. SeriesName string `description:"系列名称"` //系列名称不可同名
  801. SeriesNameEn string `description:"系列英文名称"`
  802. ChartStyle string `description:"图表类型"`
  803. ChartColor string `description:"颜色"`
  804. ChartWidth int `description:"线条大小"`
  805. IsPoint int `description:"是否用数据点展示(0 否,1是)"`
  806. IsNumber int `description:"是否用数值展示(0 否,1是)"`
  807. IsAxis int `description:"1:左轴,0:右轴"`
  808. MaxData float64 `description:"上限"`
  809. MinData float64 `description:"下限"`
  810. //IsOrder bool `description:"true:正序,false:逆序"`
  811. EdbInfoList []*ChartSectionSeriesEdbConf
  812. DataList []float64
  813. NoDataEdbIndex []int
  814. }
  815. type ChartSectionSeriesEdbConf struct {
  816. ChartSeriesEdbMappingId int `description:"映射ID"`
  817. ChartSeriesId int `description:"系列ID"`
  818. //ChartInfoId int `description:"图表ID"`
  819. EdbInfoId int `description:"指标id"`
  820. DateConf *ChartSectionSeriesDateConfItem
  821. EdbName string `description:"中文别名"`
  822. EdbNameEn string `description:"英文别名"`
  823. EdbInfoType int `description:"指标类型"`
  824. Unit string `description:"单位"`
  825. UnitEn string `description:"英文单位"`
  826. DateConfName string `description:"引用日期名称"`
  827. DateConfType int `description:"日期类型,0指标最新日期, 1引用日期"`
  828. }
  829. type ChartSectionCombineDataResp struct {
  830. DateConfList []*ChartSectionDateConfItem
  831. IsHeap int `description:"是否堆积(1.堆积,0不堆积)"`
  832. XDataList []XData `description:"横轴名称设置"`
  833. UnitList *ChartSectionCombineUnit `description:"纵轴单位设置"`
  834. BaseChartSeriesName string `description:"基准系列名称"`
  835. SortType int `description:"排序类型,0默认,1升序,2降序"`
  836. SeriesList []*ChartSectionSeriesItem
  837. LeftMin string `description:"图表左侧最小值"`
  838. LeftMax string `description:"图表左侧最大值"`
  839. RightMin string `description:"图表右侧最小值"`
  840. RightMax string `description:"图表右侧最大值"`
  841. Right2Min string `description:"图表右侧最小值"`
  842. Right2Max string `description:"图表右侧最大值"`
  843. }
  844. // 系列里的指标日期配置
  845. type ChartSectionSeriesDateConfItem struct {
  846. MoveForward int `description:"前移的期数"`
  847. DateChange []*ChartSectionDateChange
  848. }
  849. // PreviewSectionCombineChartReq 预览截面组合图的请求
  850. type PreviewSectionCombineChartReq struct {
  851. ChartName string `description:"图表名称"`
  852. ChartClassifyId int `description:"分类id"`
  853. ExtraConfig string `description:"图表额外配置信息,json字符串"`
  854. }
  855. type ChartSectionCombineUnit struct {
  856. LeftName string `description:"左轴单位"`
  857. LeftNameEn string `description:"左轴英文单位"`
  858. RightName string `description:"右轴单位"`
  859. RightNameEn string `description:"右轴英文单位"`
  860. RightTwoName string `description:"右2轴单位"`
  861. RightTwoNameEn string `description:"右2轴英文单位"`
  862. }
  863. // 时序组合图额外配置
  864. type ChartTimeCombineExtraConf struct {
  865. IsHeap int `description:"是否堆积(1.堆积,0不堆积)"`
  866. }
  867. type ChartTimeCombineDataResp struct {
  868. IsHeap int `description:"是否堆积(1.堆积,0不堆积)"`
  869. }
  870. func GetChartInfoById(chartInfoId int) (item *ChartInfo, err error) {
  871. o := orm.NewOrmUsingDB("data")
  872. sql := ` SELECT * FROM chart_info WHERE chart_info_id=? `
  873. err = o.Raw(sql, chartInfoId).QueryRow(&item)
  874. return
  875. }
  876. type MarkersLine struct {
  877. Axis int `json:"axis" description:"1左轴 2右轴 3横轴"`
  878. AxisName string `json:"axisName" description:"轴的名称,例如'左轴'"`
  879. MarkerType string `json:"markerType" description:"标识线或标识区"`
  880. MarkLineType int `json:"markLineType" description:"1:固定 2:指标计算"`
  881. Value string `json:"value" description:"连线指向的数值,例如'4000'"`
  882. FromValue string `json:"fromValue" description:"连线的起始点,可以为空"`
  883. ToValue string `json:"toValue" description:"连线的结束点,可以为空"`
  884. LineWidth float64 `json:"lineWidth" description:"连线的宽度"`
  885. DashStyle string `json:"dashStyle" description:"连线的虚线样式,例如'ShortDashDot'"`
  886. Color string `json:"color" description:"连线的颜色"`
  887. Text string `json:"text" description:"连线旁边显示的文本"`
  888. TextPosition string `json:"textPosition" description:"文本的显示位置,例如'bottom'"`
  889. TextColor string `json:"textColor" description:"文本颜色"`
  890. TextFontSize int `json:"textFontSize" description:"文本的字号大小"`
  891. IsShow bool `json:"isShow" description:"是否显示连线及文本"`
  892. EdbType int `json:"edbType" description:"指标类型 0图中第一个指标 1其他指标 前端回显用"`
  893. EdbInfoId int `json:"edbInfoId" description:"指标id"`
  894. Calculation int `json:"calculation" description:"计算方式 1区间均值 2区间均值加N倍标准差 3区间个数分位 4区间数值分位"`
  895. CalculationValue int `json:"calculationValue" description:"计算方式对应的值 2就是几倍标准差 3就是分位值 4就是数值值·"`
  896. TimeIntervalType int `json:"timeInterval" description:"时间区间 0跟随图表 1自定义"`
  897. StartDate MarkersLineTime `json:"startTime" description:"开始时间"`
  898. EndDate MarkersLineTime `json:"endTime" description:"结束时间"`
  899. }
  900. type MarkersLineTime struct {
  901. TimeType int `json:"timeType" description:"时间类型 1固定 2动态 3至今(仅结束时间)"`
  902. Date string `json:"date" description:"日期"`
  903. Conf EdbDateChangeConf `json:"conf" description:"动态时间配置"`
  904. }
  905. // EdbDateExtraConf
  906. // @Description: 导入指标日期前移和日期变换
  907. type EdbDateChangeConf struct {
  908. MoveForward int `json:"moveForward" description:"前移的期数"`
  909. BaseDate int `json:"baseDate" description:"基准日期 0系统日期 1指标最新日期"`
  910. DateChange []*EdbDateConfDateChange
  911. }
  912. type EdbDateConfDateChange struct {
  913. Year int `json:"year" description:"前移的期数"`
  914. Month int `json:"month" description:"前移的期数"`
  915. Day int `json:"day" description:"前移的期数"`
  916. Frequency string `json:"moveForward" description:"频度变换"`
  917. FrequencyDay string `json:"frequencyDay" description:"频度的固定日期"`
  918. ChangeType int `json:"changeType" description:"日期变换类型1日期位移,2指定频率"`
  919. }
  920. func getThsHfEdbDataListByMongo(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  921. list = make([]*EdbDataList, 0)
  922. mogDataObj := mgo.EdbDataThsHf{}
  923. // 构建查询条件
  924. queryConditions := bson.M{
  925. "edb_info_id": edbInfoId,
  926. }
  927. // 数据日期
  928. dateCondition, err := mgo.BuildDateCondition(startDate, endDate)
  929. if err != nil {
  930. return
  931. }
  932. if len(dateCondition) > 0 {
  933. queryConditions["data_time"] = dateCondition
  934. }
  935. // 获取列表数据
  936. tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"data_time"})
  937. if tmpErr != nil {
  938. err = tmpErr
  939. return
  940. }
  941. for k, v := range tmpDataList {
  942. list = append(list, &EdbDataList{
  943. EdbDataId: k + 1,
  944. EdbInfoId: v.EdbInfoId,
  945. DataTime: v.DataTime.Format(utils.FormatDate),
  946. DataTimestamp: v.DataTimestamp,
  947. Value: v.Value,
  948. })
  949. }
  950. return
  951. }