oilchem_data.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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. // OilchemClassify
  20. // @title 获取隆众资讯分类列表
  21. // @Description 获取隆众资讯分类列表
  22. // @Success 200 {object} models.
  23. // @router /data/oilchem/classify [get]
  24. func (this *TradeCommonController) OilchemClassify() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. classifies, e := data_manage.GetOilchemClassifyList()
  31. if e != nil {
  32. br.Msg = "获取失败"
  33. br.ErrMsg = "获取隆众资讯分类数据分类失败, Err: " + e.Error()
  34. return
  35. }
  36. br.Data = classifies
  37. br.Ret = 200
  38. br.Success = true
  39. br.Msg = "获取成功"
  40. }
  41. // OilchemIndexList
  42. // @title 获取隆众资讯指标列表
  43. // @Description 获取隆众资讯指标列表
  44. // @Success 200 {object} models.
  45. // @router /data/oilchem/indexList [get]
  46. func (this *TradeCommonController) OilchemIndexList() {
  47. br := new(models.BaseResponse).Init()
  48. defer func() {
  49. this.Data["json"] = br
  50. this.ServeJSON()
  51. }()
  52. pageSize, _ := this.GetInt("PageSize")
  53. currentIndex, _ := this.GetInt("CurrentIndex")
  54. var startSize int
  55. if pageSize <= 0 {
  56. pageSize = utils.PageSize20
  57. }
  58. if currentIndex <= 0 {
  59. currentIndex = 1
  60. }
  61. startSize = utils.StartIndex(currentIndex, pageSize)
  62. classifyId, _ := this.GetInt("ClassifyId")
  63. var condition string
  64. var pars []interface{}
  65. if classifyId > 0 {
  66. condition += ` AND b.classify_id=? `
  67. pars = append(pars, classifyId)
  68. }
  69. //keyword := this.GetString("KeyWord")
  70. //if keyword != "" {
  71. // condition += ` AND (index_code =? OR index_name LIKE ?) `
  72. // pars = append(pars, keyword)
  73. // pars = append(pars, "%"+keyword+"%")
  74. //}
  75. indexList, e := data_manage.GetOilchemIndexViewList(condition, pars, startSize, pageSize)
  76. if e != nil {
  77. br.Msg = "获取失败"
  78. br.ErrMsg = "获取隆众资讯分类数据分类失败, Err: " + e.Error()
  79. return
  80. }
  81. indexIdList := make([]int, 0)
  82. for _, v := range indexList {
  83. indexIdList = append(indexIdList, v.BaseFromOilchemIndexId)
  84. }
  85. dataList, err := data_manage.GetOilchemDataViewList(indexIdList)
  86. if err != nil {
  87. br.Msg = "获取失败"
  88. br.ErrMsg = "获取隆众资讯分类数据分类失败, Err: " + err.Error()
  89. return
  90. }
  91. for i, v := range indexList {
  92. for _, v1 := range dataList {
  93. if v.BaseFromOilchemIndexId == v1.BaseFromOilchemIndexId {
  94. indexList[i].DataTime = v1.DataTime
  95. indexList[i].Value = v1.Value
  96. }
  97. }
  98. }
  99. total, err := data_manage.GetOilchemIndexViewListCount(condition, pars)
  100. if err != nil {
  101. br.Msg = "获取失败"
  102. br.ErrMsg = "获取隆众资讯分类数据分类失败, Err: " + err.Error()
  103. return
  104. }
  105. page := paging.GetPaging(currentIndex, pageSize, total)
  106. resp := data_manage.BaseFromOilchemIndexListResp{}
  107. resp.List = indexList
  108. resp.Paging = page
  109. br.Data = resp
  110. br.Ret = 200
  111. br.Success = true
  112. br.Msg = "获取成功"
  113. }
  114. // OilchemMineData
  115. // @title 获取隆众资讯详细数据列表
  116. // @Description 获取隆众资讯详细数据接口
  117. // @Param ClassifyId query int true "数据id"
  118. // @Param GroupName query string true "分组名"
  119. // @Param Frequency query string true "频度"
  120. // @Param PageSize query int true "每页数据条数"
  121. // @Param CurrentIndex query int true "当前页页码,从1开始"
  122. // @Success 200 {object} []data_manage.CoalmineDataResp
  123. // @router /data/oilchem/data [get]
  124. func (this *TradeCommonController) OilchemData() {
  125. br := new(models.BaseResponse).Init()
  126. defer func() {
  127. this.Data["json"] = br
  128. this.ServeJSON()
  129. }()
  130. pageSize, _ := this.GetInt("PageSize")
  131. currentIndex, _ := this.GetInt("CurrentIndex")
  132. var startSize int
  133. if pageSize <= 0 {
  134. pageSize = utils.PageSize20
  135. }
  136. if currentIndex <= 0 {
  137. currentIndex = 1
  138. }
  139. startSize = utils.StartIndex(currentIndex, pageSize)
  140. indexCode := this.GetString("IndexCode")
  141. if indexCode == "" {
  142. br.Msg = "请选择指标"
  143. br.ErrMsg = "请选择指标"
  144. return
  145. }
  146. //获取指标
  147. var condition string
  148. var pars []interface{}
  149. condition += ` AND index_code =? `
  150. pars = append(pars, indexCode)
  151. index, err := data_manage.GetOilchemIndexByCode(indexCode)
  152. if err != nil {
  153. br.Msg = "获取数据失败"
  154. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  155. return
  156. }
  157. dataList, err := data_manage.GetOilchemIndexData(condition, pars, startSize, pageSize)
  158. if err != nil {
  159. br.Msg = "获取数据失败"
  160. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  161. return
  162. }
  163. resp := data_manage.BaseFromOilchemIndexList{}
  164. resp.BaseFromOilchemIndexId = index.BaseFromOilchemIndexId
  165. resp.ClassifyId = index.ClassifyId
  166. resp.IndexCode = index.IndexCode
  167. resp.IndexName = index.IndexName
  168. resp.Frequency = index.Frequency
  169. resp.Unit = index.Unit
  170. resp.EdbExist = index.EdbExist
  171. total, err := data_manage.GetOilchemDataListCount(condition, pars)
  172. page := paging.GetPaging(currentIndex, pageSize, total)
  173. if len(dataList) > 0 {
  174. resp.ModifyTime = dataList[0].ModifyTime
  175. }
  176. resp.Paging = page
  177. resp.DataList = dataList
  178. br.Ret = 200
  179. br.Success = true
  180. br.Msg = "获取成功"
  181. br.Data = resp
  182. }
  183. // CoalSearchList
  184. // @Title 隆众资讯模糊搜索
  185. // @Description 隆众资讯模糊搜索
  186. // @Param Keyword query string ture "关键字搜索"
  187. // @Success 200 {object} models.BaseResponse
  188. // @router /data/oilchem/search [get]
  189. func (this *TradeCommonController) OilchemSearchList() {
  190. br := new(models.BaseResponse).Init()
  191. defer func() {
  192. this.Data["json"] = br
  193. this.ServeJSON()
  194. }()
  195. //关键字
  196. keyword := this.GetString("Keyword")
  197. list, err := data_manage.GetOilchemItemList(keyword)
  198. if err != nil {
  199. br.ErrMsg = "获取失败,Err:" + err.Error()
  200. br.Msg = "获取失败"
  201. return
  202. }
  203. br.Ret = 200
  204. br.Success = true
  205. br.Msg = "获取成功"
  206. br.Data = list
  207. }
  208. // BatchIndexList
  209. // @Title 隆众资讯批量操作指标列表
  210. // @Description 隆众资讯批量操作指标列表
  211. // @Param request body request.SciHqDataBatchListReq true "type json string"
  212. // @Success 200 {object} data_manage.BaseFromMysteelChemicalIndexResp
  213. // @router /data/oilchem/batch_list [post]
  214. func (this *TradeCommonController) BatchIndexList() {
  215. br := new(models.BaseResponse).Init()
  216. defer func() {
  217. this.Data["json"] = br
  218. this.ServeJSON()
  219. }()
  220. var req data_manage.OilchemDataBatchListReq
  221. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  222. br.Msg = "参数解析异常!"
  223. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  224. return
  225. }
  226. var condition string
  227. var pars []interface{}
  228. if req.KeyWord != "" {
  229. condition += `AND (index_code=? OR index_name=?)`
  230. pars = utils.GetLikeKeywordPars(pars, req.KeyWord, 2)
  231. }
  232. if req.IsSelectAll {
  233. if len(req.SelectedId) > 0 {
  234. condition += ` AND base_from_oilchem_index_id NOT IN (` + utils.GetOrmInReplace(len(req.SelectedId)) + `)`
  235. pars = append(pars, req.SelectedId)
  236. }
  237. } else {
  238. if len(req.SelectedId) > 0 {
  239. condition += ` AND base_from_oilchem_index_id IN (` + utils.GetOrmInReplace(len(req.SelectedId)) + `)`
  240. pars = append(pars, req.SelectedId)
  241. }
  242. }
  243. if req.ClassifyId >= 0 {
  244. condition += ` AND classify_id=?`
  245. pars = append(pars, req.ClassifyId)
  246. }
  247. count, err := data_manage.GetOilchemIndexListCount(condition, pars)
  248. if err != nil {
  249. br.Msg = "获取失败"
  250. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  251. return
  252. }
  253. MaxLen := 30
  254. if count > MaxLen {
  255. br.Msg = "批量操作数量不能超过" + strconv.Itoa(MaxLen)
  256. return
  257. }
  258. indexList, err := data_manage.GetOilchemIndexByCondition(condition, pars)
  259. if err != nil {
  260. br.Msg = "获取失败"
  261. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  262. return
  263. }
  264. br.Ret = 200
  265. br.Success = true
  266. br.Msg = "获取成功"
  267. br.Data = indexList
  268. }
  269. // AddCheck
  270. // @Title 新增校验
  271. // @Description 新增校验
  272. // @Param request body request.BusinessDataBatchAddCheckReq true "type json string"
  273. // @Success 200 string "操作成功"
  274. // @router /data/oilchem/add_check [post]
  275. func (this *TradeCommonController) AddCheck() {
  276. br := new(models.BaseResponse).Init()
  277. defer func() {
  278. if br.ErrMsg == "" {
  279. br.IsSendEmail = false
  280. }
  281. this.Data["json"] = br
  282. this.ServeJSON()
  283. }()
  284. sysUser := this.SysUser
  285. if sysUser == nil {
  286. br.Msg = "请登录"
  287. br.ErrMsg = "请登录,SysUser Is Empty"
  288. br.Ret = 408
  289. return
  290. }
  291. var req *data_manage.OilchemDataBatchAddCheckReq
  292. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  293. br.Msg = "参数解析异常!"
  294. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  295. return
  296. }
  297. codeMax := 30
  298. codeLen := len(req.IndexCodes)
  299. if len(req.IndexCodes) == 0 {
  300. br.Msg = "请选择指标"
  301. return
  302. }
  303. if codeLen > codeMax {
  304. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
  305. return
  306. }
  307. // 获取指标库已有指标
  308. existsEdb, e := data_manage.GetEdbCodesBySource(utils.DATA_SOURCE_OILCHEM)
  309. if e != nil {
  310. br.Msg = "获取失败"
  311. br.ErrMsg = "获取自有数据已添加的指标失败, Err: " + e.Error()
  312. return
  313. }
  314. existMap := make(map[string]*data_manage.EdbInfo)
  315. for _, v := range existsEdb {
  316. existMap[v.EdbCode] = v
  317. }
  318. // 查询选中的指标
  319. cond := fmt.Sprintf(` AND index_code IN (%s)`, utils.GetOrmInReplace(codeLen))
  320. pars := make([]interface{}, 0)
  321. pars = append(pars, req.IndexCodes)
  322. list, err := data_manage.GetOilchemIndexAndEdbInfoByCondition(cond, pars)
  323. if err != nil {
  324. br.Msg = "获取失败"
  325. br.ErrMsg = "获取卓创红期原始指标列表失败, Err: " + err.Error()
  326. return
  327. }
  328. resp := make([]*data_manage.BaseFromOilchemIndexView, 0)
  329. for _, v := range list {
  330. if v.EdbInfoId > 0 {
  331. v.EdbExist = 1
  332. }
  333. resp = append(resp, v)
  334. }
  335. br.Data = resp
  336. br.Msg = "校验成功"
  337. br.Ret = 200
  338. br.Success = true
  339. }
  340. // NameCheck
  341. // @Title 重名校验
  342. // @Description 批量新增
  343. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  344. // @Success 200 string "操作成功"
  345. // @router /data/oilchem/name_check [post]
  346. func (c *TradeCommonController) NameCheck() {
  347. br := new(models.BaseResponse).Init()
  348. defer func() {
  349. if br.ErrMsg == "" {
  350. br.IsSendEmail = false
  351. }
  352. c.Data["json"] = br
  353. c.ServeJSON()
  354. }()
  355. sysUser := c.SysUser
  356. if sysUser == nil {
  357. br.Msg = "请登录"
  358. br.ErrMsg = "请登录,SysUser Is Empty"
  359. br.Ret = 408
  360. return
  361. }
  362. var req []*data_manage.NameCheckEdbInfoReq
  363. if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
  364. br.Msg = "参数解析异常!"
  365. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  366. return
  367. }
  368. if len(req) == 0 {
  369. br.Msg = "请选择指标"
  370. return
  371. }
  372. codeMax := 30
  373. codeLen := len(req)
  374. if codeLen > codeMax {
  375. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
  376. return
  377. }
  378. type NameCheckResult struct {
  379. EdbCode string
  380. EdbName string
  381. Exist bool
  382. }
  383. indexNames := make([]string, 0)
  384. resp := make([]*NameCheckResult, 0)
  385. for _, v := range req {
  386. v.EdbCode = strings.TrimSpace(v.EdbCode)
  387. if v.EdbCode == "" {
  388. br.Msg = "指标ID不可为空"
  389. return
  390. }
  391. v.EdbName = strings.TrimSpace(v.EdbName)
  392. if v.EdbName == "" {
  393. br.Msg = "请输入指标名称"
  394. return
  395. }
  396. indexNames = append(indexNames, v.EdbName)
  397. resp = append(resp, &NameCheckResult{
  398. EdbCode: v.EdbCode,
  399. EdbName: v.EdbName,
  400. })
  401. dataItems, err := data_manage.GetEdbDataAllByEdbCode(v.EdbCode, utils.DATA_SOURCE_OILCHEM, 0, utils.EDB_DATA_LIMIT)
  402. if err != nil && err.Error() != utils.ErrNoRow() {
  403. br.Msg = "获取失败"
  404. br.ErrMsg = "获取隆众资讯已存在信息失败,Err:" + err.Error()
  405. return
  406. }
  407. if len(dataItems) <= 0 {
  408. respItem, err := data.AddEdbData(utils.DATA_SOURCE_OILCHEM, v.EdbCode, v.Frequency)
  409. if err != nil {
  410. br.Msg = "获取失败"
  411. br.ErrMsg = "获取失败,Err:" + err.Error()
  412. return
  413. }
  414. if respItem.Ret != 200 {
  415. br.Msg = "未搜索到该指标"
  416. br.ErrMsg = respItem.ErrMsg + ";EdbCode:" + v.EdbCode
  417. return
  418. }
  419. }
  420. }
  421. // 重名校验
  422. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  423. if e != nil {
  424. br.Msg = "操作失败"
  425. br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
  426. return
  427. }
  428. nameExists := make(map[string]bool)
  429. for _, v := range edbList {
  430. nameExists[v.EdbName] = true
  431. }
  432. if len(nameExists) > 0 {
  433. for _, v := range resp {
  434. v.Exist = nameExists[v.EdbName]
  435. }
  436. }
  437. br.Data = resp
  438. br.Msg = "校验成功"
  439. br.Ret = 200
  440. br.Success = true
  441. }
  442. // BatchAdd
  443. // @Title 批量新增
  444. // @Description 批量新增
  445. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  446. // @Success 200 string "操作成功"
  447. // @router /data/oilchem/batch_add [post]
  448. func (this *TradeCommonController) BatchAdd() {
  449. br := new(models.BaseResponse).Init()
  450. defer func() {
  451. if br.ErrMsg == "" {
  452. br.IsSendEmail = false
  453. }
  454. this.Data["json"] = br
  455. this.ServeJSON()
  456. }()
  457. sysUser := this.SysUser
  458. if sysUser == nil {
  459. br.Msg = "请登录"
  460. br.ErrMsg = "请登录,SysUser Is Empty"
  461. br.Ret = 408
  462. return
  463. }
  464. deleteCache := true
  465. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_OILCHEM_" + strconv.Itoa(sysUser.AdminId)
  466. defer func() {
  467. if deleteCache {
  468. _ = utils.Rc.Delete(cacheKey)
  469. }
  470. }()
  471. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  472. deleteCache = false
  473. br.Msg = "系统处理中,请稍后重试!"
  474. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  475. return
  476. }
  477. var req []*data_manage.AddEdbInfoReq
  478. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  479. br.Msg = "参数解析异常!"
  480. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  481. return
  482. }
  483. if len(req) == 0 {
  484. br.Msg = "请选择指标"
  485. return
  486. }
  487. if len(req) > 30 {
  488. br.Msg = "批量添加指标数量不得超过30个"
  489. return
  490. }
  491. for _, v := range req {
  492. v.EdbCode = strings.TrimSpace(v.EdbCode)
  493. if v.EdbCode == "" {
  494. br.Msg = "指标ID不可为空"
  495. return
  496. }
  497. v.EdbName = strings.TrimSpace(v.EdbName)
  498. if v.EdbName == "" {
  499. br.Msg = "请输入指标名称"
  500. return
  501. }
  502. v.Frequency = strings.TrimSpace(v.Frequency)
  503. if v.Frequency == "" {
  504. br.Msg = "请选择频度"
  505. return
  506. }
  507. v.Unit = strings.TrimSpace(v.Unit)
  508. if v.Unit == "" {
  509. br.Msg = "请输入单位"
  510. return
  511. }
  512. if v.ClassifyId <= 0 {
  513. br.Msg = "请选择分类"
  514. return
  515. }
  516. }
  517. // 限定同一时间最多批量新增30个指标
  518. for _, v := range req {
  519. var r data.OilchemIndexSource2EdbReq
  520. r.EdbCode = v.EdbCode
  521. r.EdbName = v.EdbName
  522. r.Frequency = v.Frequency
  523. r.Unit = v.Unit
  524. r.ClassifyId = v.ClassifyId
  525. r.AdminId = sysUser.AdminId
  526. r.AdminRealName = sysUser.RealName
  527. edbInfo, errMsg, skip, e := data.OilchemIndexSource2Edb(r, this.Lang)
  528. if e != nil {
  529. br.Msg = "操作失败"
  530. if errMsg != "" {
  531. br.Msg = errMsg
  532. }
  533. br.ErrMsg = e.Error()
  534. return
  535. }
  536. if skip {
  537. continue
  538. }
  539. // 试用平台更新用户累计新增指标数
  540. if utils.BusinessCode == utils.BusinessCodeSandbox {
  541. go func() {
  542. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  543. if e != nil {
  544. tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
  545. utils.FileLog.Info(tips)
  546. return
  547. }
  548. if adminItem.DepartmentName != "ETA试用客户" {
  549. return
  550. }
  551. var ur etaTrialService.EtaTrialUserReq
  552. ur.Mobile = adminItem.Mobile
  553. _, _ = etaTrialService.UpdateUserIndexNum(ur)
  554. }()
  555. }
  556. // 新增操作日志
  557. {
  558. edbLog := new(data_manage.EdbInfoLog)
  559. edbLog.EdbInfoId = edbInfo.EdbInfoId
  560. edbLog.SourceName = edbInfo.SourceName
  561. edbLog.Source = edbInfo.Source
  562. edbLog.EdbCode = edbInfo.EdbCode
  563. edbLog.EdbName = edbInfo.EdbName
  564. edbLog.ClassifyId = edbInfo.ClassifyId
  565. edbLog.SysUserId = sysUser.AdminId
  566. edbLog.SysUserRealName = sysUser.RealName
  567. edbLog.CreateTime = time.Now()
  568. edbLog.Content = string(this.Ctx.Input.RequestBody)
  569. edbLog.Status = "新增指标"
  570. edbLog.Method = this.Ctx.Input.URI()
  571. go data_manage.AddEdbInfoLog(edbLog)
  572. }
  573. }
  574. br.Msg = "操作成功"
  575. br.Ret = 200
  576. br.Success = true
  577. br.IsAddLog = true
  578. }
  579. // ExportoilchemList
  580. // @Title 导出隆众资讯数据
  581. // @Description 导出隆众资讯数据
  582. // @Param request body request.ExportOilchemExcelReq true "type json string"
  583. // @Success 200 导出成功
  584. // @router /data/export/oilchemList [post]
  585. func (this *TradeCommonController) ExportoilchemList() {
  586. br := new(models.BaseResponse).Init()
  587. defer func() {
  588. this.Data["json"] = br
  589. this.ServeJSON()
  590. }()
  591. sysUser := this.SysUser
  592. if sysUser == nil {
  593. br.Msg = "请重新登录"
  594. return
  595. }
  596. var req data_manage.ExportOilchemExcelReq
  597. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  598. br.Msg = "参数解析异常!"
  599. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  600. return
  601. }
  602. secNameList := make([]*data_manage.BaseFromOilchemIndex, 0)
  603. dir, _ := os.Executable()
  604. exPath := filepath.Dir(dir)
  605. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  606. xlsxFile := xlsx.NewFile()
  607. var condition string
  608. var pars []interface{}
  609. if req.KeyWord != "" {
  610. condition += `AND (index_code=? OR index_name=?)`
  611. pars = utils.GetLikeKeywordPars(pars, req.KeyWord, 2)
  612. }
  613. if req.IsSelectedAll {
  614. if len(req.IndexCode) > 0 {
  615. condition += ` AND index_code NOT IN (` + utils.GetOrmInReplace(len(req.IndexCode)) + `)`
  616. pars = append(pars, req.IndexCode)
  617. }
  618. } else {
  619. if len(req.IndexCode) > 0 {
  620. condition += ` AND index_code IN (` + utils.GetOrmInReplace(len(req.IndexCode)) + `)`
  621. pars = append(pars, req.IndexCode)
  622. }
  623. }
  624. if req.ClassifyId > 0 {
  625. condition += ` AND classify_id=?`
  626. pars = append(pars, req.ClassifyId)
  627. }
  628. frequencies, err := data_manage.GetOilchemFrequencyByCondition(condition, pars)
  629. if err != nil {
  630. fmt.Println("GetSciFrequency err:", err.Error())
  631. utils.FileLog.Info("GetSciFrequency err:" + err.Error())
  632. return
  633. }
  634. for _, frequency := range frequencies {
  635. //获取指标
  636. secNameList, err = data_manage.GetOilchemIndexByConditionAndFrequency(condition, *frequency, pars)
  637. if err != nil {
  638. fmt.Println("获取数据失败,Err:" + err.Error())
  639. return
  640. }
  641. if len(secNameList) <= 0 {
  642. fmt.Println("secNameList长度为0")
  643. return
  644. }
  645. sheetNew, err := xlsxFile.AddSheet(*frequency)
  646. if err != nil {
  647. fmt.Println("新增Sheet失败", err.Error())
  648. return
  649. }
  650. secNameRow := sheetNew.AddRow()
  651. frequencyRow := sheetNew.AddRow()
  652. unitRow := sheetNew.AddRow()
  653. lastModifyDateRow := sheetNew.AddRow()
  654. var indexIdList []int
  655. for _, sv := range secNameList {
  656. indexIdList = append(indexIdList, sv.BaseFromOilchemIndexId)
  657. }
  658. dataTimeList, err := data_manage.GetOilchemDataDataTimeByIndexId(indexIdList)
  659. if err != nil {
  660. fmt.Println("获取数据时间失败", err.Error())
  661. return
  662. }
  663. // 添加excel左侧指标日期
  664. setRowIndex := 4
  665. for rk, dv := range dataTimeList {
  666. rowIndex := setRowIndex + rk
  667. row := sheetNew.Row(rowIndex)
  668. displayDate, _ := time.Parse(utils.FormatDate, dv)
  669. displayDateCell := row.AddCell()
  670. style := new(xlsx.Style)
  671. style.ApplyAlignment = true
  672. style.Alignment.WrapText = true
  673. displayDateCell.SetStyle(style)
  674. displayDateCell.SetDate(displayDate)
  675. }
  676. for k, sv := range secNameList {
  677. //获取数据
  678. dataList, err := data_manage.GetOilchemIndexDataByCode(sv.IndexCode)
  679. if err != nil {
  680. br.Msg = "获取数据失败"
  681. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  682. return
  683. }
  684. if k == 0 {
  685. secNameRow.AddCell().SetValue("指标名称/Metric Name")
  686. frequencyRow.AddCell().SetValue("频度/Frequency")
  687. unitRow.AddCell().SetValue("单位/Unit")
  688. lastModifyDateRow.AddCell().SetValue("更新时间/Update Time")
  689. min := k * 3
  690. sheetNew.SetColWidth(min, min, 15)
  691. }
  692. if len(dataList) == 0 {
  693. continue
  694. }
  695. secNameRow.AddCell().SetValue(sv.IndexName)
  696. frequencyRow.AddCell().SetValue(sv.Frequency)
  697. unitRow.AddCell().SetValue(sv.Unit)
  698. lastModifyDateRow.AddCell().SetValue(sv.ModifyTime)
  699. dataInfoMap := make(map[string]*data_manage.BaseFromOilchemData)
  700. for _, v := range dataList {
  701. dataInfoMap[v.DataTime] = v
  702. }
  703. for rk, dtv := range dataTimeList {
  704. rowIndex := setRowIndex + rk
  705. row := sheetNew.Row(rowIndex)
  706. displayDateCell := row.AddCell()
  707. tmpData, ok := dataInfoMap[dtv]
  708. if ok {
  709. displayDateCell.SetValue(tmpData.Value)
  710. }
  711. }
  712. }
  713. }
  714. err = xlsxFile.Save(downLoadnFilePath)
  715. if err != nil {
  716. //有指标无数据时先导出一遍空表
  717. sheet, err := xlsxFile.AddSheet("无数据")
  718. if err != nil {
  719. br.Msg = "新增Sheet失败"
  720. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  721. return
  722. }
  723. rowSecName := sheet.AddRow()
  724. celSecName := rowSecName.AddCell()
  725. celSecName.SetValue("")
  726. err = xlsxFile.Save(downLoadnFilePath)
  727. if err != nil {
  728. br.Msg = "保存文件失败"
  729. br.ErrMsg = "保存文件失败"
  730. return
  731. }
  732. }
  733. fileName := `隆众资讯`
  734. if len(secNameList) > 0 {
  735. fileName = secNameList[0].IndexName
  736. }
  737. fileName += time.Now().Format("2006.01.02") + `.xlsx` //文件名称
  738. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  739. defer func() {
  740. os.Remove(downLoadnFilePath)
  741. }()
  742. br.Ret = 200
  743. br.Success = true
  744. br.Msg = "success"
  745. }