hisugar_data.go 29 KB

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