|
@@ -1,483 +1,486 @@
|
|
|
-package cn.com.qmth.examcloud.core.basic.service.impl;
|
|
|
-
|
|
|
-import java.io.File;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Map.Entry;
|
|
|
-
|
|
|
-import org.apache.commons.collections.CollectionUtils;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import com.google.common.collect.Lists;
|
|
|
-import com.google.common.collect.Maps;
|
|
|
-
|
|
|
-import cn.com.qmth.examcloud.commons.base.exception.StatusException;
|
|
|
-import cn.com.qmth.examcloud.commons.base.helpers.DynamicEnum;
|
|
|
-import cn.com.qmth.examcloud.commons.base.helpers.DynamicEnumManager;
|
|
|
-import cn.com.qmth.examcloud.commons.base.helpers.poi.ExcelReader;
|
|
|
-import cn.com.qmth.examcloud.commons.base.util.PathUtil;
|
|
|
-import cn.com.qmth.examcloud.commons.web.security.enums.RoleMeta;
|
|
|
-import cn.com.qmth.examcloud.core.basic.base.constants.BasicConsts;
|
|
|
-import cn.com.qmth.examcloud.core.basic.dao.ExamSiteRepo;
|
|
|
-import cn.com.qmth.examcloud.core.basic.dao.OrgPropertyRepo;
|
|
|
-import cn.com.qmth.examcloud.core.basic.dao.OrgRepo;
|
|
|
-import cn.com.qmth.examcloud.core.basic.dao.RoleRepo;
|
|
|
-import cn.com.qmth.examcloud.core.basic.dao.StudentRepo;
|
|
|
-import cn.com.qmth.examcloud.core.basic.dao.UserRepo;
|
|
|
-import cn.com.qmth.examcloud.core.basic.dao.UserRoleRelationRepo;
|
|
|
-import cn.com.qmth.examcloud.core.basic.dao.entity.OrgEntity;
|
|
|
-import cn.com.qmth.examcloud.core.basic.dao.entity.OrgPropertyEntity;
|
|
|
-import cn.com.qmth.examcloud.core.basic.dao.entity.RoleEntity;
|
|
|
-import cn.com.qmth.examcloud.core.basic.dao.entity.UserEntity;
|
|
|
-import cn.com.qmth.examcloud.core.basic.dao.entity.UserRoleRelationEntity;
|
|
|
-import cn.com.qmth.examcloud.core.basic.dao.enums.OrgProperty;
|
|
|
-import cn.com.qmth.examcloud.core.basic.service.OrgService;
|
|
|
-import cn.com.qmth.examcloud.core.basic.service.bean.OrgInfo;
|
|
|
-import cn.com.qmth.examcloud.task.api.DataSyncCloudService;
|
|
|
-import cn.com.qmth.examcloud.task.api.request.SyncOrgReq;
|
|
|
-
|
|
|
-@Service
|
|
|
-public class OrgServiceImpl implements OrgService {
|
|
|
-
|
|
|
- @Autowired
|
|
|
- RoleRepo roleRepo;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- OrgRepo orgRepo;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- UserRepo userRepo;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- ExamSiteRepo examSiteRepo;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- UserRoleRelationRepo userRoleRelationRepo;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- OrgPropertyRepo orgPropertyRepo;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- StudentRepo studentRepo;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- DataSyncCloudService dataSyncCloudService;
|
|
|
-
|
|
|
- private static final String[] EXCEL_HEADER = new String[]{"机构名称", "机构代码", "联系人", "联系电话"};
|
|
|
-
|
|
|
- /*
|
|
|
- * 保存顶级机构
|
|
|
- *
|
|
|
- * @author WANGWEI
|
|
|
- *
|
|
|
- * @see
|
|
|
- * cn.com.qmth.examcloud.core.basic.service.OrgService#saveRootOrg(cn.com.
|
|
|
- * qmth.examcloud.core.basic.service.bean.OrgInfo)
|
|
|
- */
|
|
|
- @Override
|
|
|
- public OrgEntity saveRootOrg(OrgInfo orgInfo) {
|
|
|
-
|
|
|
- String code = orgInfo.getCode();
|
|
|
- String name = orgInfo.getName();
|
|
|
- Boolean enable = orgInfo.getEnable();
|
|
|
- String contacts = orgInfo.getContacts();
|
|
|
- String telephone = orgInfo.getTelephone();
|
|
|
- String domainName = orgInfo.getDomainName();
|
|
|
- String remark = orgInfo.getRemark();
|
|
|
- Map<String, String> properties = orgInfo.getProperties();
|
|
|
-
|
|
|
- if (StringUtils.isBlank(code)) {
|
|
|
- throw new StatusException("B-150001", "code is null");
|
|
|
- }
|
|
|
- if (StringUtils.isBlank(name)) {
|
|
|
- throw new StatusException("B-150002", "name is null");
|
|
|
- }
|
|
|
-
|
|
|
- if (StringUtils.isNotBlank(domainName)) {
|
|
|
- OrgEntity tempOrg = orgRepo.findByParentIdIsNullAndDomainName(domainName);
|
|
|
- if (null != tempOrg && !tempOrg.getCode().equals(code)) {
|
|
|
- throw new StatusException("B-150004", "域名被占用");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (null == enable) {
|
|
|
- enable = true;
|
|
|
- }
|
|
|
-
|
|
|
- OrgEntity orgEntity = orgRepo.findByParentIdIsNullAndCode(code);
|
|
|
- if (null == orgEntity) {
|
|
|
- OrgEntity tmp = new OrgEntity();
|
|
|
- tmp.setRootId(-1L);
|
|
|
- tmp.setCode(code);
|
|
|
- tmp.setEnable(enable);
|
|
|
- tmp.setName(name);
|
|
|
- orgEntity = orgRepo.save(tmp);
|
|
|
- } else {
|
|
|
- if (!orgEntity.getId().equals(orgEntity.getRootId())) {
|
|
|
- throw new StatusException("B-150003", "数据错误");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- orgEntity.setRootId(orgEntity.getId());
|
|
|
- orgEntity.setName(name);
|
|
|
- orgEntity.setEnable(enable);
|
|
|
- orgEntity.setContacts(contacts);
|
|
|
- orgEntity.setTelephone(telephone);
|
|
|
- orgEntity.setDomainName(domainName);
|
|
|
- orgEntity.setRemark(remark);
|
|
|
-
|
|
|
- OrgEntity saved = orgRepo.save(orgEntity);
|
|
|
-
|
|
|
- Map<DynamicEnum, String> map = checkAndGetOrgProperties(properties);
|
|
|
- for (Entry<DynamicEnum, String> entry : map.entrySet()) {
|
|
|
- DynamicEnum de = entry.getKey();
|
|
|
- String value = entry.getValue();
|
|
|
- OrgPropertyEntity entity = orgPropertyRepo.findByOrgIdAndKeyId(saved.getId(),
|
|
|
- de.getId());
|
|
|
- if (null == entity) {
|
|
|
- entity = new OrgPropertyEntity();
|
|
|
- entity.setOrgId(saved.getId());
|
|
|
- entity.setKeyId(de.getId());
|
|
|
- }
|
|
|
- entity.setValue(value);
|
|
|
-
|
|
|
- orgPropertyRepo.save(entity);
|
|
|
- }
|
|
|
-
|
|
|
- return saved;
|
|
|
- }
|
|
|
-
|
|
|
- /*
|
|
|
- * 保存子机构
|
|
|
- *
|
|
|
- * @author WANGWEI
|
|
|
- *
|
|
|
- * @see
|
|
|
- * cn.com.qmth.examcloud.core.basic.service.OrgService#saveSubOrg(cn.com.
|
|
|
- * qmth.examcloud.core.basic.service.bean.OrgInfo)
|
|
|
- */
|
|
|
- @Override
|
|
|
- public OrgEntity saveSubOrg(OrgInfo orgInfo) {
|
|
|
-
|
|
|
- String code = orgInfo.getCode();
|
|
|
- String name = orgInfo.getName();
|
|
|
- Boolean enable = orgInfo.getEnable();
|
|
|
- String contacts = orgInfo.getContacts();
|
|
|
- String telephone = orgInfo.getTelephone();
|
|
|
- Long rootId = orgInfo.getRootId();
|
|
|
- Long parentId = orgInfo.getParentId();
|
|
|
- String remark = orgInfo.getRemark();
|
|
|
- Map<String, String> properties = orgInfo.getProperties();
|
|
|
-
|
|
|
- if (null == enable) {
|
|
|
- enable = true;
|
|
|
- }
|
|
|
-
|
|
|
- if (null == rootId) {
|
|
|
- throw new StatusException("B-150000", "rootId is null");
|
|
|
- }
|
|
|
- if (StringUtils.isBlank(code)) {
|
|
|
- throw new StatusException("B-150001", "code is null");
|
|
|
- }
|
|
|
- if (StringUtils.isBlank(name)) {
|
|
|
- throw new StatusException("B-150002", "name is null");
|
|
|
- }
|
|
|
- if (null == parentId) {
|
|
|
- throw new StatusException("B-150003", "parentId is null");
|
|
|
- }
|
|
|
-
|
|
|
- OrgEntity rootOrgEntity = orgRepo.findOne(rootId);
|
|
|
- if (null == rootOrgEntity) {
|
|
|
- throw new StatusException("B-150004", "rootId is wrong");
|
|
|
- }
|
|
|
- if (null != rootOrgEntity.getParentId()) {
|
|
|
- throw new StatusException("B-150005", "rootId is wrong");
|
|
|
- }
|
|
|
-
|
|
|
- OrgEntity parentOrgEntity = orgRepo.findOne(parentId);
|
|
|
- if (null == parentOrgEntity) {
|
|
|
- throw new StatusException("B-150006", "parentId is wrong");
|
|
|
- }
|
|
|
-
|
|
|
- if (!rootOrgEntity.getRootId().equals(parentOrgEntity.getRootId())) {
|
|
|
- throw new StatusException("B-150007", "parentId, rootId is wrong");
|
|
|
- }
|
|
|
-
|
|
|
- long updateTime = 0L;
|
|
|
- OrgEntity orgEntity = orgRepo.findByRootIdAndCode(rootId, code);
|
|
|
- if (null == orgEntity) {
|
|
|
- orgEntity = new OrgEntity();
|
|
|
- orgEntity.setRootId(rootId);
|
|
|
- orgEntity.setParentId(parentId);
|
|
|
- orgEntity.setCode(code);
|
|
|
- } else {
|
|
|
- if (null != orgEntity.getUpdateTime()) {
|
|
|
- updateTime = orgEntity.getUpdateTime().getTime();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- orgEntity.setName(name);
|
|
|
- orgEntity.setEnable(enable);
|
|
|
- orgEntity.setContacts(contacts);
|
|
|
- orgEntity.setTelephone(telephone);
|
|
|
- orgEntity.setRemark(remark);
|
|
|
-
|
|
|
- OrgEntity saved = orgRepo.save(orgEntity);
|
|
|
-
|
|
|
- // 同步操作
|
|
|
- if (updateTime != saved.getUpdateTime().getTime()) {
|
|
|
- SyncOrgReq req = new SyncOrgReq();
|
|
|
- req.setEnable(saved.getEnable());
|
|
|
- req.setId(saved.getId());
|
|
|
- req.setName(saved.getName());
|
|
|
- req.setParentId(saved.getParentId());
|
|
|
- req.setRootId(saved.getRootId());
|
|
|
- req.setSyncType("update");
|
|
|
- dataSyncCloudService.syncOrg(req);
|
|
|
- }
|
|
|
-
|
|
|
- Map<DynamicEnum, String> map = checkAndGetOrgProperties(properties);
|
|
|
- for (Entry<DynamicEnum, String> entry : map.entrySet()) {
|
|
|
- DynamicEnum de = entry.getKey();
|
|
|
- String value = entry.getValue();
|
|
|
- OrgPropertyEntity entity = orgPropertyRepo.findByOrgIdAndKeyId(saved.getId(),
|
|
|
- de.getId());
|
|
|
- if (null == entity) {
|
|
|
- entity = new OrgPropertyEntity();
|
|
|
- entity.setOrgId(saved.getId());
|
|
|
- entity.setKeyId(de.getId());
|
|
|
- }
|
|
|
- entity.setValue(value);
|
|
|
-
|
|
|
- orgPropertyRepo.save(entity);
|
|
|
- }
|
|
|
-
|
|
|
- return saved;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 方法注释
|
|
|
- *
|
|
|
- * @author WANGWEI
|
|
|
- * @param properties
|
|
|
- * @return
|
|
|
- */
|
|
|
- private Map<DynamicEnum, String> checkAndGetOrgProperties(Map<String, String> properties) {
|
|
|
- DynamicEnumManager manager = OrgProperty.getDynamicEnumManager();
|
|
|
-
|
|
|
- Map<DynamicEnum, String> map = Maps.newHashMap();
|
|
|
- if (null == properties) {
|
|
|
- return map;
|
|
|
- }
|
|
|
- for (Entry<String, String> entry : properties.entrySet()) {
|
|
|
- String key = entry.getKey();
|
|
|
- String value = entry.getValue();
|
|
|
- if (StringUtils.isBlank(value)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- DynamicEnum de = null;
|
|
|
- try {
|
|
|
- de = manager.getByName(key);
|
|
|
- } catch (Exception e) {
|
|
|
- throw new StatusException("E-001004", "机构属性错误");
|
|
|
- }
|
|
|
- map.put(de, value.trim());
|
|
|
- }
|
|
|
-
|
|
|
- return map;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void deleteSubOrg(Long subOrgId, boolean cascade) {
|
|
|
- OrgEntity orgEntity = orgRepo.findOne(subOrgId);
|
|
|
- if (null == orgEntity.getParentId()) {
|
|
|
- throw new StatusException("E-001004", "该机构非子机构");
|
|
|
- }
|
|
|
- Long rootId = orgEntity.getRootId();
|
|
|
-
|
|
|
- if (0 < orgRepo.countByRootIdAndParentId(rootId, orgEntity.getId())) {
|
|
|
- throw new StatusException("E-001004", "该机构有子机构");
|
|
|
- }
|
|
|
-
|
|
|
- if (!cascade) {
|
|
|
- if (0 < userRepo.countByRootOrgId(rootId)) {
|
|
|
- throw new StatusException("E-001004", "该机构已关联用户");
|
|
|
- }
|
|
|
-
|
|
|
- if (0 < studentRepo.countByRootOrgId(rootId)) {
|
|
|
- throw new StatusException("E-001004", "该机构已关联学生");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- orgRepo.delete(orgEntity);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public List<Map<String, Object>> importSubOrg(Long rootOrgId, File file) {
|
|
|
-
|
|
|
- List<String[]> lineList = null;
|
|
|
- try {
|
|
|
- lineList = ExcelReader.readSheetBySax(PathUtil.getCanonicalPath(file), 1, 4);
|
|
|
- } catch (Exception e) {
|
|
|
- throw new StatusException("B-100110", "Excel 解析失败");
|
|
|
- }
|
|
|
-
|
|
|
- if (CollectionUtils.isEmpty(lineList)) {
|
|
|
- throw new StatusException("B-100111", "Excel无内容");
|
|
|
- }
|
|
|
-
|
|
|
- if (10001 < lineList.size()) {
|
|
|
- throw new StatusException("B-100112", "数据行数不能超过10000");
|
|
|
- }
|
|
|
-
|
|
|
- List<Map<String, Object>> failRecords = Collections
|
|
|
- .synchronizedList(new ArrayList<Map<String, Object>>());
|
|
|
-
|
|
|
- List<OrgEntity> courseList = Lists.newArrayList();
|
|
|
-
|
|
|
- for (int i = 0; i < lineList.size(); i++) {
|
|
|
- String[] line = lineList.get(i);
|
|
|
- if (0 == i) {
|
|
|
- if (headerError(line)) {
|
|
|
- throw new StatusException("B-100111", "Excel表头错误");
|
|
|
- }
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- boolean hasError = false;
|
|
|
- StringBuilder msg = new StringBuilder();
|
|
|
-
|
|
|
- OrgEntity org = new OrgEntity();
|
|
|
- org.setParentId(rootOrgId);
|
|
|
- org.setEnable(true);
|
|
|
-
|
|
|
- String name = trimAndNullIfBlank(line[0]);
|
|
|
- if (StringUtils.isBlank(name)) {
|
|
|
- msg.append(" 学习中心名称不能为空");
|
|
|
- hasError = true;
|
|
|
- } else if (name.length() > 30) {
|
|
|
- msg.append(" 学习中心名称不能超过30个字符");
|
|
|
- hasError = true;
|
|
|
- }
|
|
|
- org.setName(name);
|
|
|
-
|
|
|
- String code = trimAndNullIfBlank(line[1]);
|
|
|
- if (StringUtils.isBlank(code)) {
|
|
|
- msg.append(" 学习中心代码不能为空");
|
|
|
- hasError = true;
|
|
|
- } else if (code.length() > 30) {
|
|
|
- msg.append(" 学习中心代码不能超过30个字符");
|
|
|
- hasError = true;
|
|
|
- }
|
|
|
- org.setCode(code);
|
|
|
-
|
|
|
- String contacts = trimAndNullIfBlank(line[2]);
|
|
|
- if (StringUtils.isBlank(contacts)) {
|
|
|
- msg.append(" 联系人不能为空");
|
|
|
- hasError = true;
|
|
|
- } else if (contacts.length() > 30) {
|
|
|
- msg.append(" 联系人不能超过30个字符");
|
|
|
- hasError = true;
|
|
|
- }
|
|
|
- org.setContacts(contacts);
|
|
|
-
|
|
|
- String telephone = trimAndNullIfBlank(line[3]);
|
|
|
- if (StringUtils.isBlank(telephone)) {
|
|
|
- msg.append(" 联系电话不能为空");
|
|
|
- hasError = true;
|
|
|
- } else if (telephone.length() > 30) {
|
|
|
- msg.append(" 联系电话不能超过30个字符");
|
|
|
- hasError = true;
|
|
|
- }
|
|
|
- org.setTelephone(telephone);
|
|
|
-
|
|
|
- if (hasError) {
|
|
|
- failRecords.add(newError(i + 1, msg.toString()));
|
|
|
- } else {
|
|
|
- courseList.add(org);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- if (CollectionUtils.isNotEmpty(failRecords)) {
|
|
|
- return failRecords;
|
|
|
- }
|
|
|
-
|
|
|
- for (OrgEntity cur : courseList) {
|
|
|
- OrgInfo info = new OrgInfo();
|
|
|
- info.setCode(cur.getCode());
|
|
|
- info.setParentId(rootOrgId);
|
|
|
- info.setRootId(rootOrgId);
|
|
|
-
|
|
|
- info.setName(cur.getName());
|
|
|
- info.setContacts(cur.getContacts());
|
|
|
- info.setTelephone(cur.getTelephone());
|
|
|
-
|
|
|
- OrgEntity saved = saveSubOrg(info);
|
|
|
-
|
|
|
- createLearnerCenterUser(saved);
|
|
|
- }
|
|
|
-
|
|
|
- return failRecords;
|
|
|
- }
|
|
|
-
|
|
|
- public void createLearnerCenterUser(OrgEntity org) {
|
|
|
- UserEntity user = userRepo.findByRootOrgIdAndLoginName(org.getRootId(), org.getCode());
|
|
|
- if (null != user) {
|
|
|
- return;
|
|
|
- }
|
|
|
- user = new UserEntity();
|
|
|
- user.setRootOrgId(org.getRootId());
|
|
|
- user.setOrgId(org.getId());
|
|
|
- user.setLoginName(org.getCode());
|
|
|
- user.setName(org.getContacts());
|
|
|
- user.setPhoneNumber(org.getTelephone());
|
|
|
- user.setEnable(true);
|
|
|
- user.setPassword(BasicConsts.DEFAULT_PASSWORD);
|
|
|
- UserEntity savedUser = userRepo.save(user);
|
|
|
- List<UserRoleRelationEntity> userRoles = Lists.newArrayList();
|
|
|
- RoleEntity role = roleRepo.findByCode(RoleMeta.LC_USER.name());
|
|
|
- UserRoleRelationEntity relation = new UserRoleRelationEntity(savedUser.getId(),
|
|
|
- role.getId());
|
|
|
- userRoles.add(relation);
|
|
|
- userRoleRelationRepo.save(userRoles);
|
|
|
- }
|
|
|
-
|
|
|
- private Map<String, Object> newError(int lineNum, String msg) {
|
|
|
- Map<String, Object> map = Maps.newHashMap();
|
|
|
- map.put("lineNum", lineNum);
|
|
|
- map.put("msg", msg);
|
|
|
- return map;
|
|
|
- }
|
|
|
-
|
|
|
- private String trimAndNullIfBlank(String s) {
|
|
|
- if (StringUtils.isBlank(s)) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- return s.trim();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 方法注释
|
|
|
- *
|
|
|
- * @author WANGWEI
|
|
|
- * @param header
|
|
|
- */
|
|
|
- private boolean headerError(String[] header) {
|
|
|
- for (int i = 0; i < EXCEL_HEADER.length; i++) {
|
|
|
- if (null == header[i]) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- if (!EXCEL_HEADER[i].equals(header[i].trim())) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+package cn.com.qmth.examcloud.core.basic.service.impl;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Map.Entry;
|
|
|
+
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import com.google.common.collect.Maps;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.commons.base.exception.StatusException;
|
|
|
+import cn.com.qmth.examcloud.commons.base.helpers.DynamicEnum;
|
|
|
+import cn.com.qmth.examcloud.commons.base.helpers.DynamicEnumManager;
|
|
|
+import cn.com.qmth.examcloud.commons.base.helpers.poi.ExcelReader;
|
|
|
+import cn.com.qmth.examcloud.commons.base.util.PathUtil;
|
|
|
+import cn.com.qmth.examcloud.commons.web.security.enums.RoleMeta;
|
|
|
+import cn.com.qmth.examcloud.core.basic.base.constants.BasicConsts;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.ExamSiteRepo;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.OrgPropertyRepo;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.OrgRepo;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.RoleRepo;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.StudentRepo;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.UserRepo;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.UserRoleRelationRepo;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.OrgEntity;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.OrgPropertyEntity;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.RoleEntity;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.UserEntity;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.UserRoleRelationEntity;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.enums.OrgProperty;
|
|
|
+import cn.com.qmth.examcloud.core.basic.service.OrgService;
|
|
|
+import cn.com.qmth.examcloud.core.basic.service.bean.OrgInfo;
|
|
|
+import cn.com.qmth.examcloud.task.api.DataSyncCloudService;
|
|
|
+import cn.com.qmth.examcloud.task.api.request.SyncOrgReq;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class OrgServiceImpl implements OrgService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RoleRepo roleRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ OrgRepo orgRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ UserRepo userRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ExamSiteRepo examSiteRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ UserRoleRelationRepo userRoleRelationRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ OrgPropertyRepo orgPropertyRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ StudentRepo studentRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ DataSyncCloudService dataSyncCloudService;
|
|
|
+
|
|
|
+ private static final String[] EXCEL_HEADER = new String[]{"机构名称", "机构代码", "联系人", "联系电话"};
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 保存顶级机构
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ *
|
|
|
+ * @see
|
|
|
+ * cn.com.qmth.examcloud.core.basic.service.OrgService#saveRootOrg(cn.com.
|
|
|
+ * qmth.examcloud.core.basic.service.bean.OrgInfo)
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public OrgEntity saveRootOrg(OrgInfo orgInfo) {
|
|
|
+
|
|
|
+ String code = orgInfo.getCode();
|
|
|
+ String name = orgInfo.getName();
|
|
|
+ Boolean enable = orgInfo.getEnable();
|
|
|
+ String contacts = orgInfo.getContacts();
|
|
|
+ String telephone = orgInfo.getTelephone();
|
|
|
+ String domainName = orgInfo.getDomainName();
|
|
|
+ String remark = orgInfo.getRemark();
|
|
|
+ Map<String, String> properties = orgInfo.getProperties();
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(code)) {
|
|
|
+ throw new StatusException("B-150001", "code is null");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(name)) {
|
|
|
+ throw new StatusException("B-150002", "name is null");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isNotBlank(domainName)) {
|
|
|
+ OrgEntity tempOrg = orgRepo.findByParentIdIsNullAndDomainName(domainName);
|
|
|
+ if (null != tempOrg && !tempOrg.getCode().equals(code)) {
|
|
|
+ throw new StatusException("B-150004", "域名被占用");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (null == enable) {
|
|
|
+ enable = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ OrgEntity orgEntity = orgRepo.findByParentIdIsNullAndCode(code);
|
|
|
+ if (null == orgEntity) {
|
|
|
+ OrgEntity tmp = new OrgEntity();
|
|
|
+ tmp.setRootId(-1L);
|
|
|
+ tmp.setCode(code);
|
|
|
+ tmp.setEnable(enable);
|
|
|
+ tmp.setName(name);
|
|
|
+ orgEntity = orgRepo.save(tmp);
|
|
|
+ } else {
|
|
|
+ if (!orgEntity.getId().equals(orgEntity.getRootId())) {
|
|
|
+ throw new StatusException("B-150003", "数据错误");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ orgEntity.setRootId(orgEntity.getId());
|
|
|
+ orgEntity.setName(name);
|
|
|
+ orgEntity.setEnable(enable);
|
|
|
+ orgEntity.setContacts(contacts);
|
|
|
+ orgEntity.setTelephone(telephone);
|
|
|
+ orgEntity.setDomainName(domainName);
|
|
|
+ orgEntity.setRemark(remark);
|
|
|
+
|
|
|
+ OrgEntity saved = orgRepo.save(orgEntity);
|
|
|
+
|
|
|
+ Map<DynamicEnum, String> map = checkAndGetOrgProperties(properties);
|
|
|
+ for (Entry<DynamicEnum, String> entry : map.entrySet()) {
|
|
|
+ DynamicEnum de = entry.getKey();
|
|
|
+ String value = entry.getValue();
|
|
|
+ OrgPropertyEntity entity = orgPropertyRepo.findByOrgIdAndKeyId(saved.getId(),
|
|
|
+ de.getId());
|
|
|
+ if (null == entity) {
|
|
|
+ entity = new OrgPropertyEntity();
|
|
|
+ entity.setOrgId(saved.getId());
|
|
|
+ entity.setKeyId(de.getId());
|
|
|
+ }
|
|
|
+ entity.setValue(value);
|
|
|
+
|
|
|
+ orgPropertyRepo.save(entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ return saved;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 保存子机构
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ *
|
|
|
+ * @see
|
|
|
+ * cn.com.qmth.examcloud.core.basic.service.OrgService#saveSubOrg(cn.com.
|
|
|
+ * qmth.examcloud.core.basic.service.bean.OrgInfo)
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public OrgEntity saveSubOrg(OrgInfo orgInfo) {
|
|
|
+
|
|
|
+ String code = orgInfo.getCode();
|
|
|
+ String name = orgInfo.getName();
|
|
|
+ Boolean enable = orgInfo.getEnable();
|
|
|
+ String contacts = orgInfo.getContacts();
|
|
|
+ String telephone = orgInfo.getTelephone();
|
|
|
+ Long rootId = orgInfo.getRootId();
|
|
|
+ Long parentId = orgInfo.getParentId();
|
|
|
+ String remark = orgInfo.getRemark();
|
|
|
+ Map<String, String> properties = orgInfo.getProperties();
|
|
|
+
|
|
|
+ if (null == enable) {
|
|
|
+ enable = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (null == rootId) {
|
|
|
+ throw new StatusException("B-150000", "rootId is null");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(code)) {
|
|
|
+ throw new StatusException("B-150001", "code is null");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(name)) {
|
|
|
+ throw new StatusException("B-150002", "name is null");
|
|
|
+ }
|
|
|
+ if (null == parentId) {
|
|
|
+ throw new StatusException("B-150003", "parentId is null");
|
|
|
+ }
|
|
|
+
|
|
|
+ OrgEntity rootOrgEntity = orgRepo.findOne(rootId);
|
|
|
+ if (null == rootOrgEntity) {
|
|
|
+ throw new StatusException("B-150004", "rootId is wrong");
|
|
|
+ }
|
|
|
+ if (null != rootOrgEntity.getParentId()) {
|
|
|
+ throw new StatusException("B-150005", "rootId is wrong");
|
|
|
+ }
|
|
|
+
|
|
|
+ OrgEntity parentOrgEntity = orgRepo.findOne(parentId);
|
|
|
+ if (null == parentOrgEntity) {
|
|
|
+ throw new StatusException("B-150006", "parentId is wrong");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!rootOrgEntity.getRootId().equals(parentOrgEntity.getRootId())) {
|
|
|
+ throw new StatusException("B-150007", "parentId, rootId is wrong");
|
|
|
+ }
|
|
|
+
|
|
|
+ long updateTime = 0L;
|
|
|
+ OrgEntity orgEntity = orgRepo.findByRootIdAndCode(rootId, code);
|
|
|
+ if (null == orgEntity) {
|
|
|
+ orgEntity = new OrgEntity();
|
|
|
+ orgEntity.setRootId(rootId);
|
|
|
+ orgEntity.setParentId(parentId);
|
|
|
+ orgEntity.setCode(code);
|
|
|
+ } else {
|
|
|
+ if (null != orgEntity.getUpdateTime()) {
|
|
|
+ updateTime = orgEntity.getUpdateTime().getTime();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ orgEntity.setName(name);
|
|
|
+ orgEntity.setEnable(enable);
|
|
|
+ orgEntity.setContacts(contacts);
|
|
|
+ orgEntity.setTelephone(telephone);
|
|
|
+ orgEntity.setRemark(remark);
|
|
|
+
|
|
|
+ OrgEntity saved = orgRepo.save(orgEntity);
|
|
|
+
|
|
|
+ // 同步操作
|
|
|
+ if (updateTime != saved.getUpdateTime().getTime()) {
|
|
|
+ SyncOrgReq req = new SyncOrgReq();
|
|
|
+ req.setEnable(saved.getEnable());
|
|
|
+ req.setId(saved.getId());
|
|
|
+ req.setName(saved.getName());
|
|
|
+ req.setParentId(saved.getParentId());
|
|
|
+ req.setRootId(saved.getRootId());
|
|
|
+ req.setSyncType("update");
|
|
|
+ dataSyncCloudService.syncOrg(req);
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<DynamicEnum, String> map = checkAndGetOrgProperties(properties);
|
|
|
+ for (Entry<DynamicEnum, String> entry : map.entrySet()) {
|
|
|
+ DynamicEnum de = entry.getKey();
|
|
|
+ String value = entry.getValue();
|
|
|
+ OrgPropertyEntity entity = orgPropertyRepo.findByOrgIdAndKeyId(saved.getId(),
|
|
|
+ de.getId());
|
|
|
+ if (null == entity) {
|
|
|
+ entity = new OrgPropertyEntity();
|
|
|
+ entity.setOrgId(saved.getId());
|
|
|
+ entity.setKeyId(de.getId());
|
|
|
+ }
|
|
|
+ entity.setValue(value);
|
|
|
+
|
|
|
+ orgPropertyRepo.save(entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ return saved;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 方法注释
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @param properties
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Map<DynamicEnum, String> checkAndGetOrgProperties(Map<String, String> properties) {
|
|
|
+ DynamicEnumManager manager = OrgProperty.getDynamicEnumManager();
|
|
|
+
|
|
|
+ Map<DynamicEnum, String> map = Maps.newHashMap();
|
|
|
+ if (null == properties) {
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+ for (Entry<String, String> entry : properties.entrySet()) {
|
|
|
+ String key = entry.getKey();
|
|
|
+ String value = entry.getValue();
|
|
|
+ if (StringUtils.isBlank(value)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ DynamicEnum de = null;
|
|
|
+ try {
|
|
|
+ de = manager.getByName(key);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new StatusException("E-001004", "机构属性错误. key=" + key);
|
|
|
+ }
|
|
|
+ if (!de.isLegal(value)) {
|
|
|
+ throw new StatusException("E-001200", "机构属性值非法. key=" + key);
|
|
|
+ }
|
|
|
+ map.put(de, value.trim());
|
|
|
+ }
|
|
|
+
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteSubOrg(Long subOrgId, boolean cascade) {
|
|
|
+ OrgEntity orgEntity = orgRepo.findOne(subOrgId);
|
|
|
+ if (null == orgEntity.getParentId()) {
|
|
|
+ throw new StatusException("E-001004", "该机构非子机构");
|
|
|
+ }
|
|
|
+ Long rootId = orgEntity.getRootId();
|
|
|
+
|
|
|
+ if (0 < orgRepo.countByRootIdAndParentId(rootId, orgEntity.getId())) {
|
|
|
+ throw new StatusException("E-001004", "该机构有子机构");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!cascade) {
|
|
|
+ if (0 < userRepo.countByRootOrgId(rootId)) {
|
|
|
+ throw new StatusException("E-001004", "该机构已关联用户");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (0 < studentRepo.countByRootOrgId(rootId)) {
|
|
|
+ throw new StatusException("E-001004", "该机构已关联学生");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ orgRepo.delete(orgEntity);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Map<String, Object>> importSubOrg(Long rootOrgId, File file) {
|
|
|
+
|
|
|
+ List<String[]> lineList = null;
|
|
|
+ try {
|
|
|
+ lineList = ExcelReader.readSheetBySax(PathUtil.getCanonicalPath(file), 1, 4);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new StatusException("B-100110", "Excel 解析失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (CollectionUtils.isEmpty(lineList)) {
|
|
|
+ throw new StatusException("B-100111", "Excel无内容");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (10001 < lineList.size()) {
|
|
|
+ throw new StatusException("B-100112", "数据行数不能超过10000");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Map<String, Object>> failRecords = Collections
|
|
|
+ .synchronizedList(new ArrayList<Map<String, Object>>());
|
|
|
+
|
|
|
+ List<OrgEntity> courseList = Lists.newArrayList();
|
|
|
+
|
|
|
+ for (int i = 0; i < lineList.size(); i++) {
|
|
|
+ String[] line = lineList.get(i);
|
|
|
+ if (0 == i) {
|
|
|
+ if (headerError(line)) {
|
|
|
+ throw new StatusException("B-100111", "Excel表头错误");
|
|
|
+ }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ boolean hasError = false;
|
|
|
+ StringBuilder msg = new StringBuilder();
|
|
|
+
|
|
|
+ OrgEntity org = new OrgEntity();
|
|
|
+ org.setParentId(rootOrgId);
|
|
|
+ org.setEnable(true);
|
|
|
+
|
|
|
+ String name = trimAndNullIfBlank(line[0]);
|
|
|
+ if (StringUtils.isBlank(name)) {
|
|
|
+ msg.append(" 学习中心名称不能为空");
|
|
|
+ hasError = true;
|
|
|
+ } else if (name.length() > 30) {
|
|
|
+ msg.append(" 学习中心名称不能超过30个字符");
|
|
|
+ hasError = true;
|
|
|
+ }
|
|
|
+ org.setName(name);
|
|
|
+
|
|
|
+ String code = trimAndNullIfBlank(line[1]);
|
|
|
+ if (StringUtils.isBlank(code)) {
|
|
|
+ msg.append(" 学习中心代码不能为空");
|
|
|
+ hasError = true;
|
|
|
+ } else if (code.length() > 30) {
|
|
|
+ msg.append(" 学习中心代码不能超过30个字符");
|
|
|
+ hasError = true;
|
|
|
+ }
|
|
|
+ org.setCode(code);
|
|
|
+
|
|
|
+ String contacts = trimAndNullIfBlank(line[2]);
|
|
|
+ if (StringUtils.isBlank(contacts)) {
|
|
|
+ msg.append(" 联系人不能为空");
|
|
|
+ hasError = true;
|
|
|
+ } else if (contacts.length() > 30) {
|
|
|
+ msg.append(" 联系人不能超过30个字符");
|
|
|
+ hasError = true;
|
|
|
+ }
|
|
|
+ org.setContacts(contacts);
|
|
|
+
|
|
|
+ String telephone = trimAndNullIfBlank(line[3]);
|
|
|
+ if (StringUtils.isBlank(telephone)) {
|
|
|
+ msg.append(" 联系电话不能为空");
|
|
|
+ hasError = true;
|
|
|
+ } else if (telephone.length() > 30) {
|
|
|
+ msg.append(" 联系电话不能超过30个字符");
|
|
|
+ hasError = true;
|
|
|
+ }
|
|
|
+ org.setTelephone(telephone);
|
|
|
+
|
|
|
+ if (hasError) {
|
|
|
+ failRecords.add(newError(i + 1, msg.toString()));
|
|
|
+ } else {
|
|
|
+ courseList.add(org);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (CollectionUtils.isNotEmpty(failRecords)) {
|
|
|
+ return failRecords;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (OrgEntity cur : courseList) {
|
|
|
+ OrgInfo info = new OrgInfo();
|
|
|
+ info.setCode(cur.getCode());
|
|
|
+ info.setParentId(rootOrgId);
|
|
|
+ info.setRootId(rootOrgId);
|
|
|
+
|
|
|
+ info.setName(cur.getName());
|
|
|
+ info.setContacts(cur.getContacts());
|
|
|
+ info.setTelephone(cur.getTelephone());
|
|
|
+
|
|
|
+ OrgEntity saved = saveSubOrg(info);
|
|
|
+
|
|
|
+ createLearnerCenterUser(saved);
|
|
|
+ }
|
|
|
+
|
|
|
+ return failRecords;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void createLearnerCenterUser(OrgEntity org) {
|
|
|
+ UserEntity user = userRepo.findByRootOrgIdAndLoginName(org.getRootId(), org.getCode());
|
|
|
+ if (null != user) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ user = new UserEntity();
|
|
|
+ user.setRootOrgId(org.getRootId());
|
|
|
+ user.setOrgId(org.getId());
|
|
|
+ user.setLoginName(org.getCode());
|
|
|
+ user.setName(org.getContacts());
|
|
|
+ user.setPhoneNumber(org.getTelephone());
|
|
|
+ user.setEnable(true);
|
|
|
+ user.setPassword(BasicConsts.DEFAULT_PASSWORD);
|
|
|
+ UserEntity savedUser = userRepo.save(user);
|
|
|
+ List<UserRoleRelationEntity> userRoles = Lists.newArrayList();
|
|
|
+ RoleEntity role = roleRepo.findByCode(RoleMeta.LC_USER.name());
|
|
|
+ UserRoleRelationEntity relation = new UserRoleRelationEntity(savedUser.getId(),
|
|
|
+ role.getId());
|
|
|
+ userRoles.add(relation);
|
|
|
+ userRoleRelationRepo.save(userRoles);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> newError(int lineNum, String msg) {
|
|
|
+ Map<String, Object> map = Maps.newHashMap();
|
|
|
+ map.put("lineNum", lineNum);
|
|
|
+ map.put("msg", msg);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String trimAndNullIfBlank(String s) {
|
|
|
+ if (StringUtils.isBlank(s)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return s.trim();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 方法注释
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @param header
|
|
|
+ */
|
|
|
+ private boolean headerError(String[] header) {
|
|
|
+ for (int i = 0; i < EXCEL_HEADER.length; i++) {
|
|
|
+ if (null == header[i]) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ if (!EXCEL_HEADER[i].equals(header[i].trim())) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|