chart.go 15 KB

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