research_group.go 4.2 KB

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