chart.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_chart_lib/utils"
  6. "rdluck_tools/orm"
  7. "strconv"
  8. "time"
  9. "github.com/nosixtools/solarlunar"
  10. )
  11. type ChartInfo struct {
  12. ChartInfoId int `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.NewOrm()
  33. o.Using("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. Frequency string `description:"频率"`
  45. //Unit string `description:"单位"`
  46. StartDate string `description:"起始日期"`
  47. EndDate string `description:"终止日期"`
  48. //ModifyTime string `description:"指标最后更新时间"`
  49. //ChartEdbMappingId int `description:"图表指标id"`
  50. ChartInfoId int `description:"图表id"`
  51. MaxData float64 `description:"上限"`
  52. MinData float64 `description:"下限"`
  53. IsOrder bool `description:"true:正序,false:逆序"`
  54. IsAxis int `description:"1:左轴,0:右轴"`
  55. EdbInfoType int `description:"1:标准指标,0:领先指标"`
  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.NewOrm()
  65. o.Using("data")
  66. 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
  67. FROM chart_edb_mapping AS a
  68. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  69. WHERE chart_info_id=?
  70. ORDER BY chart_edb_mapping_id ASC `
  71. _, err = o.Raw(sql, chartInfoId).QueryRows(&list)
  72. return
  73. }
  74. type EdbDataList struct {
  75. EdbDataId int `json:"-" description:" 指标数据ID"`
  76. EdbInfoId int `json:"-" description:"指标ID"`
  77. DataTime string `json:"-" description:"数据日期"`
  78. DataTimestamp int64 `description:"数据日期"`
  79. Value float64 `description:"数据值"`
  80. }
  81. func GetEdbDataList(source, endInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  82. tableName := GetEdbDataTableName(source)
  83. if tableName == "" {
  84. err = errors.New("无效的渠道:" + strconv.Itoa(source))
  85. list = make([]*EdbDataList, 0)
  86. return list, err
  87. }
  88. sql := `SELECT edb_data_id,edb_info_id,data_time,TRUNCATE(value,2) AS value,data_timestamp FROM %s WHERE edb_info_id=? AND data_time>=? AND data_time<=? ORDER BY data_time ASC `
  89. sql = fmt.Sprintf(sql, tableName)
  90. o := orm.NewOrm()
  91. o.Using("data")
  92. _, err = o.Raw(sql, endInfoId, startDate, endDate).QueryRows(&list)
  93. return
  94. }
  95. //指标季度数据计算(公历转农历)
  96. func AddCalculateQuarterV4(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  97. var errMsg string
  98. defer func() {
  99. if errMsg != "" {
  100. fmt.Println("errMsg:", errMsg)
  101. }
  102. }()
  103. result = new(EdbDataResult)
  104. var yearArr []int
  105. yearMap := make(map[int]int)
  106. var cureentDate time.Time
  107. for k, v := range dataList {
  108. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  109. if err != nil {
  110. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  111. return result, err
  112. }
  113. if k == len(dataList)-1 {
  114. cureentDate = dateTime
  115. }
  116. year := dateTime.Year()
  117. if _, ok := yearMap[year]; !ok {
  118. yearArr = append(yearArr, year)
  119. }
  120. yearMap[year] = year
  121. }
  122. //排序
  123. fmt.Println(yearArr)
  124. thisYear := cureentDate.Year()
  125. thisMonth := int(cureentDate.Month())
  126. fmt.Println("thisMonth:", thisMonth)
  127. //sort.Ints(yearArr)
  128. fmt.Println("yearArr:", yearArr)
  129. fmt.Println("thisYear:", thisYear)
  130. for ky, vy := range yearArr {
  131. fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
  132. if thisMonth < 11 {
  133. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  134. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  135. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  136. if err != nil {
  137. errMsg = "生成当前春节失败,Err:" + err.Error()
  138. return result, err
  139. }
  140. fmt.Println(ky, vy)
  141. preYear := vy
  142. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  143. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  144. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  145. if err != nil {
  146. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  147. return result, err
  148. }
  149. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  150. fmt.Println("day:", day)
  151. fmt.Println("currentYearCjgl:", currentYearCjgl, "preYearCjgl:", preYearCjgl)
  152. items := new(EdbDataItems)
  153. items.Year = preYear
  154. for _, v := range dataList {
  155. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  156. if err != nil {
  157. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  158. return result, err
  159. }
  160. newDate := dateTime.AddDate(0, 0, int(day))
  161. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  162. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  163. fmt.Println("line 467:", newDate, selectDate)
  164. if newDate.Before(selectDate) {
  165. timestamp := newDate.UnixNano() / 1e6
  166. item := new(EdbDataList)
  167. item.DataTime = newDate.Format(utils.FormatDate)
  168. item.EdbInfoId = v.EdbInfoId
  169. item.Value = v.Value
  170. item.EdbDataId = v.EdbDataId
  171. item.DataTimestamp = timestamp
  172. items.Items = append(items.Items, item)
  173. }
  174. }
  175. result.List = append(result.List, items)
  176. } else {
  177. fmt.Println(ky, vy)
  178. nextYear := thisYear + 1
  179. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  180. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  181. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  182. if err != nil {
  183. errMsg = "生成当前春节失败,Err:" + err.Error()
  184. return result, err
  185. }
  186. preYear := vy
  187. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  188. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  189. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  190. if err != nil {
  191. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  192. return result, err
  193. }
  194. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  195. fmt.Println("day:", day)
  196. items := new(EdbDataItems)
  197. items.Year = preYear
  198. for _, v := range dataList {
  199. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  200. if err != nil {
  201. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  202. return result, err
  203. }
  204. newDate := dateTime.AddDate(0, 0, int(day))
  205. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  206. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  207. if newDate.Before(selectDate) {
  208. timestamp := newDate.UnixNano() / 1e6
  209. item := new(EdbDataList)
  210. item.DataTime = newDate.Format(utils.FormatDate)
  211. item.EdbInfoId = v.EdbInfoId
  212. item.Value = v.Value
  213. item.EdbDataId = v.EdbDataId
  214. item.DataTimestamp = timestamp
  215. items.Items = append(items.Items, item)
  216. }
  217. }
  218. result.List = append(result.List, items)
  219. }
  220. }
  221. return
  222. }
  223. type EdbDataItems struct {
  224. Items []*EdbDataList
  225. Year int
  226. }
  227. type EdbDataResult struct {
  228. List []*EdbDataItems
  229. }
  230. type QuarterData struct {
  231. Year int
  232. DataList []*EdbDataList
  233. }
  234. type ChartInfoDetailResp struct {
  235. ChartInfo *ChartInfo
  236. EdbInfoList []*ChartEdbInfoMapping
  237. }