base_from_smm.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. "fmt"
  9. "math"
  10. "strconv"
  11. "time"
  12. )
  13. // 有色
  14. type SmmController struct {
  15. BaseAuthController
  16. }
  17. // @Title 新增有色指标接口
  18. // @Description 新增有色指标接口
  19. // @Success 200 {object} models.AddEdbInfoReq
  20. // @router /add [post]
  21. func (this *SmmController) Add() {
  22. br := new(models.BaseResponse).Init()
  23. var cacheKey string
  24. defer func() {
  25. utils.Rc.Delete(cacheKey)
  26. this.Data["json"] = br
  27. this.ServeJSON()
  28. }()
  29. source := utils.DATA_SOURCE_YS
  30. var req models.AddEdbInfoReq
  31. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  32. if err != nil {
  33. br.Msg = "参数解析异常!"
  34. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  35. return
  36. }
  37. if req.EdbCode == "" {
  38. br.Msg = "请输入指标编码!"
  39. br.ErrMsg = "请输入指标编码,指标编码为空"
  40. return
  41. }
  42. cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(source) + "_" + req.EdbCode
  43. if !utils.Rc.IsExist(cacheKey) {
  44. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  45. smmBaseDataAllList, err := services.GetEdbDataFromSmm(req.EdbCode)
  46. if err!= nil {
  47. br.Msg = "获取指标信息失败!"
  48. br.ErrMsg = "获取指标信息失败 GetEdbDataFromSmm,Err:" + err.Error()
  49. return
  50. }
  51. for _, item := range smmBaseDataAllList {
  52. err = models.AddEdbDataFromSmm(item.IndexCode, item.Data)
  53. if err != nil {
  54. br.Msg = "获取指标信息失败!"
  55. br.ErrMsg = "获取指标信息失败 AddEdbDataFromSmm,Err:" + err.Error()
  56. return
  57. }
  58. }
  59. br.Ret = 200
  60. br.Success = true
  61. br.Msg = "获取成功"
  62. } else {
  63. br.Ret = 501
  64. br.Success = true
  65. br.Msg = "系统处理中,请稍后重试"
  66. }
  67. }
  68. // @Title 刷新有色指标接口
  69. // @Description 刷新有色指标接口
  70. // @Success 200 {object} models.RefreshEdbInfoReq
  71. // @router /refresh [post]
  72. func (this *SmmController) Refresh() {
  73. br := new(models.BaseResponse).Init()
  74. var cacheKey string
  75. defer func() {
  76. this.Data["json"] = br
  77. this.ServeJSON()
  78. }()
  79. source := utils.DATA_SOURCE_YS
  80. var req models.RefreshEdbInfoReq
  81. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  82. if err != nil {
  83. br.Msg = "参数解析异常!"
  84. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  85. return
  86. }
  87. if req.EdbCode == "" {
  88. br.Msg = "请输入指标编码!"
  89. br.ErrMsg = "请输入指标编码,指标编码为空"
  90. return
  91. }
  92. if req.EdbInfoId < 0 {
  93. br.Msg = "请输入指标ID!"
  94. br.ErrMsg = "请输入指标ID"
  95. return
  96. }
  97. // 获取指标详情
  98. edbInfo, err := models.GetEdbInfoByEdbCode(source, req.EdbCode)
  99. if err != nil {
  100. br.Msg = "指标不存在!"
  101. br.ErrMsg = "指标不存在"
  102. return
  103. }
  104. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  105. if utils.Rc.IsExist(cacheKey) {
  106. br.Ret = 501
  107. br.Success = true
  108. br.Msg = "系统处理中,请稍后重试"
  109. return
  110. }
  111. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  112. defer func() {
  113. utils.Rc.Delete(cacheKey)
  114. }()
  115. if req.EdbInfoId <= 0 {
  116. req.EdbInfoId = edbInfo.EdbInfoId
  117. }
  118. if utils.SmmDataMethod == "api" {
  119. err = services.GetSmmIndexLatestFromBridge(req.EdbInfoId, req.EdbCode, req.StartDate)
  120. if err != nil && err.Error() != utils.ErrNoRow() {
  121. br.Msg = "刷新指标信息失败!"
  122. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromSmm,Err:" + err.Error()
  123. return
  124. }
  125. } else {
  126. err = models.RefreshEdbDataFromSmm(req.EdbInfoId, req.EdbCode, req.StartDate)
  127. if err != nil && err.Error() != utils.ErrNoRow() {
  128. br.Msg = "刷新指标信息失败!"
  129. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromSmm,Err:" + err.Error()
  130. return
  131. }
  132. }
  133. // 更新指标最大最小值
  134. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  135. if err != nil {
  136. br.Msg = errMsg
  137. br.ErrMsg = err.Error()
  138. return
  139. }
  140. // 更新ES
  141. go logic.UpdateEs(edbInfo.EdbInfoId)
  142. br.Ret = 200
  143. br.Success = true
  144. br.Msg = "获取成功"
  145. }
  146. // HandleExcelData
  147. // @Title 处理有色指标的接口
  148. // @Description 处理有色指标的接口
  149. // @Success 200 string "操作成功"
  150. // @router /handle/excel_data [post]
  151. func (this *SmmController) HandleExcelData() {
  152. br := new(models.BaseResponse).Init()
  153. defer func() {
  154. this.Data["json"] = br
  155. this.ServeJSON()
  156. }()
  157. var req models.HandleSmmExcelDataReq
  158. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  159. if err != nil {
  160. br.Msg = "参数解析异常!"
  161. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  162. return
  163. }
  164. // 处理excel表数据
  165. e, errMsg := services.SmmIndexHandle(req.BaseFilePath, req.RenameFilePath, req.IndexName, req.IndexCode, req.Unit, req.Frequency, req.Source, req.ExcelDataMap, req.TerminalCode)
  166. if e != nil {
  167. br.Msg = "处理失败"
  168. br.ErrMsg = fmt.Sprintf("处理有色excel表数据失败, Err: %s, ErrMsg: %s", e.Error(), errMsg)
  169. return
  170. }
  171. br.Ret = 200
  172. br.Success = true
  173. br.Msg = "处理成功"
  174. }
  175. // AddIndexToDataSource
  176. // @Title 添加指标到数据源-SMM
  177. // @Description 添加指标到数据源-SMM
  178. // @Success 200 {object} models.AddSmmIndexToDataSourceResp
  179. // @router /add/index/to_data_source [post]
  180. func (this *SmmController) AddIndexToDataSource() {
  181. br := new(models.BaseResponse).Init()
  182. defer func() {
  183. this.Data["json"] = br
  184. this.ServeJSON()
  185. }()
  186. var req models.AddSmmIndexToDataSourceResp
  187. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  188. if err != nil {
  189. br.Msg = "参数解析异常!"
  190. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  191. return
  192. }
  193. smmIndexObj := new(models.BaseFromSmmIndex)
  194. indexItem, err := smmIndexObj.GetSmmIndexItem(req.EdbCode)
  195. var isAdd bool
  196. if err != nil {
  197. if err.Error() == utils.ErrNoRow() {
  198. isAdd = true
  199. } else {
  200. br.Msg = "判断指标是否存在失败"
  201. br.ErrMsg = "判断指标是否存在失败,Err:" + err.Error()
  202. return
  203. }
  204. }
  205. if indexItem == nil {
  206. isAdd = true
  207. }
  208. if isAdd {
  209. smmIndexObj.IndexCode = req.EdbCode
  210. smmIndexObj.ClassifyId = req.ClassifyId
  211. smmIndexObj.CreateTime = time.Now()
  212. smmIndexObj.ModifyTime = time.Now()
  213. err = smmIndexObj.Add()
  214. if err != nil {
  215. br.Msg = "新增指标失败"
  216. br.ErrMsg = "新增指标失败,Err:" + err.Error()
  217. return
  218. }
  219. }
  220. br.Ret = 200
  221. br.Success = true
  222. br.Msg = "新增成功"
  223. }
  224. // GetIndexDetailByCode
  225. // @Title 获取指标详情
  226. // @Description 获取指标详情
  227. // @Success 200 {object} models.GetIndexDetailReq
  228. // @router /index_detail/from_data_source [post]
  229. func (this *SmmController) GetIndexDetailFromDataSourceByCode() {
  230. br := new(models.BaseResponse).Init()
  231. defer func() {
  232. this.Data["json"] = br
  233. this.ServeJSON()
  234. }()
  235. var req models.GetIndexDetailReq
  236. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  237. if err != nil {
  238. br.Msg = "参数解析异常!"
  239. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  240. return
  241. }
  242. if req.IndexCode == "" {
  243. br.Msg = "参数有误"
  244. br.ErrMsg = "参数有误"
  245. return
  246. }
  247. indexObj := new(models.BaseFromSmmIndex)
  248. item, err := indexObj.GetIndexItem(req.IndexCode)
  249. if err != nil {
  250. br.Msg = "获取失败"
  251. br.ErrMsg = "获取指标失败, Err: " + err.Error()
  252. return
  253. }
  254. br.Data = item
  255. br.Ret = 200
  256. br.Success = true
  257. br.Msg = "获取成功"
  258. }
  259. // @Title 新增有色指标接口
  260. // @Description 新增有色指标接口
  261. // @Success 200 {object} models.AddEdbInfoReq
  262. // @router /zhongji/list [post]
  263. func (this *SmmController) List() {
  264. br := new(models.BaseResponse).Init()
  265. var cacheKey string
  266. defer func() {
  267. utils.Rc.Delete(cacheKey)
  268. this.Data["json"] = br
  269. this.ServeJSON()
  270. }()
  271. list, err := models.GetBaseFromSmmIndex()
  272. if err!= nil {
  273. br.Msg = "获取指标失败"
  274. br.ErrMsg = "获取指标失败, Err: " + err.Error()
  275. return
  276. }
  277. indexCodeMap := make(map[string]*models.BaseFromSmmIndex)
  278. for _, item := range list {
  279. indexCodeMap[item.IndexCode] = item
  280. }
  281. // 从桥接服务获取指标和数据
  282. var params models.BridgeZhongJiIndexListParams
  283. params.Page = 1
  284. params.PageSize = 500
  285. total, err := services.GetSmmNewIndexListTotalFromBridge(params)
  286. if err!= nil {
  287. br.Msg = "获取指标失败"
  288. br.ErrMsg = "获取指标数据失败, Err: " + err.Error()
  289. return
  290. }
  291. if total <= 0 {
  292. br.Msg = "没有指标数据"
  293. br.ErrMsg = "没有指标数据"
  294. return
  295. }
  296. var addList []*models.BaseFromSmmIndex
  297. for i := 1; i <= int(math.Ceil(float64(total)/float64(params.PageSize))); i++ {
  298. time.Sleep(time.Second * 2)
  299. params.Page = i
  300. indexList, err := services.GetSmmNewIndexListFromBridge(params)
  301. if err!= nil {
  302. br.Msg = "获取指标失败"
  303. br.ErrMsg = "获取指标数据失败, Err: " + err.Error()
  304. }
  305. for _, item := range indexList {
  306. if _, ok := indexCodeMap[item.IndexCode]; !ok {
  307. addList = append(addList, item)
  308. indexCodeMap[item.IndexCode] = item
  309. } else {
  310. if indexCodeMap[item.IndexCode].DataState != item.DataState || indexCodeMap[item.IndexCode].EndDate != item.EndDate {
  311. err = models.ModifyBaseFromSmmIndex(item)
  312. if err != nil {
  313. br.Msg = "更新指标失败"
  314. br.ErrMsg = "更新指标失败,Err:" + err.Error()
  315. return
  316. }
  317. fmt.Println("更新数据:",item.IndexCode)
  318. }
  319. }
  320. }
  321. if len(addList) > 0 {
  322. _,err = models.AddBaseFromSmmIndex(addList)
  323. if err!= nil {
  324. br.Msg = "新增指标失败"
  325. br.ErrMsg = "新增指标失败,Err:" + err.Error()
  326. return
  327. }
  328. //fmt.Println("新增数据:",len(addList))
  329. addList = []*models.BaseFromSmmIndex{}
  330. }
  331. }
  332. br.Ret = 200
  333. br.Success = true
  334. br.Msg = "操作成功"
  335. }