wzy
2022-08-14 b141df9bf9764db8567efebed48006e12ab1f657
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package com.xzx.gc.role.service;
 
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xzx.gc.common.constant.CommonEnum;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.dto.log.OperationAppLog;
import com.xzx.gc.common.exception.PlatformException;
import com.xzx.gc.common.exception.RestException;
import com.xzx.gc.common.utils.BusinessUtil;
import com.xzx.gc.common.utils.SecurityUtil;
import com.xzx.gc.entity.*;
import com.xzx.gc.model.ExceptionEnum;
import com.xzx.gc.model.JsonResult;
import com.xzx.gc.model.MiException;
import com.xzx.gc.role.mapper.CityPartnerMapper;
import com.xzx.gc.role.mapper.CoreOrgMapper;
import com.xzx.gc.role.mapper.CoreUserMapper;
import com.xzx.gc.role.model.CoreUserModel;
import com.xzx.gc.role.rbac.UserLoginInfoAdmin;
import com.xzx.gc.util.BeanUtils;
import com.xzx.gc.util.enums.DelFlagEnum;
import com.xzx.gc.util.enums.GeneralStateEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
 
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
@Transactional
public class CoreUserService {
    @Autowired
    CoreUserMapper userMapper ;
    @Autowired
    CityPartnerMapper partnerMapper ;
    @Autowired
    PartnerGaodeService gaodeService;
    @Autowired
    AddressLevelService addressLevelService;
    @Autowired
    CoreOrgMapper orgMapper;
 
    @Autowired
    private CityPartnerMapper cityPartnerMapper;
 
    @Autowired
    private CoreRoleService coreRoleService;
 
    @Autowired
    private CoreUserRoleService coreUserRoleService;
 
    @Autowired
    private CityPartnerService cityPartnerService;
 
 
 
    public UserLoginInfoAdmin login(String userName, String password){
        CoreUser query = new CoreUser();
        query.setCode(userName);
        query.setPassword(password);
 
        CoreUser user =userMapper.selectOne(query);
        if(user==null) {
            throw new RestException("用户"+"不存在或者密码错误");
        }
        if(!user.getState().equals(GeneralStateEnum.ENABLE.getValue())){
            throw new RestException("用户"+"已经失效");
        }
        if(user.getDelFlag()== DelFlagEnum.DELETED.getValue()) {
            throw new RestException("用户"+"已经删除");
        }
 
 
        List<CoreOrg>  orgs = getUserOrg(user.getId(),user.getOrgId());
        UserLoginInfoAdmin loginInfo = new UserLoginInfoAdmin();
        loginInfo.setOrgs(orgs);
        loginInfo.setUser(user);
        return loginInfo;
 
    }
 
