chart.go 41 KB

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