chart_info_share.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/cache"
  5. "eta_gn/eta_api/controllers"
  6. "eta_gn/eta_api/models"
  7. "eta_gn/eta_api/models/data_manage"
  8. "eta_gn/eta_api/models/data_manage/request"
  9. "eta_gn/eta_api/models/data_manage/response"
  10. "eta_gn/eta_api/models/system"
  11. "eta_gn/eta_api/services/data"
  12. "eta_gn/eta_api/services/elastic"
  13. "eta_gn/eta_api/utils"
  14. "fmt"
  15. "github.com/rdlucklib/rdluck_tools/paging"
  16. "sort"
  17. "strconv"
  18. "strings"
  19. )
  20. // ChartInfoShareController 数据管理
  21. type ChartInfoShareController struct {
  22. controllers.BaseAuthController
  23. }
  24. // UserList
  25. // @Title 获取图表设置共享的详情
  26. // @Description 获取图表详情接口
  27. // @Param ChartInfoId query int true "图表id"
  28. // @Success 200 {object} response.ChartInfoShareUserResp
  29. // @router /chart_info/share/user_list [get]
  30. func (c *ChartInfoShareController) UserList() {
  31. br := new(models.BaseResponse).Init()
  32. defer func() {
  33. c.Data["json"] = br
  34. c.ServeJSON()
  35. }()
  36. sysUser := c.SysUser
  37. if sysUser == nil {
  38. br.Msg = "请登录"
  39. br.ErrMsg = "请登录,SysUser Is Empty"
  40. br.Ret = 408
  41. return
  42. }
  43. chartInfoId, _ := c.GetInt("ChartInfoId")
  44. if chartInfoId <= 0 {
  45. br.Msg = "参数错误"
  46. br.ErrMsg = "参数错误"
  47. return
  48. }
  49. chartInfo, err := data_manage.GetChartInfoById(chartInfoId)
  50. if err != nil {
  51. br.Msg = "获取失败"
  52. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  53. return
  54. }
  55. if chartInfo.SysUserId != sysUser.AdminId {
  56. br.Msg = "您没有权限设置共享"
  57. br.ErrMsg = "您没有权限设置共享,ChartInfoId:" + strconv.Itoa(chartInfoId)
  58. br.IsSendEmail = false
  59. return
  60. }
  61. resp := response.ChartInfoShareUserResp{}
  62. obj := data_manage.ChartInfoShare{}
  63. list, err := obj.GetListByChartInfoId(chartInfoId)
  64. if err != nil {
  65. br.Msg = `获取失败`
  66. br.ErrMsg = `获取失败:` + err.Error()
  67. return
  68. }
  69. resp.List = list
  70. br.Ret = 200
  71. br.Success = true
  72. br.Msg = "获取成功"
  73. br.Data = resp
  74. }
  75. // Save
  76. // @Title 单个图表设置共享
  77. // @Description 单个图表设置共享
  78. // @Param request body request.SetChartInfoShareReq true "type json string"
  79. // @Success 200 {object} data_manage.ChartInfo
  80. // @router /chart_info/share/save [post]
  81. func (c *ChartInfoShareController) Save() {
  82. br := new(models.BaseResponse).Init()
  83. defer func() {
  84. c.Data["json"] = br
  85. c.ServeJSON()
  86. }()
  87. sysUser := c.SysUser
  88. if sysUser == nil {
  89. br.Msg = "请登录"
  90. br.ErrMsg = "请登录,SysUser Is Empty"
  91. br.Ret = 408
  92. return
  93. }
  94. var req request.SetChartInfoShareReq
  95. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  96. if err != nil {
  97. br.Msg = "参数解析异常!"
  98. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  99. return
  100. }
  101. if req.ChartInfoId <= 0 {
  102. br.Msg = `请选择图表`
  103. br.IsSendEmail = false
  104. return
  105. }
  106. chartItem, err := data_manage.GetChartInfoById(req.ChartInfoId)
  107. if err != nil {
  108. br.Msg = "查找图表失败"
  109. br.ErrMsg = "查找图表失败,Err:" + err.Error()
  110. return
  111. }
  112. if chartItem.ChartInfoId <= 0 {
  113. br.Msg = "图表不存在"
  114. br.ErrMsg = "图表不存在,ChartInfoId:" + strconv.Itoa(req.ChartInfoId)
  115. br.IsSendEmail = false
  116. return
  117. }
  118. if chartItem.SysUserId != c.SysUser.AdminId {
  119. br.Msg = "您没有权限设置共享"
  120. br.ErrMsg = "您没有权限设置共享,ChartInfoId:" + strconv.Itoa(req.ChartInfoId)
  121. br.IsSendEmail = false
  122. return
  123. }
  124. obj := data_manage.ChartInfoShare{}
  125. // 如果图表未公开,则判断是否被其他用户使用
  126. if chartItem.PublicStatus != utils.DataPublicSuccess {
  127. checkUserIdList := []int{sysUser.AdminId}
  128. if len(req.UserIdList) > 0 {
  129. checkUserIdList = append(checkUserIdList, req.UserIdList...)
  130. }
  131. {
  132. userIdList, err := obj.GetAllUserIdListByChartInfoIdAndNoShareType(chartItem.ChartInfoId, req.ShareType)
  133. if err != nil {
  134. br.Msg = "设置失败!"
  135. br.ErrMsg = "设置失败,Err:" + err.Error()
  136. return
  137. }
  138. if len(userIdList) > 0 {
  139. checkUserIdList = append(checkUserIdList, userIdList...)
  140. }
  141. }
  142. // 校验是否被其他用户关联
  143. noReferenced, otherUserIdList, err, tips := data.CheckChartReferencedByOthers([]int{chartItem.ChartInfoId}, []string{chartItem.UniqueCode}, checkUserIdList)
  144. if err != nil {
  145. br.Msg = "保存失败"
  146. if tips != "" {
  147. br.Msg = tips
  148. }
  149. br.ErrMsg = "设置图表共享失败,查找图表的关联用户失败,Err:" + err.Error()
  150. return
  151. }
  152. if !noReferenced {
  153. userNameStr := ``
  154. if len(otherUserIdList) > 0 {
  155. userNameList := make([]string, 0)
  156. userList, err := system.GetAdminListByIdList(otherUserIdList)
  157. if err != nil {
  158. br.Msg = "保存失败"
  159. if tips != "" {
  160. br.Msg = tips
  161. }
  162. br.ErrMsg = "设置图表共享失败,查找用户信息失败,Err:" + err.Error()
  163. return
  164. }
  165. for _, userInfo := range userList {
  166. userNameList = append(userNameList, userInfo.RealName)
  167. }
  168. userNameStr = strings.Join(userNameList, "、")
  169. }
  170. msg := `图表被其他用户使用,无法设置共享`
  171. if userNameStr != "" {
  172. msg = fmt.Sprintf(`图表被用户%s使用,无法设置共享`, userNameStr)
  173. }
  174. if tips != "" {
  175. msg = fmt.Sprintf(`%s,用户为:%s,无法设置共享`, tips, userNameStr)
  176. }
  177. br.Msg = msg
  178. br.ErrMsg = "图表被其他用户共享,无法设置共享,ChartInfoId:" + strconv.Itoa(chartItem.ChartInfoId)
  179. br.IsSendEmail = false
  180. return
  181. }
  182. }
  183. // 修改
  184. err = obj.SaveChartInfoShare([]int{req.ChartInfoId}, req.UserIdList, req.ShareType)
  185. if err != nil {
  186. br.Msg = `保存失败`
  187. br.ErrMsg = `保存失败:` + err.Error()
  188. return
  189. }
  190. // 更新es
  191. data.AddOrEditChartInfoToEs(req.ChartInfoId)
  192. // 图表收藏操作处理
  193. go cache.AddDataCollectHandler(req.ChartInfoId, utils.DataCollectTypeChart)
  194. br.Ret = 200
  195. br.Success = true
  196. br.Msg = "保存成功"
  197. }
  198. // Tree
  199. // @Title 获取共享图表的分类/图表树
  200. // @Description 获取共享图表的分类/图表树
  201. // @Success 200 {object} response.ChartShareListResp
  202. // @router /chart_info/share/tree [get]
  203. func (c *ChartInfoShareController) Tree() {
  204. br := new(models.BaseResponse).Init()
  205. defer func() {
  206. c.Data["json"] = br
  207. c.ServeJSON()
  208. }()
  209. sysUser := c.SysUser
  210. if sysUser == nil {
  211. br.Msg = "请登录"
  212. br.ErrMsg = "请登录,SysUser Is Empty"
  213. br.Ret = 408
  214. return
  215. }
  216. resp := response.ChartShareListResp{}
  217. // 我共享的
  218. {
  219. sendList, err := data.GetAllShareChartListByFromUserId(sysUser.AdminId)
  220. if err != nil {
  221. br.Msg = "获取失败"
  222. br.ErrMsg = "获取我共享的图表列表信息失败,Err:" + err.Error()
  223. return
  224. }
  225. nodeList, err := data.GetChartClassifyItemListByShareChartInfoQueryList(sendList)
  226. if err != nil {
  227. br.Msg = "获取失败"
  228. br.ErrMsg = "获取我共享出去的图表列表分类结构信息失败,Err:" + err.Error()
  229. return
  230. }
  231. var sortList data_manage.UserShareChartClassifyItemList
  232. sortList = nodeList
  233. sort.Sort(sortList)
  234. resp.Send = sortList
  235. }
  236. // 我收到的
  237. {
  238. sendList, err := data.GetAllShareChartListByReceivedUserId(sysUser.AdminId)
  239. if err != nil {
  240. br.Msg = "获取失败"
  241. br.ErrMsg = "获取我共享的图表列表信息失败,Err:" + err.Error()
  242. return
  243. }
  244. nodeList, err := data.GetChartClassifyItemListByShareChartInfoQueryList(sendList)
  245. if err != nil {
  246. br.Msg = "获取失败"
  247. br.ErrMsg = "获取我收到共享的图表列表分类结构信息失败,Err:" + err.Error()
  248. return
  249. }
  250. receivedMap := make(map[int]int)
  251. var receivedList data_manage.UserShareChartClassifyItemList
  252. for _, v := range nodeList {
  253. index, ok := receivedMap[v.SysUserId]
  254. if !ok {
  255. index = len(receivedMap)
  256. item := &data_manage.UserShareChartClassifyItem{
  257. UserId: v.SysUserId,
  258. UserName: v.SysUserRealName,
  259. ChartInfoId: 0,
  260. ChartClassifyId: v.SysUserId,
  261. ChartClassifyName: v.SysUserRealName,
  262. ChartClassifyNameEn: v.SysUserRealName,
  263. ParentId: 0,
  264. Level: 0,
  265. Sort: 0,
  266. UniqueCode: utils.MD5(fmt.Sprint(v.SysUserId, "_", v.SysUserRealName)),
  267. Source: 0,
  268. SourceName: "",
  269. SysUserId: v.SysUserId,
  270. SysUserRealName: v.SysUserRealName,
  271. DateType: 0,
  272. StartDate: "",
  273. EndDate: "",
  274. ChartType: 0,
  275. Calendar: "",
  276. SeasonStartDate: "",
  277. SeasonEndDate: "",
  278. Children: []*data_manage.UserShareChartClassifyItem{},
  279. Button: data_manage.ChartClassifyItemsButton{},
  280. IsJoinPermission: 0,
  281. HaveOperaAuth: true,
  282. ChartClassifyIdPath: "",
  283. }
  284. receivedList = append(receivedList, item)
  285. }
  286. receivedList[index].Children = append(receivedList[index].Children, v)
  287. receivedMap[v.SysUserId] = index
  288. }
  289. sort.Sort(receivedList)
  290. resp.Received = receivedList
  291. }
  292. br.Ret = 200
  293. br.Success = true
  294. br.Msg = "获取成功"
  295. br.Data = resp
  296. }
  297. // List
  298. // @Title 图表列表接口
  299. // @Description 图表列表接口
  300. // @Param PageSize query int true "每页数据条数"
  301. // @Param CurrentIndex query int true "当前页页码,从1开始"
  302. // @Param ClassifyId query int true "分类id"
  303. // @Param FilterSource query int false "共享图表的列表数据来源,0:我共享的;1:别人共享给我的"
  304. // @Param ClassifyId query int false "分类id"
  305. // @Param UserId query int false "所属用户id"
  306. // @Success 200 {object} response.ChartInfoChartListResp
  307. // @router /chart_info/share/list [get]
  308. func (c *ChartInfoShareController) List() {
  309. br := new(models.BaseResponse).Init()
  310. defer func() {
  311. if br.ErrMsg == "" {
  312. br.IsSendEmail = false
  313. }
  314. c.Data["json"] = br
  315. c.ServeJSON()
  316. }()
  317. sysUser := c.SysUser
  318. if sysUser == nil {
  319. br.Msg = "请登录"
  320. br.ErrMsg = "请登录,SysUser Is Empty"
  321. br.Ret = 408
  322. return
  323. }
  324. // 分页
  325. pageSize, _ := c.GetInt("PageSize")
  326. currentIndex, _ := c.GetInt("CurrentIndex")
  327. filterSource, _ := c.GetInt("FilterSource")
  328. var total int
  329. page := paging.GetPaging(currentIndex, pageSize, total)
  330. var startSize int
  331. if pageSize <= 0 {
  332. pageSize = utils.PageSize20
  333. }
  334. if currentIndex <= 0 {
  335. currentIndex = 1
  336. }
  337. startSize = paging.StartIndex(currentIndex, pageSize)
  338. // 基础图表
  339. var condition string
  340. var pars []interface{}
  341. condition += ` AND source = ? `
  342. pars = append(pars, utils.CHART_TYPE_CURVE)
  343. // 用户筛选
  344. userId, _ := c.GetInt("UserId")
  345. if userId > 0 {
  346. condition += ` AND b.sys_user_id = ? `
  347. pars = append(pars, userId)
  348. }
  349. // 分类筛选
  350. classifyId, _ := c.GetInt("ClassifyId")
  351. if classifyId > 0 {
  352. childClassify, e, _ := data.GetChildChartClassifyByClassifyId(classifyId)
  353. if e != nil && !utils.IsErrNoRow(e) {
  354. br.Msg = "获取失败"
  355. br.ErrMsg = "获取分类信息失败, GetChartClassify,Err:" + e.Error()
  356. return
  357. }
  358. var classifyIds []int
  359. for _, v := range childClassify {
  360. classifyIds = append(classifyIds, v.ChartClassifyId)
  361. }
  362. condition += fmt.Sprintf(` AND b.chart_classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds)))
  363. pars = append(pars, classifyIds)
  364. }
  365. switch filterSource {
  366. case 1: // 别人共享给我的
  367. condition += ` AND a.sys_user_id = ? AND b.sys_user_id != ? `
  368. pars = append(pars, sysUser.AdminId, sysUser.AdminId)
  369. default: // 我共享的
  370. condition += ` AND b.sys_user_id = ? `
  371. pars = append(pars, sysUser.AdminId)
  372. }
  373. obj := data_manage.ChartInfoShare{}
  374. dataCount, respList, err := obj.GetShareChartInfoListPageList(condition, pars, startSize, pageSize)
  375. if err != nil {
  376. br.Msg = "获取失败"
  377. br.ErrMsg = fmt.Sprintf("获取分享图表列表失败, %v", err)
  378. return
  379. }
  380. editShareChartInfoIdMap, err := data.GetAllEditSharedChartInfoIdMapByReceivedUserId(c.SysUser.AdminId)
  381. if err != nil {
  382. br.Msg = "获取失败"
  383. br.ErrMsg = "获取所有有编辑权限的分享图表失败,Err:" + err.Error()
  384. return
  385. }
  386. for _, chartInfo := range respList {
  387. chartInfo.HaveOperaAuth = true
  388. button := data.GetChartOpButton(c.SysUser, chartInfo.SysUserId, chartInfo.ChartInfoId, chartInfo.HaveOperaAuth, editShareChartInfoIdMap)
  389. chartInfo.Button.IsEdit = button.OpButton
  390. chartInfo.Button.IsCopy = button.DeleteButton
  391. }
  392. page = paging.GetPaging(currentIndex, pageSize, int(dataCount))
  393. resp := response.ChartInfoChartListResp{
  394. Paging: page,
  395. List: respList,
  396. }
  397. br.Ret = 200
  398. br.Success = true
  399. br.Msg = "获取成功"
  400. br.Data = resp
  401. }
  402. // ListByEs
  403. // @Title 图表筛选接口
  404. // @Description 图表筛选接口
  405. // @Success 200 {object} data_manage.ChartInfoList
  406. // @Param request body request.SearchChartInfoShareReq true "type json string"
  407. // @Success 200 {object} data_manage.ChartInfoFilterDataResp
  408. // @router /chart_info/share/list/es [post]
  409. func (c *ChartInfoShareController) ListByEs() {
  410. br := new(models.BaseResponse).Init()
  411. defer func() {
  412. c.Data["json"] = br
  413. c.ServeJSON()
  414. }()
  415. var req request.SearchChartInfoShareReq
  416. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  417. if err != nil {
  418. br.Msg = "参数解析异常!"
  419. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  420. return
  421. }
  422. pageSize := req.PageSize
  423. currentIndex := req.CurrentIndex
  424. var total int64
  425. var startSize int
  426. if pageSize <= 0 {
  427. pageSize = utils.PageSize20
  428. }
  429. if currentIndex <= 0 {
  430. currentIndex = 1
  431. }
  432. startSize = paging.StartIndex(currentIndex, pageSize)
  433. keyword := req.Keyword
  434. keyword = strings.TrimSpace(keyword) //移除字符串首尾空格
  435. sourceList := []int{utils.CHART_TYPE_CURVE}
  436. // 图表分享状态:1-未共享;2-已共享。可多选,多选用英文,隔开;默认是未全部
  437. chartShareList := req.ChartShareList
  438. if len(chartShareList) <= 0 {
  439. chartShareList = []int{}
  440. }
  441. chartShare := 0 // 0:全部,1:未共享,2:已共享
  442. lenChartShareList := len(chartShareList)
  443. if len(chartShareList) > 0 {
  444. if lenChartShareList > 1 {
  445. chartShare = 0
  446. } else {
  447. chartShare = chartShareList[0]
  448. }
  449. }
  450. // 图表类型
  451. chartTypeList := req.ChartTypeList
  452. // 直图表所属分类id
  453. chartClassifyIdList := req.ClassifyIdList
  454. chartAuth := 1 // 选择范围是:只有我的图表
  455. var chartInfoList []*data_manage.ChartInfoView
  456. sortMap := make(map[string]string)
  457. // 如果没有搜索关键词,则默认根据图表编码倒序排序
  458. if keyword == `` {
  459. sortMap["ChartInfoId"] = `desc`
  460. }
  461. total, chartInfoList, err = elastic.SearchChartInfoDataByShared(keyword, startSize, pageSize, chartShare, sourceList, chartTypeList, chartClassifyIdList, chartAuth, c.SysUser.AdminId, sortMap)
  462. if err != nil {
  463. chartInfoList = make([]*data_manage.ChartInfoView, 0)
  464. }
  465. page := paging.GetPaging(currentIndex, pageSize, int(total))
  466. // 因为是ES查找的,所以需要重新查一下图表的信息,主要是为了把是否授权字段找出来
  467. if len(chartInfoList) > 0 {
  468. chartTypeMap := make(map[int]*data_manage.ChartType)
  469. sysUserIdList := make([]int, 0)
  470. chartInfoIdList := make([]int, 0)
  471. currChartTypeList := make([]int, 0)
  472. for _, v := range chartInfoList {
  473. v.ConvertToResp()
  474. v.HaveOperaAuth = true
  475. chartInfoIdList = append(chartInfoIdList, v.ChartInfoId)
  476. currChartTypeList = append(currChartTypeList, v.ChartType)
  477. if v.SharedUserIdList != nil && len(v.SharedUserIdList) > 0 {
  478. sysUserIdList = append(sysUserIdList, v.SharedUserIdList...)
  479. }
  480. }
  481. // 获取图表数据
  482. tmpChartList, err := data_manage.GetChartInfoByIdList(chartInfoIdList)
  483. if err != nil {
  484. br.Msg = "获取失败"
  485. br.ErrMsg = "获取所有有权限的图表失败,Err:" + err.Error()
  486. return
  487. }
  488. chartInfoMap := make(map[int]*data_manage.ChartInfo)
  489. for _, v := range tmpChartList {
  490. chartInfoMap[v.ChartInfoId] = v
  491. }
  492. // 获取用户数据
  493. sysUserMap := make(map[int]string)
  494. if len(sysUserIdList) > 0 {
  495. sysUserList, err := system.GetAdminListByIdList(sysUserIdList)
  496. if err != nil {
  497. br.Msg = "获取失败"
  498. br.ErrMsg = "获取分享用户列表失败,Err:" + err.Error()
  499. return
  500. }
  501. for _, v := range sysUserList {
  502. sysUserMap[v.AdminId] = v.RealName
  503. }
  504. }
  505. // 图表类型
  506. {
  507. tmpChartTypeList, err := data_manage.GetChartTypeListByIdList(currChartTypeList)
  508. if err != nil {
  509. br.Msg = "获取图表类型失败"
  510. br.ErrMsg = "获取图表类型失败,Err:" + err.Error()
  511. return
  512. }
  513. for _, v := range tmpChartTypeList {
  514. chartTypeMap[v.ChartTypeId] = v
  515. }
  516. }
  517. for _, v := range chartInfoList {
  518. tmpChartInfo, ok := chartInfoMap[v.ChartInfoId]
  519. if !ok {
  520. continue
  521. }
  522. v.IsJoinPermission = tmpChartInfo.IsJoinPermission
  523. // 图表类型名称
  524. if chartType, ok := chartTypeMap[v.ChartType]; ok {
  525. v.ChartTypeName = chartType.ChartTypeName
  526. }
  527. // 分享人
  528. shareUserName := ``
  529. shareUserNameList := make([]string, 0)
  530. shareUserNameMap := make(map[int]bool)
  531. for _, userId := range v.SharedUserIdList {
  532. userName, ok := sysUserMap[userId]
  533. if !ok {
  534. continue
  535. }
  536. if _, ok = shareUserNameMap[userId]; ok {
  537. continue
  538. }
  539. shareUserNameMap[userId] = true
  540. shareUserNameList = append(shareUserNameList, userName)
  541. }
  542. if len(shareUserNameList) > 0 {
  543. shareUserName = strings.Join(shareUserNameList, `,`)
  544. }
  545. v.SharedUserName = shareUserName
  546. }
  547. }
  548. resp := data_manage.ChartInfoFilterDataResp{
  549. Paging: page,
  550. List: chartInfoList,
  551. }
  552. br.Ret = 200
  553. br.Success = true
  554. br.Msg = "获取成功"
  555. br.Data = resp
  556. }
  557. // BatchSave
  558. // @Title 批量图表设置共享
  559. // @Description 批量图表设置共享
  560. // @Param request body request.SetChartSharePermissionReq true "type json string"
  561. // @Success 200 {object} data_manage.ChartInfo
  562. // @router /chart_info/share/batch_save [post]
  563. func (c *ChartInfoShareController) BatchSave() {
  564. br := new(models.BaseResponse).Init()
  565. defer func() {
  566. c.Data["json"] = br
  567. c.ServeJSON()
  568. }()
  569. sysUser := c.SysUser
  570. if sysUser == nil {
  571. br.Msg = "请登录"
  572. br.ErrMsg = "请登录,SysUser Is Empty"
  573. br.Ret = 408
  574. return
  575. }
  576. var req request.SetChartSharePermissionReq
  577. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  578. if err != nil {
  579. br.Msg = "参数解析异常!"
  580. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  581. return
  582. }
  583. // 选中的图表id列表
  584. selectChartInfoIdList := make([]int, 0)
  585. // 选择所有图表,所以需要通过es获取数据
  586. if req.IsSelectAll {
  587. allEsChartInfoIdList, err := getAllChartInfoIdListByShared(req, c.SysUser.AdminId)
  588. if err != nil {
  589. br.Msg = "设置失败!"
  590. br.ErrMsg = "设置失败,Err:" + err.Error()
  591. return
  592. }
  593. if len(req.NoChartIdList) <= 0 {
  594. selectChartInfoIdList = allEsChartInfoIdList
  595. } else {
  596. noChartIdMap := make(map[int]bool)
  597. for _, v := range req.NoChartIdList {
  598. noChartIdMap[v] = true
  599. }
  600. for _, v := range allEsChartInfoIdList {
  601. if _, ok := noChartIdMap[v]; !ok {
  602. // 如果不在未选中的图表id列表中,那么就加入到选中的图表id列表
  603. selectChartInfoIdList = append(selectChartInfoIdList, v)
  604. }
  605. }
  606. }
  607. } else {
  608. selectChartInfoIdList = req.ChartIdList
  609. }
  610. num := len(selectChartInfoIdList)
  611. if num <= 0 {
  612. br.Msg = `请选择图表`
  613. br.IsSendEmail = false
  614. return
  615. }
  616. if num > 30 {
  617. br.Msg = `图表数量不能超过30个`
  618. br.IsSendEmail = false
  619. return
  620. }
  621. chartInfoList, err := data_manage.GetChartInfoListByChartIdList(selectChartInfoIdList)
  622. if err != nil {
  623. br.Msg = "设置失败!"
  624. br.ErrMsg = "设置失败,Err:" + err.Error()
  625. return
  626. }
  627. obj := data_manage.ChartInfoShare{}
  628. {
  629. for _, chartItem := range chartInfoList {
  630. // 如果指标已公开,则不判断是否被其他用户使用
  631. if chartItem.PublicStatus == utils.DataPublicSuccess {
  632. continue
  633. }
  634. if chartItem.PublicStatus != utils.DataPublicSuccess {
  635. checkUserIdList := []int{sysUser.AdminId}
  636. if len(req.UserIdList) > 0 {
  637. checkUserIdList = append(checkUserIdList, req.UserIdList...)
  638. }
  639. {
  640. userIdList, err := obj.GetAllUserIdListByChartInfoIdAndNoShareType(chartItem.ChartInfoId, req.ShareType)
  641. if err != nil {
  642. br.Msg = "设置失败!"
  643. br.ErrMsg = "设置失败,Err:" + err.Error()
  644. return
  645. }
  646. if len(userIdList) > 0 {
  647. checkUserIdList = append(checkUserIdList, userIdList...)
  648. }
  649. }
  650. // 校验是否被其他用户关联
  651. noReferenced, otherUserIdList, err, tips := data.CheckChartReferencedByOthers([]int{chartItem.ChartInfoId}, []string{chartItem.UniqueCode}, checkUserIdList)
  652. if err != nil {
  653. br.Msg = "保存失败"
  654. if tips != "" {
  655. br.Msg = tips
  656. }
  657. br.ErrMsg = "设置指标共享失败,查找指标的关联用户失败,Err:" + err.Error()
  658. return
  659. }
  660. if !noReferenced {
  661. userNameStr := ``
  662. if len(otherUserIdList) > 0 {
  663. userNameList := make([]string, 0)
  664. userList, err := system.GetAdminListByIdList(otherUserIdList)
  665. if err != nil {
  666. br.Msg = "保存失败"
  667. if tips != "" {
  668. br.Msg = tips
  669. }
  670. br.ErrMsg = "设置指标共享失败,查找用户信息失败,Err:" + err.Error()
  671. return
  672. }
  673. for _, userInfo := range userList {
  674. userNameList = append(userNameList, userInfo.RealName)
  675. }
  676. userNameStr = strings.Join(userNameList, "、")
  677. }
  678. msg := fmt.Sprintf(`指标《%s》被其他用户使用,无法设置共享`, chartItem.ChartName)
  679. if userNameStr != "" {
  680. msg = fmt.Sprintf(`指标《%s》被用户%s使用,无法设置共享`, chartItem.ChartName, userNameStr)
  681. }
  682. if tips != "" {
  683. msg = fmt.Sprintf(`%s,用户为:%s,无法设置共享`, tips, userNameStr)
  684. }
  685. br.Msg = msg
  686. br.ErrMsg = "指标被其他用户共享,无法设置共享,ChartInfoId:" + strconv.Itoa(chartItem.ChartInfoId)
  687. br.IsSendEmail = false
  688. return
  689. }
  690. }
  691. }
  692. }
  693. // 设置
  694. err = obj.SaveChartInfoShare(selectChartInfoIdList, req.UserIdList, req.ShareType)
  695. if err != nil {
  696. br.Msg = `保存失败`
  697. br.ErrMsg = `保存失败:` + err.Error()
  698. return
  699. }
  700. // 更新es
  701. for _, chartInfoId := range selectChartInfoIdList {
  702. data.AddOrEditChartInfoToEs(chartInfoId)
  703. // 图表收藏操作处理
  704. go cache.AddDataCollectHandler(chartInfoId, utils.DataCollectTypeChart)
  705. }
  706. br.Ret = 200
  707. br.Success = true
  708. br.Msg = "保存成功"
  709. }
  710. // getAllChartInfoIdListByShared
  711. // @Description: 获取所有的图表id列表
  712. // @author: Roc
  713. // @datetime 2024-12-10 17:51:35
  714. // @param req request.SetChartSharePermissionReq
  715. // @param userId int
  716. // @return chartInfoIdList []int
  717. // @return err error
  718. func getAllChartInfoIdListByShared(req request.SetChartSharePermissionReq, userId int) (chartInfoIdList []int, err error) {
  719. keyword := req.Keyword
  720. keyword = strings.TrimSpace(keyword) //移除字符串首尾空格
  721. //图表来源
  722. sourceList := []int{utils.CHART_TYPE_CURVE}
  723. // 图表分享状态:1-未共享;2-已共享。可多选,多选用英文,隔开;默认是未共享
  724. chartShareList := req.ChartShareList
  725. if len(chartShareList) <= 0 {
  726. chartShareList = []int{1}
  727. }
  728. chartShare := 1 // 0:全部,1:未共享,2:已共享
  729. lenChartShareList := len(chartShareList)
  730. if len(chartShareList) > 0 {
  731. if lenChartShareList > 1 {
  732. chartShare = 0
  733. } else {
  734. chartShare = chartShareList[0]
  735. }
  736. }
  737. // 图表类型
  738. chartTypeList := req.ChartTypeList
  739. // 图表分享状态:1-未共享;2-已共享。可多选,多选用英文,隔开;默认是未共享
  740. chartClassifyIdList := req.ClassifyIdList
  741. chartAuth := 1 // 选择范围是:只有我的图表
  742. sortMap := make(map[string]string)
  743. // 如果没有搜索关键词,则默认根据图表编码倒序排序
  744. if keyword == `` {
  745. sortMap["ChartInfoId"] = `desc`
  746. }
  747. _, chartInfoList, err := getAllChartInfoDataByShared(keyword, 1, chartShare, sourceList, chartTypeList, chartClassifyIdList, chartAuth, userId, sortMap)
  748. if err != nil {
  749. return
  750. }
  751. // 返回图表id列表
  752. for _, v := range chartInfoList {
  753. chartInfoIdList = append(chartInfoIdList, v.ChartInfoId)
  754. }
  755. return
  756. }
  757. // getAllChartInfoDataByShared
  758. // @Description: 获取所有的图表列表(设置共享的时候)
  759. // @author: Roc
  760. // @datetime 2024-12-10 17:51:18
  761. // @param keyword string
  762. // @param currPage int
  763. // @param chartShare int
  764. // @param sourceList []int
  765. // @param classifyIdList []int
  766. // @param chartAuth int
  767. // @param sysUserId int
  768. // @param sortMap map[string]string
  769. // @return total int64
  770. // @return list []*data_manage.ChartInfoView
  771. // @return err error
  772. func getAllChartInfoDataByShared(keyword string, currPage, chartShare int, sourceList, chartTypeList, classifyIdList []int, chartAuth, sysUserId int, sortMap map[string]string) (total int64, list []*data_manage.ChartInfoView, err error) {
  773. // 每页获取数据的数量
  774. pageSize := 5000
  775. var startSize int
  776. if currPage <= 0 {
  777. currPage = 1
  778. }
  779. startSize = paging.StartIndex(currPage, pageSize)
  780. total, list, err = elastic.SearchChartInfoDataByShared(keyword, startSize, pageSize, chartShare, sourceList, chartTypeList, classifyIdList, chartAuth, sysUserId, sortMap)
  781. if err != nil {
  782. return
  783. }
  784. page := paging.GetPaging(currPage, pageSize, int(total))
  785. if !page.IsEnd {
  786. _, nextList, tmpErr := getAllChartInfoDataByShared(keyword, page.NextIndex, chartShare, sourceList, chartTypeList, classifyIdList, chartAuth, sysUserId, sortMap)
  787. if tmpErr != nil {
  788. err = tmpErr
  789. return
  790. }
  791. list = append(list, nextList...)
  792. }
  793. return
  794. }