    public Map<String,Object> loginApi(String userName, String password,String userType){
        Map<String,Object> map = new HashMap<>();
        String msg = "";
        CoreUser query = new CoreUser();
        List<Map<String,Object>> cityTown=new ArrayList<>();
        Example example = new Example(CoreUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("code", userName);
        criteria.andEqualTo("delFlag", 0);
        CoreUser user =userMapper.selectOneByExample(example);
        //对比密码
        if(user==null) {
 
            Example example1 = new Example(CityPartner.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("accountNo", userName);
            criteria1.andEqualTo("delFlag", 0);
            CityPartner partner =cityPartnerMapper.selectOneByExample(example1);
            if(partner==null){
                msg = "用户"+"不存在或者密码错误";
            }else{
                String salt = partner.getSalt();
 
                if(null!=salt&&!"".equals(salt)){
                    String oldPassword =  SecurityUtil.decrypt(salt,partner.getPassword());
                    if(oldPassword.equals(password)){
                        user = new CoreUser();
                        user.setName(partner.getPartnerName());
                        user.setState(partner.getState());
                        user.setPassword(partner.getPassword());
                        user.setCode(partner.getAccountNo());
                        user.setDelFlag(partner.getDelFlag());
                        user.setAttachmentId(partner.getAttachmentId());
                        user.setJobType0(partner.getJobType0());
                        user.setJobType1(partner.getJobType1());
                        user.setOrgId(Long.parseLong(partner.getOrgId()+""));
                        user.setId(Long.parseLong(partner.getId()+""));
                        List<PartnerGaode> gaodeList = gaodeService.queryPartnerGaodeList(partner.getId()+"");
                        List<String> townIds = new ArrayList<>();
                        for (PartnerGaode gaode:gaodeList) {
                            townIds.add(gaode.getTownId());
                        }
                        List<AddressLevelInfo> addressLevelInfoList=addressLevelService.queryAddressList(townIds);
                        List<Map<String,String>> children = new ArrayList<>();
                        Map<String,Object> map1 = new HashMap<>();
                        String cityName = "";
                        int i=1;
                        for (AddressLevelInfo address:addressLevelInfoList) {
                            Map<String,String> townMap = new HashMap<>();
                            if(cityName.equals(address.getLevel2Name())){
                                townMap.put("name",address.getLevel3Name());
                                townMap.put("id",address.getLevel3Id());
                                children.add(townMap);
                            }else{
                                map1 = new HashMap<>();
                                if(null!=cityName&&!"".equals(cityName)){
                                    map1.put("children",children);
                                    cityTown.add(map1);
                                }else{
                                    map1.put("cityName",address.getLevel2Name());
                                    map1.put("cityId",address.getLevel2Id());
                                    cityName = address.getLevel2Name();
                                    children = new ArrayList<>();
                                    townMap.put("name",address.getLevel3Name());
                                    townMap.put("id",address.getLevel3Id());
                                    children.add(townMap);
                                }
 
                            }
                            if(addressLevelInfoList.size()==i){
                                map1.put("children",children);
                                cityTown.add(map1);
                            }
                            i++;
                        }
                        userType="2";
                    }else{
                        msg = "用户"+"不存在或者密码错误";
                    }
                }else{
                    msg = "用户"+"不存在或者密码错误";
                }
 
            }
 
        }else{
            if(!user.getState().equals(GeneralStateEnum.ENABLE.getValue())){
                msg ="用户"+"已经失效";
            }
            if(user.getDelFlag()==DelFlagEnum.DELETED.getValue()) {
                msg ="用户"+"已经删除";
            }
            String salt = user.getSalt();
 
            if(null!=salt&&!"".equals(salt)) {
                String oldPassword = SecurityUtil.decrypt(salt, user.getPassword());
                if (oldPassword.equals(password)) {
                }else{
                    msg = "用户"+"不存在或者密码错误";
                }
            }else{
                if (user.getPassword().equals(password)) {
                }else{
                    msg = "用户"+"不存在或者密码错误";
                }
            }
        }
        UserLoginInfoAdmin loginInfo = new UserLoginInfoAdmin();
 
        if(!"".equals(msg)){
            map.put("msg",msg);
            map.put("userLoginInfo",loginInfo);
        }else{
            List<CoreOrg> orgs = new ArrayList<>();
            if(null!=user.getOrgId()){
                  orgs = getUserOrg(user.getId(),user.getOrgId());
            }else{
                orgs = getUserOrg(user.getId(),1);
            }
            loginInfo.setOrgs(orgs);
            loginInfo.setUser(user);
            map.put("userLoginInfo",loginInfo);
            map.put("userType",userType);
            map.put("cityTown",cityTown);
        }
 
        return map;
 
    }
 
    public  List<CoreOrg> getUserOrg(long userId,long orgId){
        List<CoreOrg> orgs =  orgMapper.queryOrgByUser(userId);
        if(orgs.isEmpty()){
            //没有赋值任何角色,默认给一个所在部门
            //排除打包站
            if(orgId==16){
                throw new MiException(ExceptionEnum.MONITOR_EXCEPTION);
            }
            CoreOrg userOrg = orgMapper.selectByPrimaryKey(orgId);
            orgs.add(userOrg);
        }
        return orgs;
    }
 
 
 
    public void update(CoreUser user){
        userMapper.updateByPrimaryKeySelective(user);
    }
 
 
    public CoreUser getUserById(Long userId){
        return userMapper.selectOneByExample(userId);
    }
 
    public CoreUser getCoreUserById(Long id){
        return userMapper.selectByPrimaryKey(id);
    }
 
 
    public void update(CoreUserModel model) {
 
        Long roleId = model.getRoleId();
        CoreRole byId = coreRoleService.findById(roleId);
        String code = byId.getCode();
 
        String salt = null;
        String newPassword = null;
        if (null != model.getPassword() && !"".equals(model.getPassword())) {
            salt = RandomUtil.randomString(16);
            newPassword = SecurityUtil.encrypt(salt, model.getPassword());
        }
 
        //合伙人
        if (Constants.PARTNER_CODE_QG.equals(code) || Constants.PARTNER_CODE_CS.equals(code)) {
            CityPartner user = BeanUtils.copy(model, CityPartner.class);
            user.setPassword(newPassword);
            user.setSalt(salt);
            cityPartnerService.updateById(user);
 
        } else {
            //用户
            CoreUser user = BeanUtils.copy(model, CoreUser.class);
            user.setPassword(newPassword);
            user.setSalt(salt);
            update(user);
        }
        coreUserRoleService.updateRoleByUserAndOrg(model.getId(), model.getOrgId(), model.getRoleId());
    }
}