com_trade.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package controllers
  2. import (
  3. "eta/eta_hub/models"
  4. "eta/eta_hub/models/data_manage"
  5. "eta/eta_hub/utils"
  6. "fmt"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. "time"
  9. )
  10. // ComTradeCountryList
  11. // @Title UN国家列表
  12. // @Description UN国家列表接口
  13. // @Success 200 {object} []data_manage.ComTradeCountryItem
  14. // @router /com_trade/country/list [get]
  15. func (this *EdbInfoController) ComTradeCountryList() {
  16. br := new(models.BaseResponse).Init()
  17. defer func() {
  18. this.Data["json"] = br
  19. this.ServeJSON()
  20. }()
  21. list, err := data_manage.GetComTradeCountryItemAll()
  22. if err != nil && err.Error() != utils.ErrNoRow() {
  23. br.Msg = "获取失败"
  24. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  25. return
  26. }
  27. br.Ret = 200
  28. br.Msg = "获取成功"
  29. br.Data = list
  30. }
  31. // ComTradeDataList
  32. // @Title UN数据列表
  33. // @Description UN数据列表接口
  34. // @Param PageSize query int true "每页页码数"
  35. // @Param CurrentIndex query int true "当前页码"
  36. // @Param IndexCode query string true "指标code"
  37. // @Param Year query string true "年份"
  38. // @Param Date query string true "日期"
  39. // @Param Flow query string true "贸易流向:X(Export:出口);M(Import:进口)"
  40. // @Param ReporterCode query string true "出口国id"
  41. // @Param PartnerCode query string true "进口国id"
  42. // @Param Partner2Code query string true "第2进口国id"
  43. // @Param DateType query int true "日期类型,0:所有,1:年度,2:月度"
  44. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'data_time':日期"
  45. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  46. // @Success 200 {object} data_manage.ComTradeDataListResp
  47. // @router /com_trade/data/list [get]
  48. func (this *EdbInfoController) ComTradeDataList() {
  49. br := new(models.BaseResponse).Init()
  50. defer func() {
  51. this.Data["json"] = br
  52. this.ServeJSON()
  53. }()
  54. pageSize, _ := this.GetInt("PageSize")
  55. currentIndex, _ := this.GetInt("CurrentIndex")
  56. var startSize int
  57. if pageSize <= 0 {
  58. pageSize = utils.PageSize20
  59. }
  60. if currentIndex <= 0 {
  61. currentIndex = 1
  62. }
  63. startSize = utils.StartIndex(currentIndex, pageSize)
  64. var condition string
  65. var pars []interface{}
  66. // @Param IndexCode query string true "指标code"
  67. indexCode := this.GetString("IndexCode")
  68. if indexCode != `` {
  69. condition += ` AND index_code = ? `
  70. pars = append(pars, indexCode)
  71. }
  72. // @Param Flow query string true "贸易流向:X(Export:出口);M(Import:进口)"
  73. flow := this.GetString("Flow")
  74. if flow != `` {
  75. condition += ` AND flow = ? `
  76. pars = append(pars, flow)
  77. }
  78. // @Param DateType query int true "日期类型,0:所有,1:年度,2:月度"
  79. dateType, _ := this.GetInt("DateType", 0)
  80. if dateType > 0 {
  81. condition += ` AND date_type = ? `
  82. pars = append(pars, dateType)
  83. }
  84. // @Param Date query string true "日期"
  85. date := this.GetString("Date")
  86. if date != `` {
  87. switch dateType {
  88. case 1:
  89. condition += ` AND data_time = ? `
  90. pars = append(pars, date+"-12-31")
  91. case 2:
  92. condition += ` AND data_time = ? `
  93. dateTime, tmpErr := time.ParseInLocation(utils.FormatDate, date+"-01", time.Local)
  94. if tmpErr != nil {
  95. br.Msg = "日期异常"
  96. br.ErrMsg = "日期异常:" + tmpErr.Error()
  97. return
  98. }
  99. pars = append(pars, dateTime.AddDate(0, 1, -1).Format(utils.FormatDate))
  100. }
  101. }
  102. // @Param ReporterCode query string true "出口国id"
  103. reporterCode := this.GetString("ReporterCode")
  104. if reporterCode != `` && reporterCode != `-1` {
  105. condition += ` AND reporter_code = ? `
  106. pars = append(pars, reporterCode)
  107. }
  108. // @Param PartnerCode query string true "进口国id"
  109. partnerCode := this.GetString("PartnerCode")
  110. if partnerCode != `` && partnerCode != `-1` {
  111. condition += ` AND partner_code = ? `
  112. pars = append(pars, partnerCode)
  113. }
  114. // @Param Partner2Code query string true "第2进口国id"
  115. partner2Code := this.GetString("Partner2Code")
  116. if partner2Code != `` && partner2Code != `-1` {
  117. condition += ` AND partner2_code = ? `
  118. pars = append(pars, partner2Code)
  119. }
  120. sortParam := this.GetString("SortParam")
  121. sortType := this.GetString("SortType")
  122. sortStr := ``
  123. if sortParam != `` {
  124. sortStr = fmt.Sprintf("%s %s,modify_time desc ", sortParam, sortType)
  125. } else {
  126. sortStr = " data_time desc,modify_time desc "
  127. }
  128. total, err := data_manage.GetComTradeListCount(condition, pars)
  129. if err != nil && err.Error() != utils.ErrNoRow() {
  130. br.Msg = "获取失败"
  131. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  132. return
  133. }
  134. list, err := data_manage.GetComTradeList(condition, pars, startSize, pageSize, sortStr)
  135. if err != nil && err.Error() != utils.ErrNoRow() {
  136. br.Msg = "获取失败"
  137. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  138. return
  139. }
  140. var lastUpdateTime time.Time
  141. lastUpdateTimeStr := ``
  142. // 获取用到的指标信息
  143. comTradeList, err := data_manage.GetAllComTradeIndexList()
  144. if err != nil && err.Error() != utils.ErrNoRow() {
  145. br.Msg = "获取失败"
  146. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  147. return
  148. }
  149. comTradeMap := make(map[int]*data_manage.ComTradeIndex)
  150. for _, v := range comTradeList {
  151. comTradeMap[v.ComTradeId] = v
  152. if lastUpdateTime.IsZero() || lastUpdateTime.Before(v.UpdateTime) {
  153. lastUpdateTime = v.UpdateTime
  154. lastUpdateTimeStr = lastUpdateTime.Format(utils.FormatDateTime)
  155. }
  156. }
  157. if len(list) > 0 {
  158. // 给指标数据赋值指标名称
  159. for _, v := range list {
  160. if comTradeInfo, ok := comTradeMap[v.ComTradeId]; ok {
  161. v.IndexNameCn = comTradeInfo.IndexNameCn
  162. }
  163. v.Date = fmt.Sprint(v.DataTime.Year())
  164. if v.DateType == 2 {
  165. v.Date = v.DataTime.Format(utils.FormatYearMonthDate)
  166. }
  167. }
  168. }
  169. page := paging.GetPaging(currentIndex, pageSize, total)
  170. resp := data_manage.ComTradeDataListResp{
  171. Paging: page,
  172. List: list,
  173. LastUpdateTime: lastUpdateTimeStr,
  174. }
  175. br.Ret = 200
  176. br.Msg = "获取成功"
  177. br.Data = resp
  178. }
  179. // ComTradeList
  180. // @Title UN指标列表
  181. // @Description UN指标列表接口
  182. // @Param Keyword query string true "关键字搜索"
  183. // @Success 200 {object} []data_manage.ComTradeIndex
  184. // @router /com_trade/list [get]
  185. func (this *EdbInfoController) ComTradeList() {
  186. br := new(models.BaseResponse).Init()
  187. defer func() {
  188. this.Data["json"] = br
  189. this.ServeJSON()
  190. }()
  191. var condition string
  192. var pars []interface{}
  193. // @Param IndexCode query string true "指标code"
  194. keyword := this.GetString("Keyword")
  195. if keyword != `` {
  196. condition += ` AND ( index_name_cn like ? or index_code like ? ) `
  197. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  198. }
  199. list, err := data_manage.GetComTradeIndexList(condition, pars)
  200. if err != nil && err.Error() != utils.ErrNoRow() {
  201. br.Msg = "获取失败"
  202. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  203. return
  204. }
  205. if len(list) <= 0 {
  206. list = make([]*data_manage.ComTradeIndex, 0)
  207. }
  208. br.Ret = 200
  209. br.Msg = "获取成功"
  210. br.Data = list
  211. }