research_group.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package admin
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. // ResearchGroup 研究方向分组表
  7. type ResearchGroup struct {
  8. ResearchGroupId int `orm:"column(research_group_id);pk" description:"研究方向分组ID"`
  9. ResearchGroupName string `json:"research_group_name" description:"研究方向分组名称"`
  10. ParentId int `json:"parent_id" description:"父类ID"`
  11. ChartPermissionId int `json:"chart_permission_id" description:"品种权限ID"`
  12. Sort int `json:"sort" description:"排序"`
  13. State int `json:"state" description:"显示状态 0-不显示 1-显示"`
  14. CreateTime time.Time `json:"create_time" description:"创建时间"`
  15. ModifyTime time.Time `json:"modify_time" description:"更新时间"`
  16. }
  17. // GetResearchGroupList 获取研究方向分组列表
  18. func GetResearchGroupList() (list []*ResearchGroup, err error) {
  19. o := orm.NewOrm()
  20. sql := `SELECT * FROM research_group WHERE state = 1 ORDER BY sort`
  21. _, err = o.Raw(sql).QueryRows(&list)
  22. return
  23. }
  24. // GetResearchGroupById 主键获取分组
  25. func GetResearchGroupById(groupId int) (item *ResearchGroup, err error) {
  26. o := orm.NewOrm()
  27. sql := `SELECT * FROM research_group WHERE research_group_id = ? LIMIT 1`
  28. err = o.Raw(sql, groupId).QueryRow(&item)
  29. return
  30. }
  31. // ResearchGroupRelation 研究方向关系表
  32. type ResearchGroupRelation struct {
  33. Id int `orm:"column(id);pk"`
  34. ResearchGroupId int `json:"research_group_id" description:"分组ID"`
  35. AdminId int `json:"admin_id" description:"研究员ID"`
  36. }
  37. // ResearchGroupRelationListByAdminId 根据研究员ID获取研究方向分组关系列表
  38. func ResearchGroupRelationListByAdminId(adminId int) (list []*ResearchGroupRelation, err error) {
  39. o := orm.NewOrm()
  40. sql := `SELECT * FROM research_group_relation WHERE admin_id = ?`
  41. _, err = o.Raw(sql, adminId).QueryRows(&list)
  42. return
  43. }
  44. // ResearchGroupRelation 研究方向分组关系表
  45. type ResearchGroupRelationItem struct {
  46. ResearchGroupId int `json:"research_group_id" description:"研究方向分组ID"`
  47. AdminId int `json:"admin_id" description:"研究员ID"`
  48. AdminName string `json:"admin_name" description:"研究员姓名"`
  49. }
  50. // GetResearchGroupRelationList 获取研究方向分组关系列表
  51. func GetResearchGroupRelationList(include int) (list []*ResearchGroupRelationItem, err error) {
  52. // 是否包含已禁止的研究员, 默认不包含
  53. condition := ` WHERE b.enabled = 1 `
  54. if include == 1 {
  55. condition = ` WHERE 1=1 `
  56. }
  57. o := orm.NewOrm()
  58. sql := `SELECT
  59. a.research_group_id,
  60. a.admin_id,
  61. b.real_name AS admin_name
  62. FROM
  63. research_group_relation AS a
  64. INNER JOIN admin AS b ON a.admin_id = b.admin_id `
  65. sql += condition
  66. sql += ` ORDER BY
  67. a.research_group_id ASC,
  68. a.admin_id ASC`
  69. _, err = o.Raw(sql).QueryRows(&list)
  70. return
  71. }
  72. // UpdateAdminResearchGroup 更新研究员研究方向分组
  73. func UpdateAdminResearchGroup(adminId int, items []*ResearchGroupRelation) (err error) {
  74. o := orm.NewOrm()
  75. to, err := o.Begin()
  76. if err != nil {
  77. return
  78. }
  79. defer func() {
  80. if err != nil {
  81. _ = to.Rollback()
  82. } else {
  83. _ = to.Commit()
  84. }
  85. }()
  86. sql := `DELETE FROM research_group_relation WHERE admin_id = ?`
  87. if _, err = o.Raw(sql, adminId).Exec(); err != nil {
  88. return
  89. }
  90. itemsLen := len(items)
  91. if itemsLen > 0 {
  92. _, err = o.InsertMulti(itemsLen, items)
  93. }
  94. return
  95. }
  96. // AdminResearchGroupList 研究员研究方向分组列表
  97. type AdminResearchGroupList struct {
  98. AdminId int `json:"admin_id"`
  99. GroupNameStr string `json:"group_name_str"`
  100. }
  101. // GetAdminResearchGroupListByAdminId 获取研究员研究方向分组列表
  102. func GetAdminResearchGroupListByAdminId(adminIds string) (list []*AdminResearchGroupList, err error) {
  103. o := orm.NewOrm()
  104. sql := `SELECT
  105. a.admin_id,
  106. GROUP_CONCAT(b.research_group_name) AS group_name_str
  107. FROM
  108. research_group_relation AS a
  109. INNER JOIN research_group AS b ON a.research_group_id = b.research_group_id
  110. WHERE
  111. a.admin_id IN (` + adminIds + `)
  112. GROUP BY
  113. a.admin_id`
  114. _, err = o.Raw(sql).QueryRows(&list)
  115. return
  116. }