package com.product.admin.service; import java.util.Date; import org.apache.commons.lang3.RandomStringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.product.common.lang.DateUtils; import com.product.core.dao.BaseDao; import com.product.core.entity.DataTableEntity; import com.product.core.entity.FieldMetaEntity; import com.product.core.entity.FieldSetEntity; import com.product.core.exception.BaseException; import com.product.core.service.support.AbstractBaseService; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.core.transfer.Transactional; import com.product.module.sys.service.UserService; import com.product.common.lang.StringUtils; import com.product.email.service.SendEmailService; import com.product.admin.config.CmnConst; import com.product.admin.service.idel.ISystemManagerSerivce; @Component public class SystemManagerService extends AbstractBaseService implements ISystemManagerSerivce { @Autowired UserService userService; @Autowired private SendEmailService sendEmailService; public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } @Autowired public BaseDao baseDao; public DataTableEntity getManagerList(String user_uuid) throws BaseException { String sql = "SELECT user_uuid,GROUP_CONCAT(`org_level_uuid` SEPARATOR ',') org_level_uuid, role_uuids FROM product_sys_org_manager "; if (!StringUtils.isEmpty(user_uuid)) { sql += " where org_level_uuid in (SELECT uuid FROM product_sys_org_levels` where org_level_code_parent in (select org_level_uuid FROM product_sys_org_manager where user_uuid='" + user_uuid + "' and manager_type=2) )"; } else { sql += " where manager_type=2"; } sql += " GROUP BY user_uuid,role_uuids"; return baseDao.listTable(sql, new Object[]{}); } @Transactional public String addManager(FieldSetEntity fse) throws BaseException { FieldSetEntity userFs = new FieldSetEntity(); userFs.setTableName(CmnConst.PRODUCT_SYS_USERS); userFs.setValue(CmnConst.STATUS, fse.getString(CmnConst.STATUS)); userFs.setValue(CmnConst.USER_ACCOUNT, fse.getString(CmnConst.USER_ACCOUNT)); userFs.setValue(CmnConst.USER_NAME, fse.getString(CmnConst.USER_NAME)); userFs.setValue(CmnConst.USER_PWD, fse.getString(CmnConst.USER_PWD)); userFs.setValue(CmnConst.IS_MANAGER, 1);// 是否是管理员 0:否 1:是 String datetime = DateUtils.getDateTime(); userFs.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id()); userFs.setValue(CmnConst.UPDATED_UTC_DATETIME, datetime); FieldSetEntity sub = fse.getSubDataTable(CmnConst.PRODUCT_SYS_MANAGER).getData().get(0); String[] org_level_uuids = sub.getString("org_level_uuids").split(","); String user_uuid = baseDao.add(userFs); if (sub.getString("org_level_uuids") != null && org_level_uuids != null) { DataTableEntity newSubDataTable = new DataTableEntity(); FieldMetaEntity f = new FieldMetaEntity(); f.setTableName(new Object[]{CmnConst.PRODUCT_SYS_MANAGER}); newSubDataTable.setMeta(f); FieldSetEntity newSubFs = null; for (int i = 0; i < org_level_uuids.length; i++) { newSubFs = new FieldSetEntity(); newSubFs.setTableName(CmnConst.PRODUCT_SYS_MANAGER); newSubFs.setValue(CmnConst.MANAGER_TYPE, 2);// 管理员类型:1超级管理员 2.普通管理员 newSubFs.setValue(CmnConst.USER_UUID, user_uuid); newSubFs.setValue(CmnConst.ORG_LEVEL_UUID, org_level_uuids[i]);// 管理员信息表拆分保存 newSubFs.setValue(CmnConst.ROLE_UUIDS, "role_uuid_4");// 关联角色,默认普通管理员角色 newSubFs.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id()); newSubFs.setValue(CmnConst.UPDATED_UTC_DATETIME, datetime); newSubDataTable.addFieldSetEntity(newSubFs); } baseDao.add(newSubDataTable); } return user_uuid; } /** * 创建隐藏管理员,一个客户只创建一个,关联顶级公司,并且该客户下面创建子公司时,也要关联此管理员 * * @param client 客户,必须包含公司 * @return * @throws BaseException */ @Transactional public void addHideManager(FieldSetEntity client) throws BaseException { // 用户表 FieldSetEntity userFs = new FieldSetEntity(); userFs.setTableName(CmnConst.PRODUCT_SYS_USERS); userFs.setValue(CmnConst.STATUS, "1"); // 生成账户名 String account = client.getString("client_short_code") + "_hidden_" + (int) ((Math.random() * 9 + 1) * 10000); userFs.setValue(CmnConst.USER_ACCOUNT, account);// 使用客户短编码+hidden+5位 随机数字 // 随机生成8位密码 String passWord = RandomStringUtils.randomAlphanumeric(8); userFs.setValue(CmnConst.USER_NAME, account); userFs.setValue(CmnConst.USER_PWD, userService.createPassWord(account, passWord)); userFs.setValue(CmnConst.IS_MANAGER, 1);// 是否是管理员 0:否 1:是 String datetime = DateUtils.getDateTime(); userFs.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id()); userFs.setValue(CmnConst.UPDATED_UTC_DATETIME, datetime); String uuid = baseDao.add(userFs); FieldSetEntity uf = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_USERS, uuid, false); // 管理员表 FieldSetEntity newSubFs = new FieldSetEntity(); newSubFs.setTableName(CmnConst.PRODUCT_SYS_MANAGER); newSubFs.setValue(CmnConst.MANAGER_TYPE, 3);// 管理员类型:1超级管理员 2.普通管理员 3.隐藏管理员 newSubFs.setValue(CmnConst.USER_ID, uf == null ? null : uf.getInteger(CmnConst.USER_ID)); // 郑盟 2020年12月21日 下午14:05 newSubFs.setValue(CmnConst.IS_USED, "1"); newSubFs.setValue(CmnConst.ORG_LEVEL_UUID, client.getString(CmnConst.ORG_LEVEL_UUID));// 管理员关联公司 newSubFs.setValue(CmnConst.ROLE_UUIDS, "a8e2a6ac-a507-47da-851b-9ac61541cd5a-notchange");// 关联角色,隐藏管理员角色 newSubFs.setValue(CmnConst.CLIENTS_UUID, client.getString("uuid")); newSubFs.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id()); newSubFs.setValue(CmnConst.UPDATED_UTC_DATETIME, datetime); baseDao.add(newSubFs); // 新增隐藏管理员发送邮件 // 邮件内容 String user_id = client.getString(CmnConst.CREATED_BY); FieldSetEntity khfs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_USERS, " user_id = ? ", new Object[]{user_id}, false); if (khfs != null) { String login_url = client.getString(CmnConst.LOGIN_URL); FieldSetEntity nfs = new FieldSetEntity(); nfs.setTableName(CmnConst.STAFF_EMAIL_CONTENT); nfs.setValue(CmnConst.LOGIN_URL, login_url);// 登录页面 nfs.setValue(CmnConst.USER_ACCOUNT, account);// 用户名 nfs.setValue(CmnConst.PASS_WORD, passWord); nfs.setValue(CmnConst.USER_PRIMARY_EMAIL, khfs.getString(CmnConst.USER_PRIMARY_EMAIL));// 接受邮件邮箱地址 // 发送邮件 sendEmailService.parseMailTemplate(CmnConst.NEW_HIDDEN_ADMINISTRATOR_ACTIVATION, nfs); } } /** * 隐藏管理员绑定公司 * * @param org_levels 公司 */ public void updateHideManagerBindingCompany(FieldSetEntity org_levels) throws BaseException { String client_uuid = org_levels.getString(CmnConst.CLIENT_UUID); FieldSetEntity manager = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_MANAGER, "clients_uuid=? and manager_type=3", new Object[]{client_uuid}, false); if (manager != null) { String org_level_uuid = manager.getString(CmnConst.ORG_LEVEL_UUID); if (org_level_uuid == null || org_level_uuid.length() == 0) { org_level_uuid = org_levels.getString("uuid"); } else if (!("," + org_level_uuid + ",").contains("," + org_levels.getString("uuid") + ",")) { org_level_uuid += "," + org_levels.getString("uuid"); } manager.setValue(CmnConst.ORG_LEVEL_UUID, org_level_uuid); baseDao.update(manager); } } /** * 隐藏管理员解除绑定公司 * * @param org_levels 公司 */ public void updateHideManagerClearCompany(FieldSetEntity org_levels) throws BaseException { String client_uuid = org_levels.getString(CmnConst.CLIENT_UUID); FieldSetEntity manager = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_MANAGER, "clients_uuid=? and manager_type=3", new Object[]{client_uuid}, false); if (manager != null) { String org_level_uuid = manager.getString(CmnConst.ORG_LEVEL_UUID); if (org_level_uuid != null && org_level_uuid.length() > 0 && ("," + org_level_uuid + ",").contains("," + org_levels.getString("uuid") + ",")) { org_level_uuid = ("," + org_level_uuid + ",").replaceAll("," + org_levels.getString("uuid") + ",", ""); org_level_uuid = org_level_uuid.substring(1, org_level_uuid.length() - 1); } manager.setValue(CmnConst.ORG_LEVEL_UUID, org_level_uuid); baseDao.update(manager); } } public boolean updateManager(FieldSetEntity fse) throws BaseException { return baseDao.update(fse); } /** * 修改/重置密码 * * @param fse * @return * @throws BaseException */ public boolean updateManagerPwd(FieldSetEntity fse) throws BaseException { FieldSetEntity fs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_USERS, fse.getString(CmnConst.USER_UUID), false); String pwd = fse.getString(CmnConst.USER_PWD); fs.setValue(CmnConst.USER_PWD, pwd != null ? pwd : ""); fs.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id()); fs.setValue(CmnConst.UPDATED_UTC_DATETIME, new Date()); return baseDao.update(fs); } /** * 删除管理员 */ @Transactional public boolean deleteManager(FieldSetEntity fse) throws BaseException { String user_uuid = fse.getString(CmnConst.USER_UUID); boolean flag = false; if (!StringUtils.isEmpty(user_uuid)) { flag = baseDao.delete(CmnConst.PRODUCT_SYS_MANAGER, "user_uuid=?", new String[]{user_uuid}); if (!flag) return flag; flag = baseDao.delete(CmnConst.PRODUCT_SYS_USERS, "uuid=?", new String[]{user_uuid}); } return flag; } /** * 修改管理员状态 */ public boolean updateManagerStatus(FieldSetEntity fse) throws BaseException { FieldSetEntity fs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_USERS, fse.getString(CmnConst.USER_UUID), false); fs.setValue(CmnConst.STATUS, fse.getString(CmnConst.STATUS));// 状态 0:禁用 1:正常 fs.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id()); fs.setValue(CmnConst.UPDATED_UTC_DATETIME, new Date()); return baseDao.update(fs); } /** * 根据用户uuid 查询管理员 * * @param user_uuid * @return * @throws BaseException */ public FieldSetEntity findManagerTypeByUserUuid(String user_uuid) throws BaseException { return baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_MANAGER, "user_uuid=?", new String[]{user_uuid}, false); } /** * 公司uuid 查询管理员 * * @param org_level_uuid * @return * @throws BaseException */ public FieldSetEntity findManagerType(String user_uuid, String org_level_uuid) throws BaseException { String filter = " 1=1"; Object[] objects = new Object[]{}; // 郑盟 2020年12月17日 下午16:05 if (!StringUtils.isEmpty(user_uuid)) { filter += " and user_uuid=?"; objects = new Object[]{user_uuid}; } // 郑盟 2020年12月17日 下午16:05 if (!StringUtils.isEmpty(org_level_uuid)) { filter += " and org_level_uuid=?"; if (objects.length == 1) { objects = new Object[]{user_uuid, org_level_uuid}; } else { objects = new Object[]{org_level_uuid}; } } return baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_MANAGER, filter, objects, false); } // 随机字母 public static String getRandStr(int num) { String strs = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; StringBuilder buff = new StringBuilder(); for (int i = 1; i <= num; i++) { char str = strs.charAt((int) (Math.random() * 52)); buff.append(str); } return buff.toString(); } }