chart.go 11 KB

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