permission.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hz_crm_api/utils"
  6. )
  7. // ChartPermissionSearchKeyWordMapping 权限相关
  8. type ChartPermissionSearchKeyWordMapping struct {
  9. Id int `description:"id" json:"-"`
  10. ChartPermissionId int `description:"权限id"`
  11. KeyWord string `description:"二级分类名称"`
  12. From string `description:"类型标识" json:"-"`
  13. TacticType string `description:"策略表type字段值" json:"-"`
  14. TeleconferenceSort int `description:"电话会类型排序" json:"-"`
  15. ClassifyId int `description:"分类ID"`
  16. }
  17. func GetPermission(classifyNameSecond string) (items []*ChartPermissionSearchKeyWordMapping, err error) {
  18. o := orm.NewOrm()
  19. sql := `SELECT * FROM chart_permission_search_key_word_mapping AS a WHERE a.from='rddp' AND a.key_word=? `
  20. _, err = o.Raw(sql, classifyNameSecond).QueryRows(&items)
  21. return
  22. }
  23. // EditChartPermissionSearchKeyWordMappingMulti 修改报告报告权限(先删除原有的权限,再添加新的权限)
  24. func EditChartPermissionSearchKeyWordMappingMulti(keyword string, permissionIdList []int) (err error) {
  25. o := orm.NewOrm()
  26. to, err := o.Begin()
  27. if err != nil {
  28. return
  29. }
  30. defer func() {
  31. if err != nil {
  32. _ = to.Rollback()
  33. } else {
  34. _ = to.Commit()
  35. }
  36. }()
  37. sql := "delete from chart_permission_search_key_word_mapping WHERE `from`='rddp' AND key_word=? "
  38. _, err = to.Raw(sql, keyword).Exec()
  39. if err != nil {
  40. return
  41. }
  42. if len(permissionIdList) > 0 {
  43. chartPermissionSearchKeyWordMappingList := make([]*ChartPermissionSearchKeyWordMapping, 0)
  44. for _, permissionId := range permissionIdList {
  45. tmpChartPermissionSearchKeyWordMapping := &ChartPermissionSearchKeyWordMapping{
  46. ChartPermissionId: permissionId,
  47. KeyWord: keyword,
  48. From: "rddp",
  49. TacticType: "",
  50. TeleconferenceSort: 0,
  51. }
  52. chartPermissionSearchKeyWordMappingList = append(chartPermissionSearchKeyWordMappingList, tmpChartPermissionSearchKeyWordMapping)
  53. }
  54. _, err = to.InsertMulti(len(chartPermissionSearchKeyWordMappingList), chartPermissionSearchKeyWordMappingList)
  55. }
  56. return
  57. }
  58. // AddChartPermissionSearchKeyWordMappingMulti 批量新增报告权限
  59. func AddChartPermissionSearchKeyWordMappingMulti(chartPermissionSearchKeyWordMappingList []*ChartPermissionSearchKeyWordMapping) (err error) {
  60. o := orm.NewOrm()
  61. _, err = o.InsertMulti(len(chartPermissionSearchKeyWordMappingList), chartPermissionSearchKeyWordMappingList)
  62. return
  63. }
  64. func AddChartPermissionChapterMapping(chartPermissionId int, reportId int64) (err error) {
  65. sql := `INSERT INTO chart_permission_chapter_mapping (chart_permission_id, report_chapter_type_id,research_type)
  66. VALUES(?,?,?)`
  67. o := orm.NewOrm()
  68. _, err = o.Raw(sql, chartPermissionId, reportId, "rddp").Exec()
  69. return
  70. }
  71. func RemoveChartPermissionChapterMapping(reportId int64) (err error) {
  72. sql := ` DELETE FROM chart_permission_chapter_mapping WHERE research_type=? AND report_chapter_type_id=? `
  73. o := orm.NewOrm()
  74. _, err = o.Raw(sql, "rddp", reportId).Exec()
  75. return
  76. }
  77. type ChartPermissionMappingIdName struct {
  78. PermissionId int
  79. PermissionName string
  80. }
  81. func GetChartPermissionNameFromMappingByKeyword(keyword string, source string) (list []*ChartPermissionMappingIdName, err error) {
  82. o := orm.NewOrm()
  83. sql := " SELECT b.chart_permission_id AS permission_id,b.permission_name FROM chart_permission_search_key_word_mapping AS a INNER JOIN chart_permission AS b ON a.chart_permission_id = b.chart_permission_id WHERE a.`from` = ? AND a.key_word = ? "
  84. _, err = o.Raw(sql, source, keyword).QueryRows(&list)
  85. return
  86. }
  87. // UpdateChartPermissionNameFromMappingByKeyword 根据关键词及来源更新新关键词
  88. func UpdateChartPermissionNameFromMappingByKeyword(newKeyword, keyword, source string) (err error) {
  89. o := orm.NewOrm()
  90. sql := " UPDATE chart_permission_search_key_word_mapping SET key_word = ? WHERE key_word = ? AND `from` = ? "
  91. _, err = o.Raw(sql, newKeyword, keyword, source).Exec()
  92. return
  93. }
  94. // GetPermissionsByClassifyIds 根据分类IDs获取权限IDs
  95. func GetPermissionsByClassifyIds(classifyIds []int) (items []*ChartPermissionSearchKeyWordMapping, err error) {
  96. if len(classifyIds) == 0 {
  97. return
  98. }
  99. o := orm.NewOrm()
  100. sql := fmt.Sprintf(`SELECT * FROM chart_permission_search_key_word_mapping AS a WHERE a.from='rddp' AND a.classify_id IN (%s)`, utils.GetOrmInReplace(len(classifyIds)))
  101. _, err = o.Raw(sql, classifyIds).QueryRows(&items)
  102. return
  103. }