research_group.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package services
  2. import (
  3. "errors"
  4. "eta_gn/eta_api/models/system"
  5. "strconv"
  6. "strings"
  7. )
  8. // ResearchGroup 研究方向分组表
  9. type ResearchGroupItem struct {
  10. ResearchGroupId int `json:"research_group_id" description:"研究方向分组ID"`
  11. ResearchGroupName string `json:"research_group_name" description:"研究方向分组名称"`
  12. ParentId int `json:"parent_id" description:"父类ID"`
  13. ChartPermissionId int `json:"chart_permission_id" description:"品种权限ID"`
  14. Sort int `json:"sort" description:"排序"`
  15. Members []*ResearchGroupMember `json:"members"`
  16. Children []*ResearchGroupItem `json:"children"`
  17. }
  18. // ResearchGroupMember 研究方向组员信息
  19. type ResearchGroupMember struct {
  20. AdminId int `json:"admin_id"`
  21. AdminName string `json:"admin_name"`
  22. }
  23. // GetResearchGroupTree 获取研究方向分组及组员列表
  24. func GetResearchGroupTree(include int) (respList []*ResearchGroupItem, err error) {
  25. respList = make([]*ResearchGroupItem, 0)
  26. list, e := system.GetResearchGroupList()
  27. if e != nil {
  28. err = errors.New("获取研究方向分组失败, Err:" + e.Error())
  29. return
  30. }
  31. listLen := len(list)
  32. if listLen == 0 {
  33. return
  34. }
  35. // 分类
  36. firstList := make([]*ResearchGroupItem, 0)
  37. secondList := make([]*ResearchGroupItem, 0)
  38. for i := 0; i < listLen; i++ {
  39. item := new(ResearchGroupItem)
  40. item.ResearchGroupId = list[i].ResearchGroupId
  41. item.ResearchGroupName = list[i].ResearchGroupName
  42. item.ParentId = list[i].ParentId
  43. item.ChartPermissionId = list[i].ChartPermissionId
  44. item.Sort = list[i].Sort
  45. if list[i].ParentId == 0 {
  46. firstList = append(firstList, item)
  47. } else {
  48. secondList = append(secondList, item)
  49. }
  50. }
  51. if len(firstList) == 0 {
  52. return
  53. }
  54. // 匹配成员
  55. relationList, e := system.GetResearchGroupRelationList(include)
  56. if e != nil {
  57. err = errors.New("获取研究方向关系失败, Err:" + e.Error())
  58. return
  59. }
  60. for _, v := range secondList {
  61. members := make([]*ResearchGroupMember, 0)
  62. for _, r := range relationList {
  63. if v.ResearchGroupId == r.ResearchGroupId {
  64. members = append(members, &ResearchGroupMember{
  65. AdminId: r.AdminId,
  66. AdminName: r.AdminName,
  67. })
  68. }
  69. }
  70. v.Members = members
  71. }
  72. // 匹配子分类
  73. for _, p := range firstList {
  74. children := make([]*ResearchGroupItem, 0)
  75. for _, child := range secondList {
  76. if p.ResearchGroupId == child.ParentId {
  77. children = append(children, child)
  78. }
  79. }
  80. p.Children = children
  81. }
  82. respList = firstList
  83. return
  84. }
  85. // UpdateAdminResearchGroup 更新研究员研究方向分组
  86. func UpdateAdminResearchGroup(adminId int, groupIds string) (err error) {
  87. if adminId == 0 {
  88. return
  89. }
  90. items := make([]*system.ResearchGroupRelation, 0)
  91. groupIdArr := strings.Split(groupIds, ",")
  92. if groupIds != "" {
  93. for _, v := range groupIdArr {
  94. groupId, e := strconv.Atoi(v)
  95. if e != nil {
  96. err = errors.New("分组ID有误")
  97. return
  98. }
  99. items = append(items, &system.ResearchGroupRelation{
  100. ResearchGroupId: groupId,
  101. AdminId: adminId,
  102. })
  103. }
  104. }
  105. if e := system.UpdateAdminResearchGroup(adminId, items); e != nil {
  106. err = errors.New("更新研究方向分组失败, Err:" + e.Error())
  107. }
  108. return
  109. }