edb_collect.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/controllers"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/data_manage"
  7. "eta_gn/eta_api/services/data"
  8. "eta_gn/eta_api/utils"
  9. "fmt"
  10. "github.com/rdlucklib/rdluck_tools/paging"
  11. "sort"
  12. "strings"
  13. "time"
  14. )
  15. type EdbCollectController struct {
  16. controllers.BaseAuthController
  17. }
  18. // @router /edb_collect/list [get]
  19. func (this *EdbCollectController) List() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. if br.ErrMsg == "" {
  23. br.IsSendEmail = false
  24. }
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. sysUser := this.SysUser
  29. if sysUser == nil {
  30. br.Msg = "请登录"
  31. br.ErrMsg = "请登录,SysUser Is Empty"
  32. br.Ret = 408
  33. return
  34. }
  35. resp := new(data_manage.CollectEdbInfoListResp)
  36. resp.List = make([]*data_manage.CollectEdbInfoItem, 0)
  37. pageSize, _ := this.GetInt("PageSize")
  38. currentIndex, _ := this.GetInt("CurrentIndex")
  39. var total int
  40. page := paging.GetPaging(currentIndex, pageSize, total)
  41. var startSize int
  42. if pageSize <= 0 {
  43. pageSize = utils.PageSize20
  44. }
  45. if currentIndex <= 0 {
  46. currentIndex = 1
  47. }
  48. startSize = paging.StartIndex(currentIndex, pageSize)
  49. var cond string
  50. var pars []interface{}
  51. cond += ` AND a.sys_user_id = ?`
  52. pars = append(pars, sysUser.AdminId)
  53. classifyId, _ := this.GetInt("ClassifyId")
  54. if classifyId > 0 {
  55. classifyIds, e := data.GetEdbCollectClassifyChildIds(classifyId, sysUser.AdminId, true)
  56. if e != nil {
  57. br.Msg = "获取失败"
  58. br.ErrMsg = fmt.Sprintf("获取子分类IDs失败, %v", e)
  59. return
  60. }
  61. if len(classifyIds) == 0 {
  62. resp.Paging = page
  63. br.Data = resp
  64. br.Ret = 200
  65. br.Success = true
  66. br.Msg = "获取成功"
  67. return
  68. }
  69. cond += fmt.Sprintf(` AND a.edb_collect_classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds)))
  70. pars = append(pars, classifyIds)
  71. }
  72. {
  73. obj := data_manage.EdbInfoNoPermissionAdmin{}
  74. list, e := obj.GetAllListByAdminId(sysUser.AdminId)
  75. if e != nil {
  76. br.Msg = "获取失败"
  77. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + e.Error()
  78. return
  79. }
  80. var edbIds []int
  81. for _, v := range list {
  82. edbIds = append(edbIds, v.EdbInfoId)
  83. }
  84. edbLen := len(edbIds)
  85. if edbLen > 0 {
  86. cond += fmt.Sprintf(` AND a.edb_info_id NOT IN (%s)`, utils.GetOrmInReplace(edbLen))
  87. pars = append(pars, edbIds)
  88. }
  89. }
  90. keywords := this.GetString("Keyword")
  91. keywords = strings.TrimSpace(keywords)
  92. if keywords != "" {
  93. kw := fmt.Sprint("%", keywords, "%")
  94. cond += fmt.Sprintf(` AND (b.edb_code LIKE ? OR b.edb_name LIKE ?)`)
  95. pars = append(pars, kw, kw)
  96. }
  97. dataCount, e := data_manage.GetCollectEdbInfoCount(cond, pars)
  98. if e != nil {
  99. br.Msg = "获取失败"
  100. br.ErrMsg = fmt.Sprintf("获取收藏指标总数失败, %v", e)
  101. return
  102. }
  103. if dataCount == 0 {
  104. resp.Paging = page
  105. br.Data = resp
  106. br.Ret = 200
  107. br.Success = true
  108. br.Msg = "获取成功"
  109. return
  110. }
  111. list, e := data_manage.GetCollectEdbInfoPageList(cond, pars, startSize, pageSize)
  112. if e != nil {
  113. br.Msg = "获取失败"
  114. br.ErrMsg = fmt.Sprintf("获取收藏指标列表失败, %v", e)
  115. return
  116. }
  117. for _, v := range list {
  118. resp.List = append(resp.List, data_manage.FormatEdbInfo2CollectItem(v))
  119. }
  120. sort.Slice(resp.List, func(i, j int) bool {
  121. return resp.List[i].Sort < resp.List[j].Sort
  122. })
  123. page = paging.GetPaging(currentIndex, pageSize, int(dataCount))
  124. resp.Paging = page
  125. br.Data = resp
  126. br.Ret = 200
  127. br.Success = true
  128. br.Msg = "获取成功"
  129. }
  130. // @router /edb_collect/collect [post]
  131. func (this *EdbCollectController) Collect() {
  132. br := new(models.BaseResponse).Init()
  133. defer func() {
  134. if br.ErrMsg == "" {
  135. br.IsSendEmail = false
  136. }
  137. this.Data["json"] = br
  138. this.ServeJSON()
  139. }()
  140. sysUser := this.SysUser
  141. if sysUser == nil {
  142. br.Msg = "请登录"
  143. br.ErrMsg = "请登录,SysUser Is Empty"
  144. br.Ret = 408
  145. return
  146. }
  147. var req data_manage.EdbCollectReq
  148. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  149. br.Msg = "参数解析异常"
  150. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  151. return
  152. }
  153. if req.EdbInfoId <= 0 {
  154. br.Msg = "请选择指标"
  155. return
  156. }
  157. edbItem, e := data_manage.GetEdbInfoById(req.EdbInfoId)
  158. if e != nil {
  159. if utils.IsErrNoRow(e) {
  160. br.Msg = "指标不存在, 请刷新页面"
  161. return
  162. }
  163. br.Msg = "操作失败"
  164. br.ErrMsg = fmt.Sprintf("获取指标信息失败: %v", e)
  165. return
  166. }
  167. collectOb := new(data_manage.EdbCollect)
  168. addList := make([]*data_manage.EdbCollect, 0)
  169. for _, classifyId := range req.ClassifyIdList {
  170. addList = append(addList, &data_manage.EdbCollect{
  171. EdbCollectClassifyId: classifyId,
  172. EdbInfoId: req.EdbInfoId,
  173. EdbCode: edbItem.EdbCode,
  174. SysUserId: sysUser.AdminId,
  175. SysRealName: sysUser.RealName,
  176. CreateTime: time.Now().Local(),
  177. ModifyTime: time.Now().Local(),
  178. })
  179. }
  180. cond := fmt.Sprintf(" %s = ? AND %s = ?", collectOb.Cols().SysUserId, collectOb.Cols().EdbInfoId)
  181. pars := make([]interface{}, 0)
  182. pars = append(pars, sysUser.AdminId, req.EdbInfoId)
  183. e = collectOb.RemoveAndCreateMulti(cond, pars, addList)
  184. if e != nil {
  185. br.Msg = "操作失败"
  186. br.ErrMsg = fmt.Sprintf("新增指标收藏失败: %v", e)
  187. return
  188. }
  189. br.Ret = 200
  190. br.Success = true
  191. br.Msg = "操作成功"
  192. }
  193. // @router /edb_collect/cancel_collect [post]
  194. func (this *EdbCollectController) CancelCollect() {
  195. br := new(models.BaseResponse).Init()
  196. defer func() {
  197. if br.ErrMsg == "" {
  198. br.IsSendEmail = false
  199. }
  200. this.Data["json"] = br
  201. this.ServeJSON()
  202. }()
  203. sysUser := this.SysUser
  204. if sysUser == nil {
  205. br.Msg = "请登录"
  206. br.ErrMsg = "请登录,SysUser Is Empty"
  207. br.Ret = 408
  208. return
  209. }
  210. var req data_manage.EdbCollectReq
  211. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  212. br.Msg = "参数解析异常"
  213. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  214. return
  215. }
  216. if req.EdbInfoId <= 0 {
  217. br.Msg = "请选择指标"
  218. return
  219. }
  220. if req.ClassifyId <= 0 {
  221. br.Msg = "请选择分类"
  222. return
  223. }
  224. collectOb := new(data_manage.EdbCollect)
  225. cond := fmt.Sprintf("%s = ? AND %s = ? AND %s = ?", collectOb.Cols().SysUserId, collectOb.Cols().EdbCollectClassifyId, collectOb.Cols().EdbInfoId)
  226. pars := make([]interface{}, 0)
  227. pars = append(pars, sysUser.AdminId, req.ClassifyId, req.EdbInfoId)
  228. if e := collectOb.RemoveByCondition(cond, pars); e != nil {
  229. br.Msg = "操作失败"
  230. br.ErrMsg = fmt.Sprintf("取消收藏失败: %v", e)
  231. return
  232. }
  233. br.Ret = 200
  234. br.Success = true
  235. br.Msg = "操作成功"
  236. }
  237. // @router /edb_collect/move [post]
  238. func (this *EdbCollectController) Move() {
  239. br := new(models.BaseResponse).Init()
  240. defer func() {
  241. if br.ErrMsg == "" {
  242. br.IsSendEmail = false
  243. }
  244. this.Data["json"] = br
  245. this.ServeJSON()
  246. }()
  247. sysUser := this.SysUser
  248. if sysUser == nil {
  249. br.Msg = "请登录"
  250. br.ErrMsg = "请登录,SysUser Is Empty"
  251. br.Ret = 408
  252. return
  253. }
  254. var req data_manage.EdbCollectMoveReq
  255. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  256. br.Msg = "参数解析异常!"
  257. br.ErrMsg = fmt.Sprintf("参数解析失败, %v", e)
  258. return
  259. }
  260. var newSort int
  261. if req.PrevEdbInfoId <= 0 {
  262. firstOne, err := data_manage.GetEdbCollectSort(sysUser.AdminId, req.ClassifyId, 0)
  263. if err != nil {
  264. br.Msg = "获取数据失败!"
  265. br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error()
  266. return
  267. }
  268. newSort = firstOne.Sort - 1
  269. } else if req.NextEdbInfoId <= 0 {
  270. lastOne, err := data_manage.GetEdbCollectSort(sysUser.AdminId, req.ClassifyId, 1)
  271. if err != nil {
  272. br.Msg = "获取数据失败!"
  273. br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error()
  274. return
  275. }
  276. newSort = lastOne.Sort + 1
  277. } else {
  278. preMapItem, err := data_manage.GetEdbCollectByEdbInfoId(sysUser.AdminId, req.PrevEdbInfoId, req.ClassifyId)
  279. if err != nil {
  280. br.Msg = "获取数据失败!"
  281. br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error()
  282. return
  283. }
  284. nextMapItem, err := data_manage.GetEdbCollectByEdbInfoId(sysUser.AdminId, req.NextEdbInfoId, req.ClassifyId)
  285. if err != nil {
  286. br.Msg = "获取数据失败!"
  287. br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error()
  288. return
  289. }
  290. newSort = (preMapItem.Sort + nextMapItem.Sort) / 2
  291. newSort = preMapItem.Sort + 1
  292. var updateSortStr string
  293. if preMapItem.Sort == nextMapItem.Sort {
  294. updateSortStr = `sort + 2`
  295. } else if nextMapItem.Sort-preMapItem.Sort == 1 {
  296. updateSortStr = `sort + 1`
  297. }
  298. if updateSortStr != `` {
  299. _ = data_manage.UpdateEdbCollectSortByClassifyId(req.ClassifyId, preMapItem.Sort, preMapItem.EdbCollectId, updateSortStr)
  300. }
  301. }
  302. if e := data_manage.UpdateEdbCollectMove(newSort, sysUser.AdminId, req.EdbInfoId, req.ClassifyId); e != nil {
  303. br.Msg = "操作失败"
  304. br.ErrMsg = fmt.Sprintf("更新指标收藏失败, %v", e)
  305. return
  306. }
  307. br.Ret = 200
  308. br.Success = true
  309. br.Msg = "操作成功"
  310. }