xufenglei
2017-12-21 df7be8d9d9f64f88434c05328bbb03c3cf8ad4bb
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
package com.moral.service.impl;
 
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.Crypto.md5;
import static com.moral.common.util.ResourceUtil.getValue;
import static org.apache.commons.lang3.StringUtils.isNumeric;
import static org.springframework.util.ObjectUtils.isEmpty;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
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.entity.Account;
import com.moral.mapper.AccountMapper;
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 AccountMapper accountMapper;
 
    @Resource
    private OrganizationService organizationService;
 
    @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"));
        account.setPassword(md5((String) parameters.get("password")));
        account = accountMapper.selectOne(account);
        if (isEmpty(account)) {
            result.put("msg", "用户名及密码输入错误!");
        } else {
            if (IS_DELETE_FALSE.equals(account.getIsDelete())) {
                result.put("msg", "登录成功!");
                result.put("accountId", account.getId());
                result.put("orgId", account.getOrganizationId());
            } 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 void setOrgIdsByAccount(Map<String, Object> parameters) {
        String accountId = (String) parameters.get("accountId");
        accountId = accountId.replaceFirst("-", "");
        if (!isNumeric((String) parameters.get("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("name")) {
            criteria.andLike("name", "%" + (String) parameters.get("name") + "%");
        }
        if (parameters.containsKey("mobile")) {
            criteria.andLike("mobile", "%" + (String) parameters.get("mobile") + "%");
        }
        PageHelper.startPage(Integer.valueOf((String) parameters.get("pageIndex")), Integer.valueOf((String) parameters.get("pageSize")));
        List<Account> accounts = accountMapper.selectByExample(example);
        return new PageBean<Account>(accounts);
    }
 
    @Override
    @Transactional
    public Integer saveOrUpdateAccount(Account account) {
        if (ObjectUtils.isEmpty(account.getId())) {
            account.setIsDelete(Constants.IS_DELETE_FALSE);
            account.setCreateTime(new Date());
            return accountMapper.insertSelective(account);
        }else {
            return accountMapper.updateByPrimaryKeySelective(account);
        }
    }
 
}