xufenglei
2018-01-26 fae2a14914c00d4e450ef855754c3687279e398a
src/main/java/com/moral/service/impl/AccountServiceImpl.java
@@ -7,7 +7,9 @@
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.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -15,14 +17,24 @@
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.common.util.Crypto;
import com.moral.common.util.ResourceUtil;
import com.moral.entity.Account;
import com.moral.entity.Organization;
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 {
@@ -33,17 +45,19 @@
   @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>();
      Example example = new Example(Account.class);
      String password = md5((String) parameters.get("account"));
      example.or().andEqualTo("accountName",parameters.get("account")).andEqualTo("password",password);
      List<Account> accounts = accountMapper.selectByExample(example);
      if (isEmpty(accounts) || accounts.size() != 1) {
      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 {
         Account account = accounts.get(0);
         if (IS_DELETE_FALSE.equals(account.getIsDelete())) {
            result.put("msg", "登录成功!");
            result.put("accountId", account.getId());
@@ -56,17 +70,10 @@
   }
   @Override
    public List<Account> getAccountLists(String accountName, String password) {
      Example example = new Example(Account.class);
      example.or().andEqualTo("accountName",accountName).andEqualTo("password",password);
      return accountMapper.selectByExample(example);
   }
   @Override
   public List<Account> getAccountList(String accountName) {
      Example example = new Example(Account.class);
      example.or().andEqualTo("accountName",accountName);
      return accountMapper.selectByExample(example);
   public Account getAccountByAccountName(String accountName) {
      Account account = new Account();
      account.setAccountName(accountName);
      return accountMapper.selectOne(account);
   }
   @Override
@@ -84,10 +91,81 @@
      Integer orgId = account.getOrganizationId();
      // 不是摩瑞尔账号的需要根据组织来获取数据权限
      
      if (!(-1 == orgId  || getValue("orgId").equals(orgId))) {
      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 (ObjectUtils.isEmpty(account.getId())) {
         account.setIsDelete(Constants.IS_DELETE_FALSE);
         account.setCreateTime(new Date());
         account.setPassword(Crypto.md5(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);
   }
}