userLogic.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package logic
  2. import (
  3. "errors"
  4. "fmt"
  5. "git.rtzhtech.cn/iss/public-lib/dao"
  6. "git.rtzhtech.cn/iss/public-lib/dto"
  7. "git.rtzhtech.cn/iss/public-lib/model"
  8. "git.rtzhtech.cn/iss/public-lib/svc"
  9. "github.com/gogf/gf/v2/util/gconv"
  10. )
  11. // NewUserLogic
  12. // @函数名:NewUserLogic
  13. // @函数功能描述: 初始化user对象
  14. // @返回值:*UserLogic
  15. //
  16. func NewUserLogic() *UserLogic {
  17. return &UserLogic{
  18. SvcCtx: svc.SvcCtx,
  19. }
  20. }
  21. type UserLogic struct {
  22. SvcCtx *svc.ServiceContext
  23. }
  24. // SaveUser
  25. // @函数名:SaveUser
  26. // @函数功能描述: 保存用户
  27. // @对象名:c
  28. // @参数定义:req
  29. // @返回值:*model.SysUsr
  30. // @返回值:error
  31. //
  32. func (c *UserLogic) SaveUser(req *model.SysUsr) (*model.SysUsr, error) {
  33. var (
  34. resp = new(model.SysUsr)
  35. )
  36. maps := gconv.Map(req)
  37. err := c.SvcCtx.SysUsr.Base.Replace(maps)
  38. if err != nil {
  39. return resp, err
  40. }
  41. cond := dao.Condition{
  42. Where: map[string]any{
  43. "usrname": req.Usrname,
  44. },
  45. }
  46. err = c.SvcCtx.SysUsr.Base.Find(&cond, resp)
  47. if err != nil {
  48. return resp, err
  49. }
  50. return resp, nil
  51. }
  52. // UpdateUserMap
  53. // @函数名:UpdateUserMap
  54. // @函数功能描述: 通过map修改用户
  55. // @对象名:c
  56. // @参数定义:maps
  57. // @返回值:*model.SysUsr
  58. // @返回值:error
  59. //
  60. func (c *UserLogic) UpdateUserMap(maps map[string]interface{}) (*model.SysUsr, error) {
  61. var (
  62. resp = new(model.SysUsr)
  63. )
  64. if maps == nil || len(maps) == 0 {
  65. return resp, errors.New("参数为空")
  66. }
  67. if val, ok := maps["id"]; !ok || gconv.Int(val) == 0 {
  68. return resp, errors.New("缺少id")
  69. }
  70. c.SvcCtx.SysUsr.Base.Replace(maps)
  71. cond := dao.Condition{
  72. Where: map[string]any{
  73. "id": maps["id"],
  74. },
  75. }
  76. err := c.SvcCtx.SysUsr.Base.Find(&cond, resp)
  77. if err != nil {
  78. return resp, err
  79. }
  80. return resp, nil
  81. }
  82. // DelUser
  83. // @函数名:DelUser
  84. // @函数功能描述: 删除用户
  85. // @对象名:c
  86. // @参数定义:id
  87. // @返回值:error
  88. //
  89. func (c *UserLogic) DelUser(id int32) error {
  90. md := c.SvcCtx.SysUsr
  91. _, err := md.Where(md.ID.Eq(id)).Delete()
  92. return err
  93. }
  94. // GetUser
  95. // @函数名:GetUser
  96. // @函数功能描述: 获取用户信息
  97. // @对象名:c
  98. // @参数定义:req
  99. // @返回值:*dto.GetUserResponse
  100. // @返回值:error
  101. //
  102. func (c *UserLogic) GetUser(req *dto.GetUserRequest) (*dto.GetUserResponse, error) {
  103. var (
  104. resp = new(dto.GetUserResponse)
  105. )
  106. cond := new(dao.Condition)
  107. where := map[string]any{}
  108. if req.UserName != "" {
  109. UserNameLikeStr := fmt.Sprintf("%s like ?", c.SvcCtx.SysUsr.Usrname.String())
  110. where[UserNameLikeStr] = "%" + req.UserName + "%"
  111. }
  112. if req.FullName != "" {
  113. where["usrname"] = req.FullName
  114. }
  115. if req.Comment != "" {
  116. CommentLikeStr := fmt.Sprintf("%s like ?", c.SvcCtx.SysUsr.Comment.String())
  117. where[CommentLikeStr] = "%" + req.Comment + "%"
  118. }
  119. if req.Role != 0 {
  120. where[c.SvcCtx.SysUsr.Role.String()] = req.Role
  121. }
  122. if req.Status != 0 {
  123. where[c.SvcCtx.SysUsr.Status.String()] = req.Status
  124. }
  125. cond.Where = where
  126. var num int64
  127. c.SvcCtx.SysUsr.Base.Count(cond, &num)
  128. if req.Page < 1 {
  129. req.Page = 1
  130. }
  131. if req.Limit < 1 {
  132. req.Limit = 20
  133. }
  134. cond.Limit = req.Limit
  135. cond.Offset = (req.Page - 1) * req.Limit
  136. cond.OrderBy = "id desc"
  137. err := c.SvcCtx.SysUsr.Base.Find(cond, &resp.List)
  138. if err != nil {
  139. return resp, err
  140. }
  141. resp.Total = num
  142. return resp, nil
  143. }
  144. // SetUserAccess
  145. // @函数名:SetUserAccess
  146. // @函数功能描述: 设置用户权限
  147. // @对象名:c
  148. // @参数定义:req
  149. // @返回值:error
  150. //
  151. func (c *UserLogic) SetUserAccess(req *model.SysAccess) error {
  152. maps := gconv.Map(req)
  153. return c.SvcCtx.SysAccess.Base.Replace(maps)
  154. }
  155. // GetUserAccess
  156. // @函数名:GetUserAccess
  157. // @函数功能描述: 获取用户权限
  158. // @对象名:c
  159. // @参数定义:req
  160. // @返回值:[]*model.SysAccess
  161. // @返回值:error
  162. //
  163. func (c *UserLogic) GetUserAccess(req *dto.GetUserAccessRequest) ([]*model.SysAccess, error) {
  164. var (
  165. resp []*model.SysAccess
  166. )
  167. cond := new(dao.Condition)
  168. where := map[string]any{}
  169. if req.UserId != 0 {
  170. where[c.SvcCtx.SysAccess.Usrid.String()] = req.UserId
  171. }
  172. cond.Where = where
  173. err := c.SvcCtx.SysAccess.Base.Find(cond, &resp)
  174. return resp, err
  175. }
  176. func (c *UserLogic) DelUserAccess(req *dto.DelUserAccessRequest) error {
  177. cond := new(dao.Condition)
  178. where := map[string]any{}
  179. if req.AccessId != 0 {
  180. where["id"] = req.AccessId
  181. }
  182. if req.UserId != 0 {
  183. where["usrid"] = req.UserId
  184. }
  185. cond.Where = where
  186. err := c.SvcCtx.SysAccess.Base.Deletes(cond)
  187. return err
  188. }