chart.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. //指标季度数据计算(公历转农历)
  108. func AddCalculateQuarterV4(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  109. var errMsg string
  110. defer func() {
  111. if errMsg != "" {
  112. fmt.Println("errMsg:", errMsg)
  113. }
  114. }()
  115. result = new(EdbDataResult)
  116. var yearArr []int
  117. yearMap := make(map[int]int)
  118. var cureentDate time.Time
  119. for k, v := range dataList {
  120. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  121. if err != nil {
  122. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  123. return result, err
  124. }
  125. if k == len(dataList)-1 {
  126. cureentDate = dateTime
  127. }
  128. year := dateTime.Year()
  129. if _, ok := yearMap[year]; !ok {
  130. yearArr = append(yearArr, year)
  131. }
  132. yearMap[year] = year
  133. }
  134. //排序
  135. fmt.Println(yearArr)
  136. thisYear := cureentDate.Year()
  137. thisMonth := int(cureentDate.Month())
  138. fmt.Println("thisMonth:", thisMonth)
  139. //sort.Ints(yearArr)
  140. fmt.Println("yearArr:", yearArr)
  141. fmt.Println("thisYear:", thisYear)
  142. for ky, vy := range yearArr {
  143. fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
  144. if thisMonth < 11 {
  145. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  146. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  147. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  148. if err != nil {
  149. errMsg = "生成当前春节失败,Err:" + err.Error()
  150. return result, err
  151. }
  152. fmt.Println(ky, vy)
  153. preYear := vy
  154. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  155. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  156. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  157. if err != nil {
  158. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  159. return result, err
  160. }
  161. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  162. fmt.Println("day:", day)
  163. fmt.Println("currentYearCjgl:", currentYearCjgl, "preYearCjgl:", preYearCjgl)
  164. items := new(EdbDataItems)
  165. items.Year = preYear
  166. for _, v := range dataList {
  167. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  168. if err != nil {
  169. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  170. return result, err
  171. }
  172. newDate := dateTime.AddDate(0, 0, int(day))
  173. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  174. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  175. fmt.Println("line 467:", newDate, selectDate)
  176. if newDate.Before(selectDate) {
  177. timestamp := newDate.UnixNano() / 1e6
  178. item := new(EdbDataList)
  179. item.DataTime = newDate.Format(utils.FormatDate)
  180. item.EdbInfoId = v.EdbInfoId
  181. item.Value = v.Value
  182. item.EdbDataId = v.EdbDataId
  183. item.DataTimestamp = timestamp
  184. items.Items = append(items.Items, item)
  185. }
  186. }
  187. result.List = append(result.List, items)
  188. } else {
  189. fmt.Println(ky, vy)
  190. nextYear := thisYear + 1
  191. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  192. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  193. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  194. if err != nil {
  195. errMsg = "生成当前春节失败,Err:" + err.Error()
  196. return result, err
  197. }
  198. preYear := vy
  199. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  200. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  201. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  202. if err != nil {
  203. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  204. return result, err
  205. }
  206. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  207. fmt.Println("day:", day)
  208. items := new(EdbDataItems)
  209. items.Year = preYear
  210. for _, v := range dataList {
  211. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  212. if err != nil {
  213. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  214. return result, err
  215. }
  216. newDate := dateTime.AddDate(0, 0, int(day))
  217. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  218. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  219. if newDate.Before(selectDate) {
  220. timestamp := newDate.UnixNano() / 1e6
  221. item := new(EdbDataList)
  222. item.DataTime = newDate.Format(utils.FormatDate)
  223. item.EdbInfoId = v.EdbInfoId
  224. item.Value = v.Value
  225. item.EdbDataId = v.EdbDataId
  226. item.DataTimestamp = timestamp
  227. items.Items = append(items.Items, item)
  228. }
  229. }
  230. result.List = append(result.List, items)
  231. }
  232. }
  233. return
  234. }
  235. type EdbDataItems struct {
  236. Items []*EdbDataList
  237. Year int
  238. }
  239. type EdbDataResult struct {
  240. List []*EdbDataItems
  241. }
  242. type QuarterData struct {
  243. Year int
  244. DataList []*EdbDataList
  245. }
  246. type ChartInfoDetailResp struct {
  247. ChartInfo *ChartInfo
  248. EdbInfoList []*ChartEdbInfoMapping
  249. }
  250. //指标季度数据计算(公历转农历)
  251. func AddCalculateQuarterV5(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  252. var errMsg string
  253. defer func() {
  254. if errMsg != "" {
  255. fmt.Println("errMsg:", errMsg)
  256. }
  257. }()
  258. endDate := dataList[len(dataList)-1].DataTime
  259. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  260. if err != nil {
  261. return result, err
  262. }
  263. thisMonth := int(endDateForm.Month())
  264. result = new(EdbDataResult)
  265. var yearArr []int
  266. yearMap := make(map[int]int)
  267. var cureentDate time.Time
  268. if thisMonth < 11 {
  269. for k, 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. if k == len(dataList)-1 {
  276. cureentDate = dateTime
  277. }
  278. year := dateTime.Year()
  279. if _, ok := yearMap[year]; !ok {
  280. yearArr = append(yearArr, year)
  281. }
  282. yearMap[year] = year
  283. }
  284. } else {
  285. for k, v := range dataList {
  286. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  287. if err != nil {
  288. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  289. return result, err
  290. }
  291. if k == len(dataList)-1 {
  292. cureentDate = dateTime
  293. }
  294. year := dateTime.Year() + 1
  295. if _, ok := yearMap[year]; !ok {
  296. yearArr = append(yearArr, year)
  297. }
  298. yearMap[year] = year
  299. }
  300. }
  301. //排序
  302. fmt.Println("yearArr:", yearArr)
  303. thisYear := cureentDate.Year()
  304. //thisMonth := int(cureentDate.Month())
  305. fmt.Println("thisMonth:", thisMonth)
  306. for ky, vy := range yearArr {
  307. fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
  308. if thisMonth < 11 {
  309. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  310. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  311. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  312. if err != nil {
  313. errMsg = "生成当前春节失败,Err:" + err.Error()
  314. return result, err
  315. }
  316. preYear := vy
  317. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  318. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  319. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  320. if err != nil {
  321. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  322. return result, err
  323. }
  324. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  325. items := new(EdbDataItems)
  326. items.Year = preYear
  327. for _, v := range dataList {
  328. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  329. if err != nil {
  330. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  331. return result, err
  332. }
  333. newDate := dateTime.AddDate(0, 0, int(day))
  334. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  335. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  336. if newDate.Before(selectDate) || newDate == selectDate {
  337. timestamp := newDate.UnixNano() / 1e6
  338. item := new(EdbDataList)
  339. item.DataTime = newDate.Format(utils.FormatDate)
  340. item.EdbInfoId = v.EdbInfoId
  341. item.Value = v.Value
  342. item.EdbDataId = v.EdbDataId
  343. item.DataTimestamp = timestamp
  344. items.Items = append(items.Items, item)
  345. }
  346. }
  347. result.List = append(result.List, items)
  348. } else {
  349. nextYear := thisYear + 1
  350. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  351. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  352. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  353. if err != nil {
  354. errMsg = "生成当前春节失败,Err:" + err.Error()
  355. return result, err
  356. }
  357. preYear := vy
  358. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  359. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  360. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  361. if err != nil {
  362. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  363. return result, err
  364. }
  365. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  366. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  367. items := new(EdbDataItems)
  368. items.Year = preYear - 1
  369. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  370. if ky+1 < len(yearArr) {
  371. for _, v := range dataList {
  372. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  373. if err != nil {
  374. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  375. return result, err
  376. }
  377. newDate := dateTime.AddDate(0, 0, int(day))
  378. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  379. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  380. if newDate.Before(selectDate) || newDate == selectDate {
  381. timestamp := newDate.UnixNano() / 1e6
  382. item := new(EdbDataList)
  383. item.DataTime = newDate.Format(utils.FormatDate)
  384. item.EdbInfoId = v.EdbInfoId
  385. item.Value = v.Value
  386. item.EdbDataId = v.EdbDataId
  387. item.DataTimestamp = timestamp
  388. items.Items = append(items.Items, item)
  389. }
  390. }
  391. result.List = append(result.List, items)
  392. } else {
  393. for _, v := range dataList {
  394. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  395. if err != nil {
  396. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  397. return result, err
  398. }
  399. timestamp := dateTime.UnixNano() / 1e6
  400. item := new(EdbDataList)
  401. item.DataTime = dateTime.Format(utils.FormatDate)
  402. item.EdbInfoId = v.EdbInfoId
  403. item.Value = v.Value
  404. item.EdbDataId = v.EdbDataId
  405. item.DataTimestamp = timestamp
  406. items.Items = append(items.Items, item)
  407. }
  408. result.List = append(result.List, items)
  409. }
  410. }
  411. }
  412. return
  413. }