| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- package logic
- import (
- "errors"
- "fmt"
- "git.rtzhtech.cn/iss/public-lib/dao"
- "git.rtzhtech.cn/iss/public-lib/dto"
- "git.rtzhtech.cn/iss/public-lib/model"
- "git.rtzhtech.cn/iss/public-lib/svc"
- "github.com/gogf/gf/v2/util/gconv"
- )
- // NewUserLogic
- // @函数名:NewUserLogic
- // @函数功能描述: 初始化user对象
- // @返回值:*UserLogic
- //
- func NewUserLogic() *UserLogic {
- return &UserLogic{
- SvcCtx: svc.SvcCtx,
- }
- }
- type UserLogic struct {
- SvcCtx *svc.ServiceContext
- }
- // SaveUser
- // @函数名:SaveUser
- // @函数功能描述: 保存用户
- // @对象名:c
- // @参数定义:req
- // @返回值:*model.SysUsr
- // @返回值:error
- //
- func (c *UserLogic) SaveUser(req *model.SysUsr) (*model.SysUsr, error) {
- var (
- resp = new(model.SysUsr)
- )
- maps := gconv.Map(req)
- err := c.SvcCtx.SysUsr.Base.Replace(maps)
- if err != nil {
- return resp, err
- }
- cond := dao.Condition{
- Where: map[string]any{
- "usrname": req.Usrname,
- },
- }
- err = c.SvcCtx.SysUsr.Base.Find(&cond, resp)
- if err != nil {
- return resp, err
- }
- return resp, nil
- }
- // UpdateUserMap
- // @函数名:UpdateUserMap
- // @函数功能描述: 通过map修改用户
- // @对象名:c
- // @参数定义:maps
- // @返回值:*model.SysUsr
- // @返回值:error
- //
- func (c *UserLogic) UpdateUserMap(maps map[string]interface{}) (*model.SysUsr, error) {
- var (
- resp = new(model.SysUsr)
- )
- if maps == nil || len(maps) == 0 {
- return resp, errors.New("参数为空")
- }
- if val, ok := maps["id"]; !ok || gconv.Int(val) == 0 {
- return resp, errors.New("缺少id")
- }
- c.SvcCtx.SysUsr.Base.Replace(maps)
- cond := dao.Condition{
- Where: map[string]any{
- "id": maps["id"],
- },
- }
- err := c.SvcCtx.SysUsr.Base.Find(&cond, resp)
- if err != nil {
- return resp, err
- }
- return resp, nil
- }
- // DelUser
- // @函数名:DelUser
- // @函数功能描述: 删除用户
- // @对象名:c
- // @参数定义:id
- // @返回值:error
- //
- func (c *UserLogic) DelUser(id int32) error {
- md := c.SvcCtx.SysUsr
- _, err := md.Where(md.ID.Eq(id)).Delete()
- return err
- }
- // GetUser
- // @函数名:GetUser
- // @函数功能描述: 获取用户信息
- // @对象名:c
- // @参数定义:req
- // @返回值:*dto.GetUserResponse
- // @返回值:error
- //
- func (c *UserLogic) GetUser(req *dto.GetUserRequest) (*dto.GetUserResponse, error) {
- var (
- resp = new(dto.GetUserResponse)
- )
- cond := new(dao.Condition)
- where := map[string]any{}
- if req.UserName != "" {
- UserNameLikeStr := fmt.Sprintf("%s like ?", c.SvcCtx.SysUsr.Usrname.String())
- where[UserNameLikeStr] = "%" + req.UserName + "%"
- }
- if req.FullName != "" {
- where["usrname"] = req.FullName
- }
- if req.Comment != "" {
- CommentLikeStr := fmt.Sprintf("%s like ?", c.SvcCtx.SysUsr.Comment.String())
- where[CommentLikeStr] = "%" + req.Comment + "%"
- }
- if req.Role != 0 {
- where[c.SvcCtx.SysUsr.Role.String()] = req.Role
- }
- if req.Status != 0 {
- where[c.SvcCtx.SysUsr.Status.String()] = req.Status
- }
- cond.Where = where
- var num int64
- c.SvcCtx.SysUsr.Base.Count(cond, &num)
- if req.Page < 1 {
- req.Page = 1
- }
- if req.Limit < 1 {
- req.Limit = 20
- }
- cond.Limit = req.Limit
- cond.Offset = (req.Page - 1) * req.Limit
- cond.OrderBy = "id desc"
- err := c.SvcCtx.SysUsr.Base.Find(cond, &resp.List)
- if err != nil {
- return resp, err
- }
- resp.Total = num
- return resp, nil
- }
- // SetUserAccess
- // @函数名:SetUserAccess
- // @函数功能描述: 设置用户权限
- // @对象名:c
- // @参数定义:req
- // @返回值:error
- //
- func (c *UserLogic) SetUserAccess(req *model.SysAccess) error {
- maps := gconv.Map(req)
- return c.SvcCtx.SysAccess.Base.Replace(maps)
- }
- // GetUserAccess
- // @函数名:GetUserAccess
- // @函数功能描述: 获取用户权限
- // @对象名:c
- // @参数定义:req
- // @返回值:[]*model.SysAccess
- // @返回值:error
- //
- func (c *UserLogic) GetUserAccess(req *dto.GetUserAccessRequest) ([]*model.SysAccess, error) {
- var (
- resp []*model.SysAccess
- )
- cond := new(dao.Condition)
- where := map[string]any{}
- if req.UserId != 0 {
- where[c.SvcCtx.SysAccess.Usrid.String()] = req.UserId
- }
- cond.Where = where
- err := c.SvcCtx.SysAccess.Base.Find(cond, &resp)
- return resp, err
- }
- func (c *UserLogic) DelUserAccess(req *dto.DelUserAccessRequest) error {
- cond := new(dao.Condition)
- where := map[string]any{}
- if req.AccessId != 0 {
- where["id"] = req.AccessId
- }
- if req.UserId != 0 {
- where["usrid"] = req.UserId
- }
- cond.Where = where
- err := c.SvcCtx.SysAccess.Base.Deletes(cond)
- return err
- }
|