chart.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/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.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. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  57. LeadValue int `description:"领先值"`
  58. LeadUnit string `description:"领先单位"`
  59. ChartStyle string `description:"图表类型"`
  60. ChartColor string `description:"颜色"`
  61. ChartWidth float64 `description:"线条大小"`
  62. DataList interface{}
  63. }
  64. func GetChartEdbMappingList(chartInfoId int) (list []*ChartEdbInfoMapping, err error) {
  65. o := orm.NewOrm()
  66. o.Using("data")
  67. 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
  68. FROM chart_edb_mapping AS a
  69. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  70. WHERE chart_info_id=?
  71. ORDER BY chart_edb_mapping_id ASC `
  72. _, err = o.Raw(sql, chartInfoId).QueryRows(&list)
  73. return
  74. }
  75. type EdbDataList struct {
  76. EdbDataId int `json:"-" description:" 指标数据ID"`
  77. EdbInfoId int `json:"-" description:"指标ID"`
  78. DataTime string `json:"-" description:"数据日期"`
  79. DataTimestamp int64 `description:"数据日期"`
  80. Value float64 `description:"数据值"`
  81. }
  82. func GetEdbDataList(source, endInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  83. tableName := GetEdbDataTableName(source)
  84. if tableName == "" {
  85. err = errors.New("无效的渠道:" + strconv.Itoa(source))
  86. list = make([]*EdbDataList, 0)
  87. return list, err
  88. }
  89. var pars []interface{}
  90. var condition string
  91. if startDate != "" {
  92. condition += ` AND data_time>=? `
  93. pars = append(pars, startDate)
  94. }
  95. if endDate != "" {
  96. condition += ` AND data_time<=? `
  97. pars = append(pars, endDate)
  98. }
  99. sql := `SELECT edb_data_id,edb_info_id,data_time,TRUNCATE(value,4) AS value,data_timestamp FROM %s WHERE edb_info_id=? `
  100. if condition != "" {
  101. sql += condition
  102. }
  103. sql += ` ORDER BY data_time ASC `
  104. sql = fmt.Sprintf(sql, tableName)
  105. o := orm.NewOrm()
  106. o.Using("data")
  107. _, err = o.Raw(sql, endInfoId, pars).QueryRows(&list)
  108. return
  109. }
  110. //指标季度数据计算(公历转农历)
  111. func AddCalculateQuarterV4(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  112. var errMsg string
  113. defer func() {
  114. if errMsg != "" {
  115. fmt.Println("errMsg:", errMsg)
  116. }
  117. }()
  118. result = new(EdbDataResult)
  119. var yearArr []int
  120. yearMap := make(map[int]int)
  121. var cureentDate time.Time
  122. for k, v := range dataList {
  123. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  124. if err != nil {
  125. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  126. return result, err
  127. }
  128. if k == len(dataList)-1 {
  129. cureentDate = dateTime
  130. }
  131. year := dateTime.Year()
  132. if _, ok := yearMap[year]; !ok {
  133. yearArr = append(yearArr, year)
  134. }
  135. yearMap[year] = year
  136. }
  137. //排序
  138. fmt.Println(yearArr)
  139. thisYear := cureentDate.Year()
  140. thisMonth := int(cureentDate.Month())
  141. fmt.Println("thisMonth:", thisMonth)
  142. //sort.Ints(yearArr)
  143. fmt.Println("yearArr:", yearArr)
  144. fmt.Println("thisYear:", thisYear)
  145. for ky, vy := range yearArr {
  146. fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
  147. if thisMonth < 11 {
  148. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  149. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  150. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  151. if err != nil {
  152. errMsg = "生成当前春节失败,Err:" + err.Error()
  153. return result, err
  154. }
  155. fmt.Println(ky, vy)
  156. preYear := vy
  157. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  158. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  159. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  160. if err != nil {
  161. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  162. return result, err
  163. }
  164. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  165. fmt.Println("day:", day)
  166. fmt.Println("currentYearCjgl:", currentYearCjgl, "preYearCjgl:", preYearCjgl)
  167. items := new(EdbDataItems)
  168. items.Year = preYear
  169. for _, v := range dataList {
  170. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  171. if err != nil {
  172. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  173. return result, err
  174. }
  175. newDate := dateTime.AddDate(0, 0, int(day))
  176. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  177. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  178. fmt.Println("line 467:", newDate, selectDate)
  179. if newDate.Before(selectDate) {
  180. timestamp := newDate.UnixNano() / 1e6
  181. item := new(EdbDataList)
  182. item.DataTime = newDate.Format(utils.FormatDate)
  183. item.EdbInfoId = v.EdbInfoId
  184. item.Value = v.Value
  185. item.EdbDataId = v.EdbDataId
  186. item.DataTimestamp = timestamp
  187. items.Items = append(items.Items, item)
  188. }
  189. }
  190. result.List = append(result.List, items)
  191. } else {
  192. fmt.Println(ky, vy)
  193. nextYear := thisYear + 1
  194. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  195. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  196. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  197. if err != nil {
  198. errMsg = "生成当前春节失败,Err:" + err.Error()
  199. return result, err
  200. }
  201. preYear := vy
  202. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  203. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  204. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  205. if err != nil {
  206. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  207. return result, err
  208. }
  209. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  210. fmt.Println("day:", day)
  211. items := new(EdbDataItems)
  212. items.Year = preYear
  213. for _, v := range dataList {
  214. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  215. if err != nil {
  216. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  217. return result, err
  218. }
  219. newDate := dateTime.AddDate(0, 0, int(day))
  220. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  221. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  222. if newDate.Before(selectDate) {
  223. timestamp := newDate.UnixNano() / 1e6
  224. item := new(EdbDataList)
  225. item.DataTime = newDate.Format(utils.FormatDate)
  226. item.EdbInfoId = v.EdbInfoId
  227. item.Value = v.Value
  228. item.EdbDataId = v.EdbDataId
  229. item.DataTimestamp = timestamp
  230. items.Items = append(items.Items, item)
  231. }
  232. }
  233. result.List = append(result.List, items)
  234. }
  235. }
  236. return
  237. }
  238. type EdbDataItems struct {
  239. Items []*EdbDataList
  240. Year int
  241. }
  242. type EdbDataResult struct {
  243. List []*EdbDataItems
  244. }
  245. type QuarterData struct {
  246. Year int
  247. DataList []*EdbDataList
  248. }
  249. type ChartInfoDetailResp struct {
  250. ChartInfo *ChartInfo
  251. EdbInfoList []*ChartEdbInfoMapping
  252. }
  253. //指标季度数据计算(公历转农历)
  254. func AddCalculateQuarterV5(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  255. var errMsg string
  256. defer func() {
  257. if errMsg != "" {
  258. fmt.Println("errMsg:", errMsg)
  259. }
  260. }()
  261. endDate := dataList[len(dataList)-1].DataTime
  262. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  263. if err != nil {
  264. return result, err
  265. }
  266. thisMonth := int(endDateForm.Month())
  267. result = new(EdbDataResult)
  268. var yearArr []int
  269. yearMap := make(map[int]int)
  270. var cureentDate time.Time
  271. if thisMonth < 11 {
  272. for k, v := range dataList {
  273. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  274. if err != nil {
  275. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  276. return result, err
  277. }
  278. if k == len(dataList)-1 {
  279. cureentDate = dateTime
  280. }
  281. year := dateTime.Year()
  282. if _, ok := yearMap[year]; !ok {
  283. yearArr = append(yearArr, year)
  284. }
  285. yearMap[year] = year
  286. }
  287. } else {
  288. for k, v := range dataList {
  289. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  290. if err != nil {
  291. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  292. return result, err
  293. }
  294. if k == len(dataList)-1 {
  295. cureentDate = dateTime
  296. }
  297. year := dateTime.Year() + 1
  298. if _, ok := yearMap[year]; !ok {
  299. yearArr = append(yearArr, year)
  300. }
  301. yearMap[year] = year
  302. }
  303. }
  304. //排序
  305. fmt.Println("yearArr:", yearArr)
  306. thisYear := cureentDate.Year()
  307. //thisMonth := int(cureentDate.Month())
  308. fmt.Println("thisMonth:", thisMonth)
  309. for ky, vy := range yearArr {
  310. fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
  311. if thisMonth < 11 {
  312. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  313. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  314. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  315. if err != nil {
  316. errMsg = "生成当前春节失败,Err:" + err.Error()
  317. return result, err
  318. }
  319. preYear := vy
  320. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  321. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  322. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  323. if err != nil {
  324. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  325. return result, err
  326. }
  327. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  328. items := new(EdbDataItems)
  329. items.Year = preYear
  330. for _, v := range dataList {
  331. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  332. if err != nil {
  333. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  334. return result, err
  335. }
  336. newDate := dateTime.AddDate(0, 0, int(day))
  337. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  338. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  339. if newDate.Before(selectDate) || newDate == selectDate {
  340. timestamp := newDate.UnixNano() / 1e6
  341. item := new(EdbDataList)
  342. item.DataTime = newDate.Format(utils.FormatDate)
  343. item.EdbInfoId = v.EdbInfoId
  344. item.Value = v.Value
  345. item.EdbDataId = v.EdbDataId
  346. item.DataTimestamp = timestamp
  347. items.Items = append(items.Items, item)
  348. }
  349. }
  350. result.List = append(result.List, items)
  351. } else {
  352. nextYear := thisYear + 1
  353. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  354. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  355. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  356. if err != nil {
  357. errMsg = "生成当前春节失败,Err:" + err.Error()
  358. return result, err
  359. }
  360. preYear := vy
  361. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  362. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  363. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  364. if err != nil {
  365. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  366. return result, err
  367. }
  368. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  369. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  370. items := new(EdbDataItems)
  371. items.Year = preYear - 1
  372. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  373. if ky+1 < len(yearArr) {
  374. for _, v := range dataList {
  375. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  376. if err != nil {
  377. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  378. return result, err
  379. }
  380. newDate := dateTime.AddDate(0, 0, int(day))
  381. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  382. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  383. if newDate.Before(selectDate) || newDate == selectDate {
  384. timestamp := newDate.UnixNano() / 1e6
  385. item := new(EdbDataList)
  386. item.DataTime = newDate.Format(utils.FormatDate)
  387. item.EdbInfoId = v.EdbInfoId
  388. item.Value = v.Value
  389. item.EdbDataId = v.EdbDataId
  390. item.DataTimestamp = timestamp
  391. items.Items = append(items.Items, item)
  392. }
  393. }
  394. result.List = append(result.List, items)
  395. } else {
  396. for _, v := range dataList {
  397. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  398. if err != nil {
  399. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  400. return result, err
  401. }
  402. timestamp := dateTime.UnixNano() / 1e6
  403. item := new(EdbDataList)
  404. item.DataTime = dateTime.Format(utils.FormatDate)
  405. item.EdbInfoId = v.EdbInfoId
  406. item.Value = v.Value
  407. item.EdbDataId = v.EdbDataId
  408. item.DataTimestamp = timestamp
  409. items.Items = append(items.Items, item)
  410. }
  411. result.List = append(result.List, items)
  412. }
  413. }
  414. }
  415. return
  416. }