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 javax.annotation.Resource; import javax.management.monitor.Monitor; import com.moral.entity.*; import com.moral.mapper.DictionaryDataMapper; import com.moral.mapper.MonitorPointMapper; import com.moral.service.MonitorPointService; 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.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; 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; @Service public class AccountServiceImpl implements AccountService { @Resource private BCryptPasswordEncoder encoder; @Resource private AccountMapper accountMapper; @Resource private OrganizationService organizationService; @Resource private OrganizationMapper organizationMapper; @Resource private MonitorPointService monitorPointService; @Resource private MonitorPointMapper monitorPointMapper; @Override public Map getAccountInfoById(String accountId) { Map result = new HashMap(); Account account = new Account(); account.setId(Integer.parseInt(accountId)); account = accountMapper.selectOne(account); if (ObjectUtils.isEmpty(account)) { result.put("msg", "token无效"); result.put("accountId", -1); } else { result = judgeAccountInfo(account); String regionCode = getRegionCodeByAccount(account); if (!String.valueOf(result.get("accountId")).equals("-1")) { List monitorPoints = new ArrayList<>(); if (account.getOrganizationId().equals(dictionaryDataMapper.selectSupperOrgId())) { Map paramMap = new HashMap<>(); paramMap.put("regionCode", regionCode); paramMap.put("organizationId", account.getOrganizationId()); monitorPoints = monitorPointService.getMonitorPointsAndDevicesByRegion(paramMap); } else { Set orgIds = organizationService.getChildOrganizationIds(account.getOrganizationId()); orgIds.add(account.getOrganizationId()); monitorPoints = monitorPointService.getMonitorPointsByOrganizationIds(orgIds); } List monitorPointIds = new ArrayList<>(); monitorPoints.forEach(value -> { monitorPointIds.add(value.getId()); }); List menuList = accountMapper.getScreenMenuListsByAccountName(account.getAccountName()); result.put("msg", "获取成功!"); result.put("accountId", account.getId()); result.put("orgId", account.getOrganizationId()); result.put("data", menuList); result.put("regionCode", regionCode); result.put("monitorPointIds", monitorPointIds); setOrgIdsByAccount(result); } } return result; } @Override public Map webLogin(Map parameters) { Map result = new HashMap(); Account account = new Account(); account.setAccountName((String) parameters.get("account")); String rawPassword = (String) parameters.get("password"); account = accountMapper.selectOne(account); boolean isValid = account == null ? false : encoder.matches(rawPassword, account.getPassword()); if (!isValid) { result.put("msg", "用户名及密码输入错误!"); result.put("accountId", -1); } else { result = judgeAccountInfo(account); } return result; } @Override public Map screenLoginNew(Map parameters) { Map result = new HashMap(); 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 = null; List menuList = new ArrayList<>(); if (account != null) { existRole = accountMapper.getScreenRoleByAccountName(account.getAccountName()); menuList = accountMapper.getScreenMenuListsByAccountName(account.getAccountName()); } boolean isValid = account == null ? false : encoder.matches(rawPassword, account.getPassword()); if (!isValid) { result.put("msg", "用户名及密码输入错误!"); result.put("accountId", -1); } 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 Map screenLogin(Map parameters) { Map result = new HashMap(); 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 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 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 orgIds = organizationService.getChildOrganizationIds(orgId); parameters.put("orgIds", orgIds); } } @Override public PageBean getAccountListByPage(Map 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 accounts = accountMapper.selectByExample(example); Set organizationIds = new HashSet(); 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 organizations = organizationMapper.selectByExample(example); for (Account account : accounts) { for (Organization organization : organizations) { if (account.getOrganizationId() == organization.getId()) { account.setOrganization(organization); break; } } } } return new PageBean(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 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 queryAccountByName(String accountName) { Account account = accountMapper.getByAccountName(accountName); return Optional.ofNullable(account); } @Override public Account companyLogin(Map 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 getOrganizationIdByAccountId(String id) { Integer accountId = Integer.parseInt(id); Map map = accountMapper.getOrganizationIdByAccountId(accountId); return map; } @Override public List getRolesByAccountName(String accountName) { List roleList = accountMapper.getRolesByAccountName(accountName); return roleList; } @Override public Map getMenuListsByAccountName(String accountName) { List menuList = accountMapper.getParentMenuListsByAccountName(accountName); Integer organizationId = accountMapper.getByAccountName(accountName).getOrganizationId(); Map organizationMap = new LinkedHashMap<>(); organizationMap.put("organizationId", organizationId); String email = accountMapper.getEmailByAccountName(accountName); Map mapList = new LinkedHashMap<>(); Map appMap = new LinkedHashMap<>(); appMap.put("name", "七星瓢虫环境监测"); appMap.put("description", "七星瓢虫环境监测后台配置中心"); mapList.put("app", appMap); mapList.put("organization", organizationMap); Map userMap = new LinkedHashMap<>(); userMap.put("name", accountName); userMap.put("avatar", "./assets/img/zorro.svg"); userMap.put("email", email); mapList.put("user", userMap); Map navigationMap = new LinkedHashMap<>(); Map navigationChildMap = new LinkedHashMap<>(); navigationChildMap.put("text", "工作台"); navigationChildMap.put("link", "/dashboard/workplace"); navigationChildMap.put("icon", "icon-speedometer"); navigationChildMap.put("translate", "dashboard_workplace"); List navigationChildList = new ArrayList<>(); navigationChildList.add(navigationChildMap); navigationMap.put("text", "主导航"); navigationMap.put("translate", "main_navigation"); navigationMap.put("group", true); navigationMap.put("children", navigationChildList); Map systemMap = new LinkedHashMap<>(); systemMap.put("text", "系统模块"); systemMap.put("group", true); List systemList = new ArrayList<>(); if (!CollectionUtils.isEmpty(menuList)) { for (Menu m : menuList) { Map systemChildMap = new LinkedHashMap<>(); systemChildMap.put("text", m.getMenuName()); systemChildMap.put("icon", m.getMenuIcon()); List childMenuLists = accountMapper.getChildMenuIdsByAccountName(accountName, m.getId()); List systemSonList = new ArrayList<>(); if (!CollectionUtils.isEmpty(childMenuLists)) { for (Menu childMenu : childMenuLists) { Map 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 list = new ArrayList<>(); list.add(navigationMap); list.add(systemMap); mapList.put("menu", list); return mapList; } private Map judgeAccountInfo(Account account) { Map result = new HashMap(); Integer existRole = accountMapper.getScreenRoleByAccountName(account.getAccountName()); if (!IS_DELETE_FALSE.equals(account.getIsDelete())) { result.put("msg", "您的账号已禁用,请联系管理员!"); result.put("accountId", -1); } else if (ObjectUtils.isEmpty(existRole)) { result.put("msg", "账户没有权限!"); result.put("accountId", -1); } else { result.put("msg", "登录成功!"); result.put("accountId", account.getId()); } return result; } @Resource DictionaryDataMapper dictionaryDataMapper; @Override public Map bsWebLogin(Map parameters) { Map result = new HashMap(); Account account = new Account(); account.setAccountName((String) parameters.get("account")); String rawPassword = (String) parameters.get("password"); account = accountMapper.selectOne(account); boolean isValid = account == null ? false : encoder.matches(rawPassword, account.getPassword()); if (!isValid) { result.put("msg", "用户名及密码输入错误!"); result.put("accountId", -1); } else { result = judgeAccountInfo(account); } return result; } /** * @Description: 根据id获取默认显示的地区码 * @Param: [params] * @return: java.util.Map * @Author: 下雨听风 * @Date: 2020/11/25 */ private String getRegionCodeByAccount(Account account) { Organization organization = new Organization(); organization.setId(account.getOrganizationId()); organization = organizationMapper.selectOne(organization); Map regionCodeAndType = getRegionCodeAndTypeByOrg(organization); if (ObjectUtils.isEmpty(regionCodeAndType)) return null; return (String) regionCodeAndType.get("regionCode"); } public Map getRegionCodeAndTypeByOrg(Organization organization) { Map result = new HashMap<>(); String regionCode = ""; String regionType = ""; Long villageCode = organization.getVillageCode(); Long townCode = organization.getTownCode(); Integer areaCode = organization.getAreaCode(); Integer cityCode = organization.getCityCode(); Integer provinceCode = organization.getProvinceCode(); if (!ObjectUtils.isEmpty(villageCode)) { regionCode = String.valueOf(villageCode); regionType = "village"; } else if (!ObjectUtils.isEmpty(townCode)) { regionCode = String.valueOf(townCode); regionType = "town"; } else if (!ObjectUtils.isEmpty(areaCode)) { regionCode = String.valueOf(areaCode); regionType = "area"; } else if (!ObjectUtils.isEmpty(cityCode)) { regionCode = String.valueOf(cityCode); regionType = "city"; } else if (!ObjectUtils.isEmpty(provinceCode)) { regionCode = String.valueOf(provinceCode); regionType = "province"; } else { return null; } result.put("regionCode", regionCode); result.put("regionType", regionType); return result; } }