base_from_fenwei.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/logic"
  5. "eta/eta_index_lib/models"
  6. "eta/eta_index_lib/services"
  7. "eta/eta_index_lib/utils"
  8. "github.com/beego/beego/v2/core/logs"
  9. "strconv"
  10. "time"
  11. )
  12. // FenweiController 汾渭煤炭
  13. type FenweiController struct {
  14. BaseAuthController
  15. }
  16. // Add
  17. // @Title 新增汾渭煤炭指标接口
  18. // @Description 新增汾渭煤炭指标接口
  19. // @Success 200 {object} models.AddEdbInfoReq
  20. // @router /add [post]
  21. func (this *FenweiController) Add() {
  22. br := new(models.BaseResponse).Init()
  23. var cacheKey string
  24. defer func() {
  25. if br.ErrMsg == "" {
  26. br.IsSendEmail = false
  27. }
  28. this.Data["json"] = br
  29. this.ServeJSON()
  30. }()
  31. var req models.AddEdbInfoReq
  32. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  33. if err != nil {
  34. br.Msg = "参数解析异常!"
  35. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  36. return
  37. }
  38. if req.EdbCode == "" {
  39. br.Msg = "请输入指标编码!"
  40. br.ErrMsg = "请输入指标编码,指标编码为空"
  41. return
  42. }
  43. ob := new(models.BaseFromFenwei)
  44. cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(ob.GetSource()) + "_" + req.EdbCode
  45. if utils.Rc.IsExist(cacheKey) {
  46. br.Ret = 501
  47. br.Success = true
  48. br.Msg = "系统处理中,请稍后重试"
  49. return
  50. }
  51. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  52. defer func() {
  53. _ = utils.Rc.Delete(cacheKey)
  54. }()
  55. if err = ob.Add(req.EdbCode); err != nil {
  56. br.Msg = "获取指标信息失败!"
  57. br.ErrMsg = "获取指标信息失败 AddEdbDataFromFenwei,Err:" + err.Error()
  58. return
  59. }
  60. br.Ret = 200
  61. br.Success = true
  62. br.Msg = "获取成功"
  63. }
  64. // Refresh
  65. // @Title 刷新汾渭煤炭指标接口
  66. // @Description 刷新汾渭煤炭指标接口
  67. // @Success 200 {object} models.RefreshEdbInfoReq
  68. // @router /refresh [post]
  69. func (this *FenweiController) Refresh() {
  70. br := new(models.BaseResponse).Init()
  71. var cacheKey string
  72. defer func() {
  73. if br.ErrMsg == "" {
  74. br.IsSendEmail = false
  75. }
  76. this.Data["json"] = br
  77. this.ServeJSON()
  78. }()
  79. var req models.RefreshEdbInfoReq
  80. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  81. if err != nil {
  82. br.Msg = "参数解析异常!"
  83. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  84. return
  85. }
  86. if req.EdbCode == "" {
  87. br.Msg = "请输入指标编码!"
  88. br.ErrMsg = "请输入指标编码,指标编码为空"
  89. return
  90. }
  91. if req.EdbInfoId < 0 {
  92. br.Msg = "请输入指标ID!"
  93. br.ErrMsg = "请输入指标ID"
  94. return
  95. }
  96. ob := new(models.BaseFromFenwei)
  97. source := ob.GetSource()
  98. // 获取指标详情
  99. edbInfo, err := models.GetEdbInfoByEdbCode(source, req.EdbCode)
  100. if err != nil {
  101. br.Msg = "指标不存在!"
  102. br.ErrMsg = "指标不存在"
  103. return
  104. }
  105. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  106. if utils.Rc.IsExist(cacheKey) {
  107. br.Ret = 501
  108. br.Success = true
  109. br.Msg = "系统处理中,请稍后重试"
  110. return
  111. }
  112. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  113. defer func() {
  114. _ = utils.Rc.Delete(cacheKey)
  115. }()
  116. if req.EdbInfoId <= 0 {
  117. req.EdbInfoId = edbInfo.EdbInfoId
  118. }
  119. err = ob.Refresh(req.EdbInfoId, req.EdbCode, req.StartDate)
  120. if err != nil && err.Error() != utils.ErrNoRow() {
  121. br.Msg = "刷新指标信息失败!"
  122. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromFenwei,Err:" + err.Error()
  123. return
  124. }
  125. // 更新指标最大最小值
  126. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  127. if err != nil {
  128. br.Msg = errMsg
  129. br.ErrMsg = err.Error()
  130. return
  131. }
  132. // 更新ES
  133. go logic.UpdateEs(edbInfo.EdbInfoId)
  134. br.Ret = 200
  135. br.Success = true
  136. br.Msg = "获取成功"
  137. }
  138. // HandleExcelData
  139. // @Title 处理汾渭煤炭指标的接口
  140. // @Description 处理汾渭煤炭指标的接口
  141. // @Success 200 string "操作成功"
  142. // @router /handle/excel_data [post]
  143. func (this *FenweiController) HandleExcelData() {
  144. br := new(models.BaseResponse).Init()
  145. defer func() {
  146. if br.ErrMsg == "" {
  147. br.IsSendEmail = false
  148. }
  149. this.Data["json"] = br
  150. this.ServeJSON()
  151. }()
  152. var req models.HandleFenweiExcelDataReq
  153. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  154. if err != nil {
  155. br.Msg = "参数解析异常!"
  156. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  157. return
  158. }
  159. // 处理excel表数据
  160. if err = services.HandleFenweiIndex(&req); err != nil {
  161. br.Msg = "处理失败"
  162. br.ErrMsg = "处理失败,Err:" + err.Error()
  163. return
  164. }
  165. br.Ret = 200
  166. br.Success = true
  167. br.Msg = "处理成功"
  168. }
  169. // ClassifyTree
  170. // @Title 汾渭数据分类
  171. // @Description 汾渭数据分类接口
  172. // @Success 200 {object} models.BaseFromFenweiClassifyItem
  173. // @router /classify_tree [post]
  174. func (this *FenweiController) ClassifyTree() {
  175. br := new(models.BaseResponse).Init()
  176. defer func() {
  177. if br.ErrMsg == "" {
  178. br.IsSendEmail = false
  179. }
  180. this.Data["json"] = br
  181. this.ServeJSON()
  182. }()
  183. classifyOb := new(models.BaseFromFenweiClassify)
  184. classifies, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "parent_id ASC, sort ASC")
  185. if e != nil {
  186. br.Msg = "获取失败"
  187. br.ErrMsg = "获取汾渭数据分类失败, Err: " + e.Error()
  188. return
  189. }
  190. resp := make([]*models.BaseFromFenweiClassifyItem, 0)
  191. parentMap := make(map[int][]*models.BaseFromFenweiClassifyItem)
  192. for _, v := range classifies {
  193. t := classifyOb.Format2Item(v)
  194. if v.ParentId == 0 {
  195. resp = append(resp, t)
  196. }
  197. if v.ParentId > 0 {
  198. if parentMap[v.ParentId] == nil {
  199. parentMap[v.ParentId] = make([]*models.BaseFromFenweiClassifyItem, 0)
  200. }
  201. parentMap[v.ParentId] = append(parentMap[v.ParentId], t)
  202. }
  203. }
  204. for _, v := range resp {
  205. v.Child = parentMap[v.ClassifyId]
  206. }
  207. br.Data = resp
  208. br.Ret = 200
  209. br.Success = true
  210. br.Msg = "获取成功"
  211. }
  212. // BaseIndexList
  213. // @Title 汾渭指标基础信息列表
  214. // @Description 汾渭指标基础信息列表接口
  215. // @Success 200 {object} models.BaseFromFenweiIndex
  216. // @router /base_index_list [post]
  217. func (this *FenweiController) BaseIndexList() {
  218. br := new(models.BaseResponse).Init()
  219. defer func() {
  220. if br.ErrMsg == "" {
  221. br.IsSendEmail = false
  222. }
  223. this.Data["json"] = br
  224. this.ServeJSON()
  225. }()
  226. ob := new(models.BaseFromFenweiIndex)
  227. list, e := ob.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  228. if e != nil {
  229. br.Msg = "获取失败"
  230. br.ErrMsg = "获取汾渭指标列表失败, Err: " + e.Error()
  231. return
  232. }
  233. br.Data = list
  234. br.Ret = 200
  235. br.Success = true
  236. br.Msg = "获取成功"
  237. }
  238. // NetDataHandle
  239. // @Title 汾渭网页数据落库
  240. // @Description 汾渭网页数据落库
  241. // @Success 200 string "操作成功"
  242. // @router /net/data/handle [post]
  243. func (this *FenweiController) NetDataHandle() {
  244. br := new(models.BaseResponse).Init()
  245. defer func() {
  246. if br.ErrMsg == "" {
  247. br.IsSendEmail = false
  248. }
  249. this.Data["json"] = br
  250. this.ServeJSON()
  251. }()
  252. var req models.HandleFenWeiNetDataReq
  253. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  254. if err != nil {
  255. br.Msg = "参数解析异常!"
  256. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  257. return
  258. }
  259. logs.Info("汾渭网页数据落库请求参数 List size:", len(req.List))
  260. err = services.NetDataHandle(req)
  261. if err != nil {
  262. return
  263. }
  264. br.Ret = 200
  265. br.Success = true
  266. br.Msg = "操作成功"
  267. }