hisugar_data.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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. dataTimeStrList := make([]string, 0)
  685. for _, v := range dataTimeList {
  686. dataTimeStrList = append(dataTimeStrList, v.Format(utils.FormatDate))
  687. }
  688. // 添加excel左侧指标日期
  689. setRowIndex := 4
  690. for rk, dv := range dataTimeStrList {
  691. rowIndex := setRowIndex + rk
  692. row := sheetNew.Row(rowIndex)
  693. displayDate, _ := time.Parse(utils.FormatDate, dv)
  694. displayDateCell := row.AddCell()
  695. style := new(xlsx.Style)
  696. style.ApplyAlignment = true
  697. style.Alignment.WrapText = true
  698. displayDateCell.SetStyle(style)
  699. displayDateCell.SetDate(displayDate)
  700. }
  701. for k, sv := range secNameList {
  702. //获取数据
  703. dataList, err := data_manage.GetHisugarIndexDataByCode(sv.IndexCode)
  704. if err != nil {
  705. br.Msg = "获取数据失败"
  706. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  707. return
  708. }
  709. if k == 0 {
  710. secNameRow.AddCell().SetValue("指标名称/Metric Name")
  711. frequencyRow.AddCell().SetValue("频度/Frequency")
  712. unitRow.AddCell().SetValue("单位/Unit")
  713. lastModifyDateRow.AddCell().SetValue("更新时间/Update Time")
  714. min := k * 3
  715. sheetNew.SetColWidth(min, min, 15)
  716. }
  717. if len(dataList) == 0 {
  718. continue
  719. }
  720. secNameRow.AddCell().SetValue(sv.IndexName)
  721. frequencyRow.AddCell().SetValue(sv.Frequency)
  722. unitRow.AddCell().SetValue(sv.Unit)
  723. lastModifyDateRow.AddCell().SetValue(sv.ModifyTime)
  724. dataInfoMap := make(map[string]*data_manage.BaseFromHisugarDataItem)
  725. for _, v := range dataList {
  726. dataInfoMap[v.DataTime] = v
  727. }
  728. for rk, dtv := range dataTimeStrList {
  729. rowIndex := setRowIndex + rk
  730. row := sheetNew.Row(rowIndex)
  731. displayDateCell := row.AddCell()
  732. tmpData, ok := dataInfoMap[dtv]
  733. if ok {
  734. displayDateCell.SetValue(tmpData.Value)
  735. }
  736. }
  737. }
  738. }
  739. err = xlsxFile.Save(downLoadnFilePath)
  740. if err != nil {
  741. //有指标无数据时先导出一遍空表
  742. sheet, err := xlsxFile.AddSheet("无数据")
  743. if err != nil {
  744. br.Msg = "新增Sheet失败"
  745. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  746. return
  747. }
  748. rowSecName := sheet.AddRow()
  749. celSecName := rowSecName.AddCell()
  750. celSecName.SetValue("")
  751. err = xlsxFile.Save(downLoadnFilePath)
  752. if err != nil {
  753. br.Msg = "保存文件失败"
  754. br.ErrMsg = "保存文件失败"
  755. return
  756. }
  757. }
  758. fileName := `泛糖科技`
  759. if len(secNameList) > 0 {
  760. fileName = secNameList[0].IndexName
  761. }
  762. fileName += time.Now().Format("2006.01.02") + `.xlsx` //文件名称
  763. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  764. defer func() {
  765. os.Remove(downLoadnFilePath)
  766. }()
  767. br.Ret = 200
  768. br.Success = true
  769. br.Msg = "success"
  770. }
  771. // getHisugarClassifyTree 返回泛糖科技的树形结构
  772. func getHisugarClassifyTree(items []*data_manage.BaseFromHisugarClassifyItem, parentId int) []*data_manage.BaseFromHisugarClassifyItem {
  773. res := make([]*data_manage.BaseFromHisugarClassifyItem, 0)
  774. for _, item := range items {
  775. if item.ParentId == parentId {
  776. t := new(data_manage.BaseFromHisugarClassifyItem)
  777. t.BaseFromHisugarClassifyId = item.BaseFromHisugarClassifyId
  778. t.ClassifyName = item.ClassifyName
  779. t.ParentId = item.ParentId
  780. t.Level = item.Level
  781. t.Sort = item.Sort
  782. t.ModifyTime = item.ModifyTime
  783. t.CreateTime = item.CreateTime
  784. t.ClassifyNameEn = item.ClassifyNameEn
  785. if item.UniqueCode == "" {
  786. t.UniqueCode = strconv.Itoa(item.BaseFromHisugarClassifyId)
  787. }
  788. t.Children = getHisugarClassifyTree(items, item.BaseFromHisugarClassifyId)
  789. res = append(res, t)
  790. }
  791. }
  792. return res
  793. }
  794. // FenweiIndexData
  795. // @Title 获取数据
  796. // @Description 获取泛糖科技数据接口
  797. // @Param PageSize query int true "每页数据条数"
  798. // @Param CurrentIndex query int true "当前页页码,从1开始"
  799. // @Param ClassifyId query string true "分类id"
  800. // @Param Frequency query string true "频率"
  801. // @Success 200 {object} data_manage.LzFrequency
  802. // @router /data/hisugar/index/data [get]
  803. func (this *TradeCommonController) HisugarIndexData() {
  804. br := new(models.BaseResponse).Init()
  805. defer func() {
  806. if br.ErrMsg == "" {
  807. br.IsSendEmail = false
  808. }
  809. this.Data["json"] = br
  810. this.ServeJSON()
  811. }()
  812. sysUser := this.SysUser
  813. if sysUser == nil {
  814. br.Msg = "请登录"
  815. br.ErrMsg = "请登录,SysUser Is Empty"
  816. br.Ret = 408
  817. return
  818. }
  819. pageSize, _ := this.GetInt("PageSize")
  820. currentIndex, _ := this.GetInt("CurrentIndex")
  821. var startSize int
  822. if pageSize <= 0 {
  823. pageSize = utils.PageSize20
  824. }
  825. if currentIndex <= 0 {
  826. currentIndex = 1
  827. }
  828. startSize = utils.StartIndex(currentIndex, pageSize)
  829. classifyId, _ := this.GetInt("ClassifyId")
  830. if classifyId < 0 {
  831. br.Msg = "请选择分类"
  832. br.ErrMsg = "请选择分类"
  833. return
  834. }
  835. frequency := this.GetString("Frequency")
  836. indexCode := this.GetString("IndexCode")
  837. // 获取指标
  838. var condition string
  839. var pars []interface{}
  840. if classifyId >= 0 {
  841. classifyIds, err := data_manage.GetHisugarClassifyById(classifyId)
  842. if err != nil {
  843. br.Msg = "获取数据失败"
  844. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  845. return
  846. }
  847. if classifyIds != "" {
  848. condition += ` AND classify_id IN (` + classifyIds + `)`
  849. }
  850. }
  851. if frequency != "" {
  852. condition += ` AND frequency=? `
  853. pars = append(pars, frequency)
  854. }
  855. if indexCode != "" {
  856. condition += ` AND index_code=? `
  857. pars = append(pars, indexCode)
  858. }
  859. indexes, err := data_manage.GetHisugarIndex(condition, pars)
  860. if err != nil {
  861. br.Msg = "获取数据失败"
  862. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  863. return
  864. }
  865. indexCodes := make([]string, 0)
  866. for _, v := range indexes {
  867. indexCodes = append(indexCodes, v.IndexCode)
  868. }
  869. indexCounts, e := data_manage.GetHisugarIndexDataCountGroup(indexCodes)
  870. if e != nil {
  871. br.Msg = "获取失败"
  872. br.ErrMsg = "获取指标数据总量失败, Err:" + err.Error()
  873. return
  874. }
  875. countMap := make(map[string]int)
  876. for _, v := range indexCounts {
  877. countMap[v.IndexCode] = v.Count
  878. }
  879. // 判断是否存在于指标库
  880. edbCodeList, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_HISUGAR, indexCodes)
  881. if err != nil {
  882. return
  883. }
  884. edbCodeMap := make(map[string]*data_manage.EdbInfo)
  885. for _, v := range edbCodeList {
  886. edbCodeMap[v.EdbCode] = v
  887. }
  888. resultList := make([]*data_manage.BaseFromHisugarIndexList, 0)
  889. for _, v := range indexes {
  890. product := new(data_manage.BaseFromHisugarIndexList)
  891. product.BaseFromHisugarIndexId = v.BaseFromHisugarIndexId
  892. product.ClassifyId = v.ClassifyId
  893. product.Unit = v.Unit
  894. product.IndexCode = v.IndexCode
  895. product.IndexName = v.IndexName
  896. product.Frequency = v.Frequency
  897. product.CreateTime = v.CreateTime
  898. product.ModifyTime = v.ModifyTime
  899. edbInfo := edbCodeMap[v.IndexCode]
  900. if edbInfo != nil {
  901. product.EdbInfoId = edbInfo.EdbInfoId
  902. product.EdbExist = 1
  903. }
  904. total := countMap[v.IndexCode]
  905. page := paging.GetPaging(currentIndex, pageSize, total)
  906. dataList, e := data_manage.GetHisugarIndexDataV2(v.IndexCode, startSize, pageSize)
  907. if e != nil {
  908. br.Msg = "获取数据失败"
  909. br.ErrMsg = "获取指标数据失败,Err:" + e.Error()
  910. return
  911. }
  912. if dataList == nil {
  913. dataList = make([]*data_manage.BaseFromHisugarDataItem, 0)
  914. }
  915. product.DataList = dataList
  916. product.Paging = page
  917. resultList = append(resultList, product)
  918. }
  919. br.Ret = 200
  920. br.Success = true
  921. br.Msg = "获取成功"
  922. br.Data = resultList
  923. }
  924. // GetFenWeiIndexInfo
  925. // @Title 添加指标-根据条件获取指标信息
  926. // @Description 添加指标-根据条件获取指标信息
  927. // @Param KeyWord query string false "关键字"
  928. // @Param ClassifyIds query string false "分类id"
  929. // @Param Frequencies query string false "频率"
  930. // @Param PageSize query int false "每页数据条数"
  931. // @Param CurrentIndex query int false "当前页页码,从1开始"
  932. // @Success 200 {object} data_manage.BaseFromFenWeiIndexPage
  933. // @router /data/hisugar/index/info [get]
  934. func (this *TradeCommonController) GetHisugarIndexInfo() {
  935. br := new(models.BaseResponse).Init()
  936. defer func() {
  937. if br.ErrMsg == "" {
  938. br.IsSendEmail = false
  939. }
  940. this.Data["json"] = br
  941. this.ServeJSON()
  942. }()
  943. sysUser := this.SysUser
  944. if sysUser == nil {
  945. br.Msg = "请登录"
  946. br.ErrMsg = "请登录,SysUser Is Empty"
  947. br.Ret = 408
  948. return
  949. }
  950. pageSize, _ := this.GetInt("PageSize")
  951. currentIndex, _ := this.GetInt("CurrentIndex")
  952. if pageSize <= 0 {
  953. pageSize = utils.PageSize20
  954. }
  955. if currentIndex <= 0 {
  956. currentIndex = 1
  957. }
  958. keyWord := this.GetString("KeyWord")
  959. classifyIds := this.GetString("ClassifyIds")
  960. frequencies := this.GetString("Frequencies")
  961. var classifyIdList []string
  962. var frequencyList []string
  963. if classifyIds != "" {
  964. classifyIdList = strings.Split(classifyIds, ",")
  965. }
  966. if frequencies != "" {
  967. frequencyList = strings.Split(frequencies, ",")
  968. }
  969. indexInfoPage, err := data.GethisugarIndexInfo(keyWord, classifyIdList, frequencyList, currentIndex, pageSize)
  970. if err != nil {
  971. return
  972. }
  973. br.Ret = 200
  974. br.Success = true
  975. br.Msg = "获取成功"
  976. br.Data = indexInfoPage
  977. }
  978. // FrequencyList
  979. // @Title 指标频度列表
  980. // @Description 指标频度列表
  981. // @Success 200 string "获取成功"
  982. // @router /data/hisugar/frequency_list [get]
  983. func (this *TradeCommonController) FrequencyList() {
  984. br := new(models.BaseResponse).Init()
  985. defer func() {
  986. if br.ErrMsg == "" {
  987. br.IsSendEmail = false
  988. }
  989. this.Data["json"] = br
  990. this.ServeJSON()
  991. }()
  992. sysUser := this.SysUser
  993. if sysUser == nil {
  994. br.Msg = "请登录"
  995. br.ErrMsg = "请登录,SysUser Is Empty"
  996. return
  997. }
  998. classifyId, _ := this.GetInt("ClassifyId")
  999. var condition string
  1000. var pars []interface{}
  1001. if classifyId > 0 {
  1002. classifyIds, err := data_manage.GetHisugarClassifyById(classifyId)
  1003. if err != nil {
  1004. br.Msg = "获取数据失败"
  1005. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1006. return
  1007. }
  1008. if classifyIds != "" {
  1009. condition += ` AND classify_id IN (` + classifyIds + `)`
  1010. }
  1011. }
  1012. frequencies, err := data_manage.GetHisugarFrequencyByCondition(condition, pars)
  1013. if err != nil {
  1014. fmt.Println("GetSciFrequency err:", err.Error())
  1015. utils.FileLog.Info("GetSciFrequency err:" + err.Error())
  1016. return
  1017. }
  1018. br.Data = frequencies
  1019. br.Ret = 200
  1020. br.Success = true
  1021. br.Msg = "获取成功"
  1022. }