ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package com.vci.web.service.impl;
 
import com.vci.corba.common.PLException;
import com.vci.corba.omd.data.BusinessObject;
import com.vci.model.SmPasswordStrategyDO;
import com.vci.omd.utils.ObjectTool;
import com.vci.pagemodel.SmPasswordStrategyVO;
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.util.BeanUtilForVCI;
import com.vci.starter.web.util.Lcm.Func;
import com.vci.starter.web.wrapper.VciQueryWrapperForDO;
import com.vci.web.service.SmUserQueryServiceI;
import com.vci.web.service.WebBoServiceI;
import com.vci.web.service.WebPwdStrategyQueryServiceI;
import com.vci.web.util.PlatformClientUtil;
import com.vci.web.util.WebUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
import static com.vci.constant.FrameWorkBusLangCodeConstant.DATA_OID_NOT_EXIST;
 
/**
 * @Description 密码策略查询
 * @Author dangsn
 * @Date 2024/11/29 11:23
 */
@Service
public class WebPwdStrategyQueryServiceImpl implements WebPwdStrategyQueryServiceI {
 
    /**
     * 日志
     */
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    /**
     * 平台调用客户端
     */
    @Autowired
    private PlatformClientUtil platformClientUtil;
 
    /**
     * 业务数据服务
     */
    @Autowired
    private WebBoServiceI boService;
 
    /**
     * 用户查询服务
     */
    @Autowired
    private SmUserQueryServiceI smUserQueryService;
 
    /**
     * 获取默认密码策略
     * @return
     */
    public SmPasswordStrategyVO getPasswordStrategyVOByDefault(){
        //获取默认的
        VciQueryWrapperForDO queryWrapperForDO = new VciQueryWrapperForDO(null, SmPasswordStrategyDO.class);
        queryWrapperForDO.eq("plisdefault","1");
        List<BusinessObject> cboList = boService.queryBySql(queryWrapperForDO.getSelectFieldSql() + " from plpasswordstrategy " +
                queryWrapperForDO.getTableNick() + queryWrapperForDO.getLinkTableSql() +
                (StringUtils.isBlank(queryWrapperForDO.getWhereSql()) ? "" : (" where " + queryWrapperForDO.getWhereSql())), null);
        if(!CollectionUtils.isEmpty(cboList)){
            SmPasswordStrategyDO passwordStrategyDO = new SmPasswordStrategyDO();
            WebUtil.copyValueToObjectFromCbos(cboList.get(0),passwordStrategyDO);
            return pwdStrategyDO2VO(passwordStrategyDO);
        }
        return null;
    }
 
    /**
     * 密码策略do对象转vo对象
     * @param smPasswordStrategyDO
     * @return
     */
    private SmPasswordStrategyVO pwdStrategyDO2VO(SmPasswordStrategyDO smPasswordStrategyDO){
        SmPasswordStrategyVO passwordStrategyVO = new SmPasswordStrategyVO();
        BeanUtilForVCI.convert(smPasswordStrategyDO,passwordStrategyVO);
        return passwordStrategyVO;
    }
 
    /**
     * 根据用户的主键,获取用户的密码安全策略
     * @param userOid 用户的主键
     * @return 密码安全策略的显示对象,如果不存在则会返回Null
     * @throws VciBaseException 参数为空或者数据库查询出错的时候会抛出异常
     */
    @Override
    public SmPasswordStrategyVO getPasswordStrategyVOByUserOid(String userOid) throws PLException {
        WebUtil.alertNotNull(userOid,"用户的主键");
        if(!smUserQueryService.checkUserExist(null,userOid)){
            throw new VciBaseException(DATA_OID_NOT_EXIST);
        }
        String sql = "select plpasswordstrategyuid,pluseruid from pluserpasswordstrategy where pluseruid = '"+ userOid +"'";
        List<BusinessObject> cbos = boService.queryBySql(sql, null);
        if(Func.isNotEmpty(cbos)){
            return getPasswordStrategyVOByOid(ObjectTool.getNewBOAttributeValue(cbos.get(0), "plpasswordstrategyuid"));
        }
        return null;
    }
 
    /**
     * 批量根据用户的主键来获取密码策略
     * @param userOidCollection 用户主键集合
     * @return 密码策略的显示对象,key是用户主键,value是这个用户关联的密码策略
     */
    @Override
    public Map<String, SmPasswordStrategyVO> batchSmPwdStrategyByUserOids(Collection<String> userOidCollection) {
        if(CollectionUtils.isEmpty(userOidCollection)){
            return new HashMap<>();
        }
        Map<String,SmPasswordStrategyVO> smPasswordStrategyVOMap = new HashMap<>();
        Map<String,String> userPasswordStrategyVOMap = new HashMap<>();
 
        Map<String/*用户id*/, SmPasswordStrategyVO/*密码策略*/> returnMap = new HashMap<>();
        WebUtil.switchCollectionForOracleIn(userOidCollection).stream().forEach(userOids->{
            //查询密码策略关联信息,key为用户oid:value为密码策略oid
            Map<String, String> userPwdStrategyMap = mapUserPwdStrategy(userOids);
            userPasswordStrategyVOMap.putAll(userPwdStrategyMap);
            //查询密码策略,key为密码策略主键:value为密码策略
            smPasswordStrategyVOMap.putAll(mapPasswordStrategyVOMapByOid(userPwdStrategyMap.values()));
        });
        //查询默认的密码策略
        SmPasswordStrategyVO passwordStrategyVOByDefault = getPasswordStrategyVOByDefault();
        //循环用户id,查询是否有符合条件的oid
        userOidCollection.stream().forEach(oid->{
            SmPasswordStrategyVO smPasswordStrategyVO;
            //通过用户oid没获取到密码策略oid,说明没有给当前用户设置策略,直接返默认的密码策略
            String pwdStrategyId = userPasswordStrategyVOMap.get(oid);
            if(Func.isNotBlank(pwdStrategyId)){
                //通过密码策略oid去map中取密码策略
                smPasswordStrategyVO = smPasswordStrategyVOMap.get(pwdStrategyId);
            }else {
                smPasswordStrategyVO = passwordStrategyVOByDefault;
            }
            returnMap.put(oid,smPasswordStrategyVO);
        });
        return returnMap;
    }
 
