variety_tag.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. // VarietyClassify 标签库分类表
  7. type VarietyClassify struct {
  8. VarietyClassifyId int `orm:"column(variety_classify_id);pk" description:"标签分类ID"`
  9. ClassifyName string `json:"classify_name" description:"标签分类名称"`
  10. Sort int `json:"sort" description:"排序"`
  11. State int `json:"state" description:"显示状态 0-禁用 1-启用"`
  12. CreateTime time.Time `json:"create_time" description:"创建时间"`
  13. ModifyTime time.Time `json:"modify_time" description:"更新时间"`
  14. }
  15. // GetVarietyClassifyList 获取标签分类列表
  16. func GetVarietyClassifyList() (list []*VarietyClassify, err error) {
  17. o := orm.NewOrm()
  18. sql := `SELECT * FROM variety_classify WHERE state = 1 ORDER BY sort`
  19. _, err = o.Raw(sql).QueryRows(&list)
  20. return
  21. }
  22. // GetVarietyClassifyById 主键获取分类
  23. func GetVarietyClassifyById(classifyId int) (item *VarietyClassify, err error) {
  24. o := orm.NewOrm()
  25. sql := `SELECT * FROM variety_classify WHERE variety_classify_id = ? LIMIT 1`
  26. err = o.Raw(sql, classifyId).QueryRow(&item)
  27. return
  28. }
  29. // VarietyTag 标签表
  30. type VarietyTag struct {
  31. VarietyTagId int `orm:"column(variety_tag_id);pk" description:"标签ID"`
  32. VarietyClassifyId int `json:"variety_classify_id" description:"标签分类ID"`
  33. VarietyClassifyName string `json:"variety_classify_name" description:"标签分类名称"`
  34. TagName string `json:"tag_name" description:"标签名称"`
  35. ChartPermissionClassifyName string `json:"chart_permission_classify_name" description:"品种权限分类名"`
  36. ChartPermissionId int `json:"chart_permission_id" description:"品种权限ID"`
  37. ChartPermissionName string `json:"chart_permission_name" description:"品种权限名称"`
  38. Sort int `json:"sort" description:"排序"`
  39. State int `json:"state" description:"显示状态 0-禁用 1-启用"`
  40. CreateTime time.Time `json:"create_time" description:"创建时间"`
  41. ModifyTime time.Time `json:"modify_time" description:"更新时间"`
  42. }
  43. // GetVarietyTagById 主键获取标签
  44. func GetVarietyTagById(tagId int) (item *VarietyTag, err error) {
  45. o := orm.NewOrm()
  46. sql := `SELECT * FROM variety_tag WHERE variety_tag_id = ? LIMIT 1`
  47. err = o.Raw(sql, tagId).QueryRow(&item)
  48. return
  49. }
  50. // GetVarietyTagList 获取标签列表
  51. func GetVarietyTagList() (list []*VarietyTag, err error) {
  52. o := orm.NewOrm()
  53. sql := `SELECT * FROM variety_tag WHERE state = 1 ORDER BY sort`
  54. _, err = o.Raw(sql).QueryRows(&list)
  55. return
  56. }
  57. // GetVarietyTagListByClassifyId 根据分类获取标签列表
  58. func GetVarietyTagListByClassifyId(classifyId int) (list []*VarietyTag, err error) {
  59. o := orm.NewOrm()
  60. sql := `SELECT * FROM variety_tag WHERE variety_classify_id = ? AND state = 1 ORDER BY sort`
  61. _, err = o.Raw(sql).QueryRows(&list)
  62. return
  63. }
  64. // ResearchVarietyTagRelation 研究员标签关系表
  65. type ResearchVarietyTagRelation struct {
  66. Id int `orm:"column(id);pk"`
  67. VarietyTagId int `json:"variety_tag_id" description:"标签ID"`
  68. AdminId int `json:"admin_id" description:"研究员ID"`
  69. }
  70. // ResearchVarietyTagRelationListByAdminId 根据研究员ID获取标签关系列表
  71. func ResearchVarietyTagRelationListByAdminId(adminId int) (list []*ResearchVarietyTagRelation, err error) {
  72. o := orm.NewOrm()
  73. sql := `SELECT * FROM research_variety_tag_relation WHERE admin_id = ?`
  74. _, err = o.Raw(sql, adminId).QueryRows(&list)
  75. return
  76. }
  77. type ResearchTagRelation struct {
  78. Id int `json:"id"`
  79. ResearchGroupId int `json:"research_group_id" description:"分组ID"`
  80. AdminId int `json:"admin_id" description:"研究员ID"`
  81. }
  82. // ResearchTagRelationListByAdminId 根据研究员ID获取标签关系列表
  83. func ResearchTagRelationListByAdminId(adminId int) (list []*ResearchTagRelation, err error) {
  84. o := orm.NewOrm()
  85. sql := `SELECT id, variety_tag_id AS research_group_id, admin_id FROM research_variety_tag_relation WHERE admin_id = ?`
  86. _, err = o.Raw(sql, adminId).QueryRows(&list)
  87. return
  88. }
  89. // ResearchVarietyTagRelationItem 研究员标签关系
  90. type ResearchVarietyTagRelationItem struct {
  91. VarietyTagId int `json:"variety_tag_id" description:"标签ID"`
  92. AdminId int `json:"admin_id" description:"研究员ID"`
  93. AdminName string `json:"admin_name" description:"研究员姓名"`
  94. }
  95. // GetResearchVarietyTagRelationList 获取研究员标签关系列表
  96. func GetResearchVarietyTagRelationList(include int) (list []*ResearchVarietyTagRelationItem, err error) {
  97. // 是否包含已禁止的研究员, 默认不包含
  98. condition := ` WHERE b.enabled = 1 `
  99. if include == 1 {
  100. condition = ` WHERE 1=1 `
  101. }
  102. o := orm.NewOrm()
  103. sql := `SELECT
  104. a.variety_tag_id,
  105. a.admin_id,
  106. b.real_name AS admin_name
  107. FROM
  108. research_variety_tag_relation AS a
  109. INNER JOIN admin AS b ON a.admin_id = b.admin_id `
  110. sql += condition
  111. sql += ` ORDER BY
  112. a.variety_tag_id ASC,
  113. a.admin_id ASC`
  114. _, err = o.Raw(sql).QueryRows(&list)
  115. return
  116. }
  117. // UpdateAdminVarietyTagRelation 更新研究员标签关系
  118. func UpdateAdminVarietyTagRelation(adminId int, items []*ResearchVarietyTagRelation) (err error) {
  119. o := orm.NewOrm()
  120. to, err := o.Begin()
  121. if err != nil {
  122. return
  123. }
  124. defer func() {
  125. if err != nil {
  126. _ = to.Rollback()
  127. } else {
  128. _ = to.Commit()
  129. }
  130. }()
  131. sql := `DELETE FROM research_variety_tag_relation WHERE admin_id = ?`
  132. if _, err = o.Raw(sql, adminId).Exec(); err != nil {
  133. return
  134. }
  135. itemsLen := len(items)
  136. if itemsLen > 0 {
  137. _, err = o.InsertMulti(itemsLen, items)
  138. }
  139. return
  140. }
  141. // AdminVarietyTagRelationList 研究员研究方向列表
  142. type AdminVarietyTagRelationList struct {
  143. AdminId int `json:"admin_id"`
  144. GroupNameStr string `json:"group_name_str"`
  145. }
  146. // GetAdminVarietyTagRelationListByAdminId 获取研究员研究方向分组列表
  147. func GetAdminVarietyTagRelationListByAdminId(adminIds string) (list []*AdminVarietyTagRelationList, err error) {
  148. o := orm.NewOrm()
  149. sql := `SELECT
  150. a.admin_id,
  151. GROUP_CONCAT(b.tag_name) AS group_name_str
  152. FROM
  153. research_variety_tag_relation AS a
  154. INNER JOIN variety_tag AS b ON a.variety_tag_id = b.variety_tag_id
  155. WHERE
  156. a.admin_id IN (` + adminIds + `)
  157. GROUP BY
  158. a.admin_id`
  159. _, err = o.Raw(sql).QueryRows(&list)
  160. return
  161. }