fengxiang
2018-09-27 ba18a3f6631c6ea3f43a134d77e2c0233f883340
src/main/java/com/moral/service/impl/AccountServiceImpl.java
@@ -2,19 +2,16 @@
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 java.util.*;
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.ObjectUtils;
@@ -25,6 +22,7 @@
import com.moral.common.exception.BusinessException;
import com.moral.common.util.Crypto;
import com.moral.common.util.ResourceUtil;
import com.moral.common.util.ValidateUtil;
import com.moral.entity.Account;
import com.moral.entity.Organization;
import com.moral.mapper.AccountMapper;
@@ -37,7 +35,8 @@
@Service
public class AccountServiceImpl implements AccountService {
    @Resource
   private  BCryptPasswordEncoder encoder;
   @Resource
   private AccountMapper accountMapper;
@@ -46,21 +45,24 @@
   @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"));
      account.setPassword(md5((String) parameters.get("password")));
      String rawPassword = (String) parameters.get("password");
//      account.setPassword(encoder.encode((String) parameters.get("password")));
      account = accountMapper.selectOne(account);
      if (isEmpty(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","您的账号已禁用,请联系管理员!");
         }
@@ -74,12 +76,15 @@
      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 = (String) parameters.get("accountId");
      String accountId = parameters.get("accountId").toString();
      accountId = accountId.replaceFirst("-", "");
      if (!isNumeric((String) parameters.get("accountId"))) {
      if (!isNumeric(accountId)) {
         throw new BusinessException("accountId 参数不合法!");
      }
@@ -114,22 +119,40 @@
      }
      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())) {
            Organization organization = organizationMapper.selectByPrimaryKey(account.getOrganizationId());
            account.setOrganization(organization);
            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(Crypto.md5(ResourceUtil.getValue("password")));
         account.setPassword(encoder.encode(ResourceUtil.getValue("password")));
         return accountMapper.insertSelective(account);
      } else {
         return accountMapper.updateByPrimaryKeySelective(account);
@@ -153,4 +176,27 @@
      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;
   }
}