chart.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. endDate := dataList[len(dataList)-1].DataTime
  131. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  132. if err != nil {
  133. return result, err
  134. }
  135. thisMonth := int(endDateForm.Month())
  136. result = new(EdbDataResult)
  137. var yearArr []int
  138. yearMap := make(map[int]int)
  139. var cureentDate time.Time
  140. if thisMonth < 11 {
  141. for k, v := range dataList {
  142. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  143. if err != nil {
  144. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  145. return result, err
  146. }
  147. if k == len(dataList)-1 {
  148. cureentDate = dateTime
  149. }
  150. year := dateTime.Year()
  151. if _, ok := yearMap[year]; !ok {
  152. yearArr = append(yearArr, year)
  153. }
  154. yearMap[year] = year
  155. }
  156. } else {
  157. for k, v := range dataList {
  158. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  159. if err != nil {
  160. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  161. return result, err
  162. }
  163. if k == len(dataList)-1 {
  164. cureentDate = dateTime
  165. }
  166. year := dateTime.Year() + 1
  167. if _, ok := yearMap[year]; !ok {
  168. yearArr = append(yearArr, year)
  169. }
  170. yearMap[year] = year
  171. }
  172. }
  173. //排序
  174. fmt.Println("yearArr:", yearArr)
  175. thisYear := cureentDate.Year()
  176. //thisMonth := int(cureentDate.Month())
  177. fmt.Println("thisMonth:", thisMonth)
  178. for ky, vy := range yearArr {
  179. fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
  180. if thisMonth < 11 {
  181. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  182. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  183. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  184. if err != nil {
  185. errMsg = "生成当前春节失败,Err:" + err.Error()
  186. return result, err
  187. }
  188. preYear := vy
  189. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  190. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  191. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  192. if err != nil {
  193. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  194. return result, err
  195. }
  196. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  197. items := new(EdbDataItems)
  198. items.Year = preYear
  199. for _, v := range dataList {
  200. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  201. if err != nil {
  202. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  203. return result, err
  204. }
  205. newDate := dateTime.AddDate(0, 0, int(day))
  206. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  207. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  208. if newDate.Before(selectDate) || newDate == selectDate {
  209. timestamp := newDate.UnixNano() / 1e6
  210. item := new(EdbDataList)
  211. item.DataTime = newDate.Format(utils.FormatDate)
  212. item.EdbInfoId = v.EdbInfoId
  213. item.Value = v.Value
  214. item.EdbDataId = v.EdbDataId
  215. item.DataTimestamp = timestamp
  216. items.Items = append(items.Items, item)
  217. }
  218. }
  219. result.List = append(result.List, items)
  220. } else {
  221. nextYear := thisYear + 1
  222. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  223. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  224. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  225. if err != nil {
  226. errMsg = "生成当前春节失败,Err:" + err.Error()
  227. return result, err
  228. }
  229. preYear := vy
  230. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  231. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  232. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  233. if err != nil {
  234. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  235. return result, err
  236. }
  237. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  238. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  239. items := new(EdbDataItems)
  240. items.Year = preYear - 1
  241. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  242. if ky+1 < len(yearArr) {
  243. for _, v := range dataList {
  244. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  245. if err != nil {
  246. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  247. return result, err
  248. }
  249. newDate := dateTime.AddDate(0, 0, int(day))
  250. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  251. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  252. if newDate.Before(selectDate) || newDate == selectDate {
  253. timestamp := newDate.UnixNano() / 1e6
  254. item := new(EdbDataList)
  255. item.DataTime = newDate.Format(utils.FormatDate)
  256. item.EdbInfoId = v.EdbInfoId
  257. item.Value = v.Value
  258. item.EdbDataId = v.EdbDataId
  259. item.DataTimestamp = timestamp
  260. items.Items = append(items.Items, item)
  261. }
  262. }
  263. result.List = append(result.List, items)
  264. } else {
  265. for _, v := range dataList {
  266. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  267. if err != nil {
  268. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  269. return result, err
  270. }
  271. timestamp := dateTime.UnixNano() / 1e6
  272. item := new(EdbDataList)
  273. item.DataTime = dateTime.Format(utils.FormatDate)
  274. item.EdbInfoId = v.EdbInfoId
  275. item.Value = v.Value
  276. item.EdbDataId = v.EdbDataId
  277. item.DataTimestamp = timestamp
  278. items.Items = append(items.Items, item)
  279. }
  280. result.List = append(result.List, items)
  281. }
  282. }
  283. }
  284. return
  285. }