edb.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. package controllers
  2. import (
  3. "eta/eta_hub/models"
  4. "eta/eta_hub/models/data_manage"
  5. "eta/eta_hub/services"
  6. "eta/eta_hub/services/data"
  7. "eta/eta_hub/utils"
  8. "time"
  9. "github.com/rdlucklib/rdluck_tools/paging"
  10. )
  11. // EdbController 指标
  12. type EdbController struct {
  13. BaseAuthController
  14. }
  15. // SourceList
  16. // @Title 指标来源
  17. // @Description 指标来源
  18. // @Success 200 {object} data_manage.EdbSourceItem
  19. // @router /source/list [get]
  20. func (this *EdbController) SourceList() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. this.Data["json"] = br
  24. this.ServeJSON()
  25. }()
  26. sources, e := data_manage.GetEdbSourceItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  27. if e != nil {
  28. br.Msg = "获取失败"
  29. br.ErrMsg = "SourceList GetEdbSourceItemsByCondition err: " + e.Error()
  30. return
  31. }
  32. resp := make([]*data_manage.EdbSourceItem, 0)
  33. for _, v := range sources {
  34. resp = append(resp, data_manage.FormatEdbSource2Item(v))
  35. }
  36. br.Data = resp
  37. br.Ret = 200
  38. br.Msg = "获取成功"
  39. }
  40. // ClassifyList
  41. // @Title 指标分类列表
  42. // @Description 指标分类列表
  43. // @Param ClassifyType query int false "分类类型: 0-普通指标; 1-预测指标"
  44. // @Success 200 {object} data_manage.EdbClassifyItem
  45. // @router /classify/list [get]
  46. func (this *EdbController) ClassifyList() {
  47. br := new(models.BaseResponse).Init()
  48. defer func() {
  49. this.Data["json"] = br
  50. this.ServeJSON()
  51. }()
  52. classifyType, _ := this.GetInt("ClassifyType", 0)
  53. cond := ` AND classify_type = ?`
  54. pars := make([]interface{}, 0)
  55. pars = append(pars, classifyType)
  56. // 获取所有分类
  57. classifyOb := new(data_manage.EdbClassify)
  58. classifies, e := classifyOb.GetItemsByCondition(cond, pars, []string{}, "parent_id ASC, sort ASC")
  59. if e != nil {
  60. br.Msg = "获取失败"
  61. br.ErrMsg = "ClassifyTree GetItemsByCondition err: " + e.Error()
  62. return
  63. }
  64. resp := make([]*data_manage.EdbClassifyItem, 0)
  65. for _, v := range classifies {
  66. resp = append(resp, data_manage.FormatEdbClassify2Item(v))
  67. }
  68. br.Data = resp
  69. br.Ret = 200
  70. br.Msg = "获取成功"
  71. }
  72. // ClassifyTree
  73. // @Title 指标分类树
  74. // @Description 指标分类树
  75. // @Param ClassifyType query int false "分类类型: 0-普通指标; 1-预测指标"
  76. // @Success 200 {object} data_manage.EdbClassifyItem
  77. // @router /classify/tree [get]
  78. func (this *EdbController) ClassifyTree() {
  79. br := new(models.BaseResponse).Init()
  80. defer func() {
  81. this.Data["json"] = br
  82. this.ServeJSON()
  83. }()
  84. classifyType, _ := this.GetInt("ClassifyType", 0)
  85. cond := ` AND classify_type = ?`
  86. pars := make([]interface{}, 0)
  87. pars = append(pars, classifyType)
  88. // 获取所有分类
  89. classifyOb := new(data_manage.EdbClassify)
  90. classifies, e := classifyOb.GetItemsByCondition(cond, pars, []string{}, "sort ASC, parent_id ASC")
  91. if e != nil {
  92. br.Msg = "获取失败"
  93. br.ErrMsg = "EdbClassifyTree GetItemsByCondition err: " + e.Error()
  94. return
  95. }
  96. // 递归生成分类树
  97. classifyTree := services.GetEdbClassifyTreeRecursive(classifies, 0)
  98. br.Data = classifyTree
  99. br.Ret = 200
  100. br.Msg = "获取成功"
  101. }
  102. // List
  103. // @Title 指标列表
  104. // @Description 指标列表
  105. // @Param ClassifyId query int true "指标分类ID"
  106. // @Success 200 {object} data_manage.EdbInfoItem
  107. // @router /list [get]
  108. func (this *EdbController) List() {
  109. br := new(models.BaseResponse).Init()
  110. defer func() {
  111. this.Data["json"] = br
  112. this.ServeJSON()
  113. }()
  114. classifyId, _ := this.GetInt("ClassifyId")
  115. pageSize, _ := this.GetInt("PageSize")
  116. currentIndex, _ := this.GetInt("CurrentIndex")
  117. edbOb := new(data_manage.EdbInfo)
  118. cond := ``
  119. pars := make([]interface{}, 0)
  120. if classifyId > 0 {
  121. cond += ` AND classify_id = ?`
  122. pars = append(pars, classifyId)
  123. }
  124. var startSize int
  125. if pageSize > 0 {
  126. if currentIndex <= 0 {
  127. currentIndex = 1
  128. }
  129. startSize = utils.StartIndex(currentIndex, pageSize)
  130. total, e := edbOb.GetItemsTotal(cond, pars)
  131. if e != nil {
  132. br.Msg = "获取失败"
  133. br.ErrMsg = "EdbList GetItemsTotal err: " + e.Error()
  134. return
  135. }
  136. resp := new(data_manage.EdbInfoItemResp)
  137. if total == 0 {
  138. resp.List = make([]*data_manage.EdbInfoItem, 0)
  139. resp.Paging = paging.GetPaging(currentIndex, pageSize, 0)
  140. br.Data = resp
  141. br.Ret = 200
  142. br.Msg = "获取成功"
  143. return
  144. }
  145. edbList, e := edbOb.GetItemsPageByCondition(cond, pars, []string{}, "create_time DESC, sort ASC", startSize, pageSize)
  146. if e != nil {
  147. br.Msg = "获取失败"
  148. br.ErrMsg = "EdbList GetItemsPageByCondition err: " + e.Error()
  149. return
  150. }
  151. list := make([]*data_manage.EdbInfoItem, 0)
  152. for _, v := range edbList {
  153. list = append(list, data_manage.FormatEdbInfo2Item(v))
  154. }
  155. page := paging.GetPaging(currentIndex, pageSize, total)
  156. resp.List = list
  157. resp.Paging = page
  158. br.Data = resp
  159. br.Ret = 200
  160. br.Msg = "获取成功"
  161. return
  162. }
  163. edbList, e := edbOb.GetItemsByCondition(cond, pars, []string{}, "sort ASC, create_time DESC")
  164. if e != nil {
  165. br.Msg = "获取失败"
  166. br.ErrMsg = "EdbList GetItemsByCondition err: " + e.Error()
  167. return
  168. }
  169. resp := make([]*data_manage.EdbInfoItem, 0)
  170. for _, v := range edbList {
  171. resp = append(resp, data_manage.FormatEdbInfo2Item(v))
  172. }
  173. br.Data = resp
  174. br.Ret = 200
  175. br.Msg = "获取成功"
  176. }
  177. // Detail
  178. // @Title 指标详情
  179. // @Description 指标详情
  180. // @Success 200 {object} data_manage.EdbInfoItem
  181. // @router /detail [get]
  182. func (this *EdbController) Detail() {
  183. br := new(models.BaseResponse).Init()
  184. defer func() {
  185. this.Data["json"] = br
  186. this.ServeJSON()
  187. }()
  188. unicode := this.GetString("UniqueCode")
  189. edbCode := this.GetString("EdbCode")
  190. if unicode == "" && edbCode == "" {
  191. br.Msg = "参数有误"
  192. return
  193. }
  194. edbOb := new(data_manage.EdbInfo)
  195. edb := new(data_manage.EdbInfo)
  196. var e error
  197. if unicode != "" {
  198. edb, e = edbOb.GetItemByUniCode(unicode)
  199. if e != nil {
  200. if e.Error() == utils.ErrNoRow() {
  201. br.Msg = "指标不存在"
  202. return
  203. }
  204. br.Msg = "获取失败"
  205. br.ErrMsg = "EdbDetail GetItemByUniCode err: " + e.Error()
  206. return
  207. }
  208. } else if edbCode != "" {
  209. edb, e = edbOb.GetItemByEdbCode(edbCode)
  210. if e != nil {
  211. if e.Error() == utils.ErrNoRow() {
  212. br.Msg = "指标不存在"
  213. return
  214. }
  215. br.Msg = "获取失败"
  216. br.ErrMsg = "EdbDetail GetItemByEdbCode err: " + e.Error()
  217. return
  218. }
  219. }
  220. // 获取指标数据
  221. //dataOb := new(data_manage.EdbData)
  222. //dataList, e := dataOb.GetItemsBySourceAndCode(edb.Source, edb.EdbCode, []string{}, "")
  223. //if e != nil {
  224. // br.Msg = "获取失败"
  225. // br.ErrMsg = "EdbDetail GetItemsBySourceAndCode err: " + e.Error()
  226. // return
  227. //}
  228. //edbData := make([]*data_manage.EdbDataItem, 0)
  229. //for _, v := range dataList {
  230. // edbData = append(edbData, data_manage.FormatEdbData2Item(v))
  231. //}
  232. resp := data_manage.FormatEdbInfo2Item(edb)
  233. br.Data = resp
  234. br.Ret = 200
  235. br.Msg = "获取成功"
  236. }
  237. // EdbData
  238. // @Title 指标数据
  239. // @Description 指标数据
  240. // @Success 200 {object} data_manage.EdbInfoItem
  241. // @router /data [get]
  242. func (this *EdbController) EdbData() {
  243. br := new(models.BaseResponse).Init()
  244. defer func() {
  245. this.Data["json"] = br
  246. this.ServeJSON()
  247. }()
  248. unicode := this.GetString("UniqueCode")
  249. edbCode := this.GetString("EdbCode")
  250. if unicode == "" && edbCode == "" {
  251. br.Msg = "参数有误"
  252. return
  253. }
  254. startDate := this.GetString("StartDate")
  255. if startDate != "" {
  256. _, e := time.Parse(utils.FormatDate, startDate)
  257. if e != nil {
  258. br.Msg = "开始日期格式有误"
  259. return
  260. }
  261. }
  262. edbOb := new(data_manage.EdbInfo)
  263. edb := new(data_manage.EdbInfo)
  264. var e error
  265. if unicode != "" {
  266. edb, e = edbOb.GetItemByUniCode(unicode)
  267. if e != nil {
  268. if e.Error() == utils.ErrNoRow() {
  269. br.Msg = "指标不存在"
  270. return
  271. }
  272. br.Msg = "获取失败"
  273. br.ErrMsg = "EdbDetail GetItemByUniCode err: " + e.Error()
  274. return
  275. }
  276. } else if edbCode != "" {
  277. edb, e = edbOb.GetItemByEdbCode(edbCode)
  278. if e != nil {
  279. if e.Error() == utils.ErrNoRow() {
  280. br.Msg = "指标不存在"
  281. return
  282. }
  283. br.Msg = "获取失败"
  284. br.ErrMsg = "EdbDetail GetItemByEdbCode err: " + e.Error()
  285. return
  286. }
  287. }
  288. // 获取指标数据
  289. dataOb := new(data_manage.EdbData)
  290. dataList, e := dataOb.GetItemsBySourceAndCode(edb.Source, edb.SubSource, edb.EdbCode, startDate, []string{}, "")
  291. if e != nil {
  292. br.Msg = "获取失败"
  293. br.ErrMsg = "EdbData GetItemsBySourceAndCode err: " + e.Error()
  294. return
  295. }
  296. edbData := make([]*data_manage.EdbDataItem, 0)
  297. for _, v := range dataList {
  298. edbData = append(edbData, data_manage.FormatEdbData2Item(v))
  299. }
  300. br.Data = edbData
  301. br.Ret = 200
  302. br.Msg = "获取成功"
  303. }
  304. // TraceEdbInfo
  305. // @Title 指标溯源接口
  306. // @Description 指标溯源接口
  307. // @Param EdbInfoId query int true "指标id"
  308. // @Success 200 {object} data_manage.ChartListResp
  309. // @router /edb_info/trace [get]
  310. func (this *EdbInfoController) TraceEdbInfo() {
  311. br := new(models.BaseResponse).Init()
  312. defer func() {
  313. this.Data["json"] = br
  314. this.ServeJSON()
  315. }()
  316. edbInfoId, _ := this.GetInt("EdbInfoId")
  317. if edbInfoId <= 0 {
  318. br.Msg = "参数错误"
  319. br.ErrMsg = "参数错误"
  320. return
  321. }
  322. resp, err := data.TraceEdbInfoByEdbInfoId(edbInfoId)
  323. if err != nil {
  324. br.Msg = "获取失败"
  325. br.Msg = "获取失败,Err:" + err.Error()
  326. return
  327. }
  328. br.Ret = 200
  329. br.Data = resp
  330. br.Msg = "刷新成功"
  331. }
  332. // EdbInfoDataTb
  333. // @Title 获取指标数据同比值
  334. // @Description 获取指标数据
  335. // @Param EdbInfoId query int true "指标ID"
  336. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:自定义起始时间至今,7:18年至今,8:19年至今,9:20年至今,10:全部"
  337. // @Param StartDate query string true "自定义开始日期"
  338. // @Param EndDate query string true "自定义结束日期"
  339. // @Success 200 {object} data_manage.EdbInfoDataResp
  340. // @router /data/tb [get]
  341. func (this *EdbController) EdbInfoDataTb() {
  342. br := new(models.BaseResponse).Init()
  343. defer func() {
  344. this.Data["json"] = br
  345. this.ServeJSON()
  346. }()
  347. edbInfoType, _ := this.GetInt("EdbInfoType")
  348. if edbInfoType < 0 {
  349. edbInfoType = 1
  350. }
  351. startDate := this.GetString("StartDate")
  352. unicode := this.GetString("UniqueCode")
  353. edbCode := this.GetString("EdbCode")
  354. if unicode == "" && edbCode == "" {
  355. br.Msg = "参数有误"
  356. return
  357. }
  358. edbOb := new(data_manage.EdbInfo)
  359. edbInfo := new(data_manage.EdbInfo)
  360. var err error
  361. if unicode != "" {
  362. edbInfo, err = edbOb.GetItemByUniCode(unicode)
  363. if err != nil {
  364. if err.Error() == utils.ErrNoRow() {
  365. br.Msg = "指标不存在"
  366. return
  367. }
  368. br.Msg = "获取失败"
  369. br.ErrMsg = "EdbDetail GetItemByUniCode err: " + err.Error()
  370. return
  371. }
  372. } else if edbCode != "" {
  373. edbInfo, err = edbOb.GetItemByEdbCode(edbCode)
  374. if err != nil {
  375. if err.Error() == utils.ErrNoRow() {
  376. br.Msg = "指标不存在"
  377. return
  378. }
  379. br.Msg = "获取失败"
  380. br.ErrMsg = "EdbDetail GetItemByEdbCode err: " + err.Error()
  381. return
  382. }
  383. }
  384. if edbInfo.EdbInfoType != 0 {
  385. br.Msg = "不支持预测指标"
  386. return
  387. }
  388. var startDateTime time.Time
  389. if startDate != `` {
  390. startDateTime, err = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  391. if err != nil {
  392. br.Msg = "开始日期异常"
  393. br.Msg = "开始日期异常,Err:" + err.Error()
  394. return
  395. }
  396. //同比值的计算方式,是需要与去年同期对比,所以如果用户需要某个时间段的数据的话,获取基础数据时,需要往前面推(1年+兼容的日期频度(目前是35天))
  397. startDate = startDateTime.AddDate(-1, 0, -35).Format(utils.FormatDate)
  398. }
  399. tmpDataList, err := data.GetEdbDataList(edbInfo, startDate)
  400. if err != nil {
  401. br.Msg = "获取失败"
  402. br.Msg = "获取失败,Err:" + err.Error()
  403. return
  404. }
  405. // 计算同比值
  406. dataList, _, _, err := data.GetEdbDataTbz(edbInfo, tmpDataList, startDateTime)
  407. if err != nil {
  408. br.Msg = "获取失败"
  409. br.Msg = "计算同比值失败,Err:" + err.Error()
  410. return
  411. }
  412. //edbInfo.MaxValue = maxValue
  413. //edbInfo.MinValue = minValue
  414. resp := new(data_manage.EdbInfoDataResp)
  415. //resp.EdbInfo = edbInfo
  416. resp.DataList = dataList
  417. br.Ret = 200
  418. br.Msg = "获取成功"
  419. br.Data = resp
  420. }
  421. // EdbInfoDataSeasonal
  422. // @Title 获取指标数据(季节性)
  423. // @Description 获取指标数据
  424. // @Param EdbInfoId query int true "指标ID"
  425. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:自定义起始时间至今,7:18年至今,8:19年至今,9:20年至今,10:全部"
  426. // @Param StartDate query string true "自定义开始日期"
  427. // @Param EndDate query string true "自定义结束日期"
  428. // @Param Calendar query string true "公历/农历"
  429. // @Success 200 {object} data_manage.EdbInfoDataResp
  430. // @router /data/seasonal [get]
  431. func (this *EdbController) EdbInfoDataSeasonal() {
  432. br := new(models.BaseResponse).Init()
  433. defer func() {
  434. this.Data["json"] = br
  435. this.ServeJSON()
  436. }()
  437. startDate := this.GetString("StartDate")
  438. calendar := this.GetString("Calendar")
  439. if calendar == "" {
  440. calendar = "公历"
  441. }
  442. unicode := this.GetString("UniqueCode")
  443. edbCode := this.GetString("EdbCode")
  444. if unicode == "" && edbCode == "" {
  445. br.Msg = "参数有误"
  446. return
  447. }
  448. edbOb := new(data_manage.EdbInfo)
  449. edbInfo := new(data_manage.EdbInfo)
  450. var err error
  451. if unicode != "" {
  452. edbInfo, err = edbOb.GetItemByUniCode(unicode)
  453. if err != nil {
  454. if err.Error() == utils.ErrNoRow() {
  455. br.Msg = "指标不存在"
  456. return
  457. }
  458. br.Msg = "获取失败"
  459. br.ErrMsg = "EdbDetail GetItemByUniCode err: " + err.Error()
  460. return
  461. }
  462. } else if edbCode != "" {
  463. edbInfo, err = edbOb.GetItemByEdbCode(edbCode)
  464. if err != nil {
  465. if err.Error() == utils.ErrNoRow() {
  466. br.Msg = "指标不存在"
  467. return
  468. }
  469. br.Msg = "获取失败"
  470. br.ErrMsg = "EdbDetail GetItemByEdbCode err: " + err.Error()
  471. return
  472. }
  473. }
  474. if edbInfo.EdbInfoType != 0 {
  475. br.Msg = "不支持预测指标"
  476. return
  477. }
  478. dataList, err := data.GetChartEdbSeasonalData(calendar, startDate, edbInfo)
  479. if err != nil {
  480. br.Msg = "获取失败"
  481. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  482. return
  483. }
  484. resp := new(data_manage.EdbInfoDataSeasonalResp)
  485. //resp.EdbInfo = edbInfo
  486. resp.DataList = dataList
  487. br.Ret = 200
  488. br.Msg = "获取成功"
  489. br.Data = resp
  490. }