liuxm
2024-05-16 fffea208c02105c37de1a15de81f2b868fa70ab5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.dy.pmsBase.user;
 
import com.dy.common.webUtil.QueryResultVo;
import com.dy.pmsGlobal.daoBa.BaUserMapper;
import com.dy.pmsGlobal.daoBa.BaUserRoleMapper;
import com.dy.pmsGlobal.pojoBa.BaUser;
import com.dy.pmsGlobal.pojoBa.BaUserRole;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.PojoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
import java.util.Map;
import java.util.Random;
 
@Slf4j
@Service
public class UserSv {
    @Autowired
    private BaUserMapper dao;
    @Autowired
    private BaUserRoleMapper urDao;
 
    /**
     * 生成指定位数随机数字密码
     *
     * @param length 多长随机数
     * @return 随机数
     */
    public String getStringRandom(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            if ("char".equalsIgnoreCase(charOrNum)) {
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }
 
    /**
     * 得到一个用户
     *
     * @param id 用户ID
     * @return 用户实体
     */
    public BaUser selectById(Long id) {
        return this.dao.selectByPrimaryKey(id);
    }
 
 
    /**
     * 获取用户列表
     */
    public QueryResultVo<List<BaUser>> selectSome(QueryVo queryVo) {
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo);
 
        //查询符合条件的记录总数
        Long itemTotal = this.dao.selectSomeCount(params);
 
        QueryResultVo<List<BaUser>> rsVo = new QueryResultVo<>(queryVo.pageSize, queryVo.pageCurr) ;
        //计算分页等信息
        rsVo.calculateAndSet(itemTotal, params);
 
        //查询符合条件的记录
        rsVo.obj = this.dao.selectSome(params) ;
        return rsVo ;
    }
 
 
    /**
     * 保存实体
     * @param po 实体
     * @return 影响记录数量
     */
    @Transactional
    public Long save(BaUser po){
        this.dao.insertSelective(po) ;
        Long id = po.getId();
        this.saveUserRoles(id, po.roleIds) ;
        return id ;
    }
 
    /**
     * 保存修改实体
     * @param po 实体
     * @return 影响记录数量
     */
    @Transactional
    public int update(BaUser po) {
        int count = this.dao.updateByPrimaryKeySelective(po);
        this.saveUserRoles(po.id, po.roleIds) ;
        return count ;
    }
 
    /**
     * 禁用或启用用户信息
     * @param id
     * @param disabled
     * @return
     */
    @Transactional
    public int disabled(Long id,Boolean disabled) {
        BaUser user=new BaUser();
        user.id=id;
        user.disabled=disabled;
        return dao.updateByPrimaryKeySelective(user);
    }
 
    /**
     * 修改密码
     *
     * @param id       用户ID
     * @param password 新密码
     * @return 影响记录数量
     */
    @Transactional
    public int changePassword(Long id, String password) {
        return this.dao.changePassword(id, password);
    }
 
 
    /**
     * 保存用户与角色关系
     * @param userId  用户id
     * @param roleIds 选择的角色id集合
     * @return 插入用户与角色关联记录数量
     */
    @Transactional
    public int saveUserRoles(Long userId, String[] roleIds) {
        this.urDao.deleteByUserId(userId);
        int count = 0;
        if (roleIds != null && roleIds.length > 0) {
            for (String roleId : roleIds) {
                count += this.urDao.insertSelective(new BaUserRole(userId, Long.parseLong(roleId)));
            }
        }
        return count ;
    }
 
    /**
     * 逻辑删除实体
     * @param id 实体ID
     * @return 影响记录数量
     */
    @Transactional
    public int delete(Long id) {
        return this.dao.deleteLogicById(id);
    }
 
 
}