sci_data.go 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/models/data_manage/request"
  7. "eta/eta_api/services/alarm_msg"
  8. "eta/eta_api/services/data"
  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. "strings"
  16. "time"
  17. )
  18. // @Title 卓创(红桃3)数据分类
  19. // @Description 卓创(红桃3)数据分类接口
  20. // @Success 200 {object} data_manage.SciClassify
  21. // @router /sci/classify [get]
  22. func (this *EdbInfoController) SciClassify() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. sysUser := this.SysUser
  29. if sysUser == nil {
  30. br.Msg = "请登录"
  31. br.ErrMsg = "请登录,SysUser Is Empty"
  32. br.Ret = 408
  33. return
  34. }
  35. rootList, err := data_manage.GetBaseFromSciClassifyByParentId(0)
  36. if err != nil && err.Error() != utils.ErrNoRow() {
  37. br.Msg = "获取失败"
  38. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  39. return
  40. }
  41. classifyAll, err := data_manage.GetAllBaseFromSciClassify()
  42. if err != nil && err.Error() != utils.ErrNoRow() {
  43. br.Msg = "获取失败"
  44. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  45. return
  46. }
  47. baseFromSciIndexMap := make(map[int][]*data_manage.BaseFromSciClassifyItems)
  48. allBaseFromSciIndex, err := data_manage.GetSciIndexAll()
  49. if err != nil && err.Error() != utils.ErrNoRow() {
  50. br.Msg = "获取失败"
  51. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  52. return
  53. }
  54. for _, v := range allBaseFromSciIndex {
  55. baseFromSciIndexMap[v.ClassifyId] = append(baseFromSciIndexMap[v.ClassifyId], v)
  56. }
  57. rootChildMap := make(map[int][]*data_manage.BaseFromSciClassifyItems)
  58. for _, v := range classifyAll {
  59. rootChildMap[v.ParentId] = append(rootChildMap[v.ParentId], v)
  60. if existItems, ok := baseFromSciIndexMap[v.ClassifyId]; ok {
  61. v.Children = existItems
  62. } else {
  63. items := make([]*data_manage.BaseFromSciClassifyItems, 0)
  64. v.Children = items
  65. }
  66. }
  67. var ret data_manage.BaseFromSciClassifyResp
  68. nodeAll := make([]*data_manage.BaseFromSciClassifyItems, 0)
  69. for _, v := range rootList {
  70. if existItems, ok := rootChildMap[v.ClassifyId]; ok {
  71. v.Children = existItems
  72. } else {
  73. items := make([]*data_manage.BaseFromSciClassifyItems, 0)
  74. v.Children = items
  75. }
  76. nodeAll = append(nodeAll, v)
  77. }
  78. //查询未分类的指标列表
  79. initClassify := &data_manage.BaseFromSciClassifyItems{
  80. ClassifyId: 0,
  81. BaseFromSciIndexId: 0,
  82. ClassifyName: "未分类",
  83. ClassifyNameEn: "Unclassified",
  84. ParentId: 0,
  85. Level: 1,
  86. Sort: 0,
  87. Children: nil,
  88. }
  89. initIndexList, err := data_manage.GetBaseFromSciIndexByClassifyId(initClassify.ClassifyId)
  90. if err != nil {
  91. br.Msg = "获取未分类指标失败"
  92. br.ErrMsg = "获取未分类指标失败,Err:" + err.Error()
  93. return
  94. }
  95. for _, v := range initIndexList {
  96. tmp := &data_manage.BaseFromSciClassifyItems{
  97. ClassifyId: v.ClassifyId,
  98. BaseFromSciIndexId: v.BaseFromSciIndexId,
  99. BaseFromSciIndexCode: v.IndexCode,
  100. ClassifyName: v.IndexName,
  101. ParentId: 0,
  102. Level: 0,
  103. Sort: 0,
  104. Children: nil,
  105. }
  106. initClassify.Children = append(initClassify.Children, tmp)
  107. }
  108. finalList := make([]*data_manage.BaseFromSciClassifyItems, 0)
  109. finalList = append(finalList, initClassify)
  110. finalList = append(finalList, nodeAll...)
  111. ret.List = finalList
  112. br.Ret = 200
  113. br.Success = true
  114. br.Msg = "获取成功"
  115. br.Data = ret
  116. }
  117. // SciClassifyName
  118. // @Title 卓创(红桃3)数据分类名称查询
  119. // @Description 卓创(红桃3)数据分类名称查询接口
  120. // @Success 200 {object} data_manage.SciClassify
  121. // @router /sci/classify/name [get]
  122. func (this *EdbInfoController) SciClassifyName() {
  123. br := new(models.BaseResponse).Init()
  124. defer func() {
  125. this.Data["json"] = br
  126. this.ServeJSON()
  127. }()
  128. sysUser := this.SysUser
  129. if sysUser == nil {
  130. br.Msg = "请登录"
  131. br.ErrMsg = "请登录,SysUser Is Empty"
  132. br.Ret = 408
  133. return
  134. }
  135. rootList, err := data_manage.GetBaseFromSciClassifyByParentId(0)
  136. if err != nil && err.Error() != utils.ErrNoRow() {
  137. br.Msg = "获取失败"
  138. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  139. return
  140. }
  141. classifyAll, err := data_manage.GetAllBaseFromSciClassify()
  142. if err != nil && err.Error() != utils.ErrNoRow() {
  143. br.Msg = "获取失败"
  144. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  145. return
  146. }
  147. rootChildMap := make(map[int][]*data_manage.BaseFromSciClassifyNameItems)
  148. for _, v := range classifyAll {
  149. tmp := &data_manage.BaseFromSciClassifyNameItems{
  150. ClassifyId: v.ClassifyId,
  151. ClassifyName: v.ClassifyName,
  152. ParentId: v.ParentId,
  153. Children: nil,
  154. }
  155. rootChildMap[v.ParentId] = append(rootChildMap[v.ParentId], tmp)
  156. }
  157. var ret data_manage.BaseFromSciClassifyNameResp
  158. nodeAll := make([]*data_manage.BaseFromSciClassifyNameItems, 0)
  159. for _, v := range rootList {
  160. tmp := &data_manage.BaseFromSciClassifyNameItems{
  161. ClassifyId: v.ClassifyId,
  162. ClassifyName: v.ClassifyName,
  163. ParentId: v.ParentId,
  164. Children: nil,
  165. }
  166. if existItems, ok := rootChildMap[v.ClassifyId]; ok {
  167. tmp.Children = existItems
  168. } else {
  169. items := make([]*data_manage.BaseFromSciClassifyNameItems, 0)
  170. tmp.Children = items
  171. }
  172. nodeAll = append(nodeAll, tmp)
  173. }
  174. ret.List = nodeAll
  175. br.Ret = 200
  176. br.Success = true
  177. br.Msg = "获取成功"
  178. br.Data = ret
  179. }
  180. // AddSciClassify
  181. // @Title 新增分类
  182. // @Description 新增分类接口
  183. // @Param request body data_manage.AddBaseFromSciClassifyReq true "type json string"
  184. // @Success 200 Ret=200 保存成功
  185. // @router /sci/classify/add [post]
  186. func (this *EdbInfoController) AddSciClassify() {
  187. br := new(models.BaseResponse).Init()
  188. defer func() {
  189. this.Data["json"] = br
  190. this.ServeJSON()
  191. }()
  192. var req request.AddBaseFromSciClassifyReq
  193. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  194. if err != nil {
  195. br.Msg = "参数解析异常!"
  196. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  197. return
  198. }
  199. if req.ClassifyName == "" {
  200. br.Msg = "请输入分类名称"
  201. br.IsSendEmail = false
  202. return
  203. }
  204. req.ClassifyName = utils.TrimStr(req.ClassifyName)
  205. req.ClassifyName = strings.Replace(req.ClassifyName, "/t", "", -1)
  206. //添加指标
  207. sciClassifyInfo, err, errMsg := data.AddSciClassify(req.ClassifyName, req.ParentId, this.SysUser.AdminId, this.SysUser.RealName, this.Lang)
  208. if errMsg != `` {
  209. br.Msg = errMsg
  210. br.ErrMsg = errMsg
  211. if err != nil {
  212. br.ErrMsg = errMsg + ";Err:" + err.Error()
  213. } else {
  214. br.IsSendEmail = false
  215. }
  216. return
  217. }
  218. // 如果是添加一级分类,那么默认加上 价格、供应、需求、库存 四个二级分类
  219. if sciClassifyInfo.ParentId == 0 {
  220. secondClassifyList := []string{"价格", "供应", "需求", "库存"}
  221. for _, v := range secondClassifyList {
  222. _, _, tmpErrMsg := data.AddSciClassify(v, sciClassifyInfo.ClassifyId, this.SysUser.AdminId, this.SysUser.RealName, this.Lang)
  223. if tmpErrMsg != `` {
  224. go alarm_msg.SendAlarmMsg("SCI原始数据-添加一级分类时,默认添加二级分类失败,一级分类名称:"+req.ClassifyName+",二级分类名称:"+v+", Err:"+tmpErrMsg, 3)
  225. return
  226. }
  227. }
  228. }
  229. resp := data_manage.AddSciClassifyResp{
  230. ClassifyId: sciClassifyInfo.ClassifyId,
  231. //UniqueCode: fmt.Sprint(SciClassifyInfo.BaseFromSciClassifyId),
  232. }
  233. br.Data = resp
  234. br.Ret = 200
  235. br.Msg = "保存成功"
  236. br.Success = true
  237. br.IsAddLog = true
  238. }
  239. // EditSciClassify
  240. // @Title 修改分类
  241. // @Description 修改分类接口
  242. // @Param request body data_manage.EditBaseFromMysteelChemicalClassifyReq true "type json string"
  243. // @Success 200 Ret=200 修改成功
  244. // @router /sci/classify/edit [post]
  245. func (this *EdbInfoController) EditSciClassify() {
  246. br := new(models.BaseResponse).Init()
  247. defer func() {
  248. this.Data["json"] = br
  249. this.ServeJSON()
  250. }()
  251. var req request.EditBaseFromSciClassifyReq
  252. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  253. if err != nil {
  254. br.Msg = "参数解析异常!"
  255. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  256. return
  257. }
  258. if req.ClassifyName == "" {
  259. br.Msg = "请输入分类名称"
  260. br.IsSendEmail = false
  261. return
  262. }
  263. if req.ClassifyId <= 0 {
  264. br.Msg = "参数错误"
  265. br.IsSendEmail = false
  266. return
  267. }
  268. err, errMsg := data.EditSciClassify(req.ClassifyId, req.ClassifyName, this.Lang, this.SysUser)
  269. if errMsg != `` {
  270. br.Msg = errMsg
  271. br.ErrMsg = errMsg
  272. if err != nil {
  273. br.ErrMsg = errMsg + ";Err:" + err.Error()
  274. } else {
  275. br.IsSendEmail = false
  276. }
  277. return
  278. }
  279. br.Ret = 200
  280. br.Msg = "保存成功"
  281. br.Success = true
  282. br.IsAddLog = true
  283. }
  284. // @Title 卓创(红桃3)数据频度
  285. // @Description 卓创(红桃3)数据频度接口
  286. // @Param ClassifyId query string true "分类Id"
  287. // @Success 200 {object} data_manage.LzFrequency
  288. // @router /sci/frequency [get]
  289. func (this *EdbInfoController) SciFrequency() {
  290. br := new(models.BaseResponse).Init()
  291. defer func() {
  292. this.Data["json"] = br
  293. this.ServeJSON()
  294. }()
  295. sysUser := this.SysUser
  296. if sysUser == nil {
  297. br.Msg = "请登录"
  298. br.ErrMsg = "请登录,SysUser Is Empty"
  299. br.Ret = 408
  300. return
  301. }
  302. classifyId, _ := this.GetInt("ClassifyId")
  303. if classifyId < 0 {
  304. br.Msg = "请选择分类"
  305. br.ErrMsg = "请选择分类"
  306. return
  307. }
  308. frequencyList, err := data_manage.GetSciFrequencyByClassifyId(classifyId)
  309. if err != nil {
  310. br.Msg = "获取频度失败"
  311. br.ErrMsg = "获取频度失败,Err:" + err.Error()
  312. return
  313. }
  314. br.Ret = 200
  315. br.Success = true
  316. br.Msg = "获取成功"
  317. br.Data = frequencyList
  318. }
  319. // MoveSciClassify
  320. // @Title 分类移动接口
  321. // @Description 分类移动接口
  322. // @Success 200 {object} data_manage.MoveBaseFromMysteelChemicalClassifyReq
  323. // @router /sci/classify/move [post]
  324. func (this *EdbInfoController) MoveSciClassify() {
  325. br := new(models.BaseResponse).Init()
  326. defer func() {
  327. this.Data["json"] = br
  328. this.ServeJSON()
  329. }()
  330. sysUser := this.SysUser
  331. if sysUser == nil {
  332. br.Msg = "请登录"
  333. br.ErrMsg = "请登录,SysUser Is Empty"
  334. br.Ret = 408
  335. return
  336. }
  337. var req request.MoveBaseFromSciClassifyReq
  338. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  339. if err != nil {
  340. br.Msg = "参数解析异常!"
  341. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  342. return
  343. }
  344. if req.ClassifyId <= 0 {
  345. br.Msg = "参数错误"
  346. br.ErrMsg = "分类id小于等于0"
  347. br.IsSendEmail = false
  348. return
  349. }
  350. err, errMsg := data.MoveSciClassify(req.ClassifyId, req.ParentId, req.PrevClassifyId, req.NextClassifyId)
  351. if errMsg != `` {
  352. br.Msg = errMsg
  353. br.ErrMsg = errMsg
  354. if err != nil {
  355. br.ErrMsg = err.Error()
  356. } else {
  357. br.IsSendEmail = false
  358. }
  359. return
  360. }
  361. br.Ret = 200
  362. br.Success = true
  363. br.IsAddLog = true
  364. br.Msg = "移动成功"
  365. }
  366. // DeleteSciClassify
  367. // @Title 删除分类
  368. // @Description 删除分类接口
  369. // @Param request body data_manage.DelBaseFromSciClassifyReq true "type json string"
  370. // @Success 200 Ret=200 删除成功
  371. // @router /sci/classify/del [post]
  372. func (this *EdbInfoController) DeleteSciClassify() {
  373. br := new(models.BaseResponse).Init()
  374. defer func() {
  375. this.Data["json"] = br
  376. this.ServeJSON()
  377. }()
  378. var req request.DelBaseFromSciClassifyReq
  379. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  380. if err != nil {
  381. br.Msg = "参数解析异常!"
  382. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  383. return
  384. }
  385. if req.ClassifyId < 0 {
  386. br.Msg = "参数错误"
  387. br.IsSendEmail = false
  388. return
  389. }
  390. err, errMsg := data.DelSciClassify(req.ClassifyId, this.SysUser)
  391. if errMsg != `` {
  392. br.Msg = errMsg
  393. br.ErrMsg = errMsg
  394. if err != nil {
  395. br.ErrMsg = errMsg + ";Err:" + err.Error()
  396. } else {
  397. br.IsSendEmail = false
  398. }
  399. return
  400. }
  401. br.Ret = 200
  402. br.Msg = "删除成功"
  403. br.Success = true
  404. br.IsAddLog = true
  405. }
  406. // MoveSciData
  407. // @Title SCI指标移动接口
  408. // @Description SCI指标移动接口
  409. // @Success 200 {object} data_manage.MoveBaseFromSciReq
  410. // @router /sci/move [post]
  411. func (this *EdbInfoController) MoveSciData() {
  412. br := new(models.BaseResponse).Init()
  413. defer func() {
  414. this.Data["json"] = br
  415. this.ServeJSON()
  416. }()
  417. sysUser := this.SysUser
  418. if sysUser == nil {
  419. br.Msg = "请登录"
  420. br.ErrMsg = "请登录,SysUser Is Empty"
  421. br.Ret = 408
  422. return
  423. }
  424. var req request.MoveBaseFromSciReq
  425. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  426. if err != nil {
  427. br.Msg = "参数解析异常!"
  428. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  429. return
  430. }
  431. if req.BaseFromSciIndexId <= 0 {
  432. br.Msg = "参数错误"
  433. br.ErrMsg = "指标id小于等于0"
  434. br.IsSendEmail = false
  435. return
  436. }
  437. if req.ClassifyId < 0 {
  438. br.Msg = "请选择分类"
  439. br.ErrMsg = "请选择分类"
  440. br.IsSendEmail = false
  441. return
  442. }
  443. err, errMsg := data.MoveSciData2(req.BaseFromSciIndexId, req.ClassifyId, req.PrevBaseFromSciIndexId, req.NextBaseFromSciIndexId)
  444. if errMsg != `` {
  445. br.Msg = errMsg
  446. br.ErrMsg = errMsg
  447. if err != nil {
  448. br.ErrMsg = err.Error()
  449. } else {
  450. br.IsSendEmail = false
  451. }
  452. return
  453. }
  454. br.Ret = 200
  455. br.Success = true
  456. br.IsAddLog = true
  457. br.Msg = "移动成功"
  458. }
  459. // EditSci
  460. // @Title 编辑卓创(红桃3)指标
  461. // @Description 编辑卓创(红桃3)指标接口
  462. // @Param request body data_manage.AddEdbClassifyReq true "type json string"
  463. // @Success 200 Ret=200 保存成功
  464. // @router /sci/edit [post]
  465. func (this *EdbInfoController) EditSci() {
  466. br := new(models.BaseResponse).Init()
  467. defer func() {
  468. this.Data["json"] = br
  469. this.ServeJSON()
  470. }()
  471. var req request.EditBaseFromSciReq
  472. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  473. if err != nil {
  474. br.Msg = "参数解析异常!"
  475. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  476. return
  477. }
  478. if req.BaseFromSciIndexId <= 0 {
  479. br.Msg = "请选择指标"
  480. br.IsSendEmail = false
  481. return
  482. }
  483. if req.ClassifyId <= 0 {
  484. br.Msg = "请选择分类"
  485. br.IsSendEmail = false
  486. return
  487. }
  488. //编辑指标
  489. sciIndexInfo, err, errMsg := data.EditSciIndex(req.BaseFromSciIndexId, req.ClassifyId, this.SysUser)
  490. if errMsg != `` {
  491. br.Msg = errMsg
  492. br.ErrMsg = errMsg
  493. if err != nil {
  494. br.ErrMsg = errMsg + ";Err:" + err.Error()
  495. } else {
  496. br.IsSendEmail = false
  497. }
  498. return
  499. }
  500. resp := data_manage.EditSciIndexInfoResp{
  501. BaseFromSciIndexId: sciIndexInfo.BaseFromSciIndexId,
  502. IndexCode: sciIndexInfo.IndexCode,
  503. }
  504. br.Data = resp
  505. br.Ret = 200
  506. br.Msg = "保存成功"
  507. br.Success = true
  508. br.IsAddLog = true
  509. }
  510. // @Title 获取钢联数据
  511. // @Description 获取钢联数据接口
  512. // @Param PageSize query int true "每页数据条数"
  513. // @Param CurrentIndex query int true "当前页页码,从1开始"
  514. // @Param ClassifyId query string true "分类id"
  515. // @Param Frequency query string true "频度名称"
  516. // @Success 200 {object} data_manage.LzFrequency
  517. // @router /sci/data [get]
  518. func (this *EdbInfoController) SciData() {
  519. br := new(models.BaseResponse).Init()
  520. defer func() {
  521. this.Data["json"] = br
  522. this.ServeJSON()
  523. }()
  524. sysUser := this.SysUser
  525. if sysUser == nil {
  526. br.Msg = "请登录"
  527. br.ErrMsg = "请登录,SysUser Is Empty"
  528. br.Ret = 408
  529. return
  530. }
  531. pageSize, _ := this.GetInt("PageSize")
  532. currentIndex, _ := this.GetInt("CurrentIndex")
  533. var startSize int
  534. if pageSize <= 0 {
  535. pageSize = utils.PageSize20
  536. }
  537. if currentIndex <= 0 {
  538. currentIndex = 1
  539. }
  540. startSize = utils.StartIndex(currentIndex, pageSize)
  541. classifyId, _ := this.GetInt("ClassifyId")
  542. if classifyId < 0 {
  543. br.Msg = "请选择分类"
  544. br.ErrMsg = "请选择分类"
  545. return
  546. }
  547. frequency := this.GetString("Frequency")
  548. if frequency == "" {
  549. br.Msg = "请选择频度"
  550. br.ErrMsg = "请选择频度"
  551. return
  552. }
  553. //获取指标
  554. var condition string
  555. var pars []interface{}
  556. if classifyId >= 0 {
  557. condition += ` AND classify_id=? `
  558. pars = append(pars, classifyId)
  559. }
  560. if frequency != "" {
  561. condition += ` AND frequency=? `
  562. pars = append(pars, frequency)
  563. }
  564. sciList, err := data_manage.GetSciIndex(condition, pars)
  565. if err != nil {
  566. br.Msg = "获取数据失败"
  567. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  568. return
  569. }
  570. resultList := make([]*data_manage.SciIndexList, 0)
  571. for _, v := range sciList {
  572. product := new(data_manage.SciIndexList)
  573. product.BaseFromSciIndexId = v.BaseFromSciIndexId
  574. product.Unit = v.Unit
  575. product.IndexCode = v.IndexCode
  576. product.IndexName = v.IndexName
  577. product.Frequency = v.Frequency
  578. product.ModifyTime = v.ModifyTime
  579. total, err := data_manage.GetSciIndexDataCount(v.IndexCode)
  580. page := paging.GetPaging(currentIndex, pageSize, total)
  581. dataList, err := data_manage.GetSciIndexData(v.IndexCode, startSize, pageSize)
  582. if err != nil {
  583. br.Msg = "获取数据失败"
  584. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  585. return
  586. }
  587. if dataList == nil {
  588. dataList = make([]*data_manage.SciIndexData, 0)
  589. }
  590. product.DataList = dataList
  591. product.Paging = page
  592. resultList = append(resultList, product)
  593. }
  594. br.Ret = 200
  595. br.Success = true
  596. br.Msg = "获取成功"
  597. br.Data = resultList
  598. }
  599. // SciSearchList
  600. // @Title SCI模糊搜索
  601. // @Description SCI模糊搜索
  602. // @Param Keyword query string ture "关键字搜索"
  603. // @Success 200 {object} models.BaseResponse
  604. // @router /sci/search_list [get]
  605. func (this *EdbInfoController) SciSearchList() {
  606. br := new(models.BaseResponse).Init()
  607. defer func() {
  608. this.Data["json"] = br
  609. this.ServeJSON()
  610. }()
  611. sysUser := this.SysUser
  612. if sysUser == nil {
  613. br.Msg = "请重新登录"
  614. return
  615. }
  616. //关键字
  617. keyword := this.GetString("Keyword")
  618. list, err := data_manage.GetSciItemList(keyword)
  619. if err != nil {
  620. br.ErrMsg = "获取失败,Err:" + err.Error()
  621. br.Msg = "获取失败"
  622. return
  623. }
  624. br.Ret = 200
  625. br.Success = true
  626. br.Msg = "获取成功"
  627. br.Data = list
  628. }
  629. // SciSingleData
  630. // @Title 获取Sci数据
  631. // @Description 获取sci单条数据接口
  632. // @Param IndexCode query string true "指标唯一编码"
  633. // @Success 200 {object} models.BaseResponse
  634. // @router /sci/single_data [get]
  635. func (this *EdbInfoController) SciSingleData() {
  636. br := new(models.BaseResponse).Init()
  637. defer func() {
  638. this.Data["json"] = br
  639. this.ServeJSON()
  640. }()
  641. sysUser := this.SysUser
  642. if sysUser == nil {
  643. br.Msg = "请登录"
  644. br.ErrMsg = "请登录,SysUser Is Empty"
  645. br.Ret = 408
  646. return
  647. }
  648. indexCode := this.GetString("IndexCode")
  649. indexInfo, err := data_manage.GetBaseFromSciIndexByIndexCode(indexCode)
  650. if err != nil {
  651. br.Msg = "获取指标信息失败"
  652. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  653. return
  654. }
  655. dataList, err := data_manage.GetSciIndexDataByCode(indexCode)
  656. if err != nil {
  657. br.Msg = "获取数据失败"
  658. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  659. return
  660. }
  661. var ret data_manage.SciSingleDataResp
  662. ret.ClassifyId = indexInfo.ClassifyId
  663. ret.BaseFromSciIndexId = indexInfo.BaseFromSciIndexId
  664. ret.IndexCode = indexInfo.IndexCode
  665. ret.IndexName = indexInfo.IndexName
  666. ret.Frequency = indexInfo.Frequency
  667. ret.Interface = indexInfo.Interface
  668. ret.CreateTime = indexInfo.CreateTime.Format(utils.FormatDateTime)
  669. ret.ModifyTime = indexInfo.ModifyTime.Format(utils.FormatDateTime)
  670. ret.Unit = indexInfo.Unit
  671. ret.Data = dataList
  672. br.Ret = 200
  673. br.Success = true
  674. br.Msg = "获取成功"
  675. br.Data = ret
  676. }
  677. // DeleteSciData
  678. // @Title 删除指标
  679. // @Description 删除指标接口
  680. // @Param request body data_manage.DelBaseFromSciReq true "type json string"
  681. // @Success 200 Ret=200 删除成功
  682. // @router /sci/del [post]
  683. func (this *EdbInfoController) DeleteSciData() {
  684. br := new(models.BaseResponse).Init()
  685. defer func() {
  686. this.Data["json"] = br
  687. this.ServeJSON()
  688. }()
  689. var req request.DelBaseFromSciReq
  690. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  691. if err != nil {
  692. br.Msg = "参数解析异常!"
  693. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  694. return
  695. }
  696. if req.BaseFromSciIndexId < 0 {
  697. br.Msg = "参数错误"
  698. br.IsSendEmail = false
  699. return
  700. }
  701. err, errMsg := data.DelSciData(req.BaseFromSciIndexId)
  702. if errMsg != `` {
  703. br.Msg = errMsg
  704. br.ErrMsg = errMsg
  705. if err != nil {
  706. br.ErrMsg = errMsg + ";Err:" + err.Error()
  707. } else {
  708. br.IsSendEmail = false
  709. }
  710. return
  711. }
  712. br.Ret = 200
  713. br.Msg = "删除成功"
  714. br.Success = true
  715. br.IsAddLog = true
  716. }
  717. // ResetSciData
  718. // @Title 指标数据清除分类
  719. // @Description 指标数据清除分类
  720. // @Param request body data_manage.DelBaseFromSciReq true "type json string"
  721. // @Success 200 Ret=200 操作成功
  722. // @router /sci/reset [post]
  723. func (this *EdbInfoController) ResetSciData() {
  724. br := new(models.BaseResponse).Init()
  725. defer func() {
  726. this.Data["json"] = br
  727. this.ServeJSON()
  728. }()
  729. var req request.DelBaseFromSciReq
  730. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  731. if err != nil {
  732. br.Msg = "参数解析异常!"
  733. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  734. return
  735. }
  736. if req.BaseFromSciIndexId < 0 {
  737. br.Msg = "参数错误"
  738. br.IsSendEmail = false
  739. return
  740. }
  741. err, errMsg := data.ResetSciData(req.BaseFromSciIndexId)
  742. if errMsg != `` {
  743. br.Msg = errMsg
  744. br.ErrMsg = errMsg
  745. if err != nil {
  746. br.ErrMsg = errMsg + ";Err:" + err.Error()
  747. } else {
  748. br.IsSendEmail = false
  749. }
  750. return
  751. }
  752. br.Ret = 200
  753. br.Msg = "操作成功"
  754. br.Success = true
  755. br.IsAddLog = true
  756. }
  757. // ExportSciList
  758. // @Title 导出Sci数据
  759. // @Description 导出Sci数据
  760. // @Param IndexName query string false "名称关键词"
  761. // @Param IndexCode query string false "指标唯一编码"
  762. // @Param ClassifyId query string true "分类"
  763. // @Param Frequency query string true "频度"
  764. // @Param UnitName query string false "单位"
  765. // @Param ModifyTime query string false "更新时间"
  766. // @Success 200 导出成功
  767. // @router /export/sciList [get]
  768. func (this *EdbInfoController) ExportSciList() {
  769. br := new(models.BaseResponse).Init()
  770. defer func() {
  771. this.Data["json"] = br
  772. this.ServeJSON()
  773. }()
  774. sysUser := this.SysUser
  775. if sysUser == nil {
  776. br.Msg = "请重新登录"
  777. return
  778. }
  779. //typeName := this.GetString("TypeName") //分类
  780. //frequency := this.GetString("Frequency")
  781. indexCode := this.GetString("IndexCode") //指标唯一编码
  782. classifyId, _ := this.GetInt("ClassifyId")
  783. secNameList := make([]*models.EdbdataExportList, 0)
  784. dir, _ := os.Executable()
  785. exPath := filepath.Dir(dir)
  786. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  787. xlsxFile := xlsx.NewFile()
  788. if classifyId >= 0 && indexCode == "" {
  789. frequencies, err := data_manage.GetSciFrequency(classifyId)
  790. if err != nil {
  791. fmt.Println("GetSciFrequency err:", err.Error())
  792. utils.FileLog.Info("GetSciFrequency err:" + err.Error())
  793. return
  794. }
  795. for _, frequency := range frequencies {
  796. //获取指标
  797. var pars []interface{}
  798. pars = append(pars, classifyId)
  799. pars = append(pars, frequency)
  800. condition := " AND classify_id=? AND frequency=? "
  801. secNameList, err := data_manage.GetSciIndex(condition, pars)
  802. if err != nil {
  803. fmt.Println("获取数据失败,Err:" + err.Error())
  804. return
  805. }
  806. if len(secNameList) <= 0 {
  807. fmt.Println("secNameList长度为0")
  808. return
  809. }
  810. sheetNew, err := xlsxFile.AddSheet(*frequency)
  811. if err != nil {
  812. fmt.Println("新增Sheet失败", err.Error())
  813. return
  814. }
  815. //sheetNew.SetColWidth()
  816. //获取指标数据
  817. windRow := sheetNew.AddRow()
  818. secNameRow := sheetNew.AddRow()
  819. indexCodeRow := sheetNew.AddRow()
  820. frequencyRow := sheetNew.AddRow()
  821. unitRow := sheetNew.AddRow()
  822. lastModifyDateRow := sheetNew.AddRow()
  823. //获取分类下指标最大数据量
  824. dataMax, err := data_manage.GetSciDataMaxCount(classifyId)
  825. if err != nil {
  826. fmt.Println("获取指标最大数据量失败", err.Error())
  827. return
  828. }
  829. fmt.Println("dataMax:", dataMax)
  830. setRowIndex := 6
  831. for k, sv := range secNameList {
  832. //获取数据
  833. dataList, err := data_manage.GetSciIndexDataByCode(sv.IndexCode)
  834. if err != nil {
  835. br.Msg = "获取数据失败"
  836. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  837. return
  838. }
  839. if len(dataList) > 0 {
  840. windRow.AddCell().SetValue("Sci")
  841. secNameRow.AddCell().SetValue("指标名称")
  842. indexCodeRow.AddCell().SetValue("指标ID")
  843. frequencyRow.AddCell().SetValue("频率")
  844. unitRow.AddCell().SetValue("单位")
  845. lastModifyDateRow.AddCell().SetValue("更新时间")
  846. secNameRow.AddCell().SetValue(sv.IndexName)
  847. indexCodeRow.AddCell().SetValue(sv.IndexCode)
  848. frequencyRow.AddCell().SetValue(sv.Frequency)
  849. unitRow.AddCell().SetValue(sv.Unit)
  850. lastModifyDateRow.AddCell().SetValue(sv.ModifyTime)
  851. windRow.AddCell()
  852. windRow.AddCell()
  853. secNameRow.AddCell()
  854. indexCodeRow.AddCell()
  855. frequencyRow.AddCell()
  856. unitRow.AddCell()
  857. lastModifyDateRow.AddCell()
  858. min := k * 3
  859. sheetNew.SetColWidth(min, min, 15)
  860. if len(dataList) <= 0 {
  861. for n := 0; n < dataMax; n++ {
  862. rowIndex := setRowIndex + n
  863. row := sheetNew.Row(rowIndex)
  864. row.AddCell()
  865. row.AddCell()
  866. row.AddCell()
  867. }
  868. } else {
  869. endRowIndex := 0
  870. for rk, dv := range dataList {
  871. rowIndex := setRowIndex + rk
  872. row := sheetNew.Row(rowIndex)
  873. displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
  874. displayDateCell := row.AddCell()
  875. style := new(xlsx.Style)
  876. style.ApplyAlignment = true
  877. style.Alignment.WrapText = true
  878. displayDateCell.SetStyle(style)
  879. displayDateCell.SetDate(displayDate)
  880. row.AddCell().SetValue(dv.Value)
  881. row.AddCell()
  882. endRowIndex = rowIndex
  883. }
  884. if len(dataList) < dataMax {
  885. dataLen := dataMax - len(dataList)
  886. for n := 0; n < dataLen; n++ {
  887. rowIndex := (endRowIndex + 1) + n
  888. row := sheetNew.Row(rowIndex)
  889. row.AddCell()
  890. row.AddCell()
  891. row.AddCell()
  892. }
  893. }
  894. }
  895. }
  896. }
  897. }
  898. } else {
  899. frequencies, err := data_manage.GetSciFrequencyByCode(indexCode)
  900. if err != nil {
  901. fmt.Println("GetSciFrequencyByCode err:", err.Error())
  902. utils.FileLog.Info("GetSciFrequencyByCode err:" + err.Error())
  903. return
  904. }
  905. for _, frequency := range frequencies {
  906. //获取数据
  907. dataList, err := data_manage.GetSciIndexDataByCode(indexCode)
  908. if err != nil {
  909. br.Msg = "获取数据失败"
  910. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  911. return
  912. }
  913. if len(dataList) > 0 {
  914. name := this.GetString("IndexName")
  915. unit := this.GetString("UnitName")
  916. modifyTime := this.GetString("ModifyTime")
  917. sheet, err := xlsxFile.AddSheet(*frequency)
  918. if err != nil {
  919. br.Msg = "新增Sheet失败"
  920. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  921. return
  922. }
  923. //获取指标数据
  924. windRow := sheet.AddRow()
  925. windRow.AddCell().SetValue("Sci")
  926. rowSecName := sheet.AddRow()
  927. celSecName := rowSecName.AddCell()
  928. celSecName.SetValue("指标名称")
  929. cellSenName := rowSecName.AddCell()
  930. cellSenName.SetValue(name)
  931. indexCodeRow := sheet.AddRow()
  932. indexCodeRow.AddCell().SetValue("指标ID")
  933. indexCodeRow.AddCell().SetValue(indexCode)
  934. rowFrequency := sheet.AddRow()
  935. celFrequency := rowFrequency.AddCell()
  936. celFrequency.SetValue("频率")
  937. cellFrequency := rowFrequency.AddCell()
  938. cellFrequency.SetValue(frequency)
  939. rowUnit := sheet.AddRow()
  940. celUnit := rowUnit.AddCell()
  941. celUnit.SetValue("单位")
  942. cellUnit := rowUnit.AddCell()
  943. cellUnit.SetValue(unit)
  944. rowModifyDate := sheet.AddRow()
  945. rowModifyCell := rowModifyDate.AddCell()
  946. rowModifyCell.SetValue("更新时间")
  947. rowModifyCell = rowModifyDate.AddCell()
  948. rowModifyCell.SetValue(modifyTime)
  949. dataMax, err := data_manage.GetSciIndexDataCount(indexCode)
  950. if len(dataList) <= 0 {
  951. for n := 0; n < dataMax; n++ {
  952. rowIndex := 6 + n
  953. row := sheet.Row(rowIndex)
  954. row.AddCell()
  955. row.AddCell()
  956. row.AddCell()
  957. }
  958. } else {
  959. endRowIndex := 0
  960. for rk, dv := range dataList {
  961. rowIndex := 6 + rk
  962. row := sheet.Row(rowIndex)
  963. displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
  964. displayDateCell := row.AddCell()
  965. style := new(xlsx.Style)
  966. style.ApplyAlignment = true
  967. style.Alignment.WrapText = true
  968. displayDateCell.SetStyle(style)
  969. displayDateCell.SetDate(displayDate)
  970. row.AddCell().SetValue(dv.Value)
  971. row.AddCell()
  972. endRowIndex = rowIndex
  973. }
  974. if len(dataList) < dataMax {
  975. dataLen := dataMax - len(dataList)
  976. for n := 0; n < dataLen; n++ {
  977. rowIndex := (endRowIndex + 1) + n
  978. row := sheet.Row(rowIndex)
  979. row.AddCell()
  980. row.AddCell()
  981. row.AddCell()
  982. }
  983. }
  984. }
  985. }
  986. }
  987. }
  988. err := xlsxFile.Save(downLoadnFilePath)
  989. if err != nil {
  990. //有指标无数据时先导出一遍空表
  991. sheet, err := xlsxFile.AddSheet("无数据")
  992. if err != nil {
  993. br.Msg = "新增Sheet失败"
  994. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  995. return
  996. }
  997. rowSecName := sheet.AddRow()
  998. celSecName := rowSecName.AddCell()
  999. celSecName.SetValue("")
  1000. err = xlsxFile.Save(downLoadnFilePath)
  1001. if err != nil {
  1002. br.Msg = "保存文件失败"
  1003. br.ErrMsg = "保存文件失败"
  1004. return
  1005. }
  1006. }
  1007. fileName := `Sci数据`
  1008. if len(secNameList) > 0 {
  1009. fileName = secNameList[0].ClassifyName
  1010. }
  1011. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  1012. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  1013. defer func() {
  1014. os.Remove(downLoadnFilePath)
  1015. }()
  1016. br.Ret = 200
  1017. br.Success = true
  1018. br.Msg = "success"
  1019. }