guagnzhouqihuo.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. package data_source
  2. import (
  3. "eta/eta_api/controllers"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_source"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "github.com/tealeg/xlsx"
  9. "os"
  10. "path/filepath"
  11. "strconv"
  12. "time"
  13. )
  14. type DataSourceController struct {
  15. controllers.BaseAuthController
  16. }
  17. // ComTradeCountryList
  18. // @Title 获取广州期货交易所分类
  19. // @Description 获取广州期货交易所分类
  20. // @Success 200 {object} []data_manage.ComTradeCountryItem
  21. // @router /gfex/classify/list [get]
  22. func (this *DataSourceController) GfexClassifyList() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. sysUser := this.SysUser
  29. if sysUser == nil {
  30. br.Msg = "请登录"
  31. br.ErrMsg = "请登录,SysUser Is Empty"
  32. br.Ret = 408
  33. return
  34. }
  35. gzqhObj := new(data_source.BaseFromTradeGuangzhouIndex)
  36. classifyList, err := gzqhObj.GetBaseFromTradeGuangzhouClassifyAll()
  37. if err != nil {
  38. br.Msg = "获取分类失败"
  39. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  40. return
  41. }
  42. classifyMap := make(map[int][]*data_source.BaseFromTradeGuangzhouClassifyView)
  43. for _, v := range classifyList {
  44. if v.ParentId == 0 {
  45. continue
  46. }
  47. if items, ok := classifyMap[v.ParentId]; !ok {
  48. list := make([]*data_source.BaseFromTradeGuangzhouClassifyView, 0)
  49. list = append(list, v)
  50. classifyMap[v.ParentId] = list
  51. } else {
  52. items = append(items, v)
  53. classifyMap[v.ParentId] = items
  54. }
  55. }
  56. classifyItems := make([]*data_source.BaseFromTradeGuangzhouClassifyView, 0)
  57. for _, v := range classifyList {
  58. if v.ParentId == 0 {
  59. v.Children = classifyMap[v.BaseFromTradeGuangzhouClassifyId]
  60. classifyItems = append(classifyItems, v)
  61. }
  62. }
  63. br.Ret = 200
  64. br.Success = true
  65. br.Msg = "获取成功"
  66. br.Data = classifyItems
  67. }
  68. // ComTradeCountryList
  69. // @Title 获取广州期货交易所数据最大日期
  70. // @Description 获取广州期货交易所数据最大日期
  71. // @Success 200 {object} []data_source.ComTradeCountryItem
  72. // @router /gfex/max/date [get]
  73. func (this *DataSourceController) GfexMaxDate() {
  74. br := new(models.BaseResponse).Init()
  75. defer func() {
  76. this.Data["json"] = br
  77. this.ServeJSON()
  78. }()
  79. sysUser := this.SysUser
  80. if sysUser == nil {
  81. br.Msg = "请登录"
  82. br.ErrMsg = "请登录,SysUser Is Empty"
  83. br.Ret = 408
  84. return
  85. }
  86. gzqhObj := new(data_source.BaseFromTradeGuangzhouIndex)
  87. maxDate, err := gzqhObj.GetBaseFromTradeGuangzhouMaxDate()
  88. if err != nil {
  89. br.ErrMsg = "获取最新日期失败,Err:" + err.Error()
  90. br.Msg = "获取最新日期失败"
  91. return
  92. }
  93. br.Ret = 200
  94. br.Success = true
  95. br.Msg = "获取成功"
  96. br.Data = maxDate
  97. }
  98. // ComTradeCountryList
  99. // @Title 获取广州期货交易所-分类下合约
  100. // @Description 获取广州期货交易所-分类下合约
  101. // @Param BaseFromTradeGuangzhouClassifyId query int true "分类id"
  102. // @Param TradeDate query string true "日期"
  103. // @Success 200 {object} []data_source.BaseFromTradeGuangzhouContract
  104. // @router /gfex/contract [get]
  105. func (this *DataSourceController) GfexContract() {
  106. br := new(models.BaseResponse).Init()
  107. defer func() {
  108. this.Data["json"] = br
  109. this.ServeJSON()
  110. }()
  111. sysUser := this.SysUser
  112. if sysUser == nil {
  113. br.Msg = "请登录"
  114. br.ErrMsg = "请登录,SysUser Is Empty"
  115. br.Ret = 408
  116. return
  117. }
  118. baseFromTradeGuangzhouClassifyId, _ := this.GetInt("BaseFromTradeGuangzhouClassifyId", 0)
  119. if baseFromTradeGuangzhouClassifyId <= 0 {
  120. br.Msg = "参数错误"
  121. br.Msg = "分类id错误"
  122. return
  123. }
  124. tradeDate := this.GetString("TradeDate")
  125. if tradeDate == "" {
  126. br.Msg = "参数错误"
  127. br.Msg = "交易日期不能为空"
  128. return
  129. }
  130. gzqhObj := new(data_source.BaseFromTradeGuangzhouIndex)
  131. list, err := gzqhObj.GetBaseFromTradeGuangzhouContract(baseFromTradeGuangzhouClassifyId, tradeDate)
  132. if err != nil {
  133. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  134. br.Msg = "获取数据失败"
  135. return
  136. }
  137. br.Ret = 200
  138. br.Success = true
  139. br.Msg = "获取成功"
  140. br.Data = list
  141. }
  142. // ComTradeCountryList
  143. // @Title 获取广州期货交易所-分类下指标信息
  144. // @Description 获取广州期货交易所-分类下指标信息
  145. // @Param BaseFromTradeGuangzhouClassifyId query int true "分类id"
  146. // @Param BaseFromTradeGuangzhouContractId query int true "合约id"
  147. // @Param TradeDate query string true "日期"
  148. // @Success 200 {object} []data_source.BaseFromTradeGuangzhouIndexView
  149. // @router /gfex/index_data [get]
  150. func (this *DataSourceController) GfexIndexData() {
  151. br := new(models.BaseResponse).Init()
  152. defer func() {
  153. this.Data["json"] = br
  154. this.ServeJSON()
  155. }()
  156. sysUser := this.SysUser
  157. if sysUser == nil {
  158. br.Msg = "请登录"
  159. br.ErrMsg = "请登录,SysUser Is Empty"
  160. br.Ret = 408
  161. return
  162. }
  163. baseFromTradeGuangzhouClassifyId, _ := this.GetInt("BaseFromTradeGuangzhouClassifyId", 0)
  164. if baseFromTradeGuangzhouClassifyId <= 0 {
  165. br.Msg = "参数错误"
  166. br.ErrMsg = "分类id错误"
  167. return
  168. }
  169. tradeDate := this.GetString("TradeDate")
  170. if tradeDate == "" {
  171. br.Msg = "参数错误"
  172. br.ErrMsg = "交易日期不能为空"
  173. return
  174. }
  175. baseFromTradeGuangzhouContractId, err := this.GetInt("BaseFromTradeGuangzhouContractId", 0)
  176. fmt.Println(err)
  177. fmt.Println(baseFromTradeGuangzhouContractId)
  178. var condition string
  179. var pars []interface{}
  180. if baseFromTradeGuangzhouContractId > 0 {
  181. condition += ` b.data_time=? `
  182. pars = append(pars, tradeDate)
  183. condition += ` AND a.base_from_trade_guangzhou_classify_id=? `
  184. pars = append(pars, baseFromTradeGuangzhouClassifyId)
  185. } else {
  186. if baseFromTradeGuangzhouClassifyId == 11 || baseFromTradeGuangzhouClassifyId == 12 { //月度数据处理
  187. td, err := time.Parse(utils.FormatDate, tradeDate)
  188. if err != nil {
  189. br.Msg = "日期格式错误"
  190. br.ErrMsg = "日期格式错误,Err:" + err.Error()
  191. return
  192. }
  193. _, monthEndDay := utils.GetMonthStartAndEnd(strconv.Itoa(td.Year()), strconv.Itoa(int(td.Month())))
  194. condition += ` a.data_time=? `
  195. pars = append(pars, monthEndDay)
  196. } else {
  197. condition += ` a.data_time=? `
  198. pars = append(pars, tradeDate)
  199. }
  200. condition += ` AND b.base_from_trade_guangzhou_classify_id=? `
  201. pars = append(pars, baseFromTradeGuangzhouClassifyId)
  202. }
  203. //if baseFromTradeGuangzhouContractId > 0 {
  204. // condition += ` AND c.base_from_trade_guangzhou_contract_id=? `
  205. // pars = append(pars, baseFromTradeGuangzhouContractId)
  206. //}
  207. gzqhObj := new(data_source.BaseFromTradeGuangzhouIndex)
  208. list, err := gzqhObj.GetBaseFromTradeGuangzhouIndex(condition, pars, baseFromTradeGuangzhouContractId)
  209. if err != nil {
  210. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  211. br.Msg = "获取数据失败"
  212. return
  213. }
  214. br.Ret = 200
  215. br.Success = true
  216. br.Msg = "获取成功"
  217. br.Data = list
  218. }
  219. // @Title 获取广州期货交易所-一次性交割卖方仓单明细
  220. // @Description 获取广州期货交易所-一次性交割卖方仓单明细
  221. // @Param BaseFromTradeGuangzhouIndexId query int true "指标id"
  222. // @Param TradeDate query string true "日期"
  223. // @Success 200 {object} []data_source.BaseFromTradeGuangzhouIndexView
  224. // @router /gfex/index/detail [get]
  225. func (this *DataSourceController) GfexIndexDetail() {
  226. br := new(models.BaseResponse).Init()
  227. defer func() {
  228. this.Data["json"] = br
  229. this.ServeJSON()
  230. }()
  231. sysUser := this.SysUser
  232. if sysUser == nil {
  233. br.Msg = "请登录"
  234. br.ErrMsg = "请登录,SysUser Is Empty"
  235. br.Ret = 408
  236. return
  237. }
  238. baseFromTradeGuangzhouIndexId, _ := this.GetInt("BaseFromTradeGuangzhouIndexId", 0)
  239. if baseFromTradeGuangzhouIndexId < 0 {
  240. br.Msg = "参数错误"
  241. return
  242. }
  243. tradeDate := this.GetString("TradeDate")
  244. if tradeDate == "" {
  245. br.Msg = "参数错误"
  246. br.ErrMsg = "交易日期不能为空"
  247. return
  248. }
  249. var condition string
  250. var pars []interface{}
  251. condition += ` a.base_from_trade_guangzhou_classify_id = ? `
  252. pars = append(pars, baseFromTradeGuangzhouIndexId)
  253. td, err := time.Parse(utils.FormatDate, tradeDate)
  254. if err != nil {
  255. br.Msg = "日期格式错误"
  256. br.ErrMsg = "日期格式错误,Err:" + err.Error()
  257. return
  258. }
  259. _, monthEndDay := utils.GetMonthStartAndEnd(strconv.Itoa(td.Year()), strconv.Itoa(int(td.Month())))
  260. condition += ` AND a.end_date = ? `
  261. pars = append(pars, monthEndDay)
  262. gzqhObj := new(data_source.BaseFromTradeGuangzhouIndex)
  263. list, err := gzqhObj.GetBaseFromTradeGuangzhouIndexDetail(condition, pars)
  264. if err != nil {
  265. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  266. br.Msg = "获取数据失败"
  267. return
  268. }
  269. br.Ret = 200
  270. br.Success = true
  271. br.Msg = "获取成功"
  272. br.Data = list
  273. }
  274. // @Title 导出广州期货交易所-一次性交割卖方仓单明细
  275. // @Description 导出广州期货交易所-一次性交割卖方仓单明细
  276. // @Param BaseFromTradeGuangzhouIndexId query int true "指标id"
  277. // @Param TradeDate query string true "日期"
  278. // @Success 200 导出成功
  279. // @router /gfex/index/detail/export [get]
  280. func (this *DataSourceController) ExportGfexIndexDetail() {
  281. br := new(models.BaseResponse).Init()
  282. defer func() {
  283. this.Data["json"] = br
  284. this.ServeJSON()
  285. }()
  286. sysUser := this.SysUser
  287. if sysUser == nil {
  288. br.Msg = "请重新登录"
  289. return
  290. }
  291. baseFromTradeGuangzhouIndexId, _ := this.GetInt("BaseFromTradeGuangzhouIndexId", 0)
  292. if baseFromTradeGuangzhouIndexId < 0 {
  293. br.Msg = "参数错误"
  294. return
  295. }
  296. tradeDate := this.GetString("TradeDate")
  297. if tradeDate == "" {
  298. br.Msg = "参数错误"
  299. br.ErrMsg = "交易日期不能为空"
  300. return
  301. }
  302. var condition string
  303. var pars []interface{}
  304. condition += ` a.base_from_trade_guangzhou_classify_id = ? `
  305. pars = append(pars, baseFromTradeGuangzhouIndexId)
  306. td, err := time.Parse(utils.FormatDate, tradeDate)
  307. if err != nil {
  308. br.Msg = "日期格式错误"
  309. br.ErrMsg = "日期格式错误,Err:" + err.Error()
  310. return
  311. }
  312. _, monthEndDay := utils.GetMonthStartAndEnd(strconv.Itoa(td.Year()), strconv.Itoa(int(td.Month())))
  313. condition += ` AND a.end_date = ? `
  314. pars = append(pars, monthEndDay)
  315. gzqhObj := new(data_source.BaseFromTradeGuangzhouIndex)
  316. list, err := gzqhObj.GetBaseFromTradeGuangzhouIndexDetail(condition, pars)
  317. if err != nil {
  318. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  319. br.Msg = "获取数据失败"
  320. return
  321. }
  322. dir, _ := os.Executable()
  323. exPath := filepath.Dir(dir)
  324. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  325. xlsxFile := xlsx.NewFile()
  326. sheetNew, err := xlsxFile.AddSheet("详情")
  327. if err != nil {
  328. br.ErrMsg = "导出失败,Err:" + err.Error()
  329. br.Msg = "导出失败"
  330. return
  331. }
  332. titleRow := sheetNew.AddRow()
  333. titleRow.AddCell().SetValue("指标ID")
  334. titleRow.AddCell().SetValue("指标名称")
  335. titleRow.AddCell().SetValue("数值")
  336. titleRow.AddCell().SetValue("单位")
  337. titleRow.AddCell().SetValue("频度")
  338. titleRow.AddCell().SetValue("起始日期")
  339. titleRow.AddCell().SetValue("最新日期")
  340. var indexCode string
  341. for _, sv := range list {
  342. dataRow := sheetNew.AddRow()
  343. dataRow.AddCell().SetValue(sv.IndexCode)
  344. dataRow.AddCell().SetValue(sv.IndexName)
  345. dataRow.AddCell().SetValue(sv.Value)
  346. dataRow.AddCell().SetValue(sv.Unit)
  347. dataRow.AddCell().SetValue(sv.Frequency)
  348. dataRow.AddCell().SetValue(sv.StartDate)
  349. dataRow.AddCell().SetValue(sv.EndDate)
  350. indexCode = sv.IndexCode
  351. }
  352. err = xlsxFile.Save(downLoadnFilePath)
  353. if err != nil {
  354. //有指标无数据时先导出一遍空表
  355. sheet, err := xlsxFile.AddSheet("无数据")
  356. if err != nil {
  357. br.Msg = "新增Sheet失败"
  358. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  359. return
  360. }
  361. rowSecName := sheet.AddRow()
  362. celSecName := rowSecName.AddCell()
  363. celSecName.SetValue("")
  364. err = xlsxFile.Save(downLoadnFilePath)
  365. if err != nil {
  366. br.Msg = "保存文件失败"
  367. br.ErrMsg = "保存文件失败"
  368. return
  369. }
  370. }
  371. fileName := `一次性交割卖方仓单详情`
  372. fileName = indexCode[:6] + fileName + `.xlsx` //文件名称
  373. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  374. defer func() {
  375. os.Remove(downLoadnFilePath)
  376. }()
  377. br.Ret = 200
  378. br.Success = true
  379. br.Msg = "success"
  380. }