edb_info.go 11 KB

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