yongyi_data.go 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/models/data_manage/request"
  7. "eta/eta_api/models/system"
  8. "eta/eta_api/services/data"
  9. etaTrialService "eta/eta_api/services/eta_trial"
  10. "eta/eta_api/utils"
  11. "fmt"
  12. "os"
  13. "path/filepath"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "github.com/rdlucklib/rdluck_tools/paging"
  18. "github.com/tealeg/xlsx"
  19. )
  20. // YongyiClassify
  21. // @Title 涌益咨询数据分类
  22. // @Description 涌益咨询数据分类接口
  23. // @Success 200 {object} data_manage.BaseFromYongyiClassify
  24. // @router /yongyi/classify [get]
  25. func (this *EdbInfoController) YongyiClassify() {
  26. br := new(models.BaseResponse).Init()
  27. defer func() {
  28. this.Data["json"] = br
  29. this.ServeJSON()
  30. }()
  31. sysUser := this.SysUser
  32. if sysUser == nil {
  33. br.Msg = "请登录"
  34. br.ErrMsg = "请登录,SysUser Is Empty"
  35. br.Ret = 408
  36. return
  37. }
  38. classifyAll, err := data_manage.GetAllBaseFromYongyiClassify()
  39. if err != nil && err.Error() != utils.ErrNoRow() {
  40. br.Msg = "获取失败"
  41. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  42. return
  43. }
  44. //组装一级分类
  45. rootMap := make(map[int][]*data_manage.BaseFromYongyiClassifyItems)
  46. list := make([]*data_manage.BaseFromYongyiClassifyItems, 0)
  47. for _, classify := range classifyAll {
  48. classify.UniqueCode = strconv.Itoa(classify.ClassifyId)
  49. if classify.ParentId == 0 {
  50. if _, ok := rootMap[classify.ClassifyId]; !ok {
  51. rootMap[classify.ClassifyId] = make([]*data_manage.BaseFromYongyiClassifyItems, 0)
  52. list = append(list, classify)
  53. }
  54. } else {
  55. child, ok := rootMap[classify.ParentId]
  56. if ok {
  57. child = append(child, classify)
  58. rootMap[classify.ParentId] = child
  59. }
  60. }
  61. }
  62. for k, v := range list {
  63. child, ok := rootMap[v.ClassifyId]
  64. if ok {
  65. list[k].Children = child
  66. }
  67. }
  68. //组装二级分类
  69. var ret data_manage.BaseFromYongyiClassifyResp
  70. ret.List = list
  71. br.Ret = 200
  72. br.Success = true
  73. br.Msg = "获取成功"
  74. br.Data = ret
  75. }
  76. // YongyiIndexData
  77. // @Title 获取涌益咨询数据
  78. // @Description 获取涌益咨询数据接口
  79. // @Param PageSize query int true "每页数据条数"
  80. // @Param CurrentIndex query int true "当前页页码,从1开始"
  81. // @Param ClassifyId query string true "分类id"
  82. // @Success 200 {object} data_manage.LzFrequency
  83. // @router /yongyi/index/data [get]
  84. func (this *EdbInfoController) YongyiIndexData() {
  85. br := new(models.BaseResponse).Init()
  86. defer func() {
  87. this.Data["json"] = br
  88. this.ServeJSON()
  89. }()
  90. sysUser := this.SysUser
  91. if sysUser == nil {
  92. br.Msg = "请登录"
  93. br.ErrMsg = "请登录,SysUser Is Empty"
  94. br.Ret = 408
  95. return
  96. }
  97. pageSize, _ := this.GetInt("PageSize")
  98. currentIndex, _ := this.GetInt("CurrentIndex")
  99. var startSize int
  100. if pageSize <= 0 {
  101. pageSize = utils.PageSize20
  102. }
  103. if currentIndex <= 0 {
  104. currentIndex = 1
  105. }
  106. startSize = utils.StartIndex(currentIndex, pageSize)
  107. classifyId, _ := this.GetInt("ClassifyId")
  108. if classifyId < 0 {
  109. br.Msg = "请选择分类"
  110. br.ErrMsg = "请选择分类"
  111. return
  112. }
  113. //获取指标
  114. var condition string
  115. var pars []interface{}
  116. if classifyId >= 0 {
  117. classifyInfo, err := data_manage.GetBaseFromYongyiClassifyById(classifyId)
  118. if err != nil {
  119. if err.Error() == utils.ErrNoRow() {
  120. br.Msg = "分类不存在"
  121. return
  122. }
  123. br.Msg = "获取分类信息失败"
  124. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  125. return
  126. }
  127. if classifyInfo.Level == 2 || classifyInfo.ParentId > 0 {
  128. condition += ` AND classify_id=? `
  129. pars = append(pars, classifyId)
  130. } else if classifyInfo.Level == 1 {
  131. childClassify, err := data_manage.GetBaseFromYongyiClassifyByParentId(classifyId)
  132. if err != nil {
  133. br.Msg = "获取分类信息失败"
  134. br.ErrMsg = "获取子分类信息失败,Err:" + err.Error()
  135. return
  136. }
  137. var classifyList []int
  138. for _, v := range childClassify {
  139. classifyList = append(classifyList, v.ClassifyId)
  140. }
  141. condition += ` AND classify_id IN (` + utils.GetOrmInReplace(len(classifyList)) + `) `
  142. pars = append(pars, classifyList)
  143. }
  144. }
  145. yongyiList, err := data_manage.GetYongyiIndex(condition, pars)
  146. if err != nil {
  147. br.Msg = "获取数据失败"
  148. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  149. return
  150. }
  151. edbCodeList := make([]string, 0)
  152. for _, v := range yongyiList {
  153. edbCodeList = append(edbCodeList, v.IndexCode)
  154. }
  155. edbInfoList, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_YONYI, edbCodeList)
  156. if err != nil {
  157. br.Msg = "获取数据源失败"
  158. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  159. return
  160. }
  161. edbInfoMap := make(map[string]*data_manage.EdbInfo)
  162. for _, v := range edbInfoList {
  163. edbInfoMap[v.EdbCode] = v
  164. }
  165. resultList := make([]*data_manage.BaseFromYongyiIndexList, 0)
  166. for _, v := range yongyiList {
  167. product := new(data_manage.BaseFromYongyiIndexList)
  168. product.YongyiIndexId = v.YongyiIndexId
  169. product.Unit = v.Unit
  170. product.IndexCode = v.IndexCode
  171. product.IndexName = v.IndexName
  172. product.Frequency = v.Frequency
  173. product.ModifyTime = v.ModifyTime
  174. if edb, ok := edbInfoMap[v.IndexCode]; ok {
  175. product.EdbInfoId = edb.EdbInfoId
  176. product.EdbExist = 1
  177. }
  178. total, err := data_manage.GetYongyiIndexDataCount(v.IndexCode)
  179. if err != nil {
  180. br.Msg = "获取数据失败"
  181. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  182. }
  183. page := paging.GetPaging(currentIndex, pageSize, total)
  184. dataList, err := data_manage.GetYongyiIndexData(v.IndexCode, startSize, pageSize)
  185. if err != nil {
  186. br.Msg = "获取数据失败"
  187. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  188. return
  189. }
  190. if dataList == nil {
  191. dataList = make([]*data_manage.BaseFromYongyiData, 0)
  192. }
  193. product.DataList = dataList
  194. product.Paging = page
  195. resultList = append(resultList, product)
  196. }
  197. br.Ret = 200
  198. br.Success = true
  199. br.Msg = "获取成功"
  200. br.Data = resultList
  201. }
  202. // YongyiSearchList
  203. // @Title Yongyi模糊搜索
  204. // @Description Yongyi模糊搜索
  205. // @Param Keyword query string ture "关键字搜索"
  206. // @Success 200 {object} models.BaseResponse
  207. // @router /yongyi/search_list [get]
  208. func (this *EdbInfoController) YongyiSearchList() {
  209. br := new(models.BaseResponse).Init()
  210. defer func() {
  211. this.Data["json"] = br
  212. this.ServeJSON()
  213. }()
  214. sysUser := this.SysUser
  215. if sysUser == nil {
  216. br.Msg = "请重新登录"
  217. return
  218. }
  219. list := make([]*data_manage.BaseFromYongyiIndexSearchItem, 0)
  220. var err error
  221. //关键字
  222. keyword := this.GetString("Keyword")
  223. if keyword != "" {
  224. keyWordArr := strings.Split(keyword, " ")
  225. if len(keyWordArr) > 0 {
  226. condition := ""
  227. for _, v := range keyWordArr {
  228. condition += ` AND CONCAT(index_name,index_code) LIKE '%` + v + `%'`
  229. }
  230. list, err = data_manage.GetYongyiItemList(condition)
  231. if err != nil {
  232. br.ErrMsg = "获取失败,Err:" + err.Error()
  233. br.Msg = "获取失败"
  234. return
  235. }
  236. }
  237. } else {
  238. // todo es 模糊搜索
  239. list, err = data_manage.GetYongyiItemList("")
  240. if err != nil {
  241. br.ErrMsg = "获取失败,Err:" + err.Error()
  242. br.Msg = "获取失败"
  243. return
  244. }
  245. }
  246. classifyIds := make([]int, 0)
  247. for _, v := range list {
  248. classifyIds = append(classifyIds, v.ClassifyId)
  249. }
  250. classifyList, err := data_manage.GetBaseFromYongyiClassifyByIds(classifyIds)
  251. if err != nil {
  252. br.Msg = "搜索失败"
  253. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  254. return
  255. }
  256. classifyMap := make(map[int]int)
  257. for _, v := range classifyList {
  258. classifyMap[v.ClassifyId] = v.ParentId
  259. }
  260. for _, v := range list {
  261. v.ParentClassifyId = classifyMap[v.ClassifyId]
  262. }
  263. br.Ret = 200
  264. br.Success = true
  265. br.Msg = "获取成功"
  266. br.Data = list
  267. }
  268. // YongyiSingleData
  269. // @Title 获取Yongyi数据
  270. // @Description 获取Yongyi单条数据接口
  271. // @Param IndexCode query string true "指标唯一编码"
  272. // @Success 200 {object} models.BaseResponse
  273. // @router /yongyi/single_data [get]
  274. func (this *EdbInfoController) YongyiSingleData() {
  275. br := new(models.BaseResponse).Init()
  276. defer func() {
  277. this.Data["json"] = br
  278. this.ServeJSON()
  279. }()
  280. sysUser := this.SysUser
  281. if sysUser == nil {
  282. br.Msg = "请登录"
  283. br.ErrMsg = "请登录,SysUser Is Empty"
  284. br.Ret = 408
  285. return
  286. }
  287. indexCode := this.GetString("IndexCode")
  288. indexInfo, err := data_manage.GetBaseFromYongyiIndexByIndexCode(indexCode)
  289. if err != nil {
  290. br.Msg = "获取指标信息失败"
  291. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  292. return
  293. }
  294. dataTmpList, err := data_manage.GetYongyiIndexDataByCode(indexCode)
  295. if err != nil {
  296. br.Msg = "获取数据失败"
  297. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  298. return
  299. }
  300. edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_YONYI, indexCode)
  301. if err != nil && err.Error() != utils.ErrNoRow() {
  302. br.Msg = "获取数据源失败"
  303. br.ErrMsg = "获取数据源失败,Err:" + err.Error()
  304. return
  305. }
  306. var ret data_manage.YongyiSingleDataResp
  307. var dataList []*data_manage.YongyiSingleData
  308. if edbInfo != nil {
  309. ret.EdbInfoId = edbInfo.EdbInfoId
  310. ret.EdbExist = 1
  311. }
  312. ret.ClassifyId = indexInfo.ClassifyId
  313. ret.YongyiIndexId = indexInfo.YongyiIndexId
  314. ret.IndexCode = indexInfo.IndexCode
  315. ret.IndexName = indexInfo.IndexName
  316. ret.Frequency = indexInfo.Frequency
  317. ret.CreateTime = indexInfo.CreateTime.Format(utils.FormatDateTime)
  318. ret.ModifyTime = indexInfo.ModifyTime.Format(utils.FormatDateTime)
  319. ret.Unit = indexInfo.Unit
  320. for _, v := range dataTmpList {
  321. tmp := &data_manage.YongyiSingleData{
  322. Value: v.Value,
  323. DataTime: v.DataTime,
  324. }
  325. dataList = append(dataList, tmp)
  326. }
  327. ret.Data = dataList
  328. br.Ret = 200
  329. br.Success = true
  330. br.Msg = "获取成功"
  331. br.Data = ret
  332. }
  333. // YongyiIndexList
  334. // @Title 涌益咨询指标列表
  335. // @Description 涌益咨询指标列表
  336. // @Param ClassifyId query int true "分类id"
  337. // @Success 200 {object} data_manage.BaseFromMysteelChemicalIndexResp
  338. // @router /yongyi/classify/index/list [get]
  339. func (this *EdbInfoController) YongyiIndexList() {
  340. br := new(models.BaseResponse).Init()
  341. defer func() {
  342. this.Data["json"] = br
  343. this.ServeJSON()
  344. }()
  345. classifyId, _ := this.GetInt("ClassifyId", 0)
  346. indexList, err := data_manage.GetYongyiIndexByClassifyId(classifyId)
  347. if err != nil && err.Error() != utils.ErrNoRow() {
  348. br.Msg = "获取失败"
  349. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  350. return
  351. }
  352. var ret data_manage.BaseFromYongyiClassifyResp
  353. list := make([]*data_manage.BaseFromYongyiClassifyItems, 0)
  354. for _, v := range indexList {
  355. classify := new(data_manage.BaseFromYongyiClassifyItems)
  356. classify.ClassifyId = classifyId
  357. classify.YongyiIndexId = v.YongyiIndexId
  358. classify.YongyiIndexCode = v.IndexCode
  359. classify.ClassifyName = v.IndexName
  360. classify.UniqueCode = fmt.Sprintf("%d_%d", classifyId, v.YongyiIndexId)
  361. list = append(list, classify)
  362. }
  363. ret.List = list
  364. br.Ret = 200
  365. br.Success = true
  366. br.Msg = "获取成功"
  367. br.Data = ret
  368. }
  369. // YongyiBatchSearch
  370. // @Title 涌益咨询指标查询
  371. // @Description 涌益咨询指标查询
  372. // @Param ClassifyIds query string true "分类id, 多个分类用英文"
  373. // @Param Keyword query string true "关键词, 指标ID/指标名称"
  374. // @Success 200 {object} data_manage.LzFrequency
  375. // @router /yongyi/batch_search [get]
  376. func (this *EdbInfoController) YongyiBatchSearch() {
  377. br := new(models.BaseResponse).Init()
  378. defer func() {
  379. this.Data["json"] = br
  380. this.ServeJSON()
  381. }()
  382. sysUser := this.SysUser
  383. if sysUser == nil {
  384. br.Msg = "请登录"
  385. br.ErrMsg = "请登录,SysUser Is Empty"
  386. br.Ret = 408
  387. return
  388. }
  389. classifyIdStr := this.GetString("ClassifyIds")
  390. var condition string
  391. var pars []interface{}
  392. classifyIds := strings.Split(classifyIdStr, ",")
  393. if len(classifyIds) > 0 && classifyIds[0] != `` {
  394. condition += " AND classify_id IN (" + utils.GetOrmInReplace(len(classifyIds)) + " ) "
  395. pars = append(pars, classifyIds)
  396. }
  397. keyword := this.GetString("Keyword")
  398. if keyword != `` {
  399. condition += " AND (index_name like ? OR index_code like ?) "
  400. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  401. }
  402. if classifyIdStr == `` && keyword == `` {
  403. var list = make([]*data_manage.BaseFromYongyiIndexList, 0)
  404. br.Ret = 200
  405. br.Success = true
  406. br.Msg = "获取成功"
  407. br.Data = list
  408. return
  409. }
  410. list, err := data_manage.GetYongyiIndex(condition, pars)
  411. if err != nil {
  412. br.Msg = "获取失败"
  413. br.ErrMsg = "获取失败,Err:" + err.Error()
  414. return
  415. }
  416. br.Ret = 200
  417. br.Success = true
  418. br.Msg = "获取成功"
  419. br.Data = list
  420. }
  421. // YongyiBatchAdd
  422. // @Title 涌益批量新增
  423. // @Description 涌益批量新增
  424. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  425. // @Success 200 string "操作成功"
  426. // @router /yongyi/batch_add [post]
  427. func (this *EdbInfoController) YongyiBatchAdd() {
  428. br := new(models.BaseResponse).Init()
  429. defer func() {
  430. if br.ErrMsg == "" {
  431. br.IsSendEmail = false
  432. }
  433. this.Data["json"] = br
  434. this.ServeJSON()
  435. }()
  436. sysUser := this.SysUser
  437. if sysUser == nil {
  438. br.Msg = "请登录"
  439. br.ErrMsg = "请登录,SysUser Is Empty"
  440. br.Ret = 408
  441. return
  442. }
  443. deleteCache := true
  444. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_YONGYI_" + strconv.Itoa(sysUser.AdminId)
  445. defer func() {
  446. if deleteCache {
  447. _ = utils.Rc.Delete(cacheKey)
  448. }
  449. }()
  450. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  451. deleteCache = false
  452. br.Msg = "系统处理中,请稍后重试!"
  453. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  454. return
  455. }
  456. var req []*data_manage.AddEdbInfoReq
  457. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  458. br.Msg = "参数解析异常!"
  459. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  460. return
  461. }
  462. if len(req) == 0 {
  463. br.Msg = "请选择指标"
  464. return
  465. }
  466. if len(req) > 30 {
  467. br.Msg = "批量添加指标数量不得超过30个"
  468. return
  469. }
  470. for _, v := range req {
  471. v.EdbCode = strings.TrimSpace(v.EdbCode)
  472. if v.EdbCode == "" {
  473. br.Msg = "指标ID不可为空"
  474. return
  475. }
  476. v.EdbName = strings.TrimSpace(v.EdbName)
  477. if v.EdbName == "" {
  478. br.Msg = "请输入指标名称"
  479. return
  480. }
  481. v.Frequency = strings.TrimSpace(v.Frequency)
  482. if v.Frequency == "" {
  483. br.Msg = "请选择频度"
  484. return
  485. }
  486. v.Unit = strings.TrimSpace(v.Unit)
  487. if v.Unit == "" {
  488. br.Msg = "请输入单位"
  489. return
  490. }
  491. if v.ClassifyId <= 0 {
  492. br.Msg = "请选择分类"
  493. return
  494. }
  495. }
  496. // 限定同一时间最多批量新增30个指标
  497. for _, v := range req {
  498. var r request.YongyiIndexSource2EdbReq
  499. r.EdbCode = v.EdbCode
  500. r.EdbName = v.EdbName
  501. r.Frequency = v.Frequency
  502. r.Unit = v.Unit
  503. r.ClassifyId = v.ClassifyId
  504. r.AdminId = sysUser.AdminId
  505. r.AdminRealName = sysUser.RealName
  506. edbInfo, e, errMsg, skip := data.YongyiIndexSource2Edb(r, this.Lang)
  507. if e != nil {
  508. br.Msg = "操作失败"
  509. if errMsg != "" {
  510. br.Msg = errMsg
  511. }
  512. br.ErrMsg = e.Error()
  513. return
  514. }
  515. if skip {
  516. continue
  517. }
  518. // 试用平台更新用户累计新增指标数
  519. if utils.BusinessCode == utils.BusinessCodeSandbox {
  520. go func() {
  521. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  522. if e != nil {
  523. tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
  524. utils.FileLog.Info(tips)
  525. return
  526. }
  527. if adminItem.DepartmentName != "ETA试用客户" {
  528. return
  529. }
  530. var ur etaTrialService.EtaTrialUserReq
  531. ur.Mobile = adminItem.Mobile
  532. _, _ = etaTrialService.UpdateUserIndexNum(ur)
  533. }()
  534. }
  535. // 新增操作日志
  536. {
  537. edbLog := new(data_manage.EdbInfoLog)
  538. edbLog.EdbInfoId = edbInfo.EdbInfoId
  539. edbLog.SourceName = edbInfo.SourceName
  540. edbLog.Source = edbInfo.Source
  541. edbLog.EdbCode = edbInfo.EdbCode
  542. edbLog.EdbName = edbInfo.EdbName
  543. edbLog.ClassifyId = edbInfo.ClassifyId
  544. edbLog.SysUserId = sysUser.AdminId
  545. edbLog.SysUserRealName = sysUser.RealName
  546. edbLog.CreateTime = time.Now()
  547. edbLog.Content = string(this.Ctx.Input.RequestBody)
  548. edbLog.Status = "新增指标"
  549. edbLog.Method = this.Ctx.Input.URI()
  550. go data_manage.AddEdbInfoLog(edbLog)
  551. }
  552. }
  553. br.Msg = "操作成功"
  554. br.Ret = 200
  555. br.Success = true
  556. br.IsAddLog = true
  557. }
  558. // YongyiNameCheck
  559. // @Title 加入指标库的重名检测
  560. // @Description 加入指标库的重名检测
  561. // @Param ClassifyIds query string true "分类id, 多个分类用英文"
  562. // @Param Keyword query string true "关键词, 指标ID/指标名称"
  563. // @Success 200 {object} NameCheckResult
  564. // @router /yongyi/edb_info/name_check [post]
  565. func (this *EdbInfoController) YongyiNameCheck() {
  566. br := new(models.BaseResponse).Init()
  567. defer func() {
  568. if br.ErrMsg == "" {
  569. br.IsSendEmail = false
  570. }
  571. this.Data["json"] = br
  572. this.ServeJSON()
  573. }()
  574. sysUser := this.SysUser
  575. if sysUser == nil {
  576. br.Msg = "请登录"
  577. br.ErrMsg = "请登录,SysUser Is Empty"
  578. br.Ret = 408
  579. return
  580. }
  581. var req []*data_manage.NameCheckEdbInfoReq
  582. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  583. br.Msg = "参数解析异常!"
  584. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  585. return
  586. }
  587. if len(req) == 0 {
  588. br.Msg = "请选择指标"
  589. return
  590. }
  591. codeMax := 30
  592. codeLen := len(req)
  593. if codeLen > codeMax {
  594. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
  595. return
  596. }
  597. type NameCheckResult struct {
  598. EdbCode string
  599. EdbName string
  600. Exist bool
  601. }
  602. indexNames := make([]string, 0)
  603. resp := make([]*NameCheckResult, 0)
  604. for _, v := range req {
  605. v.EdbCode = strings.TrimSpace(v.EdbCode)
  606. if v.EdbCode == "" {
  607. br.Msg = "指标ID不可为空"
  608. return
  609. }
  610. v.EdbName = strings.TrimSpace(v.EdbName)
  611. if v.EdbName == "" {
  612. br.Msg = "请输入指标名称"
  613. return
  614. }
  615. indexNames = append(indexNames, v.EdbName)
  616. resp = append(resp, &NameCheckResult{
  617. EdbCode: v.EdbCode,
  618. EdbName: v.EdbName,
  619. })
  620. dataItems, err := data_manage.GetEdbDataAllByEdbCode(v.EdbCode, utils.DATA_SOURCE_YONYI, 0, utils.EDB_DATA_LIMIT)
  621. if err != nil && err.Error() != utils.ErrNoRow() {
  622. br.Msg = "获取失败"
  623. br.ErrMsg = "获取钢联已存在信息失败,Err:" + err.Error()
  624. return
  625. }
  626. if len(dataItems) <= 0 {
  627. respItem, err := data.AddEdbData(utils.DATA_SOURCE_YONYI, v.EdbCode, v.Frequency)
  628. if err != nil {
  629. br.Msg = "获取失败"
  630. br.ErrMsg = "获取失败,Err:" + err.Error()
  631. return
  632. }
  633. if respItem.Ret != 200 {
  634. br.Msg = "未搜索到该指标"
  635. br.ErrMsg = respItem.ErrMsg + ";EdbCode:" + v.EdbCode
  636. return
  637. }
  638. }
  639. }
  640. // 重名校验
  641. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  642. if e != nil {
  643. br.Msg = "操作失败"
  644. br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
  645. return
  646. }
  647. nameExists := make(map[string]bool)
  648. for _, v := range edbList {
  649. nameExists[v.EdbName] = true
  650. }
  651. if len(nameExists) > 0 {
  652. for _, v := range resp {
  653. v.Exist = nameExists[v.EdbName]
  654. }
  655. }
  656. br.Data = resp
  657. br.Msg = "校验成功"
  658. br.Ret = 200
  659. br.Success = true
  660. }
  661. // YongyiAddCheck
  662. // @Title 加入指标库指标Id检测
  663. // @Description 加入指标库指标Id检测
  664. // @Param request body request.BatchAddCheckReq true "type json string"
  665. // @Success 200 string "操作成功"
  666. // @router /yongyi/edb_info/add_check [post]
  667. func (c *EdbInfoController) YongyiAddCheck() {
  668. br := new(models.BaseResponse).Init()
  669. defer func() {
  670. if br.ErrMsg == "" {
  671. br.IsSendEmail = false
  672. }
  673. c.Data["json"] = br
  674. c.ServeJSON()
  675. }()
  676. sysUser := c.SysUser
  677. if sysUser == nil {
  678. br.Msg = "请登录"
  679. br.ErrMsg = "请登录,SysUser Is Empty"
  680. br.Ret = 408
  681. return
  682. }
  683. var req data_manage.BatchAddCheckReq
  684. if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
  685. br.Msg = "参数解析异常!"
  686. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  687. return
  688. }
  689. codeMax := 30
  690. codeLen := len(req.IndexCodes)
  691. // 获取指标库已有指标
  692. existsEdb, e := data_manage.GetEdbCodesBySource(utils.DATA_SOURCE_YONYI)
  693. if e != nil {
  694. br.Msg = "获取失败"
  695. br.ErrMsg = "获取自有数据已添加的指标失败, Err: " + e.Error()
  696. return
  697. }
  698. existMap := make(map[string]*data_manage.EdbInfo)
  699. for _, v := range existsEdb {
  700. existMap[v.EdbCode] = v
  701. }
  702. if codeLen == 0 {
  703. br.Msg = "请选择指标"
  704. return
  705. }
  706. if codeLen > codeMax {
  707. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
  708. return
  709. }
  710. // 查询选中的指标
  711. cond := fmt.Sprintf(` AND index_code IN (%s)`, utils.GetOrmInReplace(codeLen))
  712. pars := make([]interface{}, 0)
  713. pars = append(pars, req.IndexCodes)
  714. list, err := data_manage.GetYongyiIndex(cond, pars)
  715. if err != nil {
  716. br.Msg = "获取失败"
  717. br.ErrMsg = "获取钢联已存在信息失败,Err:" + err.Error()
  718. return
  719. }
  720. if len(list) > codeMax {
  721. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
  722. return
  723. }
  724. resp := make([]*data_manage.BaseFromYongyiIndexList, 0)
  725. for _, v := range list {
  726. if edb, ok := existMap[v.IndexCode]; ok {
  727. v.EdbInfoId = edb.EdbInfoId
  728. v.EdbClassifyId = edb.ClassifyId
  729. v.EdbUniqueCode = edb.UniqueCode
  730. v.EdbExist = 1
  731. }
  732. resp = append(resp, v)
  733. }
  734. br.Data = resp
  735. br.Msg = "校验成功"
  736. br.Ret = 200
  737. br.Success = true
  738. }
  739. // YongyiEdbInfoAdd
  740. // @Title 新增指标接口
  741. // @Description 新增指标接口
  742. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  743. // @Success Ret=200 保存成功
  744. // @router /yongyi/edb_info/add [post]
  745. func (this *EdbInfoController) YongyiEdbInfoAdd() {
  746. br := new(models.BaseResponse).Init()
  747. defer func() {
  748. this.Data["json"] = br
  749. this.ServeJSON()
  750. }()
  751. sysUser := this.SysUser
  752. if sysUser == nil {
  753. br.Msg = "请登录"
  754. br.ErrMsg = "请登录,SysUser Is Empty"
  755. br.Ret = 408
  756. return
  757. }
  758. deleteCache := true
  759. cacheKey := "CACHE_EDB_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  760. defer func() {
  761. if deleteCache {
  762. utils.Rc.Delete(cacheKey)
  763. }
  764. }()
  765. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  766. deleteCache = false
  767. br.Msg = "系统处理中,请稍后重试!"
  768. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  769. return
  770. }
  771. var req data_manage.AddEdbInfoReq
  772. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  773. if err != nil {
  774. br.Msg = "参数解析异常!"
  775. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  776. return
  777. }
  778. req.EdbName = strings.Trim(req.EdbName, " ")
  779. req.EdbCode = strings.Trim(req.EdbCode, " ")
  780. if req.EdbCode == "" {
  781. br.Msg = "指标ID不能为空"
  782. return
  783. }
  784. if req.EdbName == "" {
  785. br.Msg = "指标名称不能为空"
  786. return
  787. }
  788. if req.Frequency == "" {
  789. br.Msg = "频率不能为空"
  790. return
  791. }
  792. if req.Unit == "" {
  793. br.Msg = "单位不能为空"
  794. return
  795. }
  796. if req.ClassifyId <= 0 {
  797. br.Msg = "请选择分类"
  798. return
  799. }
  800. count, err := data_manage.GetYongyiIndexDataCount(req.EdbCode)
  801. if err != nil {
  802. br.Msg = "获取失败"
  803. br.ErrMsg = "获取失败,Err:" + err.Error()
  804. return
  805. }
  806. if count == 0 {
  807. br.Msg = "指标不存在"
  808. }
  809. // 指标入库
  810. edbInfo, err, errMsg, isSendEmail := data.EdbInfoAdd(utils.DATA_SOURCE_YONYI, utils.DATA_SUB_SOURCE_EDB, req.ClassifyId, req.EdbCode, req.EdbName, req.Frequency, req.Unit, req.StartDate, req.EndDate, sysUser.AdminId, sysUser.RealName, this.Lang)
  811. if err != nil {
  812. br.Msg = "保存失败"
  813. if errMsg != `` {
  814. br.Msg = errMsg
  815. }
  816. br.ErrMsg = err.Error()
  817. br.IsSendEmail = isSendEmail
  818. return
  819. }
  820. // 试用平台更新用户累计新增指标数
  821. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  822. if e != nil {
  823. br.Msg = "操作失败"
  824. br.ErrMsg = "获取系统用户数据失败,Err:" + e.Error()
  825. return
  826. }
  827. if utils.BusinessCode == utils.BusinessCodeSandbox && adminItem.DepartmentName == "ETA试用客户" {
  828. go func() {
  829. var r etaTrialService.EtaTrialUserReq
  830. r.Mobile = adminItem.Mobile
  831. _, _ = etaTrialService.UpdateUserIndexNum(r)
  832. }()
  833. }
  834. //新增操作日志
  835. {
  836. edbLog := new(data_manage.EdbInfoLog)
  837. edbLog.EdbInfoId = edbInfo.EdbInfoId
  838. edbLog.SourceName = edbInfo.SourceName
  839. edbLog.Source = edbInfo.Source
  840. edbLog.EdbCode = edbInfo.EdbCode
  841. edbLog.EdbName = edbInfo.EdbName
  842. edbLog.ClassifyId = edbInfo.ClassifyId
  843. edbLog.SysUserId = sysUser.AdminId
  844. edbLog.SysUserRealName = sysUser.RealName
  845. edbLog.CreateTime = time.Now()
  846. edbLog.Content = string(this.Ctx.Input.RequestBody)
  847. edbLog.Status = "新增指标"
  848. edbLog.Method = this.Ctx.Input.URI()
  849. go data_manage.AddEdbInfoLog(edbLog)
  850. }
  851. // 更新es
  852. go data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  853. resp := new(data_manage.AddEdbInfoResp)
  854. resp.EdbInfoId = edbInfo.EdbInfoId
  855. resp.UniqueCode = edbInfo.UniqueCode
  856. br.Ret = 200
  857. br.Success = true
  858. br.Msg = "保存成功"
  859. br.Data = resp
  860. br.IsAddLog = true
  861. }
  862. // ExportYongyiList
  863. // @Title 导出涌益数据
  864. // @Description 导出涌益数据
  865. // @Param ClassifyId query int true "关键字搜索"
  866. // @Param IndexCode query string true "指标编码"
  867. // @Success 200 导出成功
  868. // @router /yongyi/export [get]
  869. func (this *EdbInfoController) ExportYongyiList() {
  870. br := new(models.BaseResponse).Init()
  871. defer func() {
  872. this.Data["json"] = br
  873. this.ServeJSON()
  874. }()
  875. sysUser := this.SysUser
  876. if sysUser == nil {
  877. br.Msg = "请重新登录"
  878. return
  879. }
  880. classifyId, _ := this.GetInt("ClassifyId")
  881. indexCode := this.GetString("IndexCode")
  882. if classifyId < 0 {
  883. br.Msg = "请选择分类"
  884. return
  885. }
  886. dir, _ := os.Executable()
  887. exPath := filepath.Dir(dir)
  888. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  889. xlsxFile := xlsx.NewFile()
  890. var condition string
  891. var pars []interface{}
  892. var classifyName string
  893. if classifyId > 0 {
  894. classifyInfo, err := data_manage.GetBaseFromYongyiClassifyById(classifyId)
  895. if err != nil {
  896. if err.Error() == utils.ErrNoRow() {
  897. br.Msg = "分类不存在"
  898. return
  899. }
  900. br.Msg = "下载失败"
  901. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  902. return
  903. }
  904. classifyName = classifyInfo.ClassifyName
  905. childClassify, err := data_manage.GetBaseFromYongyiClassifyByParentId(classifyId)
  906. if err != nil {
  907. br.Msg = "下载失败"
  908. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  909. return
  910. }
  911. if len(childClassify) > 0 {
  912. condition += `AND classify_id IN (` + utils.GetOrmInReplace(len(childClassify)) + `)`
  913. for _, child := range childClassify {
  914. pars = append(pars, child.ClassifyId)
  915. }
  916. } else {
  917. condition += ` AND classify_id=?`
  918. pars = append(pars, classifyId)
  919. }
  920. } else {
  921. condition += ` AND classify_id=?`
  922. pars = append(pars, classifyId)
  923. }
  924. if indexCode != "" {
  925. condition += ` AND index_code=? `
  926. pars = append(pars, indexCode)
  927. }
  928. indexList, err := data_manage.GetYongyiIndex(condition, pars)
  929. if err != nil {
  930. fmt.Println("获取数据失败,Err:" + err.Error())
  931. return
  932. }
  933. if len(indexList) <= 0 {
  934. fmt.Println("indexList 为空")
  935. br.Ret = 200
  936. br.Success = true
  937. br.Msg = "success"
  938. return
  939. }
  940. indexCodeList := make([]string, 0)
  941. frequenciesMap := make(map[string][]*data_manage.BaseFromYongyiIndexList)
  942. for _, v := range indexList {
  943. indexCodeList = append(indexCodeList, v.IndexCode)
  944. frequenciesMap[v.Frequency] = append(frequenciesMap[v.Frequency], v)
  945. }
  946. dataListMap := make(map[string][]*data_manage.BaseFromYongyiData)
  947. if len(indexList) > 0 {
  948. allDataList, e := data_manage.GetYongyiIndexDataByCodes(indexCodeList)
  949. if e != nil {
  950. br.Msg = "获取数据失败"
  951. br.ErrMsg = "获取数据失败,Err:" + e.Error()
  952. return
  953. }
  954. for _, v := range allDataList {
  955. dataListMap[v.IndexCode] = append(dataListMap[v.IndexCode], v)
  956. }
  957. }
  958. // 按照频率分组排序
  959. frequencies := []string{
  960. "日度", "周度", "旬度", "月度", "季度", "半年度", "年度",
  961. }
  962. for _, frequency := range frequencies {
  963. //获取指标
  964. indexCodeList, ok := frequenciesMap[frequency]
  965. if !ok {
  966. continue
  967. }
  968. if len(indexCodeList) <= 0 {
  969. fmt.Printf("sheet:%s, 不存在指标", frequency)
  970. return
  971. }
  972. var sheetName string
  973. switch frequency {
  974. case "日度":
  975. sheetName = "日度(Daily)"
  976. case "周度":
  977. sheetName = "周度(Weekly)"
  978. case "旬度":
  979. sheetName = "旬度(ten-day)"
  980. case "月度":
  981. sheetName = "月度(Monthly)"
  982. case "季度":
  983. sheetName = "季度(Quarterly)"
  984. case "半年度":
  985. sheetName = "半年度(Semi-annual)"
  986. case "年度":
  987. sheetName = "年度(Annual)"
  988. default:
  989. sheetName = "其他数据"
  990. }
  991. sheetNew, err := xlsxFile.AddSheet(sheetName)
  992. if err != nil {
  993. fmt.Println("新增Sheet失败", err.Error())
  994. return
  995. }
  996. secNameRow := sheetNew.AddRow()
  997. frequencyRow := sheetNew.AddRow()
  998. unitRow := sheetNew.AddRow()
  999. lastModifyDateRow := sheetNew.AddRow()
  1000. var indexIdList []int
  1001. for _, idx := range frequenciesMap[frequency] {
  1002. indexIdList = append(indexIdList, idx.YongyiIndexId)
  1003. }
  1004. dataTimeList, err := data_manage.GetYongyiDataDataTimeByIndexId(indexIdList)
  1005. if err != nil {
  1006. fmt.Println("获取数据时间失败", err.Error())
  1007. return
  1008. }
  1009. // 添加excel左侧指标日期
  1010. setRowIndex := 4
  1011. for rk, dv := range dataTimeList {
  1012. rowIndex := setRowIndex + rk
  1013. row := sheetNew.Row(rowIndex)
  1014. displayDate, _ := time.Parse(utils.FormatDate, dv)
  1015. displayDateCell := row.AddCell()
  1016. style := new(xlsx.Style)
  1017. style.ApplyAlignment = true
  1018. style.Alignment.WrapText = true
  1019. displayDateCell.SetStyle(style)
  1020. displayDateCell.SetDate(displayDate)
  1021. }
  1022. for k, icl := range indexCodeList {
  1023. // 获取数据
  1024. dataList, ok := dataListMap[icl.IndexCode]
  1025. if !ok {
  1026. continue
  1027. }
  1028. if k == 0 {
  1029. secNameRow.AddCell().SetValue("指标名称/Metric Name")
  1030. frequencyRow.AddCell().SetValue("频度/Frequency")
  1031. unitRow.AddCell().SetValue("单位/Unit")
  1032. lastModifyDateRow.AddCell().SetValue("更新时间/Update Time")
  1033. min := k * 3
  1034. sheetNew.SetColWidth(min, min, 15)
  1035. }
  1036. if len(dataList) == 0 {
  1037. continue
  1038. }
  1039. secNameRow.AddCell().SetValue(icl.IndexName)
  1040. frequencyRow.AddCell().SetValue(icl.Frequency)
  1041. unitRow.AddCell().SetValue(icl.Unit)
  1042. timeDate, err := time.Parse(utils.FormatDateTime, dataList[0].ModifyTime)
  1043. if err != nil {
  1044. continue
  1045. }
  1046. lastModifyDateRow.AddCell().SetValue(timeDate.Format(utils.FormatDate))
  1047. dataInfoMap := make(map[string]*data_manage.BaseFromYongyiData)
  1048. for _, v := range dataList {
  1049. dataInfoMap[v.DataTime] = v
  1050. }
  1051. for rk, dtv := range dataTimeList {
  1052. rowIndex := setRowIndex + rk
  1053. row := sheetNew.Row(rowIndex)
  1054. displayDateCell := row.AddCell()
  1055. tmpData, ok := dataInfoMap[dtv]
  1056. if ok {
  1057. displayDateCell.SetValue(tmpData.Value)
  1058. }
  1059. }
  1060. }
  1061. }
  1062. err = xlsxFile.Save(downLoadnFilePath)
  1063. if err != nil {
  1064. //有指标无数据时先导出一遍空表
  1065. sheet, err := xlsxFile.AddSheet("无数据")
  1066. if err != nil {
  1067. br.Msg = "新增Sheet失败"
  1068. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1069. return
  1070. }
  1071. rowSecName := sheet.AddRow()
  1072. celSecName := rowSecName.AddCell()
  1073. celSecName.SetValue("")
  1074. err = xlsxFile.Save(downLoadnFilePath)
  1075. if err != nil {
  1076. br.Msg = "保存文件失败"
  1077. br.ErrMsg = "保存文件失败"
  1078. return
  1079. }
  1080. }
  1081. fileName := classifyName
  1082. if indexCode != "" && len(indexList) == 1 {
  1083. fileName = indexList[0].IndexName
  1084. }
  1085. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  1086. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  1087. defer func() {
  1088. os.Remove(downLoadnFilePath)
  1089. }()
  1090. br.Ret = 200
  1091. br.Success = true
  1092. br.Msg = "success"
  1093. }