sci_data.go 29 KB

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