ZhuDongming
2019-12-27 47a26c3365cfe2a9705aea8ae0887086f521ef7b
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package com.moral.service.impl;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
 
import static com.moral.common.bean.Constants.IS_DELETE_FALSE;
import static com.moral.common.bean.Constants.IS_DELETE_TRUE;
import static com.moral.common.util.ResourceUtil.getValue;
import static org.apache.commons.lang3.StringUtils.isNumeric;
import static org.springframework.util.ObjectUtils.isEmpty;
 
import javax.annotation.Resource;
 
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
 
import com.github.pagehelper.PageHelper;
import com.moral.common.bean.Constants;
import com.moral.common.bean.PageBean;
import com.moral.common.exception.BusinessException;
import com.moral.common.util.ResourceUtil;
import com.moral.common.util.ValidateUtil;
import com.moral.entity.Account;
import com.moral.entity.Menu;
import com.moral.entity.Organization;
import com.moral.entity.Role;
import com.moral.mapper.AccountMapper;
import com.moral.mapper.OrganizationMapper;
import com.moral.service.AccountService;
import com.moral.service.OrganizationService;
 
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
 
@Service
public class AccountServiceImpl implements AccountService {
    @Resource
    private BCryptPasswordEncoder encoder;
 
    @Resource
    private AccountMapper accountMapper;
 
    @Resource
    private OrganizationService organizationService;
 
    @Resource
    private OrganizationMapper organizationMapper;
 
    @Override
    public Map<String, Object> screenLogin(Map<String, Object> parameters) {
        Map<String, Object> result = new HashMap<String, Object>();
        Account account = new Account();
        account.setAccountName((String) parameters.get("account"));
        String rawPassword = (String) parameters.get("password");
//        account.setPassword(encoder.encode((String) parameters.get("password")));
        account = accountMapper.selectOne(account);
        boolean isValid = account == null ? false : encoder.matches(rawPassword, account.getPassword());
        if (!isValid) {
            result.put("msg", "用户名及密码输入错误!");
        } else {
            if (IS_DELETE_FALSE.equals(account.getIsDelete())) {
                result.put("msg", "登录成功!");
                result.put("accountId", account.getId());
                result.put("orgId", account.getOrganizationId());
                setOrgIdsByAccount(result);
            } else {
                result.put("msg", "您的账号已禁用,请联系管理员!");
            }
        }
        return result;
    }
 
    @Override
    public Map<String, Object> screenLoginNew(Map<String, Object> parameters) {
        Map<String, Object> result = new HashMap<String, Object>();
        Account account = new Account();
        account.setAccountName((String) parameters.get("account"));
        String rawPassword = (String) parameters.get("password");
//        account.setPassword(encoder.encode((String) parameters.get("password")));
        account = accountMapper.selectOne(account);
        Integer existRole=accountMapper.getScreenRoleByAccountName(account.getAccountName());
        List<Menu> menuList = accountMapper.getScreenMenuListsByAccountName(account.getAccountName());
        boolean isValid = account == null ? false : encoder.matches(rawPassword, account.getPassword());
        if (!isValid) {
            result.put("msg", "用户名及密码输入错误!");
        } else {
            if (IS_DELETE_FALSE.equals(account.getIsDelete())) {
                if(existRole!=null) {
                    result.put("msg", "登录成功!");
                    result.put("accountId", account.getId());
                    result.put("orgId", account.getOrganizationId());
                    result.put("data",menuList);
                    setOrgIdsByAccount(result);
                }else{
                    result.put("msg", "账户没有权限!");
                }
            } else {
                result.put("msg", "您的账号已禁用,请联系管理员!");
            }
        }
        return result;
    }
 
    @Override
    public Account getAccountByAccountName(String accountName) {
        Account account = new Account();
        account.setAccountName(accountName);
        return accountMapper.selectOne(account);
    }
 
