base_ent_empower.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package entity
  2. import (
  3. "database/sql"
  4. "strings"
  5. . "app.yhyue.com/moapp/jybase/common"
  6. . "app.yhyue.com/moapp/jybase/date"
  7. . "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/db"
  8. )
  9. var Base_ent_empower = base_ent_empower{}
  10. //企业授权表
  11. type base_ent_empower struct {
  12. Id int64
  13. Appid string
  14. Ent_id int64 //企业id
  15. Ent_user_id int64 //企业员工id
  16. Function_code string //功能代码
  17. }
  18. //授权记录
  19. func (b *base_ent_empower) List(appid, function_code string, ent_id, page_num, page_size int64) *[]*base_ent_empower {
  20. q := `select * from base_ent_empower where appid=? and function_code=? and ent_id=? order by id desc`
  21. args := []interface{}{appid, function_code, ent_id}
  22. if page_num > 0 {
  23. q += ` limit ?,?`
  24. args = append(args, (page_num-1)*page_size, page_size)
  25. }
  26. list := Mysql_BaseService.SelectBySql(q, args...)
  27. return JsonUnmarshal(list, &[]*base_ent_empower{}).(*[]*base_ent_empower)
  28. }
  29. //授权记录数量
  30. func (b *base_ent_empower) Count(appid, function_code string, ent_id int64) int64 {
  31. 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)
  32. }
  33. //是否授权
  34. func (b *base_ent_empower) HasEmpower(appid, function_code string, ent_id, ent_user_id int64) bool {
  35. if Base_ent_wait_empower.IsAllEmpower(appid, function_code, ent_id) {
  36. return true
  37. }
  38. empower := b.List(appid, function_code, ent_id, 0, 0)
  39. if empower != nil {
  40. if len(*empower) == 1 && (*empower)[0].Ent_user_id == 0 {
  41. return true
  42. } else {
  43. for _, vv := range *empower {
  44. if vv.Ent_id == ent_id && vv.Ent_user_id == ent_user_id {
  45. return true
  46. }
  47. }
  48. }
  49. }
  50. return false
  51. }
  52. //重新授权
  53. func (b *base_ent_empower) ReEmpower(appid, function_code string, ent_id int64, ent_user_id []int64) bool {
  54. return Mysql_BaseService.ExecTx("重新授权", func(tx *sql.Tx) bool {
  55. r1 := Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `delete from base_ent_empower where appid=? and ent_id=? and function_code=?`, appid, ent_id, function_code)
  56. values := []interface{}{}
  57. now := NowFormat(Date_Full_Layout)
  58. for _, v := range ent_user_id {
  59. if v <= 0 {
  60. continue
  61. }
  62. values = append(values, appid, ent_id, v, function_code, now)
  63. }
  64. r2, r3 := int64(-1), int64(-1)
  65. if len(values) > 0 {
  66. r2, r3 = Mysql_BaseService.InsertBatchByTx(tx, "base_ent_empower", []string{"appid", "ent_id", "ent_user_id", "function_code", "create_time"}, values)
  67. }
  68. return r1 >= 0 && r2 == int64(len(ent_user_id)) && r3 > 0
  69. })
  70. }
  71. //授权
  72. func (b *base_ent_empower) Empower(appid, function_code string, ent_id int64, ent_user_id []int64) bool {
  73. b.CancelEmpower(appid, function_code, ent_id, ent_user_id)
  74. fields := []string{"appid", "ent_id", "ent_user_id", "function_code", "create_time"}
  75. nowFormat := NowFormat(Date_Full_Layout)
  76. values := []interface{}{}
  77. for _, v := range ent_user_id {
  78. for _, vv := range strings.Split(function_code, ",") {
  79. values = append(values, appid, ent_id, Int64All(v), vv, nowFormat)
  80. }
  81. }
  82. r1, _ := Mysql_BaseService.InsertBatch("base_ent_empower", fields, values)
  83. return r1 == int64(len(ent_user_id))
  84. }
  85. //取消授权
  86. func (b *base_ent_empower) CancelEmpower(appid, function_code string, ent_id int64, ent_user_id []int64) bool {
  87. args := []interface{}{appid, ent_id}
  88. wh1 := []string{}
  89. for _, v := range strings.Split(function_code, ",") {
  90. wh1 = append(wh1, "?")
  91. args = append(args, v)
  92. }
  93. wh2 := []string{}
  94. for _, v := range ent_user_id {
  95. wh2 = append(wh2, "?")
  96. args = append(args, v)
  97. }
  98. r1 := Mysql_BaseService.UpdateOrDeleteBySql(`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...)
  99. return r1 == int64(len(ent_user_id))
  100. }