    /**
     * 根据用户主键查询密码策略关联表中的密码策略主键
     * @param userOids
     * @return key为用户oid:value为密码策略oid
     */
    private Map<String,String> mapUserPwdStrategy(Collection<String> userOids){
        Map<String,String> userPwdStrategyMap = new HashMap<>();
        WebUtil.switchCollectionForOracleIn(userOids).stream().forEach(userOidSplit->{
            //查关联表sql
            String sql = "select plpasswordstrategyuid,pluseruid from pluserpasswordstrategy where pluseruid in (" + WebUtil.toInSql(userOidSplit.toArray(new String[0])) + ")";
            List<BusinessObject> cbos = boService.queryBySql(sql, null);
            cbos.stream().forEach(cbo->{
                String pluseruid = ObjectTool.getNewBOAttributeValue(cbo, "pluseruid");
                String plpasswordstrategyuid = ObjectTool.getNewBOAttributeValue(cbo, "plpasswordstrategyuid");
                userPwdStrategyMap.put(pluseruid,plpasswordstrategyuid);
            });
        });
        return userPwdStrategyMap;
    }
 
    /**
     * 根据主键查询密码策略map对象
     * @param oidList
     * @return key为密码策略主键 value为密码策略
     */
    @Override
    public Map<String, SmPasswordStrategyVO> mapPasswordStrategyVOMapByOid(Collection<String> oidList) {
        VciQueryWrapperForDO queryWrapperForDO = new VciQueryWrapperForDO(null, SmPasswordStrategyDO.class);
        queryWrapperForDO.in("oid",oidList.stream().collect(Collectors.joining(",")));
        List<BusinessObject> cboList = boService.queryBySql(queryWrapperForDO.getSelectFieldSql() + " from plpasswordstrategy " +
                queryWrapperForDO.getTableNick() + queryWrapperForDO.getLinkTableSql() +
                (StringUtils.isBlank(queryWrapperForDO.getWhereSql()) ? "" : (" where " + queryWrapperForDO.getWhereSql())), null);
        Map<String,SmPasswordStrategyVO> smPasswordStrategyVOMap = new HashMap<>();
        if(Func.isEmpty(cboList)){
            return new HashMap<>();
        }
        cboList.stream().forEach(item->{
            SmPasswordStrategyDO passwordStrategyDO = new SmPasswordStrategyDO();
            WebUtil.copyValueToObjectFromCbos(item,passwordStrategyDO);
            SmPasswordStrategyVO passwordStrategyVO = new SmPasswordStrategyVO();
            BeanUtilForVCI.convert(passwordStrategyDO,passwordStrategyVO);
            smPasswordStrategyVOMap.put(passwordStrategyVO.getOid(),passwordStrategyVO);
        });
        return smPasswordStrategyVOMap;
    }
 
    /**
     * 使用主键获取密码策略
     * @param oid 主键
     * @return 密码策略显示对象
     */
    public SmPasswordStrategyVO getPasswordStrategyVOByOid(String oid){
        VciQueryWrapperForDO queryWrapperForDO = new VciQueryWrapperForDO(null, SmPasswordStrategyDO.class);
        queryWrapperForDO.eq("oid",oid.trim());
        List<BusinessObject> cboList = boService.queryBySql(queryWrapperForDO.getSelectFieldSql() + " from plpasswordstrategy " +
                queryWrapperForDO.getTableNick() + queryWrapperForDO.getLinkTableSql() +
                (StringUtils.isBlank(queryWrapperForDO.getWhereSql()) ? "" : (" where " + queryWrapperForDO.getWhereSql())), null);
        if(!CollectionUtils.isEmpty(cboList)){
            SmPasswordStrategyDO passwordStrategyDO = new SmPasswordStrategyDO();
            WebUtil.copyValueToObjectFromCbos(cboList.get(0),passwordStrategyDO);
            SmPasswordStrategyVO passwordStrategyVO = new SmPasswordStrategyVO();
            BeanUtilForVCI.convert(passwordStrategyDO,passwordStrategyVO);
            return passwordStrategyVO;
        }else{
            //获取默认的
            queryWrapperForDO = new VciQueryWrapperForDO(null, SmPasswordStrategyDO.class);
            queryWrapperForDO.eq("plisdefault","1");
            cboList = boService.queryBySql(queryWrapperForDO.getSelectFieldSql() + " from plpasswordstrategy " +
                    queryWrapperForDO.getTableNick() + queryWrapperForDO.getLinkTableSql() +
                    (StringUtils.isBlank(queryWrapperForDO.getWhereSql()) ? "" : (" where " + queryWrapperForDO.getWhereSql())), null);
            if(!CollectionUtils.isEmpty(cboList)){
                SmPasswordStrategyDO passwordStrategyDO = new SmPasswordStrategyDO();
                WebUtil.copyValueToObjectFromCbos(cboList.get(0),passwordStrategyDO);
                return pwdStrategyDO2VO(passwordStrategyDO);
            }
        }
        return null;
    }
}