usda_fas_data.go 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/data_manage"
  7. "eta/eta_api/models/system"
  8. "eta/eta_api/services/data"
  9. etaTrialService "eta/eta_api/services/eta_trial"
  10. "eta/eta_api/utils"
  11. "fmt"
  12. "os"
  13. "path/filepath"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "github.com/rdlucklib/rdluck_tools/paging"
  18. "github.com/tealeg/xlsx"
  19. )
  20. type BaseFromUsdaFasController struct {
  21. controllers.BaseAuthController
  22. }
  23. // UsdaFasClassify
  24. // @Title 美国农业部数据分类
  25. // @Description 美国农业部数据分类接口
  26. // @Success 200 {object} data_manage.BaseFromUsdaFasClassify
  27. // @router /usda_fas/classify [get]
  28. func (this *BaseFromUsdaFasController) UsdaFasClassify() {
  29. br := new(models.BaseResponse).Init()
  30. defer func() {
  31. this.Data["json"] = br
  32. this.ServeJSON()
  33. }()
  34. sysUser := this.SysUser
  35. if sysUser == nil {
  36. br.Msg = "请登录"
  37. br.ErrMsg = "请登录,SysUser Is Empty"
  38. br.Ret = 408
  39. return
  40. }
  41. classifyAll, err := data_manage.GetAllBaseFromUsdaFasClassify()
  42. if err != nil && err.Error() != utils.ErrNoRow() {
  43. br.Msg = "获取失败"
  44. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  45. return
  46. }
  47. //组装一级分类
  48. rootMap := make(map[int][]*data_manage.BaseFromUsdaFasClassifyItems)
  49. list := make([]*data_manage.BaseFromUsdaFasClassifyItems, 0)
  50. for _, classify := range classifyAll {
  51. classify.UniqueCode = strconv.Itoa(classify.ClassifyId)
  52. if classify.ParentId == 0 {
  53. if _, ok := rootMap[classify.ClassifyId]; !ok {
  54. rootMap[classify.ClassifyId] = make([]*data_manage.BaseFromUsdaFasClassifyItems, 0)
  55. list = append(list, classify)
  56. }
  57. } else {
  58. child, ok := rootMap[classify.ParentId]
  59. if ok {
  60. child = append(child, classify)
  61. rootMap[classify.ParentId] = child
  62. }
  63. }
  64. }
  65. for k, v := range list {
  66. child, ok := rootMap[v.ClassifyId]
  67. if ok {
  68. list[k].Children = child
  69. }
  70. }
  71. //组装二级分类
  72. var ret data_manage.BaseFromUsdaFasClassifyResp
  73. ret.List = list
  74. br.Ret = 200
  75. br.Success = true
  76. br.Msg = "获取成功"
  77. br.Data = ret
  78. }
  79. // UsdaFasIndexData
  80. // @Title 获取美国农业部数据
  81. // @Description 获取美国农业部数据接口
  82. // @Param PageSize query int true "每页数据条数"
  83. // @Param CurrentIndex query int true "当前页页码,从1开始"
  84. // @Param ClassifyId query string true "分类id"
  85. // @Success 200 {object} data_manage.LzFrequency
  86. // @router /usda_fas/index/data [get]
  87. func (this *BaseFromUsdaFasController) UsdaFasIndexData() {
  88. br := new(models.BaseResponse).Init()
  89. defer func() {
  90. this.Data["json"] = br
  91. this.ServeJSON()
  92. }()
  93. sysUser := this.SysUser
  94. if sysUser == nil {
  95. br.Msg = "请登录"
  96. br.ErrMsg = "请登录,SysUser Is Empty"
  97. br.Ret = 408
  98. return
  99. }
  100. pageSize, _ := this.GetInt("PageSize")
  101. currentIndex, _ := this.GetInt("CurrentIndex")
  102. var startSize int
  103. if pageSize <= 0 {
  104. pageSize = utils.PageSize20
  105. }
  106. if currentIndex <= 0 {
  107. currentIndex = 1
  108. }
  109. startSize = utils.StartIndex(currentIndex, pageSize)
  110. classifyId, _ := this.GetInt("ClassifyId")
  111. if classifyId < 0 {
  112. br.Msg = "请选择分类"
  113. br.ErrMsg = "请选择分类"
  114. return
  115. }
  116. // 增加频度请求入参
  117. frequency := this.GetString("Frequency")
  118. //获取指标
  119. var condition string
  120. var pars []interface{}
  121. if classifyId >= 0 {
  122. classifyInfo, err := data_manage.GetBaseFromUsdaFasClassifyById(classifyId)
  123. if err != nil {
  124. if err.Error() == utils.ErrNoRow() {
  125. br.Msg = "分类不存在"
  126. return
  127. }
  128. br.Msg = "获取分类信息失败"
  129. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  130. return
  131. }
  132. if classifyInfo.Level == 2 || classifyInfo.ParentId > 0 {
  133. condition += ` AND classify_id=? `
  134. pars = append(pars, classifyId)
  135. } else if classifyInfo.Level == 1 {
  136. childClassify, err := data_manage.GetBaseFromUsdaFasClassifyByParentId(classifyId)
  137. if err != nil {
  138. br.Msg = "获取分类信息失败"
  139. br.ErrMsg = "获取子分类信息失败,Err:" + err.Error()
  140. return
  141. }
  142. var classifyList []int
  143. for _, v := range childClassify {
  144. classifyList = append(classifyList, v.ClassifyId)
  145. }
  146. condition += ` AND classify_id IN (` + utils.GetOrmInReplace(len(classifyList)) + `) `
  147. pars = append(pars, classifyList)
  148. }
  149. }
  150. if frequency != "" {
  151. condition += ` AND frequency=? `
  152. pars = append(pars, frequency)
  153. }
  154. UsdaFasList, err := data_manage.GetUsdaFasIndex(condition, pars)
  155. if err != nil {
  156. br.Msg = "获取数据失败"
  157. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  158. return
  159. }
  160. edbCodeList := make([]string, 0)
  161. for _, v := range UsdaFasList {
  162. edbCodeList = append(edbCodeList, v.IndexCode)
  163. }
  164. edbInfoMap := make(map[string]*data_manage.EdbInfo)
  165. dataMap := make(map[string][]*data_manage.BaseFromUsdaFasData)
  166. total := 0
  167. if len(edbCodeList) > 0 {
  168. edbInfoList, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_USDA_FAS, edbCodeList)
  169. if err != nil {
  170. br.Msg = "获取数据源失败"
  171. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  172. return
  173. }
  174. for _, v := range edbInfoList {
  175. edbInfoMap[v.EdbCode] = v
  176. }
  177. // 首先对分类下的指标按照日期进行分页,再针对日期,进行排序
  178. dataTimes, err := data_manage.GetUsdaFasIndexDataTimePageByCodes(edbCodeList, startSize, pageSize)
  179. if err != nil {
  180. br.Msg = "获取数据失败"
  181. br.ErrMsg = "获取指标数据日期信息失败,Err:" + err.Error()
  182. return
  183. }
  184. if len(dataTimes) > 0 {
  185. startDate := dataTimes[len(dataTimes)-1]
  186. endDate := dataTimes[0]
  187. // 把截止日往后加1天
  188. endDateT, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  189. endDate = endDateT.AddDate(0, 0, 1).Format(utils.FormatDate)
  190. dataList, e := data_manage.GetUsdaFasIndexDataByDataTime(edbCodeList, startDate, endDate)
  191. if e != nil {
  192. br.Msg = "获取数据失败"
  193. br.ErrMsg = "获取指标数据失败,Err:" + e.Error()
  194. return
  195. }
  196. //将数据按照指标进行分类
  197. for _, v := range dataList {
  198. dataMap[v.IndexCode] = append(dataMap[v.IndexCode], v)
  199. }
  200. }
  201. total, err = data_manage.GetUsdaFasIndexDataTimePageCount(edbCodeList)
  202. if err != nil {
  203. br.Msg = "获取数据失败"
  204. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  205. return
  206. }
  207. }
  208. page := paging.GetPaging(currentIndex, pageSize, total)
  209. resultList := make([]*data_manage.BaseFromUsdaFasIndexList, 0)
  210. for _, v := range UsdaFasList {
  211. product := new(data_manage.BaseFromUsdaFasIndexList)
  212. product.BaseFromUsdaFasIndexId = v.BaseFromUsdaFasIndexId
  213. product.Unit = v.Unit
  214. product.IndexCode = v.IndexCode
  215. product.IndexName = v.IndexName
  216. product.Frequency = v.Frequency
  217. product.ModifyTime = v.ModifyTime
  218. if edb, ok := edbInfoMap[v.IndexCode]; ok {
  219. product.EdbInfoId = edb.EdbInfoId
  220. product.EdbExist = 1
  221. }
  222. /*total, err := data_manage.GetUsdaFasIndexDataCount(v.IndexCode)
  223. if err != nil {
  224. br.Msg = "获取数据失败"
  225. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  226. return
  227. }
  228. page := paging.GetPaging(currentIndex, pageSize, total)
  229. dataList, err := data_manage.GetUsdaFasIndexData(v.IndexCode, startSize, pageSize)
  230. if err != nil {
  231. br.Msg = "获取数据失败"
  232. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  233. return
  234. }*/
  235. dataListTmp, ok := dataMap[v.IndexCode]
  236. if !ok {
  237. dataListTmp = make([]*data_manage.BaseFromUsdaFasData, 0)
  238. }
  239. product.DataList = dataListTmp
  240. product.Paging = page
  241. resultList = append(resultList, product)
  242. }
  243. br.Ret = 200
  244. br.Success = true
  245. br.Msg = "获取成功"
  246. br.Data = resultList
  247. }
  248. // UsdaFasSearchList
  249. // @Title UsdaFas模糊搜索
  250. // @Description UsdaFas模糊搜索
  251. // @Param Keyword query string ture "关键字搜索"
  252. // @Success 200 {object} models.BaseResponse
  253. // @router /usda_fas/search_list [get]
  254. func (this *BaseFromUsdaFasController) UsdaFasSearchList() {
  255. br := new(models.BaseResponse).Init()
  256. defer func() {
  257. this.Data["json"] = br
  258. this.ServeJSON()
  259. }()
  260. sysUser := this.SysUser
  261. if sysUser == nil {
  262. br.Msg = "请重新登录"
  263. return
  264. }
  265. list := make([]*data_manage.BaseFromUsdaFasIndexSearchItem, 0)
  266. var err error
  267. //关键字
  268. keyword := this.GetString("Keyword")
  269. if keyword != "" {
  270. keyWordArr := strings.Split(keyword, " ")
  271. if len(keyWordArr) > 0 {
  272. condition := ""
  273. for _, v := range keyWordArr {
  274. condition += ` AND CONCAT(index_name,index_code) LIKE '%` + v + `%'`
  275. }
  276. list, err = data_manage.GetUsdaFasItemList(condition)
  277. if err != nil {
  278. br.ErrMsg = "获取失败,Err:" + err.Error()
  279. br.Msg = "获取失败"
  280. return
  281. }
  282. }
  283. } else {
  284. // todo es 模糊搜索
  285. list, err = data_manage.GetUsdaFasItemList("")
  286. if err != nil {
  287. br.ErrMsg = "获取失败,Err:" + err.Error()
  288. br.Msg = "获取失败"
  289. return
  290. }
  291. }
  292. classifyIds := make([]int, 0)
  293. for _, v := range list {
  294. classifyIds = append(classifyIds, v.ClassifyId)
  295. }
  296. classifyList, err := data_manage.GetBaseFromUsdaFasClassifyByIds(classifyIds)
  297. if err != nil {
  298. br.Msg = "搜索失败"
  299. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  300. return
  301. }
  302. classifyMap := make(map[int]int)
  303. for _, v := range classifyList {
  304. classifyMap[v.ClassifyId] = v.ParentId
  305. }
  306. for _, v := range list {
  307. v.ParentClassifyId = classifyMap[v.ClassifyId]
  308. }
  309. br.Ret = 200
  310. br.Success = true
  311. br.Msg = "获取成功"
  312. br.Data = list
  313. }
  314. // UsdaFasSingleData
  315. // @Title 获取UsdaFas数据
  316. // @Description 获取UsdaFas单条数据接口
  317. // @Param IndexCode query string true "指标唯一编码"
  318. // @Success 200 {object} models.BaseResponse
  319. // @router /usda_fas/single_data [get]
  320. func (this *BaseFromUsdaFasController) UsdaFasSingleData() {
  321. br := new(models.BaseResponse).Init()
  322. defer func() {
  323. this.Data["json"] = br
  324. this.ServeJSON()
  325. }()
  326. sysUser := this.SysUser
  327. if sysUser == nil {
  328. br.Msg = "请登录"
  329. br.ErrMsg = "请登录,SysUser Is Empty"
  330. br.Ret = 408
  331. return
  332. }
  333. indexCode := this.GetString("IndexCode")
  334. indexInfo, err := data_manage.GetBaseFromUsdaFasIndexByIndexCode(indexCode)
  335. if err != nil {
  336. br.Msg = "获取指标信息失败"
  337. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  338. return
  339. }
  340. dataTmpList, err := data_manage.GetUsdaFasIndexDataByCode(indexCode)
  341. if err != nil {
  342. br.Msg = "获取数据失败"
  343. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  344. return
  345. }
  346. edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_USDA_FAS, indexCode)
  347. if err != nil && err.Error() != utils.ErrNoRow() {
  348. br.Msg = "获取数据源失败"
  349. br.ErrMsg = "获取数据源失败,Err:" + err.Error()
  350. return
  351. }
  352. var ret data_manage.UsdaFasSingleDataResp
  353. var dataList []*data_manage.UsdaFasSingleData
  354. if edbInfo != nil {
  355. ret.EdbInfoId = edbInfo.EdbInfoId
  356. ret.EdbExist = 1
  357. }
  358. ret.ClassifyId = indexInfo.ClassifyId
  359. ret.BaseFromUsdaFasIndexId = indexInfo.BaseFromUsdaFasIndexId
  360. ret.IndexCode = indexInfo.IndexCode
  361. ret.IndexName = indexInfo.IndexName
  362. ret.Frequency = indexInfo.Frequency
  363. ret.CreateTime = indexInfo.CreateTime.Format(utils.FormatDateTime)
  364. ret.ModifyTime = indexInfo.ModifyTime.Format(utils.FormatDateTime)
  365. ret.Unit = indexInfo.Unit
  366. for _, v := range dataTmpList {
  367. tmp := &data_manage.UsdaFasSingleData{
  368. Value: v.Value,
  369. DataTime: v.DataTime,
  370. }
  371. dataList = append(dataList, tmp)
  372. }
  373. ret.Data = dataList
  374. br.Ret = 200
  375. br.Success = true
  376. br.Msg = "获取成功"
  377. br.Data = ret
  378. }
  379. // UsdaFasIndexList
  380. // @Title 美国农业部指标列表
  381. // @Description 美国农业部指标列表
  382. // @Param ClassifyId query int true "分类id"
  383. // @Success 200 {object} data_manage.BaseFromUsdaFasClassifyResp
  384. // @router /usda_fas/classify/index/list [get]
  385. func (this *BaseFromUsdaFasController) UsdaFasIndexList() {
  386. br := new(models.BaseResponse).Init()
  387. defer func() {
  388. this.Data["json"] = br
  389. this.ServeJSON()
  390. }()
  391. classifyId, _ := this.GetInt("ClassifyId", 0)
  392. indexList, err := data_manage.GetUsdaFasIndexByClassifyId(classifyId)
  393. if err != nil && err.Error() != utils.ErrNoRow() {
  394. br.Msg = "获取失败"
  395. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  396. return
  397. }
  398. var ret data_manage.BaseFromUsdaFasClassifyResp
  399. list := make([]*data_manage.BaseFromUsdaFasClassifyItems, 0)
  400. for _, v := range indexList {
  401. classify := new(data_manage.BaseFromUsdaFasClassifyItems)
  402. classify.ClassifyId = classifyId
  403. classify.BaseFromUsdaFasIndexId = v.BaseFromUsdaFasIndexId
  404. classify.IndexCode = v.IndexCode
  405. classify.ClassifyName = v.IndexName
  406. classify.UniqueCode = fmt.Sprintf("%d_%d", classifyId, v.BaseFromUsdaFasIndexId)
  407. list = append(list, classify)
  408. }
  409. ret.List = list
  410. br.Ret = 200
  411. br.Success = true
  412. br.Msg = "获取成功"
  413. br.Data = ret
  414. }
  415. // UsdaFasBatchSearch
  416. // @Title 美国农业部指标查询
  417. // @Description 美国农业部指标查询
  418. // @Param ClassifyIds query string true "分类id, 多个分类用英文"
  419. // @Param Keyword query string true "关键词, 指标ID/指标名称"
  420. // @Success 200 {object} data_manage.LzFrequency
  421. // @router /usda_fas/batch_search [get]
  422. func (this *BaseFromUsdaFasController) UsdaFasBatchSearch() {
  423. br := new(models.BaseResponse).Init()
  424. defer func() {
  425. this.Data["json"] = br
  426. this.ServeJSON()
  427. }()
  428. sysUser := this.SysUser
  429. if sysUser == nil {
  430. br.Msg = "请登录"
  431. br.ErrMsg = "请登录,SysUser Is Empty"
  432. br.Ret = 408
  433. return
  434. }
  435. classifyIdStr := this.GetString("ClassifyIds")
  436. pageSize, _ := this.GetInt("PageSize")
  437. currentIndex, _ := this.GetInt("CurrentIndex")
  438. var startSize int
  439. if pageSize <= 0 {
  440. pageSize = utils.PageSize20
  441. }
  442. if currentIndex <= 0 {
  443. currentIndex = 1
  444. }
  445. startSize = utils.StartIndex(currentIndex, pageSize)
  446. resp := data_manage.BaseFromUsdaFasIndexSearchList{}
  447. total := 0
  448. page := paging.GetPaging(currentIndex, pageSize, total)
  449. var list = make([]*data_manage.BaseFromUsdaFasIndexList, 0)
  450. var condition string
  451. var pars []interface{}
  452. classifyIds := strings.Split(classifyIdStr, ",")
  453. if len(classifyIds) > 0 && classifyIds[0] != `` {
  454. condition += " AND classify_id IN (" + utils.GetOrmInReplace(len(classifyIds)) + " ) "
  455. pars = append(pars, classifyIds)
  456. }
  457. keyword := this.GetString("Keyword")
  458. if keyword != `` {
  459. condition += " AND (index_name like ? OR index_code like ?) "
  460. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  461. }
  462. frequencies := this.GetString("Frequencies")
  463. if frequencies != "" {
  464. frequencyList := strings.Split(frequencies, ",")
  465. condition += " AND frequency IN (" + utils.GetOrmInReplace(len(frequencyList)) + " ) "
  466. pars = append(pars, frequencyList)
  467. }
  468. if classifyIdStr == `` && keyword == `` && frequencies == `` {
  469. resp.Paging = page
  470. resp.List = list
  471. br.Ret = 200
  472. br.Success = true
  473. br.Msg = "获取成功"
  474. br.Data = resp
  475. return
  476. }
  477. list, err := data_manage.GetUsdaFasIndexPage(condition, pars, startSize, pageSize)
  478. if err != nil {
  479. br.Msg = "获取失败"
  480. br.ErrMsg = "获取失败,Err:" + err.Error()
  481. return
  482. }
  483. total, err = data_manage.GetUsdaFasIndexPageCount(condition, pars)
  484. if err != nil {
  485. br.Msg = "获取数据失败"
  486. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  487. return
  488. }
  489. page = paging.GetPaging(currentIndex, pageSize, total)
  490. resp.Paging = page
  491. resp.List = list
  492. br.Ret = 200
  493. br.Success = true
  494. br.Msg = "获取成功"
  495. br.Data = resp
  496. }
  497. // UsdaFasBatchAdd
  498. // @Title 美国农业部批量新增
  499. // @Description 美国农业部批量新增
  500. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  501. // @Success 200 string "操作成功"
  502. // @router /usda_fas/batch_add [post]
  503. func (this *BaseFromUsdaFasController) UsdaFasBatchAdd() {
  504. br := new(models.BaseResponse).Init()
  505. defer func() {
  506. if br.ErrMsg == "" {
  507. br.IsSendEmail = false
  508. }
  509. this.Data["json"] = br
  510. this.ServeJSON()
  511. }()
  512. sysUser := this.SysUser
  513. if sysUser == nil {
  514. br.Msg = "请登录"
  515. br.ErrMsg = "请登录,SysUser Is Empty"
  516. br.Ret = 408
  517. return
  518. }
  519. deleteCache := true
  520. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_UsdaFas_" + strconv.Itoa(sysUser.AdminId)
  521. defer func() {
  522. if deleteCache {
  523. _ = utils.Rc.Delete(cacheKey)
  524. }
  525. }()
  526. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  527. deleteCache = false
  528. br.Msg = "系统处理中,请稍后重试!"
  529. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  530. return
  531. }
  532. var req []*data_manage.AddEdbInfoReq
  533. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  534. br.Msg = "参数解析异常!"
  535. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  536. return
  537. }
  538. if len(req) == 0 {
  539. br.Msg = "请选择指标"
  540. return
  541. }
  542. if len(req) > 30 {
  543. br.Msg = "批量添加指标数量不得超过30个"
  544. return
  545. }
  546. for _, v := range req {
  547. v.EdbCode = strings.TrimSpace(v.EdbCode)
  548. if v.EdbCode == "" {
  549. br.Msg = "指标ID不可为空"
  550. return
  551. }
  552. v.EdbName = strings.TrimSpace(v.EdbName)
  553. if v.EdbName == "" {
  554. br.Msg = "请输入指标名称"
  555. return
  556. }
  557. v.Frequency = strings.TrimSpace(v.Frequency)
  558. if v.Frequency == "" {
  559. br.Msg = "请选择频度"
  560. return
  561. }
  562. v.Unit = strings.TrimSpace(v.Unit)
  563. if v.Unit == "" {
  564. br.Msg = "请输入单位"
  565. return
  566. }
  567. if v.ClassifyId <= 0 {
  568. br.Msg = "请选择分类"
  569. return
  570. }
  571. }
  572. // 限定同一时间最多批量新增30个指标
  573. for _, v := range req {
  574. var r data_manage.UsdaFasIndexSource2EdbReq
  575. r.EdbCode = v.EdbCode
  576. r.EdbName = v.EdbName
  577. r.Frequency = v.Frequency
  578. r.Unit = v.Unit
  579. r.ClassifyId = v.ClassifyId
  580. r.AdminId = sysUser.AdminId
  581. r.AdminRealName = sysUser.RealName
  582. edbInfo, e, errMsg, skip := data.UsdaFasIndexSource2Edb(r, this.Lang)
  583. if e != nil {
  584. br.Msg = "操作失败"
  585. if errMsg != "" {
  586. br.Msg = errMsg
  587. }
  588. br.ErrMsg = e.Error()
  589. return
  590. }
  591. if skip {
  592. continue
  593. }
  594. // 试用平台更新用户累计新增指标数
  595. if utils.BusinessCode == utils.BusinessCodeSandbox {
  596. go func() {
  597. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  598. if e != nil {
  599. tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
  600. utils.FileLog.Info(tips)
  601. return
  602. }
  603. if adminItem.DepartmentName != "ETA试用客户" {
  604. return
  605. }
  606. var ur etaTrialService.EtaTrialUserReq
  607. ur.Mobile = adminItem.Mobile
  608. _, _ = etaTrialService.UpdateUserIndexNum(ur)
  609. }()
  610. }
  611. // 新增操作日志
  612. {
  613. edbLog := new(data_manage.EdbInfoLog)
  614. edbLog.EdbInfoId = edbInfo.EdbInfoId
  615. edbLog.SourceName = edbInfo.SourceName
  616. edbLog.Source = edbInfo.Source
  617. edbLog.EdbCode = edbInfo.EdbCode
  618. edbLog.EdbName = edbInfo.EdbName
  619. edbLog.ClassifyId = edbInfo.ClassifyId
  620. edbLog.SysUserId = sysUser.AdminId
  621. edbLog.SysUserRealName = sysUser.RealName
  622. edbLog.CreateTime = time.Now()
  623. edbLog.Content = string(this.Ctx.Input.RequestBody)
  624. edbLog.Status = "新增指标"
  625. edbLog.Method = this.Ctx.Input.URI()
  626. go data_manage.AddEdbInfoLog(edbLog)
  627. }
  628. }
  629. br.Msg = "操作成功"
  630. br.Ret = 200
  631. br.Success = true
  632. br.IsAddLog = true
  633. }
  634. // UsdaFasNameCheck
  635. // @Title 加入指标库的重名检测
  636. // @Description 加入指标库的重名检测
  637. // @Param ClassifyIds query string true "分类id, 多个分类用英文"
  638. // @Param Keyword query string true "关键词, 指标ID/指标名称"
  639. // @Success 200 {object} NameCheckResult
  640. // @router /usda_fas/edb_info/name_check [post]
  641. func (this *BaseFromUsdaFasController) UsdaFasNameCheck() {
  642. br := new(models.BaseResponse).Init()
  643. defer func() {
  644. if br.ErrMsg == "" {
  645. br.IsSendEmail = false
  646. }
  647. this.Data["json"] = br
  648. this.ServeJSON()
  649. }()
  650. sysUser := this.SysUser
  651. if sysUser == nil {
  652. br.Msg = "请登录"
  653. br.ErrMsg = "请登录,SysUser Is Empty"
  654. br.Ret = 408
  655. return
  656. }
  657. var req []*data_manage.NameCheckEdbInfoReq
  658. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  659. br.Msg = "参数解析异常!"
  660. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  661. return
  662. }
  663. if len(req) == 0 {
  664. br.Msg = "请选择指标"
  665. return
  666. }
  667. codeMaxT := 30
  668. codeLen := len(req)
  669. if codeLen > codeMaxT {
  670. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMaxT)
  671. return
  672. }
  673. indexNames := make([]string, 0)
  674. resp := make([]*data_manage.EdbNameCheckResult, 0)
  675. for _, v := range req {
  676. v.EdbCode = strings.TrimSpace(v.EdbCode)
  677. if v.EdbCode == "" {
  678. br.Msg = "指标ID不可为空"
  679. return
  680. }
  681. v.EdbName = strings.TrimSpace(v.EdbName)
  682. if v.EdbName == "" {
  683. br.Msg = "请输入指标名称"
  684. return
  685. }
  686. indexNames = append(indexNames, v.EdbName)
  687. resp = append(resp, &data_manage.EdbNameCheckResult{
  688. EdbCode: v.EdbCode,
  689. EdbName: v.EdbName,
  690. })
  691. dataItems, err := data_manage.GetEdbDataAllByEdbCode(v.EdbCode, utils.DATA_SOURCE_USDA_FAS, 0, utils.EDB_DATA_LIMIT)
  692. if err != nil && err.Error() != utils.ErrNoRow() {
  693. br.Msg = "获取失败"
  694. br.ErrMsg = "获取钢联已存在信息失败,Err:" + err.Error()
  695. return
  696. }
  697. if len(dataItems) <= 0 {
  698. respItem, err := data.AddEdbData(utils.DATA_SOURCE_USDA_FAS, v.EdbCode, v.Frequency)
  699. if err != nil {
  700. br.Msg = "获取失败"
  701. br.ErrMsg = "获取失败,Err:" + err.Error()
  702. return
  703. }
  704. if respItem.Ret != 200 {
  705. br.Msg = "未搜索到该指标"
  706. br.ErrMsg = respItem.ErrMsg + ";EdbCode:" + v.EdbCode
  707. return
  708. }
  709. }
  710. }
  711. // 重名校验
  712. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  713. if e != nil {
  714. br.Msg = "操作失败"
  715. br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
  716. return
  717. }
  718. nameExists := make(map[string]bool)
  719. for _, v := range edbList {
  720. nameExists[v.EdbName] = true
  721. }
  722. if len(nameExists) > 0 {
  723. for _, v := range resp {
  724. v.Exist = nameExists[v.EdbName]
  725. }
  726. }
  727. br.Data = resp
  728. br.Msg = "校验成功"
  729. br.Ret = 200
  730. br.Success = true
  731. }
  732. // UsdaFasAddCheck
  733. // @Title 加入指标库指标Id检测
  734. // @Description 加入指标库指标Id检测
  735. // @Param request body request.BatchAddCheckReq true "type json string"
  736. // @Success 200 string "操作成功"
  737. // @router /usda_fas/edb_info/add_check [post]
  738. func (c *BaseFromUsdaFasController) UsdaFasAddCheck() {
  739. br := new(models.BaseResponse).Init()
  740. defer func() {
  741. if br.ErrMsg == "" {
  742. br.IsSendEmail = false
  743. }
  744. c.Data["json"] = br
  745. c.ServeJSON()
  746. }()
  747. sysUser := c.SysUser
  748. if sysUser == nil {
  749. br.Msg = "请登录"
  750. br.ErrMsg = "请登录,SysUser Is Empty"
  751. br.Ret = 408
  752. return
  753. }
  754. var req data_manage.BatchAddCheckReq
  755. if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
  756. br.Msg = "参数解析异常!"
  757. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  758. return
  759. }
  760. codeMaxT := 30
  761. codeLen := len(req.IndexCodes)
  762. // 获取指标库已有指标
  763. existsEdb, e := data_manage.GetEdbCodesBySource(utils.DATA_SOURCE_USDA_FAS)
  764. if e != nil {
  765. br.Msg = "获取失败"
  766. br.ErrMsg = "获取自有数据已添加的指标失败, Err: " + e.Error()
  767. return
  768. }
  769. existMap := make(map[string]*data_manage.EdbInfo)
  770. for _, v := range existsEdb {
  771. existMap[v.EdbCode] = v
  772. }
  773. if codeLen == 0 {
  774. br.Msg = "请选择指标"
  775. return
  776. }
  777. if codeLen > codeMaxT {
  778. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMaxT)
  779. return
  780. }
  781. // 查询选中的指标
  782. cond := fmt.Sprintf(` AND index_code IN (%s)`, utils.GetOrmInReplace(codeLen))
  783. pars := make([]interface{}, 0)
  784. pars = append(pars, req.IndexCodes)
  785. list, err := data_manage.GetUsdaFasIndex(cond, pars)
  786. if err != nil {
  787. br.Msg = "获取失败"
  788. br.ErrMsg = "获取钢联已存在信息失败,Err:" + err.Error()
  789. return
  790. }
  791. if len(list) > codeMaxT {
  792. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMaxT)
  793. return
  794. }
  795. resp := make([]*data_manage.BaseFromUsdaFasIndexList, 0)
  796. for _, v := range list {
  797. if edb, ok := existMap[v.IndexCode]; ok {
  798. v.EdbInfoId = edb.EdbInfoId
  799. v.EdbClassifyId = edb.ClassifyId
  800. v.EdbUniqueCode = edb.UniqueCode
  801. v.EdbExist = 1
  802. }
  803. resp = append(resp, v)
  804. }
  805. br.Data = resp
  806. br.Msg = "校验成功"
  807. br.Ret = 200
  808. br.Success = true
  809. }
  810. // UsdaFasEdbInfoAdd
  811. // @Title 新增指标接口
  812. // @Description 新增指标接口
  813. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  814. // @Success Ret=200 保存成功
  815. // @router /usda_fas/edb_info/add [post]
  816. func (this *BaseFromUsdaFasController) UsdaFasEdbInfoAdd() {
  817. br := new(models.BaseResponse).Init()
  818. defer func() {
  819. this.Data["json"] = br
  820. this.ServeJSON()
  821. }()
  822. sysUser := this.SysUser
  823. if sysUser == nil {
  824. br.Msg = "请登录"
  825. br.ErrMsg = "请登录,SysUser Is Empty"
  826. br.Ret = 408
  827. return
  828. }
  829. deleteCache := true
  830. cacheKey := "CACHE_EDB_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  831. defer func() {
  832. if deleteCache {
  833. utils.Rc.Delete(cacheKey)
  834. }
  835. }()
  836. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  837. deleteCache = false
  838. br.Msg = "系统处理中,请稍后重试!"
  839. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  840. return
  841. }
  842. var req data_manage.AddEdbInfoReq
  843. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  844. if err != nil {
  845. br.Msg = "参数解析异常!"
  846. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  847. return
  848. }
  849. req.EdbName = strings.Trim(req.EdbName, " ")
  850. req.EdbCode = strings.Trim(req.EdbCode, " ")
  851. if req.EdbCode == "" {
  852. br.Msg = "指标ID不能为空"
  853. return
  854. }
  855. if req.EdbName == "" {
  856. br.Msg = "指标名称不能为空"
  857. return
  858. }
  859. if req.Frequency == "" {
  860. br.Msg = "频率不能为空"
  861. return
  862. }
  863. if req.Unit == "" {
  864. br.Msg = "单位不能为空"
  865. return
  866. }
  867. if req.ClassifyId <= 0 {
  868. br.Msg = "请选择分类"
  869. return
  870. }
  871. count, err := data_manage.GetUsdaFasIndexDataCount(req.EdbCode)
  872. if err != nil {
  873. br.Msg = "获取失败"
  874. br.ErrMsg = "获取失败,Err:" + err.Error()
  875. return
  876. }
  877. if count == 0 {
  878. br.Msg = "指标不存在"
  879. }
  880. // 指标入库
  881. edbInfo, err, errMsg, isSendEmail := data.EdbInfoAdd(utils.DATA_SOURCE_USDA_FAS, utils.DATA_SUB_SOURCE_EDB, req.ClassifyId, req.EdbCode, req.EdbName, req.Frequency, req.Unit, req.StartDate, req.EndDate, sysUser.AdminId, sysUser.RealName, this.Lang)
  882. if err != nil {
  883. br.Msg = "保存失败"
  884. if errMsg != `` {
  885. br.Msg = errMsg
  886. }
  887. br.ErrMsg = err.Error()
  888. br.IsSendEmail = isSendEmail
  889. return
  890. }
  891. // 试用平台更新用户累计新增指标数
  892. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  893. if e != nil {
  894. br.Msg = "操作失败"
  895. br.ErrMsg = "获取系统用户数据失败,Err:" + e.Error()
  896. return
  897. }
  898. if utils.BusinessCode == utils.BusinessCodeSandbox && adminItem.DepartmentName == "ETA试用客户" {
  899. go func() {
  900. var r etaTrialService.EtaTrialUserReq
  901. r.Mobile = adminItem.Mobile
  902. _, _ = etaTrialService.UpdateUserIndexNum(r)
  903. }()
  904. }
  905. //新增操作日志
  906. {
  907. edbLog := new(data_manage.EdbInfoLog)
  908. edbLog.EdbInfoId = edbInfo.EdbInfoId
  909. edbLog.SourceName = edbInfo.SourceName
  910. edbLog.Source = edbInfo.Source
  911. edbLog.EdbCode = edbInfo.EdbCode
  912. edbLog.EdbName = edbInfo.EdbName
  913. edbLog.ClassifyId = edbInfo.ClassifyId
  914. edbLog.SysUserId = sysUser.AdminId
  915. edbLog.SysUserRealName = sysUser.RealName
  916. edbLog.CreateTime = time.Now()
  917. edbLog.Content = string(this.Ctx.Input.RequestBody)
  918. edbLog.Status = "新增指标"
  919. edbLog.Method = this.Ctx.Input.URI()
  920. go data_manage.AddEdbInfoLog(edbLog)
  921. }
  922. // 更新es
  923. go data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  924. resp := new(data_manage.AddEdbInfoResp)
  925. resp.EdbInfoId = edbInfo.EdbInfoId
  926. resp.UniqueCode = edbInfo.UniqueCode
  927. br.Ret = 200
  928. br.Success = true
  929. br.Msg = "保存成功"
  930. br.Data = resp
  931. br.IsAddLog = true
  932. }
  933. // ExportUsdaFasList
  934. // @Title 导出美国农业部数据
  935. // @Description 导出美国农业部数据
  936. // @Param ClassifyId query int true "关键字搜索"
  937. // @Param IndexCode query string true "指标编码"
  938. // @Success 200 导出成功
  939. // @router /usda_fas/export [get]
  940. func (this *BaseFromUsdaFasController) ExportUsdaFasList() {
  941. br := new(models.BaseResponse).Init()
  942. defer func() {
  943. this.Data["json"] = br
  944. this.ServeJSON()
  945. }()
  946. sysUser := this.SysUser
  947. if sysUser == nil {
  948. br.Msg = "请重新登录"
  949. return
  950. }
  951. classifyId, _ := this.GetInt("ClassifyId")
  952. indexCode := this.GetString("IndexCode")
  953. if classifyId < 0 {
  954. br.Msg = "请选择分类"
  955. return
  956. }
  957. dir, _ := os.Executable()
  958. exPath := filepath.Dir(dir)
  959. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  960. xlsxFile := xlsx.NewFile()
  961. var condition string
  962. var pars []interface{}
  963. var classifyName string
  964. if classifyId > 0 {
  965. classifyInfo, err := data_manage.GetBaseFromUsdaFasClassifyById(classifyId)
  966. if err != nil {
  967. if err.Error() == utils.ErrNoRow() {
  968. br.Msg = "分类不存在"
  969. return
  970. }
  971. br.Msg = "下载失败"
  972. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  973. return
  974. }
  975. classifyName = classifyInfo.ClassifyName
  976. childClassify, err := data_manage.GetBaseFromUsdaFasClassifyByParentId(classifyId)
  977. if err != nil {
  978. br.Msg = "下载失败"
  979. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  980. return
  981. }
  982. if len(childClassify) > 0 {
  983. condition += `AND classify_id IN (` + utils.GetOrmInReplace(len(childClassify)) + `)`
  984. for _, child := range childClassify {
  985. pars = append(pars, child.ClassifyId)
  986. }
  987. } else {
  988. condition += ` AND classify_id=?`
  989. pars = append(pars, classifyId)
  990. }
  991. } else {
  992. condition += ` AND classify_id=?`
  993. pars = append(pars, classifyId)
  994. }
  995. if indexCode != "" {
  996. condition += ` AND index_code=? `
  997. pars = append(pars, indexCode)
  998. }
  999. indexList, err := data_manage.GetUsdaFasIndex(condition, pars)
  1000. if err != nil {
  1001. br.Msg = "下载失败"
  1002. br.ErrMsg = "获取指标失败,Err:" + err.Error()
  1003. fmt.Println("获取数据失败,Err:" + err.Error())
  1004. return
  1005. }
  1006. if len(indexList) <= 0 {
  1007. fmt.Println("indexList 为空")
  1008. br.Ret = 200
  1009. br.Success = true
  1010. br.Msg = "success"
  1011. return
  1012. }
  1013. codeList := make([]string, 0)
  1014. frequenciesMap := make(map[string][]*data_manage.BaseFromUsdaFasIndexList)
  1015. for _, v := range indexList {
  1016. codeList = append(codeList, v.IndexCode)
  1017. frequenciesMap[v.Frequency] = append(frequenciesMap[v.Frequency], v)
  1018. }
  1019. dataListMap := make(map[string][]*data_manage.BaseFromUsdaFasData)
  1020. if len(indexList) > 0 {
  1021. allDataList, e := data_manage.GetUsdaFasIndexDataByCodes(codeList)
  1022. if e != nil {
  1023. br.Msg = "获取数据失败"
  1024. br.ErrMsg = "获取数据失败,Err:" + e.Error()
  1025. return
  1026. }
  1027. for _, v := range allDataList {
  1028. dataListMap[v.IndexCode] = append(dataListMap[v.IndexCode], v)
  1029. }
  1030. }
  1031. // 按照频率分组排序
  1032. frequencies := []string{
  1033. "日度", "周度", "旬度", "月度", "季度", "半年度", "年度",
  1034. }
  1035. for _, frequency := range frequencies {
  1036. //获取指标
  1037. indexCodeList, ok := frequenciesMap[frequency]
  1038. if !ok {
  1039. continue
  1040. }
  1041. if len(indexCodeList) <= 0 {
  1042. fmt.Printf("sheet:%s, 不存在指标", frequency)
  1043. return
  1044. }
  1045. var sheetName string
  1046. switch frequency {
  1047. case "日度":
  1048. sheetName = "日度(Daily)"
  1049. case "周度":
  1050. sheetName = "周度(Weekly)"
  1051. case "旬度":
  1052. sheetName = "旬度(ten-day)"
  1053. case "月度":
  1054. sheetName = "月度(Monthly)"
  1055. case "季度":
  1056. sheetName = "季度(Quarterly)"
  1057. case "半年度":
  1058. sheetName = "半年度(Semi-annual)"
  1059. case "年度":
  1060. sheetName = "年度(Annual)"
  1061. default:
  1062. sheetName = "其他数据"
  1063. }
  1064. sheetNew, err := xlsxFile.AddSheet(sheetName)
  1065. if err != nil {
  1066. fmt.Println("新增Sheet失败", err.Error())
  1067. return
  1068. }
  1069. secNameRow := sheetNew.AddRow()
  1070. frequencyRow := sheetNew.AddRow()
  1071. unitRow := sheetNew.AddRow()
  1072. lastModifyDateRow := sheetNew.AddRow()
  1073. var indexIdList []int
  1074. for _, idx := range frequenciesMap[frequency] {
  1075. indexIdList = append(indexIdList, idx.BaseFromUsdaFasIndexId)
  1076. }
  1077. dataTimeList, err := data_manage.GetUsdaFasDataDataTimeByIndexId(indexIdList)
  1078. if err != nil {
  1079. br.Msg = "下载失败"
  1080. br.ErrMsg = "获取数据时间失败,Err:" + err.Error()
  1081. fmt.Println("获取数据时间失败", err.Error())
  1082. return
  1083. }
  1084. // 添加excel左侧指标日期
  1085. setRowIndex := 4
  1086. for rk, dv := range dataTimeList {
  1087. rowIndex := setRowIndex + rk
  1088. row := sheetNew.Row(rowIndex)
  1089. displayDate, _ := time.Parse(utils.FormatDate, dv)
  1090. displayDateCell := row.AddCell()
  1091. style := new(xlsx.Style)
  1092. style.ApplyAlignment = true
  1093. style.Alignment.WrapText = true
  1094. displayDateCell.SetStyle(style)
  1095. displayDateCell.SetDate(displayDate)
  1096. }
  1097. for k, icl := range indexCodeList {
  1098. // 获取数据
  1099. dataList, ok := dataListMap[icl.IndexCode]
  1100. if !ok {
  1101. continue
  1102. }
  1103. if k == 0 {
  1104. secNameRow.AddCell().SetValue("指标名称/Metric Name")
  1105. frequencyRow.AddCell().SetValue("频度/Frequency")
  1106. unitRow.AddCell().SetValue("单位/Unit")
  1107. lastModifyDateRow.AddCell().SetValue("更新时间/Update Time")
  1108. min := k * 3
  1109. sheetNew.SetColWidth(min, min, 15)
  1110. }
  1111. if len(dataList) == 0 {
  1112. continue
  1113. }
  1114. secNameRow.AddCell().SetValue(icl.IndexName)
  1115. frequencyRow.AddCell().SetValue(icl.Frequency)
  1116. unitRow.AddCell().SetValue(icl.Unit)
  1117. timeDate, err := time.Parse(utils.FormatDateTime, dataList[0].ModifyTime)
  1118. if err != nil {
  1119. continue
  1120. }
  1121. lastModifyDateRow.AddCell().SetValue(timeDate.Format(utils.FormatDate))
  1122. dataInfoMap := make(map[string]*data_manage.BaseFromUsdaFasData)
  1123. for _, v := range dataList {
  1124. dataInfoMap[v.DataTime] = v
  1125. }
  1126. for rk, dtv := range dataTimeList {
  1127. rowIndex := setRowIndex + rk
  1128. row := sheetNew.Row(rowIndex)
  1129. displayDateCell := row.AddCell()
  1130. tmpData, ok := dataInfoMap[dtv]
  1131. if ok {
  1132. displayDateCell.SetValue(tmpData.Value)
  1133. }
  1134. }
  1135. }
  1136. }
  1137. err = xlsxFile.Save(downLoadnFilePath)
  1138. if err != nil {
  1139. //有指标无数据时先导出一遍空表
  1140. sheet, err := xlsxFile.AddSheet("无数据")
  1141. if err != nil {
  1142. br.Msg = "新增Sheet失败"
  1143. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1144. return
  1145. }
  1146. rowSecName := sheet.AddRow()
  1147. celSecName := rowSecName.AddCell()
  1148. celSecName.SetValue("")
  1149. e := xlsxFile.Save(downLoadnFilePath)
  1150. if e != nil {
  1151. br.Msg = "保存文件失败"
  1152. br.ErrMsg = "保存文件失败"
  1153. return
  1154. }
  1155. }
  1156. fileName := classifyName
  1157. if indexCode != "" && len(indexList) == 1 {
  1158. fileName = indexList[0].IndexName
  1159. }
  1160. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  1161. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  1162. defer func() {
  1163. os.Remove(downLoadnFilePath)
  1164. }()
  1165. br.Ret = 200
  1166. br.Success = true
  1167. br.Msg = "success"
  1168. }
  1169. // GetFrequency
  1170. // @Title 美国农业部数据频度
  1171. // @Description 美国农业部数据频度接口
  1172. // @Param ClassifyId query string true "分类Id"
  1173. // @Success 200 {object} data_manage.LzFrequency
  1174. // @router /usda_fas/frequency [get]
  1175. func (this *BaseFromUsdaFasController) GetFrequency() {
  1176. br := new(models.BaseResponse).Init()
  1177. defer func() {
  1178. this.Data["json"] = br
  1179. this.ServeJSON()
  1180. }()
  1181. sysUser := this.SysUser
  1182. if sysUser == nil {
  1183. br.Msg = "请登录"
  1184. br.ErrMsg = "请登录,SysUser Is Empty"
  1185. br.Ret = 408
  1186. return
  1187. }
  1188. classifyId, _ := this.GetInt("ClassifyId")
  1189. if classifyId < 0 {
  1190. br.Msg = "请选择分类"
  1191. br.ErrMsg = "请选择分类"
  1192. return
  1193. }
  1194. frequencyList, err := data_manage.GetUsdaFasFrequencyByClassifyId(classifyId)
  1195. if err != nil {
  1196. br.Msg = "获取频度失败"
  1197. br.ErrMsg = "获取频度失败,Err:" + err.Error()
  1198. return
  1199. }
  1200. br.Ret = 200
  1201. br.Success = true
  1202. br.Msg = "获取成功"
  1203. br.Data = frequencyList
  1204. }
  1205. // BatchAddEdbCheck
  1206. // @Title 新增校验
  1207. // @Description 新增校验
  1208. // @Param request body data_manage.BatchManualEdbReq true "type json string"
  1209. // @Success 200 string "操作成功"
  1210. // @router /usda_fas/batch/add/check [post]
  1211. func (c *BaseFromUsdaFasController) BatchAddEdbCheck() {
  1212. br := new(models.BaseResponse).Init()
  1213. defer func() {
  1214. if br.ErrMsg == "" {
  1215. br.IsSendEmail = false
  1216. }
  1217. c.Data["json"] = br
  1218. c.ServeJSON()
  1219. }()
  1220. sysUser := c.SysUser
  1221. if sysUser == nil {
  1222. br.Msg = "请登录"
  1223. br.ErrMsg = "请登录,SysUser Is Empty"
  1224. br.Ret = 408
  1225. return
  1226. }
  1227. // 最大批量添加的数量
  1228. codeMaxT := 31
  1229. var req data_manage.BatchCheckUsdaFasEdbReq
  1230. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1231. if err != nil {
  1232. br.Msg = "参数错误"
  1233. br.ErrMsg = "参数错误,Err:" + err.Error()
  1234. return
  1235. }
  1236. req.Keyword = strings.TrimSpace(req.Keyword)
  1237. classifyIdStr := req.ClassifyIds
  1238. var list = make([]*data_manage.BaseFromUsdaFasIndexList, 0)
  1239. var condition string
  1240. var pars []interface{}
  1241. if req.ListAll {
  1242. classifyIds := strings.Split(classifyIdStr, ",")
  1243. if len(classifyIds) > 0 && classifyIds[0] != `` {
  1244. condition += " AND classify_id IN (" + utils.GetOrmInReplace(len(classifyIds)) + " ) "
  1245. pars = append(pars, classifyIds)
  1246. }
  1247. keyword := req.Keyword
  1248. if keyword != `` {
  1249. condition += " AND (index_name like ? OR index_code like ?) "
  1250. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  1251. }
  1252. frequencies := req.Frequencies
  1253. if frequencies != "" {
  1254. frequencyList := strings.Split(frequencies, ",")
  1255. condition += " AND frequency IN (" + utils.GetOrmInReplace(len(frequencyList)) + " ) "
  1256. pars = append(pars, frequencyList)
  1257. }
  1258. codes := req.TradeCodeList
  1259. codeList := make([]string, 0)
  1260. if codes != "" {
  1261. codeList = strings.Split(codes, ",")
  1262. }
  1263. if len(codeList) > 0 {
  1264. condition += ` AND index_code not in (` + utils.GetOrmInReplace(len(codeList)) + `) `
  1265. pars = append(pars, codeList)
  1266. }
  1267. } else {
  1268. codes := req.TradeCodeList
  1269. codeList := make([]string, 0)
  1270. if codes != "" {
  1271. codeList = strings.Split(codes, ",")
  1272. }
  1273. if len(codeList) <= 0 {
  1274. br.Msg = "请选择指标"
  1275. br.ErrMsg = "请选择指标"
  1276. return
  1277. }
  1278. // 指标
  1279. condition += ` AND index_code in (` + utils.GetOrmInReplace(len(codeList)) + `) `
  1280. pars = append(pars, codeList)
  1281. }
  1282. condition += ` AND index_code not in (SELECT edb_code FROM edb_info WHERE source=?) `
  1283. pars = append(pars, utils.DATA_SOURCE_USDA_FAS)
  1284. list, err = data_manage.GetUsdaFasIndexPage(condition, pars, 0, codeMaxT)
  1285. if err != nil {
  1286. br.Msg = "获取失败"
  1287. br.ErrMsg = "获取失败,Err:" + err.Error()
  1288. return
  1289. }
  1290. if len(list) >= codeMaxT {
  1291. br.Msg = "最多只能添加" + strconv.Itoa(codeMaxT) + "条数据"
  1292. return
  1293. }
  1294. br.Data = list
  1295. br.Msg = "校验成功"
  1296. br.Ret = 200
  1297. br.Success = true
  1298. }