chart.go 10 KB

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