com_trade.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package data_manage
  2. import (
  3. "eta/eta_api/models"
  4. "eta/eta_api/models/data_manage"
  5. "eta/eta_api/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. 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. switch sortParam {
  139. case "data_time":
  140. default:
  141. sortParam = " data_time "
  142. }
  143. switch sortType {
  144. case "desc", "asc":
  145. default:
  146. sortType = "desc"
  147. }
  148. sortStr = fmt.Sprintf("%s %s,modify_time desc ", sortParam, sortType)
  149. total, err := data_manage.GetComTradeListCount(condition, pars)
  150. if err != nil && err.Error() != utils.ErrNoRow() {
  151. br.Msg = "获取失败"
  152. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  153. return
  154. }
  155. list, err := data_manage.GetComTradeList(condition, pars, startSize, pageSize, sortStr)
  156. if err != nil && err.Error() != utils.ErrNoRow() {
  157. br.Msg = "获取失败"
  158. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  159. return
  160. }
  161. var lastUpdateTime time.Time
  162. lastUpdateTimeStr := ``
  163. // 获取用到的指标信息
  164. comTradeList, err := data_manage.GetAllComTradeIndexList()
  165. if err != nil && err.Error() != utils.ErrNoRow() {
  166. br.Msg = "获取失败"
  167. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  168. return
  169. }
  170. comTradeMap := make(map[int]*data_manage.ComTradeIndex)
  171. for _, v := range comTradeList {
  172. comTradeMap[v.ComTradeId] = v
  173. if lastUpdateTime.IsZero() || lastUpdateTime.Before(v.UpdateTime) {
  174. lastUpdateTime = v.UpdateTime
  175. lastUpdateTimeStr = lastUpdateTime.Format(utils.FormatDateTime)
  176. }
  177. }
  178. if len(list) > 0 {
  179. // 给指标数据赋值指标名称
  180. for _, v := range list {
  181. if comTradeInfo, ok := comTradeMap[v.ComTradeId]; ok {
  182. v.IndexNameCn = comTradeInfo.IndexNameCn
  183. }
  184. v.Date = fmt.Sprint(v.DataTime.Year())
  185. if v.DateType == 2 {
  186. v.Date = v.DataTime.Format(utils.FormatYearMonthDate)
  187. }
  188. }
  189. }
  190. page := paging.GetPaging(currentIndex, pageSize, total)
  191. resp := data_manage.ComTradeDataListResp{
  192. Paging: page,
  193. List: list,
  194. LastUpdateTime: lastUpdateTimeStr,
  195. }
  196. br.Ret = 200
  197. br.Success = true
  198. br.Msg = "获取成功"
  199. br.Data = resp
  200. }
  201. // ComTradeList
  202. // @Title UN指标列表
  203. // @Description UN指标列表接口
  204. // @Param Keyword query string true "关键字搜索"
  205. // @Success 200 {object} []data_manage.ComTradeIndex
  206. // @router /com_trade/list [get]
  207. func (this *EdbInfoController) ComTradeList() {
  208. br := new(models.BaseResponse).Init()
  209. defer func() {
  210. this.Data["json"] = br
  211. this.ServeJSON()
  212. }()
  213. sysUser := this.SysUser
  214. if sysUser == nil {
  215. br.Msg = "请登录"
  216. br.ErrMsg = "请登录,SysUser Is Empty"
  217. br.Ret = 408
  218. return
  219. }
  220. var condition string
  221. var pars []interface{}
  222. // @Param IndexCode query string true "指标code"
  223. keyword := this.GetString("Keyword")
  224. if keyword != `` {
  225. condition += ` AND ( index_name_cn like ? or index_code like ? ) `
  226. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  227. }
  228. list, err := data_manage.GetComTradeIndexList(condition, pars)
  229. if err != nil && err.Error() != utils.ErrNoRow() {
  230. br.Msg = "获取失败"
  231. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  232. return
  233. }
  234. if len(list) <= 0 {
  235. list = make([]*data_manage.ComTradeIndex, 0)
  236. }
  237. br.Ret = 200
  238. br.Success = true
  239. br.Msg = "获取成功"
  240. br.Data = list
  241. }