en_company_permission.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hz_crm_api/utils"
  6. "strings"
  7. "time"
  8. )
  9. type EnCompanyPermission struct {
  10. EnCompanyPermissionId int `orm:"column(en_company_permission_id);pk" description:"英文品种权限ID"`
  11. EnCompanyId int `description:"英文客户ID"`
  12. EnPermissionId int `description:"英文品种ID"`
  13. CreateTime time.Time `description:"创建时间"`
  14. }
  15. var EnCompanyPermissionColumns = struct {
  16. EnCompanyPermissionId string
  17. EnCompanyId string
  18. EnPermissionId string
  19. CreateTime string
  20. }{
  21. EnCompanyPermissionId: "en_company_permission_id",
  22. EnCompanyId: "en_company_id",
  23. EnPermissionId: "en_permission_id",
  24. CreateTime: "create_time",
  25. }
  26. func (m *EnCompanyPermission) TableName() string {
  27. return "en_company_permission"
  28. }
  29. func (m *EnCompanyPermission) Create() (err error) {
  30. o := orm.NewOrmUsingDB("rddp")
  31. id, err := o.Insert(m)
  32. if err != nil {
  33. return
  34. }
  35. m.EnCompanyPermissionId = int(id)
  36. return
  37. }
  38. func (m *EnCompanyPermission) Update(cols []string) (err error) {
  39. o := orm.NewOrmUsingDB("rddp")
  40. _, err = o.Update(m, cols...)
  41. return
  42. }
  43. func (m *EnCompanyPermission) Del() (err error) {
  44. o := orm.NewOrmUsingDB("rddp")
  45. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), EnCompanyPermissionColumns.EnCompanyPermissionId)
  46. _, err = o.Raw(sql, m.EnCompanyPermissionId).Exec()
  47. return
  48. }
  49. func (m *EnCompanyPermission) GetItemById(id int) (item *EnCompanyPermission, err error) {
  50. o := orm.NewOrmUsingDB("rddp")
  51. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), EnCompanyPermissionColumns.EnCompanyPermissionId)
  52. err = o.Raw(sql, id).QueryRow(&item)
  53. return
  54. }
  55. func (m *EnCompanyPermission) GetItemByCondition(condition string, pars []interface{}) (item *EnCompanyPermission, err error) {
  56. o := orm.NewOrmUsingDB("rddp")
  57. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 `, m.TableName())
  58. sql += condition
  59. sql += ` LIMIT 1`
  60. err = o.Raw(sql, pars).QueryRow(&item)
  61. return
  62. }
  63. func (m *EnCompanyPermission) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  64. o := orm.NewOrmUsingDB("rddp")
  65. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  66. err = o.Raw(sql, pars).QueryRow(&count)
  67. return
  68. }
  69. func (m *EnCompanyPermission) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EnCompanyPermission, err error) {
  70. o := orm.NewOrmUsingDB("rddp")
  71. fields := strings.Join(fieldArr, ",")
  72. if len(fieldArr) == 0 {
  73. fields = `*`
  74. }
  75. order := `ORDER BY create_time DESC`
  76. if orderRule != "" {
  77. order = ` ORDER BY ` + orderRule
  78. }
  79. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  80. _, err = o.Raw(sql, pars).QueryRows(&items)
  81. return
  82. }
  83. func (m *EnCompanyPermission) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*EnCompanyPermission, err error) {
  84. o := orm.NewOrmUsingDB("rddp")
  85. fields := strings.Join(fieldArr, ",")
  86. if len(fieldArr) == 0 {
  87. fields = `*`
  88. }
  89. order := `ORDER BY create_time DESC`
  90. if orderRule != "" {
  91. order = ` ORDER BY ` + orderRule
  92. }
  93. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  94. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z`
  95. if err = o.Raw(totalSql, pars).QueryRow(&total); err != nil {
  96. return
  97. }
  98. sql += ` LIMIT ?,?`
  99. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  100. return
  101. }
  102. func ClearAndCreateEnCompanyPermissions(companyId int, permissions []*EnCompanyPermission) (err error) {
  103. o := orm.NewOrmUsingDB("rddp")
  104. sql := `DELETE FROM en_company_permission WHERE en_company_id = ?`
  105. if _, e := o.Raw(sql, companyId).Exec(); e != nil {
  106. err = fmt.Errorf("delete err: %s", e.Error())
  107. return
  108. }
  109. if len(permissions) > 0 {
  110. if _, e := o.InsertMulti(len(permissions), permissions); e != nil {
  111. err = fmt.Errorf("insert multi err: %s", e.Error())
  112. return
  113. }
  114. }
  115. return
  116. }
  117. // GetEnglishCompanyIdsByEnPermissionIds 根据权限IDs获取报告IDs
  118. func GetEnglishCompanyIdsByEnPermissionIds(permissionIds []int) (companyIds []int, err error) {
  119. if len(permissionIds) == 0 {
  120. return
  121. }
  122. o := orm.NewOrmUsingDB("rddp")
  123. sql := `SELECT DISTINCT a.en_company_id FROM en_company_permission AS a
  124. JOIN english_company AS b ON a.en_company_id = b.company_id
  125. WHERE
  126. a.en_permission_id IN (` + utils.GetOrmInReplace(len(permissionIds)) + `) AND b.status <> 3 AND b.enabled <> 0 AND b.is_deleted = 0`
  127. _, err = o.Raw(sql, permissionIds).QueryRows(&companyIds)
  128. return
  129. }