hisugar_data.go 29 KB

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