base_ent_empower.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 string, function_code []string, ent_id int64, ent_user_id []int64) bool {
  54. return Mysql_BaseService.ExecTx("重新授权", func(tx *sql.Tx) bool {
  55. args := []interface{}{appid, ent_id}
  56. wh := []string{}
  57. for _, v := range function_code {
  58. wh = append(wh, "?")
  59. args = append(args, v)
  60. }
  61. r1 := Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `delete from base_ent_empower where appid=? and ent_id=? and function_code in (`+strings.Join(wh, ",")+`)`, args...)
  62. nowFormat := NowFormat(Date_Full_Layout)
  63. values := []interface{}{}
  64. for _, v := range ent_user_id {
  65. for _, vv := range function_code {
  66. values = append(values, appid, ent_id, v, vv, nowFormat)
  67. }
  68. }
  69. r2, r3 := int64(-1), int64(-1)
  70. if len(values) > 0 {
  71. r2, r3 = Mysql_BaseService.InsertBatchByTx(tx, "base_ent_empower", []string{"appid", "ent_id", "ent_user_id", "function_code", "create_time"}, values)
  72. }
  73. return r1 >= 0 && r2 == int64(len(function_code)*len(ent_user_id)) && r3 > 0
  74. })
  75. }
  76. //授权
  77. func (b *base_ent_empower) Empower(appid string, function_code []string, ent_id int64, ent_user_id []int64) bool {
  78. fields := []string{"appid", "ent_id", "ent_user_id", "function_code", "create_time"}
  79. nowFormat := NowFormat(Date_Full_Layout)
  80. values := []interface{}{}
  81. for _, v := range ent_user_id {
  82. for _, vv := range function_code {
  83. values = append(values, appid, ent_id, v, vv, nowFormat)
  84. }
  85. }
  86. return Mysql_BaseService.ExecTx("授权", func(tx *sql.Tx) bool {
  87. b.CancelEmpower(tx, appid, function_code, ent_id, ent_user_id)
  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. //取消授权
  93. func (b *base_ent_empower) CancelEmpower(tx *sql.Tx, appid string, function_code []string, ent_id int64, ent_user_id []int64) bool {
  94. args := []interface{}{appid, ent_id}
  95. wh1 := []string{}
  96. for _, v := range function_code {
  97. wh1 = append(wh1, "?")
  98. args = append(args, v)
  99. }
  100. wh2 := []string{}
  101. for _, v := range ent_user_id {
  102. wh2 = append(wh2, "?")
  103. args = append(args, v)
  104. }
  105. return Mysql_BaseService.ExecTx("取消授权", func(tx *sql.Tx) bool {
  106. 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...)
  107. return r1 == int64(len(function_code)*len(ent_user_id))
  108. })
  109. }