edb_info.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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/utils"
  7. "fmt"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // 指标
  13. type EdbInfoController struct {
  14. BaseAuthController
  15. }
  16. // @Title 新增指标接口
  17. // @Description 新增指标接口
  18. // @Success 200 {object} models.AddEdbInfoParams
  19. // @router /add [post]
  20. func (this *EdbInfoController) Add() {
  21. br := new(models.BaseResponse).Init()
  22. var cacheKey string
  23. defer func() {
  24. utils.Rc.Delete(cacheKey)
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. var req models.AddEdbInfoParams
  29. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  30. if err != nil {
  31. br.Msg = "参数解析异常!"
  32. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  33. return
  34. }
  35. if req.EdbCode == "" {
  36. br.Msg = "请输入指标编码!"
  37. br.ErrMsg = "请输入指标编码,指标编码为空"
  38. return
  39. }
  40. deleteCache := true
  41. cacheKey = "CACHE_EDB_INFO_ADD_" + req.EdbCode
  42. defer func() {
  43. if deleteCache {
  44. utils.Rc.Delete(cacheKey)
  45. }
  46. }()
  47. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  48. deleteCache = false
  49. br.Msg = "系统处理中,请稍后重试!"
  50. br.ErrMsg = "系统处理中,请稍后重试!"
  51. return
  52. }
  53. if req.ClassifyId <= 0 {
  54. br.Msg = "请输入指标分类!"
  55. br.ErrMsg = "请输入指标分类"
  56. return
  57. }
  58. if req.Source <= 0 {
  59. br.Msg = "请输入指标来源!"
  60. br.ErrMsg = "请输入指标来源"
  61. return
  62. }
  63. if req.EdbName == "" {
  64. br.Msg = "请输入指标名称!"
  65. br.ErrMsg = "请输入指标名称"
  66. return
  67. }
  68. if req.Frequency == "" {
  69. br.Msg = "请输入指标频度!"
  70. br.ErrMsg = "请输入指标频度"
  71. return
  72. }
  73. if req.Unit == "" {
  74. br.Msg = "请输入指标单位!"
  75. br.ErrMsg = "请输入指标单位"
  76. return
  77. }
  78. var isAdd bool
  79. item, err := models.GetEdbInfoByEdbCode(req.Source, req.EdbCode)
  80. if err != nil {
  81. if err.Error() == utils.ErrNoRow() {
  82. isAdd = true
  83. } else {
  84. br.Msg = "判断指标是否存在失败!"
  85. br.ErrMsg = "判断指标是否存在失败"
  86. return
  87. }
  88. }
  89. if item != nil && item.EdbInfoId <= 0 {
  90. isAdd = true
  91. }
  92. var windUrl string
  93. if isAdd {
  94. if req.Source == utils.DATA_SOURCE_WIND {
  95. windUrl, err = GetServerUrl(req.EdbCode)
  96. if err != nil {
  97. br.Msg = "判断失败!"
  98. br.ErrMsg = "判断失败,Err:" + err.Error()
  99. return
  100. }
  101. }
  102. }
  103. admnId, _ := strconv.Atoi(utils.InitAdminId)
  104. edbInfo, err := models.EdbInfoAdd(&req, windUrl, admnId, utils.InitAdminName)
  105. if err != nil {
  106. br.Msg = "新增指标失败!"
  107. br.ErrMsg = "新增指标失败,Err:" + err.Error()
  108. return
  109. }
  110. // 更新ES
  111. go logic.UpdateEs(edbInfo.EdbInfoId)
  112. br.Ret = 200
  113. br.Msg = "新增成功"
  114. br.Data = edbInfo
  115. }
  116. // @Title 获取基础指标详情
  117. // @Description 获取基础指标详情
  118. // @Param request body models.EdbInfoDetailReq true "type json string"
  119. // @Success 200 {object} models.EdbInfo
  120. // @router /detail [post]
  121. func (this *EdbInfoController) EdbIndexDetail() {
  122. br := new(models.BaseResponse).Init()
  123. defer func() {
  124. this.Data["json"] = br
  125. this.ServeJSON()
  126. }()
  127. var req models.EdbInfoDetailReq
  128. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  129. if err != nil {
  130. br.Msg = "参数解析异常!"
  131. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  132. return
  133. }
  134. if req.EdbCode == "" {
  135. br.Msg = "请输入指标编码!"
  136. return
  137. }
  138. detail, err := models.GetEdbInfoOnlyByEdbCode(req.EdbCode)
  139. if err != nil {
  140. if err.Error() == utils.ErrNoRow() {
  141. br.Msg = "指标数据不存在!"
  142. br.ErrMsg = "指标数据不存在,Err:" + err.Error()
  143. br.ErrCode = "400"
  144. return
  145. }
  146. br.Msg = "获取数据失败!"
  147. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  148. return
  149. }
  150. br.Ret = 200
  151. br.Success = true
  152. br.Msg = "获取成功"
  153. br.Data = detail
  154. }
  155. // @Title 复制新增来自弘则的指标接口
  156. // @Description 复制新增来自弘则的指标接口
  157. // @Success 200 {object} models.EdbInfo
  158. // @router /copy/from/hz [post]
  159. func (this *EdbInfoController) CopyFromHz() {
  160. br := new(models.BaseResponse).Init()
  161. var cacheKey string
  162. defer func() {
  163. utils.Rc.Delete(cacheKey)
  164. this.Data["json"] = br
  165. this.ServeJSON()
  166. }()
  167. req := new(models.EdbInfo)
  168. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  169. if err != nil {
  170. br.Msg = "参数解析异常!"
  171. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  172. return
  173. }
  174. if req.EdbCode == "" {
  175. br.Msg = "请输入指标编码!"
  176. br.ErrMsg = "请输入指标编码,指标编码为空"
  177. return
  178. }
  179. deleteCache := true
  180. cacheKey = "CACHE_EDB_INFO_COPY_FROM_HZ_" + req.EdbCode
  181. defer func() {
  182. if deleteCache {
  183. utils.Rc.Delete(cacheKey)
  184. }
  185. }()
  186. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  187. deleteCache = false
  188. br.Msg = "系统处理中,请稍后重试!"
  189. br.ErrMsg = "系统处理中,请稍后重试!"
  190. return
  191. }
  192. if req.ClassifyId <= 0 {
  193. br.Msg = "请输入指标分类!"
  194. br.ErrMsg = "请输入指标分类"
  195. return
  196. }
  197. if req.Source <= 0 {
  198. br.Msg = "请输入指标来源!"
  199. br.ErrMsg = "请输入指标来源"
  200. return
  201. }
  202. if req.EdbName == "" {
  203. br.Msg = "请输入指标名称!"
  204. br.ErrMsg = "请输入指标名称"
  205. return
  206. }
  207. if req.Frequency == "" {
  208. br.Msg = "请输入指标频度!"
  209. br.ErrMsg = "请输入指标频度"
  210. return
  211. }
  212. if req.Unit == "" {
  213. br.Msg = "请输入指标单位!"
  214. br.ErrMsg = "请输入指标单位"
  215. return
  216. }
  217. var isAdd bool
  218. item, err := models.GetEdbInfoByEdbCode(req.Source, req.EdbCode)
  219. if err != nil {
  220. if err.Error() == utils.ErrNoRow() {
  221. isAdd = true
  222. } else {
  223. br.Msg = "判断指标是否存在失败!"
  224. br.ErrMsg = "判断指标是否存在失败"
  225. return
  226. }
  227. }
  228. if item != nil && item.EdbInfoId <= 0 {
  229. isAdd = true
  230. }
  231. if isAdd {
  232. req.EdbInfoId = 0
  233. req.StartDate = "1990-01-01"
  234. err = req.Add()
  235. if err != nil {
  236. br.Msg = "新增指标失败!"
  237. br.ErrMsg = "新增指标失败"
  238. return
  239. }
  240. }
  241. edbInfo, err := models.GetEdbInfoOnlyByEdbCode(req.EdbCode)
  242. // 更新ES
  243. go logic.UpdateEs(edbInfo.EdbInfoId)
  244. br.Ret = 200
  245. br.Msg = "新增成功"
  246. br.Data = edbInfo
  247. }
  248. // RefreshCheck @Title 指标数据更新检查
  249. // @Description 指标数据更新检查
  250. // @Param request body models.EdbInfoRefreshCheckReq true "type json string"
  251. // @Success 200 {object} models.EdbInfo
  252. // @router /refresh_check [post]
  253. func (this *EdbInfoController) RefreshCheck() {
  254. br := new(models.BaseResponse).Init()
  255. defer func() {
  256. this.Data["json"] = br
  257. this.ServeJSON()
  258. }()
  259. var req models.EdbInfoRefreshCheckReq
  260. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  261. if err != nil {
  262. br.Msg = "参数解析异常!"
  263. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  264. return
  265. }
  266. if req.Source == 0 {
  267. br.Msg = "请输入指标来源"
  268. return
  269. }
  270. if req.LatestDate == "" {
  271. br.Msg = "请输入查询日期"
  272. return
  273. }
  274. _, err = time.Parse(utils.FormatDate, req.LatestDate)
  275. if err != nil {
  276. br.Msg = "查询日期格式错误"
  277. return
  278. }
  279. condition := ` and source=? and no_update=0 `
  280. pars := make([]interface{}, 0)
  281. pars = append(pars, req.Source)
  282. if req.FrequencyBatch != "" {
  283. frequency := strings.Split(req.FrequencyBatch, ",")
  284. condition += ` and frequency in (` + utils.GetOrmInReplace(len(frequency)) + `)`
  285. for _, v := range frequency {
  286. pars = append(pars, v)
  287. }
  288. }
  289. updateCondition := condition + ` and latest_date = ?`
  290. pars = append(pars, req.LatestDate)
  291. unUpdateCondition := condition + ` and latest_date < ?`
  292. updateNum, err := models.GetEdbInfoCountByCondition(updateCondition, pars)
  293. if err != nil {
  294. br.Msg = "获取数据失败!"
  295. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  296. return
  297. }
  298. unUpdateNum, err := models.GetEdbInfoCountByCondition(unUpdateCondition, pars)
  299. if err != nil {
  300. br.Msg = "获取数据失败!"
  301. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  302. return
  303. }
  304. resp := models.EdbInfoRefreshCheckResp{
  305. UpdateNum: updateNum,
  306. UnUpdateNum: unUpdateNum,
  307. }
  308. br.Ret = 200
  309. br.Success = true
  310. br.Msg = "获取成功"
  311. br.Data = resp
  312. }
  313. // ResetEdbClassify
  314. // @Title 重置指标分类
  315. // @Description 重置指标分类
  316. // @Param request body models.ResetEdbClassifyReq true "type json string"
  317. // @Success 200 {object} models.EdbInfo
  318. // @router /classify_reset [post]
  319. func (this *EdbInfoController) ResetEdbClassify() {
  320. br := new(models.BaseResponse).Init()
  321. defer func() {
  322. if br.ErrMsg == "" {
  323. br.IsSendEmail = false
  324. }
  325. this.Data["json"] = br
  326. this.ServeJSON()
  327. }()
  328. var req models.ResetEdbClassifyReq
  329. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  330. if err != nil {
  331. br.Msg = "参数解析异常!"
  332. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  333. return
  334. }
  335. req.IndexCode = strings.TrimSpace(req.IndexCode)
  336. if req.IndexCode == "" {
  337. br.Msg = "指标编码不可为空"
  338. return
  339. }
  340. req.ClassifyFirst = strings.TrimSpace(req.ClassifyFirst)
  341. if req.ClassifyFirst == "" {
  342. br.Msg = "一级分类名称不可为空"
  343. return
  344. }
  345. req.ClassifySecond = strings.TrimSpace(req.ClassifySecond)
  346. if req.ClassifySecond == "" {
  347. br.Msg = "二级分类名称不可为空"
  348. return
  349. }
  350. req.ClassifyThird = strings.TrimSpace(req.ClassifyThird)
  351. if req.ClassifyThird == "" {
  352. br.Msg = "三级分类名称不可为空"
  353. return
  354. }
  355. // 获取指标信息
  356. edbInfo, e := models.GetEdbInfoOnlyByEdbCode(req.IndexCode)
  357. if e != nil {
  358. if e.Error() == utils.ErrNoRow() {
  359. br.Msg = "指标不存在"
  360. return
  361. }
  362. br.Msg = "获取指标信息失败"
  363. br.ErrMsg = "获取指标信息失败, Err: " + e.Error()
  364. return
  365. }
  366. edbInfoType := uint8(edbInfo.EdbInfoType)
  367. // 新增分类
  368. firstClassify, e, msg := models.SaveEdbClassify(req.ClassifyFirst, 0, 0, edbInfoType, utils.InitAdminId, utils.InitAdminName)
  369. if msg != "" {
  370. br.Msg = "新增一级分类失败"
  371. br.ErrMsg = "新增一级分类失败, ErrMsg: " + e.Error()
  372. if e != nil {
  373. br.ErrMsg = fmt.Sprintf("新增一级分类失败, Err: %s, ErrMsg: %s", e.Error(), msg)
  374. } else {
  375. br.IsSendEmail = false
  376. }
  377. return
  378. }
  379. secondClassify, e, msg := models.SaveEdbClassify(req.ClassifySecond, firstClassify.ClassifyId, 1, edbInfoType, utils.InitAdminId, utils.InitAdminName)
  380. if msg != "" {
  381. br.Msg = "新增二级分类失败"
  382. br.ErrMsg = "新增二级分类失败, ErrMsg: " + e.Error()
  383. if e != nil {
  384. br.ErrMsg = fmt.Sprintf("新增二级分类失败, Err: %s, ErrMsg: %s", e.Error(), msg)
  385. } else {
  386. br.IsSendEmail = false
  387. }
  388. return
  389. }
  390. thirdClassify, e, msg := models.SaveEdbClassify(req.ClassifyThird, secondClassify.ClassifyId, 2, edbInfoType, utils.InitAdminId, utils.InitAdminName)
  391. if msg != "" {
  392. br.Msg = "新增三级分类失败"
  393. br.ErrMsg = "新增三级分类失败, ErrMsg: " + e.Error()
  394. if e != nil {
  395. br.ErrMsg = fmt.Sprintf("新增三级分类失败, Err: %s, ErrMsg: %s", e.Error(), msg)
  396. } else {
  397. br.IsSendEmail = false
  398. }
  399. return
  400. }
  401. // 更新指标分类
  402. cols := []string{"ClassifyId", "ModifyTime"}
  403. edbInfo.ClassifyId = thirdClassify.ClassifyId
  404. edbInfo.ModifyTime = time.Now().Local()
  405. if e := edbInfo.Update(cols); e != nil {
  406. br.Msg = "更新指标失败"
  407. br.ErrMsg = "更新指标失败, Err: " + e.Error()
  408. return
  409. }
  410. // 更新ES
  411. go logic.UpdateEs(edbInfo.EdbInfoId)
  412. br.Data = edbInfo
  413. br.Ret = 200
  414. br.Success = true
  415. br.Msg = "操作成功"
  416. }