    @Override
    public Account getAccountById(Integer id) {
        return accountMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public void setOrgIdsByAccount(Map<String, Object> parameters) {
        String accountId = parameters.get("accountId").toString();
        accountId = accountId.replaceFirst("-", "");
        if (!isNumeric(accountId)) {
            throw new BusinessException("accountId 参数不合法!");
        }
 
        Account account = accountMapper.selectByPrimaryKey((Integer.valueOf(accountId)));
        if (isEmpty(account) || IS_DELETE_TRUE.equals(account.getIsDelete())) {
            throw new BusinessException(accountId + ":该账号不存在!");
        }
        Integer orgId = account.getOrganizationId();
        // 不是摩瑞尔账号的需要根据组织来获取数据权限
 
        if (!(-1 == orgId || getValue("orgId").equals(orgId + ""))) {
            Set<Integer> orgIds = organizationService.getChildOrganizationIds(orgId);
            parameters.put("orgIds", orgIds);
        }
    }
 
    @Override
    public PageBean<Account> getAccountListByPage(Map<String, Object> parameters) {
        Example example = new Example(Account.class);
        Criteria criteria = example.createCriteria();
        if (parameters.containsKey("accountName")) {
            criteria.andLike("accountName", "%" + (String) parameters.get("accountName") + "%");
        }
        if (parameters.containsKey("mobile")) {
            criteria.andLike("mobile", "%" + (String) parameters.get("mobile") + "%");
        }
        if (parameters.containsKey("isDelete")) {
            criteria.andEqualTo("isDelete", parameters.get("isDelete"));
        }
        if (parameters.containsKey("sorter")) {
            example.setOrderByClause((String) parameters.get("sorter"));
        }
        PageHelper.startPage(Integer.valueOf((String) parameters.get("pageIndex")), Integer.valueOf((String) parameters.get("pageSize")));
        List<Account> accounts = accountMapper.selectByExample(example);
        Set<Integer> organizationIds = new HashSet<Integer>();
        for (Account account : accounts) {
            if (!ObjectUtils.isEmpty(account.getOrganizationId())) {
                organizationIds.add(account.getOrganizationId());
            }
        }
        if (!ObjectUtils.isEmpty(organizationIds)) {
            example = new Example(Organization.class);
            example.or().andIn("id", organizationIds);
            List<Organization> organizations = organizationMapper.selectByExample(example);
            for (Account account : accounts) {
                for (Organization organization : organizations) {
                    if (account.getOrganizationId() == organization.getId()) {
                        account.setOrganization(organization);
                        break;
                    }
                }
            }
        }
 
        return new PageBean<Account>(accounts);
    }
 
    @Override
    @Transactional
    public Integer saveOrUpdateAccount(Account account) {
        // 重置密码
        if (!StringUtils.isBlank(account.getPassword())) {
            account.setPassword(encoder.encode(account.getPassword()));
        }
        if (ObjectUtils.isEmpty(account.getId())) {
            account.setIsDelete(Constants.IS_DELETE_FALSE);
            account.setCreateTime(new Date());
            account.setPassword(encoder.encode(ResourceUtil.getValue("password")));
            return accountMapper.insertSelective(account);
        } else {
            return accountMapper.updateByPrimaryKeySelective(account);
        }
    }
 
    @Override
    @Transactional
    public Integer deleteAccountsByLogic(List<Integer> ids) {
        Account account = new Account();
        account.setIsDelete(Constants.IS_DELETE_TRUE);
        Example example = new Example(Account.class);
        example.or().andIn("id", ids);
        return accountMapper.updateByExampleSelective(account, example);
    }
 
    @Override
    public Integer getAccountCountByAccountName(String accountName) {
        Account account = new Account();
        account.setAccountName(accountName);
        return accountMapper.selectCount(account);
    }
 
    @Override
    public Optional<Account> queryAccountByName(String accountName) {
        Account account = accountMapper.getByAccountName(accountName);
        return Optional.ofNullable(account);
    }
 
    @Override
    public Account companyLogin(Map<String, Object> parameters) {
        ValidateUtil.notNull(parameters.get("account"), "param.is.null");
        ValidateUtil.notNull(parameters.get("password"), "param.is.null");
        Account account = new Account();
        account.setIsDelete(Constants.IS_DELETE_FALSE);
        account.setAccountName(parameters.get("account").toString());
        account = accountMapper.selectOne(account);
        if (ObjectUtils.isEmpty(account)) {
            throw new BusinessException("账户不存在,请联系管理员!");
        }
        if (!encoder.matches(parameters.get("password").toString(), account.getPassword())) {
            throw new BusinessException("密码错误,请重新输入!");
        }
        account.setPassword(parameters.get("password").toString());
        return account;
    }
 
    @Override
    public Map<String, Object> getOrganizationIdByAccountId(String id) {
        Integer accountId = Integer.parseInt(id);
        Map<String, Object> map = accountMapper.getOrganizationIdByAccountId(accountId);
        return map;
    }
 
    @Override
    public List<Role> getRolesByAccountName(String accountName) {
        List<Role> roleList = accountMapper.getRolesByAccountName(accountName);
        return roleList;
    }
 
    @Override
    public Map<String, Object> getMenuListsByAccountName(String accountName) {
        List<Menu> menuList = accountMapper.getParentMenuListsByAccountName(accountName);
        String email = accountMapper.getEmailByAccountName(accountName);
        Map<String, Object> mapList = new LinkedHashMap<>();
        Map<String, Object> appMap = new LinkedHashMap<>();
        appMap.put("name", "七星瓢虫环境监测");
        appMap.put("description", "七星瓢虫环境监测后台配置中心");
        mapList.put("app", appMap);
        Map<String, Object> userMap = new LinkedHashMap<>();
        userMap.put("name", accountName);
        userMap.put("avatar", "./assets/img/zorro.svg");
        userMap.put("email", email);
        mapList.put("user", userMap);
        Map<String, Object> navigationMap = new LinkedHashMap<>();
        Map<String, Object> navigationChildMap = new LinkedHashMap<>();
        navigationChildMap.put("text", "工作台");
        navigationChildMap.put("link", "/dashboard/workplace");
        navigationChildMap.put("icon", "icon-speedometer");
        navigationChildMap.put("translate", "dashboard_workplace");
        List<Map> navigationChildList = new ArrayList<>();
        navigationChildList.add(navigationChildMap);
        navigationMap.put("text", "主导航");
        navigationMap.put("translate", "main_navigation");
        navigationMap.put("group", true);
        navigationMap.put("children", navigationChildList);
        Map<String, Object> systemMap = new LinkedHashMap<>();
        systemMap.put("text", "系统模块");
        systemMap.put("group", true);
        List<Map> systemList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(menuList)) {
            for (Menu m : menuList) {
                Map<String, Object> systemChildMap = new LinkedHashMap<>();
                systemChildMap.put("text", m.getMenuName());
                systemChildMap.put("icon", m.getMenuIcon());
                List<Menu> childMenuLists = accountMapper.getChildMenuIdsByAccountName(accountName, m.getId());
                List<Map> systemSonList = new ArrayList<>();
                if (!CollectionUtils.isEmpty(childMenuLists)) {
                    for (Menu childMenu : childMenuLists) {
                        Map<String, Object> systemSonMap = new LinkedHashMap<>();
                        systemSonMap.put("text", childMenu.getMenuName());
                        systemSonMap.put("link", childMenu.getMenuUrl());
                        systemSonList.add(systemSonMap);
                    }
                }
                systemChildMap.put("children", systemSonList);
                systemList.add(systemChildMap);
            }
        }
        systemMap.put("children", systemList);
        List<Map> list = new ArrayList<>();
        list.add(navigationMap);
        list.add(systemMap);
        mapList.put("menu", list);
        return mapList;
    }
 
}