com_trade.go 7.5 KB

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