usda_fas_data.go 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  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. condition += ` AND index_code not in (SELECT edb_code FROM edb_info WHERE source=?) `
  478. pars = append(pars, utils.DATA_SOURCE_USDA_FAS)
  479. list, err := data_manage.GetUsdaFasIndexPage(condition, pars, startSize, pageSize)
  480. if err != nil {
  481. br.Msg = "获取失败"
  482. br.ErrMsg = "获取失败,Err:" + err.Error()
  483. return
  484. }
  485. total, err = data_manage.GetUsdaFasIndexPageCount(condition, pars)
  486. if err != nil {
  487. br.Msg = "获取数据失败"
  488. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  489. return
  490. }
  491. page = paging.GetPaging(currentIndex, pageSize, total)
  492. resp.Paging = page
  493. resp.List = list
  494. br.Ret = 200
  495. br.Success = true
  496. br.Msg = "获取成功"
  497. br.Data = resp
  498. }
  499. // UsdaFasBatchAdd
  500. // @Title 美国农业部批量新增
  501. // @Description 美国农业部批量新增
  502. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  503. // @Success 200 string "操作成功"
  504. // @router /usda_fas/batch_add [post]
  505. func (this *BaseFromUsdaFasController) UsdaFasBatchAdd() {
  506. br := new(models.BaseResponse).Init()
  507. defer func() {
  508. if br.ErrMsg == "" {
  509. br.IsSendEmail = false
  510. }
  511. this.Data["json"] = br
  512. this.ServeJSON()
  513. }()
  514. sysUser := this.SysUser
  515. if sysUser == nil {
  516. br.Msg = "请登录"
  517. br.ErrMsg = "请登录,SysUser Is Empty"
  518. br.Ret = 408
  519. return
  520. }
  521. deleteCache := true
  522. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_UsdaFas_" + strconv.Itoa(sysUser.AdminId)
  523. defer func() {
  524. if deleteCache {
  525. _ = utils.Rc.Delete(cacheKey)
  526. }
  527. }()
  528. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  529. deleteCache = false
  530. br.Msg = "系统处理中,请稍后重试!"
  531. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  532. return
  533. }
  534. var req []*data_manage.AddEdbInfoReq
  535. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  536. br.Msg = "参数解析异常!"
  537. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  538. return
  539. }
  540. if len(req) == 0 {
  541. br.Msg = "请选择指标"
  542. return
  543. }
  544. if len(req) > 30 {
  545. br.Msg = "批量添加指标数量不得超过30个"
  546. return
  547. }
  548. for _, v := range req {
  549. v.EdbCode = strings.TrimSpace(v.EdbCode)
  550. if v.EdbCode == "" {
  551. br.Msg = "指标ID不可为空"
  552. return
  553. }
  554. v.EdbName = strings.TrimSpace(v.EdbName)
  555. if v.EdbName == "" {
  556. br.Msg = "请输入指标名称"
  557. return
  558. }
  559. v.Frequency = strings.TrimSpace(v.Frequency)
  560. if v.Frequency == "" {
  561. br.Msg = "请选择频度"
  562. return
  563. }
  564. v.Unit = strings.TrimSpace(v.Unit)
  565. if v.Unit == "" {
  566. br.Msg = "请输入单位"
  567. return
  568. }
  569. if v.ClassifyId <= 0 {
  570. br.Msg = "请选择分类"
  571. return
  572. }
  573. }
  574. // 限定同一时间最多批量新增30个指标
  575. for _, v := range req {
  576. var r data_manage.UsdaFasIndexSource2EdbReq
  577. r.EdbCode = v.EdbCode
  578. r.EdbName = v.EdbName
  579. r.Frequency = v.Frequency
  580. r.Unit = v.Unit
  581. r.ClassifyId = v.ClassifyId
  582. r.AdminId = sysUser.AdminId
  583. r.AdminRealName = sysUser.RealName
  584. edbInfo, e, errMsg, skip := data.UsdaFasIndexSource2Edb(r, this.Lang)
  585. if e != nil {
  586. br.Msg = "操作失败"
  587. if errMsg != "" {
  588. br.Msg = errMsg
  589. }
  590. br.ErrMsg = e.Error()
  591. return
  592. }
  593. if skip {
  594. continue
  595. }
  596. // 试用平台更新用户累计新增指标数
  597. if utils.BusinessCode == utils.BusinessCodeSandbox {
  598. go func() {
  599. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  600. if e != nil {
  601. tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
  602. utils.FileLog.Info(tips)
  603. return
  604. }
  605. if adminItem.DepartmentName != "ETA试用客户" {
  606. return
  607. }
  608. var ur etaTrialService.EtaTrialUserReq
  609. ur.Mobile = adminItem.Mobile
  610. _, _ = etaTrialService.UpdateUserIndexNum(ur)
  611. }()
  612. }
  613. // 新增操作日志
  614. {
  615. edbLog := new(data_manage.EdbInfoLog)
  616. edbLog.EdbInfoId = edbInfo.EdbInfoId
  617. edbLog.SourceName = edbInfo.SourceName
  618. edbLog.Source = edbInfo.Source
  619. edbLog.EdbCode = edbInfo.EdbCode
  620. edbLog.EdbName = edbInfo.EdbName
  621. edbLog.ClassifyId = edbInfo.ClassifyId
  622. edbLog.SysUserId = sysUser.AdminId
  623. edbLog.SysUserRealName = sysUser.RealName
  624. edbLog.CreateTime = time.Now()
  625. edbLog.Content = string(this.Ctx.Input.RequestBody)
  626. edbLog.Status = "新增指标"
  627. edbLog.Method = this.Ctx.Input.URI()
  628. go data_manage.AddEdbInfoLog(edbLog)
  629. }
  630. }
  631. br.Msg = "操作成功"
  632. br.Ret = 200
  633. br.Success = true
  634. br.IsAddLog = true
  635. }
  636. // UsdaFasNameCheck
  637. // @Title 加入指标库的重名检测
  638. // @Description 加入指标库的重名检测
  639. // @Param ClassifyIds query string true "分类id, 多个分类用英文"
  640. // @Param Keyword query string true "关键词, 指标ID/指标名称"
  641. // @Success 200 {object} NameCheckResult
  642. // @router /usda_fas/edb_info/name_check [post]
  643. func (this *BaseFromUsdaFasController) UsdaFasNameCheck() {
  644. br := new(models.BaseResponse).Init()
  645. defer func() {
  646. if br.ErrMsg == "" {
  647. br.IsSendEmail = false
  648. }
  649. this.Data["json"] = br
  650. this.ServeJSON()
  651. }()
  652. sysUser := this.SysUser
  653. if sysUser == nil {
  654. br.Msg = "请登录"
  655. br.ErrMsg = "请登录,SysUser Is Empty"
  656. br.Ret = 408
  657. return
  658. }
  659. var req []*data_manage.NameCheckEdbInfoReq
  660. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  661. br.Msg = "参数解析异常!"
  662. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  663. return
  664. }
  665. if len(req) == 0 {
  666. br.Msg = "请选择指标"
  667. return
  668. }
  669. codeMaxT := 30
  670. codeLen := len(req)
  671. if codeLen > codeMaxT {
  672. br.Msg = "批量添加指标数量不得超过30个"
  673. return
  674. }
  675. indexNames := make([]string, 0)
  676. resp := make([]*data_manage.EdbNameCheckResult, 0)
  677. for _, v := range req {
  678. v.EdbCode = strings.TrimSpace(v.EdbCode)
  679. if v.EdbCode == "" {
  680. br.Msg = "指标ID不可为空"
  681. return
  682. }
  683. v.EdbName = strings.TrimSpace(v.EdbName)
  684. if v.EdbName == "" {
  685. br.Msg = "请输入指标名称"
  686. return
  687. }
  688. indexNames = append(indexNames, v.EdbName)
  689. resp = append(resp, &data_manage.EdbNameCheckResult{
  690. EdbCode: v.EdbCode,
  691. EdbName: v.EdbName,
  692. })
  693. dataItems, err := data_manage.GetEdbDataAllByEdbCode(v.EdbCode, utils.DATA_SOURCE_USDA_FAS, 0, utils.EDB_DATA_LIMIT)
  694. if err != nil && err.Error() != utils.ErrNoRow() {
  695. br.Msg = "获取失败"
  696. br.ErrMsg = "获取钢联已存在信息失败,Err:" + err.Error()
  697. return
  698. }
  699. if len(dataItems) <= 0 {
  700. respItem, err := data.AddEdbData(utils.DATA_SOURCE_USDA_FAS, v.EdbCode, v.Frequency)
  701. if err != nil {
  702. br.Msg = "获取失败"
  703. br.ErrMsg = "获取失败,Err:" + err.Error()
  704. return
  705. }
  706. if respItem.Ret != 200 {
  707. br.Msg = "未搜索到该指标"
  708. br.ErrMsg = respItem.ErrMsg + ";EdbCode:" + v.EdbCode
  709. return
  710. }
  711. }
  712. }
  713. // 重名校验
  714. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  715. if e != nil {
  716. br.Msg = "操作失败"
  717. br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
  718. return
  719. }
  720. nameExists := make(map[string]bool)
  721. for _, v := range edbList {
  722. nameExists[v.EdbName] = true
  723. }
  724. if len(nameExists) > 0 {
  725. for _, v := range resp {
  726. v.Exist = nameExists[v.EdbName]
  727. }
  728. }
  729. br.Data = resp
  730. br.Msg = "校验成功"
  731. br.Ret = 200
  732. br.Success = true
  733. }
  734. // UsdaFasAddCheck
  735. // @Title 加入指标库指标Id检测
  736. // @Description 加入指标库指标Id检测
  737. // @Param request body request.BatchAddCheckReq true "type json string"
  738. // @Success 200 string "操作成功"
  739. // @router /usda_fas/edb_info/add_check [post]
  740. func (c *BaseFromUsdaFasController) UsdaFasAddCheck() {
  741. br := new(models.BaseResponse).Init()
  742. defer func() {
  743. if br.ErrMsg == "" {
  744. br.IsSendEmail = false
  745. }
  746. c.Data["json"] = br
  747. c.ServeJSON()
  748. }()
  749. sysUser := c.SysUser
  750. if sysUser == nil {
  751. br.Msg = "请登录"
  752. br.ErrMsg = "请登录,SysUser Is Empty"
  753. br.Ret = 408
  754. return
  755. }
  756. var req data_manage.BatchAddCheckReq
  757. if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
  758. br.Msg = "参数解析异常!"
  759. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  760. return
  761. }
  762. codeMaxT := 30
  763. codeLen := len(req.IndexCodes)
  764. // 获取指标库已有指标
  765. existsEdb, e := data_manage.GetEdbCodesBySource(utils.DATA_SOURCE_USDA_FAS)
  766. if e != nil {
  767. br.Msg = "获取失败"
  768. br.ErrMsg = "获取自有数据已添加的指标失败, Err: " + e.Error()
  769. return
  770. }
  771. existMap := make(map[string]*data_manage.EdbInfo)
  772. for _, v := range existsEdb {
  773. existMap[v.EdbCode] = v
  774. }
  775. if codeLen == 0 {
  776. br.Msg = "请选择指标"
  777. return
  778. }
  779. if codeLen > codeMaxT {
  780. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMaxT)
  781. return
  782. }
  783. // 查询选中的指标
  784. cond := fmt.Sprintf(` AND index_code IN (%s)`, utils.GetOrmInReplace(codeLen))
  785. pars := make([]interface{}, 0)
  786. pars = append(pars, req.IndexCodes)
  787. list, err := data_manage.GetUsdaFasIndex(cond, pars)
  788. if err != nil {
  789. br.Msg = "获取失败"
  790. br.ErrMsg = "获取钢联已存在信息失败,Err:" + err.Error()
  791. return
  792. }
  793. if len(list) > codeMaxT {
  794. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMaxT)
  795. return
  796. }
  797. resp := make([]*data_manage.BaseFromUsdaFasIndexList, 0)
  798. for _, v := range list {
  799. if edb, ok := existMap[v.IndexCode]; ok {
  800. v.EdbInfoId = edb.EdbInfoId
  801. v.EdbClassifyId = edb.ClassifyId
  802. v.EdbUniqueCode = edb.UniqueCode
  803. v.EdbExist = 1
  804. }
  805. resp = append(resp, v)
  806. }
  807. br.Data = resp
  808. br.Msg = "校验成功"
  809. br.Ret = 200
  810. br.Success = true
  811. }
  812. // UsdaFasEdbInfoAdd
  813. // @Title 新增指标接口
  814. // @Description 新增指标接口
  815. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  816. // @Success Ret=200 保存成功
  817. // @router /usda_fas/edb_info/add [post]
  818. func (this *BaseFromUsdaFasController) UsdaFasEdbInfoAdd() {
  819. br := new(models.BaseResponse).Init()
  820. defer func() {
  821. this.Data["json"] = br
  822. this.ServeJSON()
  823. }()
  824. sysUser := this.SysUser
  825. if sysUser == nil {
  826. br.Msg = "请登录"
  827. br.ErrMsg = "请登录,SysUser Is Empty"
  828. br.Ret = 408
  829. return
  830. }
  831. deleteCache := true
  832. cacheKey := "CACHE_EDB_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  833. defer func() {
  834. if deleteCache {
  835. utils.Rc.Delete(cacheKey)
  836. }
  837. }()
  838. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  839. deleteCache = false
  840. br.Msg = "系统处理中,请稍后重试!"
  841. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  842. return
  843. }
  844. var req data_manage.AddEdbInfoReq
  845. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  846. if err != nil {
  847. br.Msg = "参数解析异常!"
  848. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  849. return
  850. }
  851. req.EdbName = strings.Trim(req.EdbName, " ")
  852. req.EdbCode = strings.Trim(req.EdbCode, " ")
  853. if req.EdbCode == "" {
  854. br.Msg = "指标ID不能为空"
  855. return
  856. }
  857. if req.EdbName == "" {
  858. br.Msg = "指标名称不能为空"
  859. return
  860. }
  861. if req.Frequency == "" {
  862. br.Msg = "频率不能为空"
  863. return
  864. }
  865. if req.Unit == "" {
  866. br.Msg = "单位不能为空"
  867. return
  868. }
  869. if req.ClassifyId <= 0 {
  870. br.Msg = "请选择分类"
  871. return
  872. }
  873. count, err := data_manage.GetUsdaFasIndexDataCount(req.EdbCode)
  874. if err != nil {
  875. br.Msg = "获取失败"
  876. br.ErrMsg = "获取失败,Err:" + err.Error()
  877. return
  878. }
  879. if count == 0 {
  880. br.Msg = "指标不存在"
  881. }
  882. // 指标入库
  883. 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)
  884. if err != nil {
  885. br.Msg = "保存失败"
  886. if errMsg != `` {
  887. br.Msg = errMsg
  888. }
  889. br.ErrMsg = err.Error()
  890. br.IsSendEmail = isSendEmail
  891. return
  892. }
  893. // 试用平台更新用户累计新增指标数
  894. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  895. if e != nil {
  896. br.Msg = "操作失败"
  897. br.ErrMsg = "获取系统用户数据失败,Err:" + e.Error()
  898. return
  899. }
  900. if utils.BusinessCode == utils.BusinessCodeSandbox && adminItem.DepartmentName == "ETA试用客户" {
  901. go func() {
  902. var r etaTrialService.EtaTrialUserReq
  903. r.Mobile = adminItem.Mobile
  904. _, _ = etaTrialService.UpdateUserIndexNum(r)
  905. }()
  906. }
  907. //新增操作日志
  908. {
  909. edbLog := new(data_manage.EdbInfoLog)
  910. edbLog.EdbInfoId = edbInfo.EdbInfoId
  911. edbLog.SourceName = edbInfo.SourceName
  912. edbLog.Source = edbInfo.Source
  913. edbLog.EdbCode = edbInfo.EdbCode
  914. edbLog.EdbName = edbInfo.EdbName
  915. edbLog.ClassifyId = edbInfo.ClassifyId
  916. edbLog.SysUserId = sysUser.AdminId
  917. edbLog.SysUserRealName = sysUser.RealName
  918. edbLog.CreateTime = time.Now()
  919. edbLog.Content = string(this.Ctx.Input.RequestBody)
  920. edbLog.Status = "新增指标"
  921. edbLog.Method = this.Ctx.Input.URI()
  922. go data_manage.AddEdbInfoLog(edbLog)
  923. }
  924. // 更新es
  925. go data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  926. resp := new(data_manage.AddEdbInfoResp)
  927. resp.EdbInfoId = edbInfo.EdbInfoId
  928. resp.UniqueCode = edbInfo.UniqueCode
  929. br.Ret = 200
  930. br.Success = true
  931. br.Msg = "保存成功"
  932. br.Data = resp
  933. br.IsAddLog = true
  934. }
  935. // ExportUsdaFasList
  936. // @Title 导出美国农业部数据
  937. // @Description 导出美国农业部数据
  938. // @Param ClassifyId query int true "关键字搜索"
  939. // @Param IndexCode query string true "指标编码"
  940. // @Success 200 导出成功
  941. // @router /usda_fas/export [get]
  942. func (this *BaseFromUsdaFasController) ExportUsdaFasList() {
  943. br := new(models.BaseResponse).Init()
  944. defer func() {
  945. this.Data["json"] = br
  946. this.ServeJSON()
  947. }()
  948. sysUser := this.SysUser
  949. if sysUser == nil {
  950. br.Msg = "请重新登录"
  951. return
  952. }
  953. classifyId, _ := this.GetInt("ClassifyId")
  954. indexCode := this.GetString("IndexCode")
  955. if classifyId <= 0 && indexCode == "" {
  956. br.Msg = "请选择分类或者指标"
  957. return
  958. }
  959. dir, _ := os.Executable()
  960. exPath := filepath.Dir(dir)
  961. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  962. xlsxFile := xlsx.NewFile()
  963. var condition string
  964. var pars []interface{}
  965. var classifyName string
  966. if classifyId > 0 {
  967. classifyInfo, err := data_manage.GetBaseFromUsdaFasClassifyById(classifyId)
  968. if err != nil {
  969. if err.Error() == utils.ErrNoRow() {
  970. br.Msg = "分类不存在"
  971. return
  972. }
  973. br.Msg = "下载失败"
  974. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  975. return
  976. }
  977. classifyName = classifyInfo.ClassifyName
  978. childClassify, err := data_manage.GetBaseFromUsdaFasClassifyByParentId(classifyId)
  979. if err != nil {
  980. br.Msg = "下载失败"
  981. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  982. return
  983. }
  984. if len(childClassify) > 0 {
  985. condition += `AND classify_id IN (` + utils.GetOrmInReplace(len(childClassify)) + `)`
  986. for _, child := range childClassify {
  987. pars = append(pars, child.ClassifyId)
  988. }
  989. } else {
  990. condition += ` AND classify_id=?`
  991. pars = append(pars, classifyId)
  992. }
  993. }
  994. if indexCode != "" {
  995. condition += ` AND index_code=? `
  996. pars = append(pars, indexCode)
  997. }
  998. indexList, err := data_manage.GetUsdaFasIndex(condition, pars)
  999. if err != nil {
  1000. br.Msg = "下载失败"
  1001. br.ErrMsg = "获取指标失败,Err:" + err.Error()
  1002. fmt.Println("获取数据失败,Err:" + err.Error())
  1003. return
  1004. }
  1005. if len(indexList) <= 0 {
  1006. fmt.Println("indexList 为空")
  1007. br.Ret = 200
  1008. br.Success = true
  1009. br.Msg = "success"
  1010. return
  1011. }
  1012. codeList := make([]string, 0)
  1013. frequenciesMap := make(map[string][]*data_manage.BaseFromUsdaFasIndexList)
  1014. for _, v := range indexList {
  1015. codeList = append(codeList, v.IndexCode)
  1016. frequenciesMap[v.Frequency] = append(frequenciesMap[v.Frequency], v)
  1017. }
  1018. dataListMap := make(map[string][]*data_manage.BaseFromUsdaFasData)
  1019. if len(indexList) > 0 {
  1020. allDataList, e := data_manage.GetUsdaFasIndexDataByCodes(codeList)
  1021. if e != nil {
  1022. br.Msg = "获取数据失败"
  1023. br.ErrMsg = "获取数据失败,Err:" + e.Error()
  1024. return
  1025. }
  1026. for _, v := range allDataList {
  1027. dataListMap[v.IndexCode] = append(dataListMap[v.IndexCode], v)
  1028. }
  1029. }
  1030. // 按照频率分组排序
  1031. frequencies := []string{
  1032. "日度", "周度", "旬度", "月度", "季度", "半年度", "年度",
  1033. }
  1034. for _, frequency := range frequencies {
  1035. //获取指标
  1036. indexCodeList, ok := frequenciesMap[frequency]
  1037. if !ok {
  1038. continue
  1039. }
  1040. if len(indexCodeList) <= 0 {
  1041. fmt.Printf("sheet:%s, 不存在指标", frequency)
  1042. return
  1043. }
  1044. var sheetName string
  1045. switch frequency {
  1046. case "日度":
  1047. sheetName = "日度(Daily)"
  1048. case "周度":
  1049. sheetName = "周度(Weekly)"
  1050. case "旬度":
  1051. sheetName = "旬度(ten-day)"
  1052. case "月度":
  1053. sheetName = "月度(Monthly)"
  1054. case "季度":
  1055. sheetName = "季度(Quarterly)"
  1056. case "半年度":
  1057. sheetName = "半年度(Semi-annual)"
  1058. case "年度":
  1059. sheetName = "年度(Annual)"
  1060. default:
  1061. sheetName = "其他数据"
  1062. }
  1063. sheetNew, err := xlsxFile.AddSheet(sheetName)
  1064. if err != nil {
  1065. fmt.Println("新增Sheet失败", err.Error())
  1066. return
  1067. }
  1068. secNameRow := sheetNew.AddRow()
  1069. frequencyRow := sheetNew.AddRow()
  1070. unitRow := sheetNew.AddRow()
  1071. lastModifyDateRow := sheetNew.AddRow()
  1072. var indexIdList []int
  1073. for _, idx := range frequenciesMap[frequency] {
  1074. indexIdList = append(indexIdList, idx.BaseFromUsdaFasIndexId)
  1075. }
  1076. dataTimeList, err := data_manage.GetUsdaFasDataDataTimeByIndexId(indexIdList)
  1077. if err != nil {
  1078. br.Msg = "下载失败"
  1079. br.ErrMsg = "获取数据时间失败,Err:" + err.Error()
  1080. fmt.Println("获取数据时间失败", err.Error())
  1081. return
  1082. }
  1083. // 添加excel左侧指标日期
  1084. setRowIndex := 4
  1085. for rk, dv := range dataTimeList {
  1086. rowIndex := setRowIndex + rk
  1087. row := sheetNew.Row(rowIndex)
  1088. displayDate, _ := time.Parse(utils.FormatDate, dv)
  1089. displayDateCell := row.AddCell()
  1090. style := new(xlsx.Style)
  1091. style.ApplyAlignment = true
  1092. style.Alignment.WrapText = true
  1093. displayDateCell.SetStyle(style)
  1094. displayDateCell.SetDate(displayDate)
  1095. }
  1096. for k, icl := range indexCodeList {
  1097. // 获取数据
  1098. dataList, ok := dataListMap[icl.IndexCode]
  1099. if !ok {
  1100. continue
  1101. }
  1102. if k == 0 {
  1103. secNameRow.AddCell().SetValue("指标名称/Metric Name")
  1104. frequencyRow.AddCell().SetValue("频度/Frequency")
  1105. unitRow.AddCell().SetValue("单位/Unit")
  1106. lastModifyDateRow.AddCell().SetValue("更新时间/Update Time")
  1107. min := k * 3
  1108. sheetNew.SetColWidth(min, min, 15)
  1109. }
  1110. if len(dataList) == 0 {
  1111. continue
  1112. }
  1113. secNameRow.AddCell().SetValue(icl.IndexName)
  1114. frequencyRow.AddCell().SetValue(icl.Frequency)
  1115. unitRow.AddCell().SetValue(icl.Unit)
  1116. timeDate, err := time.Parse(utils.FormatDateTime, dataList[0].ModifyTime)
  1117. if err != nil {
  1118. continue
  1119. }
  1120. lastModifyDateRow.AddCell().SetValue(timeDate.Format(utils.FormatDate))
  1121. dataInfoMap := make(map[string]*data_manage.BaseFromUsdaFasData)
  1122. for _, v := range dataList {
  1123. dataInfoMap[v.DataTime] = v
  1124. }
  1125. for rk, dtv := range dataTimeList {
  1126. rowIndex := setRowIndex + rk
  1127. row := sheetNew.Row(rowIndex)
  1128. displayDateCell := row.AddCell()
  1129. tmpData, ok := dataInfoMap[dtv]
  1130. if ok {
  1131. displayDateCell.SetValue(tmpData.Value)
  1132. }
  1133. }
  1134. }
  1135. }
  1136. err = xlsxFile.Save(downLoadnFilePath)
  1137. if err != nil {
  1138. //有指标无数据时先导出一遍空表
  1139. sheet, err := xlsxFile.AddSheet("无数据")
  1140. if err != nil {
  1141. br.Msg = "新增Sheet失败"
  1142. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1143. return
  1144. }
  1145. rowSecName := sheet.AddRow()
  1146. celSecName := rowSecName.AddCell()
  1147. celSecName.SetValue("")
  1148. e := xlsxFile.Save(downLoadnFilePath)
  1149. if e != nil {
  1150. br.Msg = "保存文件失败"
  1151. br.ErrMsg = "保存文件失败"
  1152. return
  1153. }
  1154. }
  1155. fileName := classifyName
  1156. if indexCode != "" && len(indexList) == 1 {
  1157. fileName = indexList[0].IndexName
  1158. }
  1159. fileName = strings.Replace(fileName, ": ", "_", -1)
  1160. fileName = strings.Replace(fileName, ", ", "_", -1)
  1161. fileName = strings.Replace(fileName, " ", "_", -1)
  1162. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  1163. fmt.Println(fileName)
  1164. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  1165. defer func() {
  1166. os.Remove(downLoadnFilePath)
  1167. }()
  1168. br.Ret = 200
  1169. br.Success = true
  1170. br.Msg = "success"
  1171. }
  1172. // GetFrequency
  1173. // @Title 美国农业部数据频度
  1174. // @Description 美国农业部数据频度接口
  1175. // @Param ClassifyId query string true "分类Id"
  1176. // @Success 200 {object} data_manage.LzFrequency
  1177. // @router /usda_fas/frequency [get]
  1178. func (this *BaseFromUsdaFasController) GetFrequency() {
  1179. br := new(models.BaseResponse).Init()
  1180. defer func() {
  1181. this.Data["json"] = br
  1182. this.ServeJSON()
  1183. }()
  1184. sysUser := this.SysUser
  1185. if sysUser == nil {
  1186. br.Msg = "请登录"
  1187. br.ErrMsg = "请登录,SysUser Is Empty"
  1188. br.Ret = 408
  1189. return
  1190. }
  1191. classifyId, _ := this.GetInt("ClassifyId")
  1192. if classifyId < 0 {
  1193. br.Msg = "请选择分类"
  1194. br.ErrMsg = "请选择分类"
  1195. return
  1196. }
  1197. frequencyList, err := data_manage.GetUsdaFasFrequencyByClassifyId(classifyId)
  1198. if err != nil {
  1199. br.Msg = "获取频度失败"
  1200. br.ErrMsg = "获取频度失败,Err:" + err.Error()
  1201. return
  1202. }
  1203. br.Ret = 200
  1204. br.Success = true
  1205. br.Msg = "获取成功"
  1206. br.Data = frequencyList
  1207. }
  1208. // BatchAddEdbCheck
  1209. // @Title 新增校验
  1210. // @Description 新增校验
  1211. // @Param request body data_manage.BatchManualEdbReq true "type json string"
  1212. // @Success 200 string "操作成功"
  1213. // @router /usda_fas/batch/add/check [post]
  1214. func (c *BaseFromUsdaFasController) BatchAddEdbCheck() {
  1215. br := new(models.BaseResponse).Init()
  1216. defer func() {
  1217. if br.ErrMsg == "" {
  1218. br.IsSendEmail = false
  1219. }
  1220. c.Data["json"] = br
  1221. c.ServeJSON()
  1222. }()
  1223. sysUser := c.SysUser
  1224. if sysUser == nil {
  1225. br.Msg = "请登录"
  1226. br.ErrMsg = "请登录,SysUser Is Empty"
  1227. br.Ret = 408
  1228. return
  1229. }
  1230. // 最大批量添加的数量
  1231. codeMaxT := 31
  1232. var req data_manage.BatchCheckUsdaFasEdbReq
  1233. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1234. if err != nil {
  1235. br.Msg = "参数错误"
  1236. br.ErrMsg = "参数错误,Err:" + err.Error()
  1237. return
  1238. }
  1239. req.Keyword = strings.TrimSpace(req.Keyword)
  1240. classifyIdStr := req.ClassifyIds
  1241. var list = make([]*data_manage.BaseFromUsdaFasIndexList, 0)
  1242. var condition string
  1243. var pars []interface{}
  1244. if req.ListAll {
  1245. classifyIds := strings.Split(classifyIdStr, ",")
  1246. if len(classifyIds) > 0 && classifyIds[0] != `` {
  1247. condition += " AND classify_id IN (" + utils.GetOrmInReplace(len(classifyIds)) + " ) "
  1248. pars = append(pars, classifyIds)
  1249. }
  1250. keyword := req.Keyword
  1251. if keyword != `` {
  1252. condition += " AND (index_name like ? OR index_code like ?) "
  1253. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  1254. }
  1255. frequencies := req.Frequencies
  1256. if frequencies != "" {
  1257. frequencyList := strings.Split(frequencies, ",")
  1258. condition += " AND frequency IN (" + utils.GetOrmInReplace(len(frequencyList)) + " ) "
  1259. pars = append(pars, frequencyList)
  1260. }
  1261. codes := req.TradeCodeList
  1262. codeList := make([]string, 0)
  1263. if codes != "" {
  1264. codeList = strings.Split(codes, ",")
  1265. }
  1266. if len(codeList) > 0 {
  1267. condition += ` AND index_code not in (` + utils.GetOrmInReplace(len(codeList)) + `) `
  1268. pars = append(pars, codeList)
  1269. }
  1270. } else {
  1271. codes := req.TradeCodeList
  1272. codeList := make([]string, 0)
  1273. if codes != "" {
  1274. codeList = strings.Split(codes, ",")
  1275. }
  1276. if len(codeList) <= 0 {
  1277. br.Msg = "请选择指标"
  1278. br.ErrMsg = "请选择指标"
  1279. return
  1280. }
  1281. // 指标
  1282. condition += ` AND index_code in (` + utils.GetOrmInReplace(len(codeList)) + `) `
  1283. pars = append(pars, codeList)
  1284. }
  1285. condition += ` AND index_code not in (SELECT edb_code FROM edb_info WHERE source=?) `
  1286. pars = append(pars, utils.DATA_SOURCE_USDA_FAS)
  1287. list, err = data_manage.GetUsdaFasIndexPage(condition, pars, 0, codeMaxT)
  1288. if err != nil {
  1289. br.Msg = "获取失败"
  1290. br.ErrMsg = "获取失败,Err:" + err.Error()
  1291. return
  1292. }
  1293. if len(list) >= codeMaxT {
  1294. br.Msg = "批量添加指标数量不得超过30个"
  1295. return
  1296. }
  1297. br.Data = list
  1298. br.Msg = "校验成功"
  1299. br.Ret = 200
  1300. br.Success = true
  1301. }