business_edb.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_hub/models"
  5. "eta/eta_hub/models/data_manage"
  6. "eta/eta_hub/services/data"
  7. "eta/eta_hub/utils"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. "strings"
  10. )
  11. // Push
  12. // @Title 指标数据
  13. // @Description 指标数据
  14. // @Success 200 {object} data.BaseFromBusinessIndexResp
  15. // @router /push [post]
  16. func (c *EdbController) Push() {
  17. br := new(models.BaseResponse).Init()
  18. defer func() {
  19. c.Data["json"] = br
  20. c.ServeJSON()
  21. }()
  22. body := c.Ctx.Input.RequestBody
  23. var req data_manage.PushBusinessIndexReq
  24. err := json.Unmarshal(body, &req)
  25. if err != nil {
  26. br.Msg = "参数解析异常!"
  27. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  28. return
  29. }
  30. //if req.IndexCode == `` {
  31. // br.Msg = "指标编码不能为空"
  32. // return
  33. //}
  34. if req.IndexName == `` {
  35. br.Msg = "指标名称不能为空"
  36. return
  37. }
  38. if req.Unit == `` {
  39. br.Msg = "指标单位不能为空"
  40. return
  41. }
  42. if req.Frequency == `` {
  43. br.Msg = "指标频度不能为空"
  44. return
  45. }
  46. if req.SourceName == `` {
  47. br.Msg = "数据来源名称不能为空"
  48. return
  49. }
  50. // 兼容频度缺少度的字段
  51. if !strings.Contains(req.Frequency, "度") {
  52. req.Frequency = req.Frequency + "度"
  53. }
  54. if !utils.VerifyFrequency(req.Frequency) {
  55. br.Msg = "指标频度不合法:" + req.Frequency
  56. return
  57. }
  58. // 先过滤掉重复的日期
  59. dataList := make([]data_manage.AddBusinessDataReq, 0)
  60. dateKeyMap := make(map[string]int)
  61. for _, v := range req.DataList {
  62. dataIndex, ok := dateKeyMap[v.Date]
  63. if !ok {
  64. dataList = append(dataList, v)
  65. dateKeyMap[v.Date] = len(dataList) - 1
  66. } else {
  67. dataList[dataIndex] = v
  68. }
  69. }
  70. req.DataList = dataList
  71. reqJson, err := json.Marshal(req)
  72. if err != nil {
  73. br.Msg = "参数解析异常!"
  74. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  75. return
  76. }
  77. respItem, err := data.PushEdb(string(reqJson))
  78. if err != nil {
  79. br.Msg = "处理失败"
  80. br.ErrMsg = "处理失败,Err:" + err.Error()
  81. return
  82. }
  83. if respItem.Ret != 200 {
  84. br.Msg = respItem.Msg
  85. br.ErrMsg = respItem.ErrMsg
  86. return
  87. }
  88. br.Data = respItem.Data
  89. br.Ret = 200
  90. br.Msg = "处理成功"
  91. }
  92. // BusinessEdbList
  93. // @Title 获取自有数据列表
  94. // @Description 获取自有数据列表
  95. // @Success 200 {object} data_manage.BaseFromBusinessIndexResp
  96. // @router /business/edb/list [get]
  97. func (c *EdbController) BusinessEdbList() {
  98. br := new(models.BaseResponse).Init()
  99. defer func() {
  100. c.Data["json"] = br
  101. c.ServeJSON()
  102. }()
  103. pageSize, _ := c.GetInt("PageSize")
  104. currentIndex, _ := c.GetInt("CurrentIndex")
  105. var startSize int
  106. if pageSize <= 0 {
  107. pageSize = utils.PageSize20
  108. }
  109. if currentIndex <= 0 {
  110. currentIndex = 1
  111. }
  112. startSize = utils.StartIndex(currentIndex, pageSize)
  113. //通过指标ID、指标名称、渠道、单位、频度查询匹配自有数据中已有的指标;
  114. indexCode := c.GetString("IndexCode")
  115. indexName := c.GetString("IndexName")
  116. //if indexName == `` && indexCode == `` {
  117. // br.Msg = "IndexCode与IndexName不能同时为空"
  118. // br.ErrMsg = "IndexCode与IndexName不能同时为空"
  119. // return
  120. //}
  121. cond := ``
  122. pars := make([]interface{}, 0)
  123. if indexCode != `` {
  124. cond += ` AND index_code = ?`
  125. pars = append(pars, indexCode)
  126. }
  127. if indexName != `` {
  128. cond += ` AND index_name like ?`
  129. pars = utils.GetLikeKeywordPars(pars, indexName, 1)
  130. }
  131. sourceName := c.GetString("SourceName")
  132. if sourceName != `` {
  133. cond += ` AND source_name = ?`
  134. pars = append(pars, sourceName)
  135. }
  136. unit := c.GetString("Unit")
  137. if unit != `` {
  138. cond += ` AND unit = ?`
  139. pars = append(pars, unit)
  140. }
  141. frequency := c.GetString("Frequency")
  142. if frequency != `` {
  143. cond += ` AND frequency = ?`
  144. pars = append(pars, frequency)
  145. }
  146. edbOb := new(data_manage.BaseFromBusinessIndex)
  147. total, edbList, e := edbOb.GetItemsByCondition(cond, pars, []string{"index_code", "index_name", "frequency", "unit", "source_name"}, "base_from_business_index_id DESC", startSize, pageSize)
  148. if e != nil {
  149. br.Msg = "获取失败"
  150. br.ErrMsg = "EdbList GetItemsByCondition err: " + e.Error()
  151. return
  152. }
  153. page := paging.GetPaging(currentIndex, pageSize, total)
  154. resp := data_manage.BaseFromBusinessIndexResp{
  155. Paging: page,
  156. List: edbList,
  157. }
  158. br.Data = resp
  159. br.Ret = 200
  160. br.Msg = "获取成功"
  161. }
  162. // BusinessEdbDel
  163. // @Title 删除指标数据
  164. // @Description 删除指标数据
  165. // @Success 200 {object} data_manage.EdbInfoItem
  166. // @router /business/edb/del [post]
  167. func (c *EdbController) BusinessEdbDel() {
  168. br := new(models.BaseResponse).Init()
  169. defer func() {
  170. c.Data["json"] = br
  171. c.ServeJSON()
  172. }()
  173. body := c.Ctx.Input.RequestBody
  174. var req data_manage.DelBusinessIndexReq
  175. err := json.Unmarshal(body, &req)
  176. if err != nil {
  177. br.Msg = "参数解析异常!"
  178. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  179. return
  180. }
  181. if len(req.IndexCodeList) < 0 {
  182. br.Msg = "指标编码不允许为空"
  183. return
  184. }
  185. reqJson, err := json.Marshal(req)
  186. if err != nil {
  187. br.Msg = "参数解析异常!"
  188. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  189. return
  190. }
  191. respItem, err := data.DelBusinessIndex(string(reqJson))
  192. if err != nil {
  193. br.Msg = "删除失败"
  194. br.ErrMsg = "删除失败,Err:" + err.Error()
  195. return
  196. }
  197. if respItem.Ret != 200 {
  198. br.Msg = respItem.Msg
  199. br.ErrMsg = respItem.ErrMsg
  200. return
  201. }
  202. br.Data = respItem.Data
  203. br.Ret = 200
  204. br.Msg = "删除成功"
  205. }
  206. // BusinessEdbDataDel
  207. // @Title 删除指标数据
  208. // @Description 删除指标数据
  209. // @Success 200 {object} data_manage.EdbInfoItem
  210. // @router /business/data/del [post]
  211. func (c *EdbController) BusinessEdbDataDel() {
  212. br := new(models.BaseResponse).Init()
  213. defer func() {
  214. c.Data["json"] = br
  215. c.ServeJSON()
  216. }()
  217. body := c.Ctx.Input.RequestBody
  218. var req data_manage.DelBusinessDataReq
  219. err := json.Unmarshal(body, &req)
  220. if err != nil {
  221. br.Msg = "参数解析异常!"
  222. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  223. return
  224. }
  225. if req.IndexCode == `` {
  226. br.Msg = "指标编码不能为空"
  227. return
  228. }
  229. if req.StartDate == `` && req.EndDate == `` {
  230. br.Msg = "开始日期和结束日期不允许同时为空"
  231. return
  232. }
  233. reqJson, err := json.Marshal(req)
  234. if err != nil {
  235. br.Msg = "参数解析异常!"
  236. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  237. return
  238. }
  239. respItem, err := data.DelBusinessIndexData(string(reqJson))
  240. if err != nil {
  241. br.Msg = "删除失败"
  242. br.ErrMsg = "删除失败,Err:" + err.Error()
  243. return
  244. }
  245. if respItem.Ret != 200 {
  246. br.Msg = respItem.Msg
  247. br.ErrMsg = respItem.ErrMsg
  248. return
  249. }
  250. br.Data = respItem.Data
  251. br.Ret = 200
  252. br.Msg = "删除成功"
  253. }