guagnzhouqihuo.go 12 KB

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