base_ent_empower.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package entity
  2. import (
  3. "database/sql"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. . "app.yhyue.com/moapp/jybase/common"
  8. . "app.yhyue.com/moapp/jybase/date"
  9. . "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/db"
  10. )
  11. var Base_ent_empower = base_ent_empower{}
  12. //企业授权表
  13. type base_ent_empower struct {
  14. Id int64
  15. Appid string
  16. Ent_id int64 //企业id
  17. Ent_user_id int64 //企业员工id
  18. Function_code string //功能代码
  19. }
  20. //授权记录
  21. func (b *base_ent_empower) List(appid, function_code string, ent_id, page_num, page_size int64) *[]*base_ent_empower {
  22. q := `select * from base_ent_empower where appid=? and function_code=? and ent_id=? order by id desc`
  23. args := []interface{}{appid, function_code, ent_id}
  24. if page_num > 0 {
  25. q += ` limit ?,?`
  26. args = append(args, (page_num-1)*page_size, page_size)
  27. }
  28. list := Mysql_BaseService.SelectBySql(q, args...)
  29. return JsonUnmarshal(list, &[]*base_ent_empower{}).(*[]*base_ent_empower)
  30. }
  31. //授权记录数量
  32. func (b *base_ent_empower) Count(appid, function_code string, ent_id int64) int64 {
  33. return Mysql_BaseService.CountBySql(`select count(1) as count from base_ent_empower where appid=? and function_code=? and ent_id=?`, appid, function_code, ent_id)
  34. }
  35. //是否授权
  36. func (b *base_ent_empower) HasEmpower(appid, function_code string, ent_id, ent_user_id int64) bool {
  37. if Base_ent_wait_empower.IsAllEmpower(appid, function_code, ent_id) {
  38. return true
  39. }
  40. empower := b.List(appid, function_code, ent_id, 0, 0)
  41. if empower != nil {
  42. if len(*empower) == 1 && (*empower)[0].Ent_user_id == 0 {
  43. return true
  44. } else {
  45. for _, vv := range *empower {
  46. if vv.Ent_id == ent_id && vv.Ent_user_id == ent_user_id {
  47. return true
  48. }
  49. }
  50. }
  51. }
  52. return false
  53. }
  54. //先把有权限的人清空,再把权益授权给新的人
  55. func (b *base_ent_empower) ReEmpower(appid string, function_code []string, ent_id int64, ent_user_id []int64) bool {
  56. return Mysql_BaseService.ExecTx("重新授权", func(tx *sql.Tx) bool {
  57. args := []interface{}{appid, ent_id}
  58. wh := []string{}
  59. for _, v := range function_code {
  60. wh = append(wh, "?")
  61. args = append(args, v)
  62. }
  63. r1 := Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `delete from base_ent_empower where appid=? and ent_id=? and function_code in (`+strings.Join(wh, ",")+`)`, args...)
  64. nowFormat := NowFormat(Date_Full_Layout)
  65. values := []interface{}{}
  66. for _, v := range ent_user_id {
  67. for _, vv := range function_code {
  68. values = append(values, appid, ent_id, v, vv, nowFormat)
  69. }
  70. }
  71. r2, r3 := int64(-1), int64(-1)
  72. if len(values) > 0 {
  73. r2, r3 = Mysql_BaseService.InsertBatchByTx(tx, "base_ent_empower", []string{"appid", "ent_id", "ent_user_id", "function_code", "create_time"}, values)
  74. }
  75. return r1 >= 0 && r2 == int64(len(function_code)*len(ent_user_id)) && r3 > 0
  76. })
  77. }
  78. //授权
  79. func (b *base_ent_empower) Empower(tx *sql.Tx, appid string, function_code []string, ent_id int64, ent_user_id []int64) bool {
  80. fields := []string{"appid", "ent_id", "ent_user_id", "function_code", "create_time"}
  81. nowFormat := NowFormat(Date_Full_Layout)
  82. values := []interface{}{}
  83. for _, v := range ent_user_id {
  84. for _, vv := range function_code {
  85. values = append(values, appid, ent_id, v, vv, nowFormat)
  86. }
  87. }
  88. r1, _ := Mysql_BaseService.InsertBatchByTx(tx, "base_ent_empower", fields, values)
  89. return r1 == int64(len(function_code)*len(ent_user_id))
  90. }
  91. //根据功能代码取消授权
  92. func (b *base_ent_empower) CancelEmpower(tx *sql.Tx, appid string, function_code []string, ent_id int64, ent_user_id []int64) bool {
  93. args := []interface{}{appid, ent_id}
  94. wh1 := []string{}
  95. for _, v := range function_code {
  96. wh1 = append(wh1, "?")
  97. args = append(args, v)
  98. }
  99. wh2 := []string{}
  100. for _, v := range ent_user_id {
  101. wh2 = append(wh2, "?")
  102. args = append(args, v)
  103. }
  104. r1 := Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `delete from base_ent_empower where appid=? and ent_id=? and function_code in (`+strings.Join(wh1, ",")+`) and ent_user_id in (`+strings.Join(wh2, ",")+`)`, args...)
  105. return r1 == int64(len(function_code)*len(ent_user_id))
  106. }
  107. //根据功能分类取消所有授权
  108. func (b *base_ent_empower) CancelAllEmpower(tx *sql.Tx, appid string, function_module []string, ent_id int64, ent_user_id []int64) bool {
  109. args := []interface{}{appid}
  110. wh1 := []string{}
  111. for _, v := range function_module {
  112. wh1 = append(wh1, "?")
  113. args = append(args, v)
  114. }
  115. args = append(args, appid, appid, ent_id)
  116. wh2 := []string{}
  117. for _, v := range ent_user_id {
  118. wh2 = append(wh2, "?")
  119. args = append(args, v)
  120. }
  121. return Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `delete c from base_function_module a
  122. inner join base_function b on (a.appid=? and a.name in (`+strings.Join(wh1, ",")+`) and a.id=b.pid and b.appid=?)
  123. inner join base_ent_empower c on (b.code=c.function_code and c.appid=? and c.ent_id=? and c.ent_user_id in (`+strings.Join(wh2, ",")+`))`, args...) >= 0
  124. }
  125. //校验功能代码
  126. func (b *base_ent_empower) CheckEmpowerCount(tx *sql.Tx, appid string, function_code []string, ent_id int64) (int64, error) {
  127. m := map[string]int64{}
  128. for _, v := range *Base_ent_wait_empower.AllWaitEmpowers(tx, appid, ent_id) {
  129. if v.Empower_count < 0 {
  130. m[v.Function_code] = v.Empower_count
  131. } else {
  132. m[v.Function_code] = m[v.Function_code] + v.Empower_count
  133. }
  134. }
  135. c := map[string]int64{}
  136. list := Mysql_BaseService.SelectBySqlByTx(tx, `SELECT function_code,SUM(1) AS sum FROM base_ent_empower WHERE appid=? and ent_id=? GROUP BY function_code`, appid, ent_id)
  137. if list == nil {
  138. return -3, errors.New(fmt.Sprintf("企业%d查询已授权数量失败", ent_id))
  139. }
  140. for _, v := range *list {
  141. c[ObjToString(v["function_code"])] = Int64All(v["sum"])
  142. }
  143. for _, v := range function_code {
  144. if count, ok := m[v]; !ok {
  145. return -1, errors.New(fmt.Sprintf("企业%d没有该待授权%s功能代码", ent_id, v))
  146. } else if count >= 0 && c[v] > count {
  147. return -2, errors.New(fmt.Sprintf("企业%d功能代码%s可授权数量不足", ent_id, v))
  148. }
  149. }
  150. return 1, nil
  151. }