base_from_business.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // BusinessIndexController 自有数据(商家)
  14. type BusinessIndexController struct {
  15. BaseAuthController
  16. }
  17. // HandleBusinessIndexData
  18. // @Title 处理外部指标(商家)的接口
  19. // @Description 处理外部指标(商家)的接口
  20. // @Success 200 {object} models.BaseFromBusinessIndexResp
  21. // @router /handle [post]
  22. func (c *BusinessIndexController) HandleBusinessIndexData() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. c.Data["json"] = br
  26. c.ServeJSON()
  27. }()
  28. body := c.Ctx.Input.RequestBody
  29. var req models.AddBusinessIndexReq
  30. err := json.Unmarshal(body, &req)
  31. if err != nil {
  32. br.Msg = "参数解析异常!"
  33. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  34. return
  35. }
  36. // 如果没有传入指标编码的话,那么自动生成指标编码
  37. if req.IndexCode == `` {
  38. cacheKey := utils.CACHE_SELF_EDB_HANDLE
  39. if utils.Rc.IsExist(cacheKey) {
  40. br.Ret = 501
  41. br.Success = true
  42. br.Msg = "系统处理中,请稍后重试"
  43. return
  44. }
  45. // 刷新指标
  46. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  47. defer func() {
  48. _ = utils.Rc.Delete(cacheKey)
  49. }()
  50. }
  51. businessIndex, err := services.HandleBusinessIndex(&req)
  52. if err != nil {
  53. fmt.Println("HandleBusinessIndexData Err:" + err.Error())
  54. br.Msg = "处理失败"
  55. br.ErrMsg = "处理失败,Err:" + err.Error()
  56. return
  57. }
  58. br.Ret = 200
  59. br.Success = true
  60. br.Msg = "处理成功"
  61. br.Data = businessIndex
  62. }
  63. // DelIndex
  64. // @Title 删除外部指标(商家)的接口
  65. // @Description 删除外部指标(商家)的接口
  66. // @Success 200 string "删除成功"
  67. // @router /index/del [post]
  68. func (c *BusinessIndexController) DelIndex() {
  69. br := new(models.BaseResponse).Init()
  70. defer func() {
  71. c.Data["json"] = br
  72. c.ServeJSON()
  73. }()
  74. body := c.Ctx.Input.RequestBody
  75. var req models.DelBusinessIndexReq
  76. err := json.Unmarshal(body, &req)
  77. if err != nil {
  78. br.Msg = "参数解析异常!"
  79. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  80. return
  81. }
  82. // 如果没有传入指标编码的话,那么自动生成指标编码
  83. if len(req.IndexCodeList) < 0 {
  84. br.Msg = "指标编码不允许为空"
  85. return
  86. }
  87. joinEdbCodeList, needDelEdbCodeList, err, errMsg := services.DelBusinessIndex(req.IndexCodeList)
  88. if err != nil {
  89. br.Msg = errMsg
  90. br.ErrMsg = "删除失败,Err:" + err.Error()
  91. return
  92. }
  93. resp := services.DelBusinessIndexResp{
  94. IsDeleteEdbCodeList: needDelEdbCodeList,
  95. NoDeleteEdbCodeList: joinEdbCodeList,
  96. }
  97. br.Ret = 200
  98. br.Success = true
  99. br.Msg = "删除成功"
  100. br.Data = resp
  101. }
  102. // DelIndexData
  103. // @Title 删除外部指标(商家)的指定日期数据接口
  104. // @Description 删除外部指标(商家)的指定日期数据接口
  105. // @Success 200 string "删除成功"
  106. // @router /data/del [post]
  107. func (c *BusinessIndexController) DelIndexData() {
  108. br := new(models.BaseResponse).Init()
  109. defer func() {
  110. c.Data["json"] = br
  111. c.ServeJSON()
  112. }()
  113. body := c.Ctx.Input.RequestBody
  114. var req models.DelBusinessIndexDataReq
  115. err := json.Unmarshal(body, &req)
  116. if err != nil {
  117. br.Msg = "参数解析异常!"
  118. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  119. return
  120. }
  121. // 如果没有传入指标编码的话,那么自动生成指标编码
  122. if req.IndexCode == `` {
  123. br.Msg = "指标编码不允许为空"
  124. return
  125. }
  126. if req.StartDate == `` && req.EndDate == `` {
  127. br.Msg = "开始日期和结束日期不允许同时为空"
  128. return
  129. }
  130. err, errMsg := services.DelBusinessIndexData(req.IndexCode, req.StartDate, req.EndDate)
  131. if err != nil {
  132. br.Msg = errMsg
  133. br.ErrMsg = "删除失败,Err:" + err.Error()
  134. return
  135. }
  136. br.Ret = 200
  137. br.Success = true
  138. br.Msg = "删除成功"
  139. }
  140. // Add
  141. // @Title 新增自有数据(商家)指标接口
  142. // @Description 新增自有数据(商家)指标接口
  143. // @Success 200 {object} models.AddEdbBaseInfoReq
  144. // @router /add [post]
  145. func (c *BusinessIndexController) Add() {
  146. br := new(models.BaseResponse).Init()
  147. var cacheKey string
  148. defer func() {
  149. c.Data["json"] = br
  150. c.ServeJSON()
  151. }()
  152. var req models.AddEdbBaseInfoReq
  153. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  154. if err != nil {
  155. br.Msg = "参数解析异常!"
  156. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  157. return
  158. }
  159. if req.EdbCode == "" {
  160. br.Msg = "请输入指标编码!"
  161. br.ErrMsg = "请输入指标编码,指标编码为空"
  162. return
  163. }
  164. cacheKey = utils.CACHE_EDB_DATA_ADD + "_BusinessIndexController_" + req.EdbCode
  165. if utils.Rc.IsExist(cacheKey) {
  166. br.Ret = 501
  167. br.Success = true
  168. br.Msg = "系统处理中,请稍后重试"
  169. return
  170. }
  171. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  172. defer func() {
  173. _ = utils.Rc.Delete(cacheKey)
  174. }()
  175. baseFromBusinessIndexObj := models.BaseFromBusinessIndex{}
  176. businessIndexItem, err := baseFromBusinessIndexObj.GetIndexItem(req.EdbCode)
  177. if err != nil {
  178. br.Msg = "获取数据源中指标信息失败!"
  179. br.ErrMsg = "获取数据源中指标信息失败 baseFromBusinessIndexObj.GetIndexItem,Err:" + err.Error()
  180. return
  181. }
  182. // 添加指标
  183. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  184. uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  185. params := models.AddBaseParams{
  186. EdbCode: req.EdbCode,
  187. EdbName: req.EdbName,
  188. Unit: req.Unit,
  189. ClassifyId: req.ClassifyId,
  190. SysUserId: req.SysUserId,
  191. SysUserRealName: req.SysUserRealName,
  192. UniqueCode: uniqueCode,
  193. }
  194. obj := models.Business{}
  195. edbInfo, err, errMsg := obj.Add(params, businessIndexItem)
  196. if err != nil {
  197. br.Msg = "生成" + obj.GetSourceName() + "失败"
  198. if errMsg != `` {
  199. br.Msg = errMsg
  200. }
  201. br.ErrMsg = "生成" + obj.GetSourceName() + "失败 Err:" + err.Error()
  202. return
  203. }
  204. if edbInfo == nil {
  205. br.Msg = "生成" + obj.GetSourceName() + "失败2"
  206. br.ErrMsg = "生成" + obj.GetSourceName() + "失败"
  207. return
  208. }
  209. // 更新指标最大最小值
  210. err = obj.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  211. if err != nil {
  212. br.Msg = "更新指标最大最小值失败"
  213. br.ErrMsg = err.Error()
  214. return
  215. }
  216. // 添加到es
  217. go logic.UpdateEs(edbInfo.EdbInfoId)
  218. resp := models.AddEdbInfoResp{
  219. EdbInfoId: edbInfo.EdbInfoId,
  220. UniqueCode: edbInfo.UniqueCode,
  221. }
  222. br.Ret = 200
  223. br.Success = true
  224. br.Msg = "保存成功"
  225. br.Data = resp
  226. br.IsAddLog = true
  227. }
  228. // Edit
  229. // @Title 编辑自有数据(商家)指标接口
  230. // @Description 编辑自有数据(商家)指标接口
  231. // @Success 200 {object} models.EditEdbBaseInfoReq
  232. // @router /edit [post]
  233. func (c *BusinessIndexController) Edit() {
  234. br := new(models.BaseResponse).Init()
  235. defer func() {
  236. c.Data["json"] = br
  237. c.ServeJSON()
  238. }()
  239. var req models.EditEdbBaseInfoReq
  240. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  241. if err != nil {
  242. br.Msg = "参数解析异常!"
  243. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  244. return
  245. }
  246. cacheKey := utils.CACHE_EDB_DATA_EDIT + "_BusinessIndexController_" + fmt.Sprint(req.EdbInfoId)
  247. if utils.Rc.IsExist(cacheKey) {
  248. br.Ret = 501
  249. br.Success = true
  250. br.Msg = "系统处理中,请稍后重试"
  251. return
  252. }
  253. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  254. defer func() {
  255. _ = utils.Rc.Delete(cacheKey)
  256. }()
  257. if req.EdbInfoId <= 0 {
  258. br.Msg = "参数错误"
  259. br.ErrMsg = "指标ID:" + strconv.Itoa(req.EdbInfoId)
  260. br.IsSendEmail = false
  261. return
  262. }
  263. req.EdbName = strings.Trim(req.EdbName, " ")
  264. if req.EdbName == "" {
  265. br.Msg = "指标名称不能为空"
  266. br.IsSendEmail = false
  267. return
  268. }
  269. //if req.Frequency == "" {
  270. // br.Msg = "频率不能为空"
  271. // return
  272. //}
  273. if req.Unit == "" {
  274. br.Msg = "单位不能为空"
  275. br.IsSendEmail = false
  276. return
  277. }
  278. if req.ClassifyId <= 0 {
  279. br.Msg = "请选择分类"
  280. br.IsSendEmail = false
  281. return
  282. }
  283. // 根据指标名称和指标ID校验库中是否还存在其他同名指标
  284. existEdbName, err := logic.CheckExistByEdbNameAndEdbInfoId(0, req.EdbInfoId, req.EdbName, c.Lang)
  285. if err != nil {
  286. br.Msg = "判断指标名称是否存在失败"
  287. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  288. return
  289. }
  290. if existEdbName {
  291. br.Msg = "指标名称已存在,请重新填写"
  292. br.ErrMsg = "指标名称已存在,请重新填写"
  293. br.IsSendEmail = false
  294. return
  295. }
  296. //判断公式,指标是否有改动
  297. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  298. if err != nil {
  299. br.Msg = "修改失败"
  300. br.Msg = "获取指标信息失败,GetEdbInfoById Err:" + err.Error()
  301. return
  302. }
  303. if edbInfo == nil {
  304. br.Msg = "修改失败"
  305. br.Msg = "指标信息不存在,EdbInfoId:" + strconv.Itoa(req.EdbInfoId)
  306. return
  307. }
  308. baseFromBusinessIndexObj := models.BaseFromBusinessIndex{}
  309. businessIndexItem, err := baseFromBusinessIndexObj.GetIndexItem(edbInfo.EdbCode)
  310. if err != nil {
  311. br.Msg = "获取数据源中指标信息失败!"
  312. br.ErrMsg = "获取数据源中指标信息失败 baseFromBusinessIndexObj.GetIndexItem,Err:" + err.Error()
  313. return
  314. }
  315. // 额外赋值
  316. switch c.Lang {
  317. case utils.EnLangVersion:
  318. req.EdbNameEn = req.EdbName
  319. req.UnitEn = req.Unit
  320. req.EdbName = edbInfo.EdbName
  321. req.Unit = edbInfo.Unit
  322. default:
  323. req.EdbNameEn = edbInfo.EdbNameEn
  324. req.UnitEn = edbInfo.UnitEn
  325. }
  326. // 编辑指标
  327. params := models.EditBaseParams{
  328. EdbCode: edbInfo.EdbCode,
  329. EdbName: req.EdbName,
  330. Unit: req.Unit,
  331. ClassifyId: req.ClassifyId,
  332. SysUserId: req.SysUserId,
  333. SysUserRealName: req.SysUserRealName,
  334. Lang: c.Lang,
  335. EdbInfo: edbInfo,
  336. EdbNameEn: req.EdbNameEn,
  337. UnitEn: req.UnitEn,
  338. }
  339. obj := models.Business{}
  340. err, errMsg := obj.Edit(params, businessIndexItem)
  341. if err != nil {
  342. br.Msg = "修改" + obj.GetSourceName() + "失败"
  343. if errMsg != `` {
  344. br.Msg = errMsg
  345. }
  346. br.ErrMsg = "修改" + obj.GetSourceName() + "失败 Err:" + err.Error()
  347. return
  348. }
  349. // 更新指标最大最小值
  350. err = obj.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  351. if err != nil {
  352. br.Msg = "更新指标最大最小值失败"
  353. br.ErrMsg = err.Error()
  354. return
  355. }
  356. // 添加到es
  357. go logic.UpdateEs(edbInfo.EdbInfoId)
  358. resp := models.AddEdbInfoResp{
  359. EdbInfoId: edbInfo.EdbInfoId,
  360. UniqueCode: edbInfo.UniqueCode,
  361. }
  362. br.Ret = 200
  363. br.Success = true
  364. br.Msg = "保存成功"
  365. br.Data = resp
  366. br.IsAddLog = true
  367. }
  368. // Refresh
  369. // @Title 刷新计算指标接口
  370. // @Description 刷新计算指标接口
  371. // @Success 200 {object} models.RefreshEdbInfoReq
  372. // @router /refresh [post]
  373. func (c *BusinessIndexController) Refresh() {
  374. br := new(models.BaseResponse).Init()
  375. var cacheKey string
  376. defer func() {
  377. c.Data["json"] = br
  378. c.ServeJSON()
  379. }()
  380. var req models.RefreshEdbInfoReq
  381. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  382. if err != nil {
  383. br.Msg = "参数解析异常!"
  384. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  385. return
  386. }
  387. if req.EdbCode == "" {
  388. br.Msg = "请输入指标编码!"
  389. br.ErrMsg = "请输入指标编码,指标编码为空"
  390. return
  391. }
  392. if req.EdbInfoId <= 0 {
  393. br.Msg = "请输入指标ID!"
  394. br.ErrMsg = "请输入指标ID"
  395. return
  396. }
  397. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  398. if err != nil {
  399. br.Msg = "指标不存在!"
  400. br.ErrMsg = "指标不存在"
  401. return
  402. }
  403. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(edbInfo.Source) + "_" + req.EdbCode
  404. if utils.Rc.IsExist(cacheKey) {
  405. br.Ret = 501
  406. br.Success = true
  407. br.Msg = "系统处理中,请稍后重试"
  408. return
  409. }
  410. // 刷新指标
  411. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  412. defer func() {
  413. _ = utils.Rc.Delete(cacheKey)
  414. }()
  415. params := models.RefreshBaseParams{
  416. EdbInfo: edbInfo,
  417. StartDate: req.StartDate,
  418. }
  419. obj := models.Business{}
  420. err, errMsg := obj.Refresh(params)
  421. if errMsg != `` {
  422. br.Msg = "刷新指标失败!"
  423. br.ErrMsg = "刷新指标失败,err:" + errMsg
  424. return
  425. }
  426. if err != nil && err.Error() != utils.ErrNoRow() {
  427. br.Msg = "刷新指标信息失败!"
  428. br.ErrMsg = "刷新指标信息失败 BusinessIndexController,Err:" + err.Error()
  429. return
  430. }
  431. // 更新指标最大最小值
  432. err = obj.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  433. if err != nil {
  434. br.Msg = "更新指标最大最小值失败"
  435. br.ErrMsg = err.Error()
  436. return
  437. }
  438. // 更新ES
  439. go logic.UpdateEs(edbInfo.EdbInfoId)
  440. br.Ret = 200
  441. br.Success = true
  442. br.Msg = "刷新成功"
  443. }