|
@@ -0,0 +1,416 @@
|
|
|
|
+package cn.com.qmth.examcloud.core.examwork.service.impl;
|
|
|
|
+
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+import javax.persistence.criteria.Predicate;
|
|
|
|
+
|
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
|
+import org.springframework.data.domain.PageRequest;
|
|
|
|
+import org.springframework.data.domain.Sort;
|
|
|
|
+import org.springframework.data.domain.Sort.Direction;
|
|
|
|
+import org.springframework.data.jpa.domain.Specification;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
+import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
|
|
|
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
+
|
|
|
|
+import cn.com.qmth.examcloud.api.commons.exchange.PageInfo;
|
|
|
|
+import cn.com.qmth.examcloud.api.commons.security.bean.User;
|
|
|
|
+import cn.com.qmth.examcloud.api.commons.security.bean.UserDataRule;
|
|
|
|
+import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
|
+import cn.com.qmth.examcloud.commons.helpers.poi.ExcelReader;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.api.OrgCloudService;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.api.UserCloudService;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.api.request.GetOrgReq;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.api.request.GetUserReq;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.api.response.GetOrgResp;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.api.response.GetUserResp;
|
|
|
|
+import cn.com.qmth.examcloud.core.examwork.dao.OrgIpRepo;
|
|
|
|
+import cn.com.qmth.examcloud.core.examwork.dao.entity.OrgIpEntity;
|
|
|
|
+import cn.com.qmth.examcloud.core.examwork.service.OrgIpService;
|
|
|
|
+import cn.com.qmth.examcloud.core.examwork.service.bean.orgip.OrgIpInfo;
|
|
|
|
+import cn.com.qmth.examcloud.core.examwork.service.bean.orgip.OrgIpQuery;
|
|
|
|
+import cn.com.qmth.examcloud.core.examwork.service.bean.orgip.OrgIpSave;
|
|
|
|
+import cn.com.qmth.examcloud.support.cache.CacheHelper;
|
|
|
|
+import cn.com.qmth.examcloud.support.cache.bean.OrgCacheBean;
|
|
|
|
+import cn.com.qmth.examcloud.web.helpers.GlobalHelper;
|
|
|
|
+import cn.com.qmth.examcloud.web.jpa.PageUtils;
|
|
|
|
+
|
|
|
|
+@Service
|
|
|
|
+public class OrgIpServiceImpl implements OrgIpService {
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(OrgIpService.class);
|
|
|
|
+ private static final String[] EXCEL_HEADER = new String[] { "IP/IP段", "学习中心代码", "学习中心名称", "备注" };
|
|
|
|
+ @Autowired
|
|
|
|
+ private OrgIpRepo orgIpRepo;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private UserCloudService userCloudService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private OrgCloudService orgCloudService;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public PageInfo<OrgIpInfo> getPage(OrgIpQuery req) {
|
|
|
|
+ if (req.getOrgUd().assertEmptyQueryResult()) {
|
|
|
|
+ return PageUtils.toPageInfo(Page.empty());
|
|
|
|
+ }
|
|
|
|
+ Specification<OrgIpEntity> specification = (root, query, cb) -> {
|
|
|
|
+ List<Predicate> predicates = new ArrayList<>();
|
|
|
|
+ predicates.add(cb.equal(root.get("rootOrgId"), req.getRootOrgId()));
|
|
|
|
+
|
|
|
|
+ if (req.getOrgUd().assertNeedQueryRefIds()) {
|
|
|
|
+ predicates.add(root.get("orgId").in(req.getOrgUd().getRefIds()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (req.getOrgId() != null) {
|
|
|
|
+ predicates.add(cb.equal(root.get("orgId"), req.getOrgId()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return cb.and(predicates.toArray(new Predicate[predicates.size()]));
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ PageRequest pageRequest = PageRequest.of(req.getPageNumber(), req.getPageSize(),
|
|
|
|
+ Sort.by(Direction.DESC, "updateTime"));
|
|
|
|
+
|
|
|
|
+ Page<OrgIpEntity> page = orgIpRepo.findAll(specification, pageRequest);
|
|
|
|
+ if (CollectionUtils.isEmpty(page.getContent())) {
|
|
|
|
+ return PageUtils.toPageInfo(Page.empty());
|
|
|
|
+ }
|
|
|
|
+ List<OrgIpInfo> ret = new ArrayList<>();
|
|
|
|
+ for (OrgIpEntity e : page.getContent()) {
|
|
|
|
+ OrgIpInfo info = new OrgIpInfo();
|
|
|
|
+ ret.add(info);
|
|
|
|
+ info.setId(e.getId());
|
|
|
|
+ info.setIp(e.getIp());
|
|
|
|
+ info.setOrgId(e.getOrgId());
|
|
|
|
+ OrgCacheBean org = CacheHelper.getOrg(e.getOrgId());
|
|
|
|
+ info.setOrgCode(org.getCode());
|
|
|
|
+ info.setOrgName(org.getName());
|
|
|
|
+ GetUserReq uq = new GetUserReq();
|
|
|
|
+ uq.setRootOrgId(req.getRootOrgId());
|
|
|
|
+ uq.setUserId(e.getUpdateBy());
|
|
|
|
+ GetUserResp res = userCloudService.getUser(uq);
|
|
|
|
+ info.setRemark(e.getRemark());
|
|
|
|
+ info.setUpdateTime(e.getUpdateTime());
|
|
|
|
+ info.setUpdateName(res.getUserBean().getName() + "(" + res.getUserBean().getLoginName() + ")");
|
|
|
|
+ }
|
|
|
|
+ return PageUtils.toPageInfo(page, ret);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ @Override
|
|
|
|
+ public void add(OrgIpSave req) {
|
|
|
|
+ if (req.getOrgId() == null) {
|
|
|
|
+ throw new StatusException("学习中心不能为空");
|
|
|
|
+ }
|
|
|
|
+ checkIp(req.getIp());
|
|
|
|
+ req.setIp(req.getIp().trim());
|
|
|
|
+ if (req.getRemark() != null && req.getRemark().length() > 200) {
|
|
|
|
+ throw new StatusException("备注长度不能超过200");
|
|
|
|
+ }
|
|
|
|
+ checkIpExists(req);
|
|
|
|
+ OrgIpEntity e = new OrgIpEntity();
|
|
|
|
+ e.setCreationBy(req.getUser().getUserId());
|
|
|
|
+ e.setUpdateBy(req.getUser().getUserId());
|
|
|
|
+ e.setIp(req.getIp());
|
|
|
|
+ e.setRemark(req.getRemark());
|
|
|
|
+ e.setRootOrgId(req.getUser().getRootOrgId());
|
|
|
|
+ orgIpRepo.save(e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ @Override
|
|
|
|
+ public void update(OrgIpSave req) {
|
|
|
|
+ if (req.getId() == null) {
|
|
|
|
+ throw new StatusException("id不能为空");
|
|
|
|
+ }
|
|
|
|
+ if (req.getOrgId() == null) {
|
|
|
|
+ throw new StatusException("学习中心不能为空");
|
|
|
|
+ }
|
|
|
|
+ checkIp(req.getIp());
|
|
|
|
+ req.setIp(req.getIp().trim());
|
|
|
|
+ if (req.getRemark() != null && req.getRemark().length() > 200) {
|
|
|
|
+ throw new StatusException("备注长度不能超过200");
|
|
|
|
+ }
|
|
|
|
+ checkIpExists(req);
|
|
|
|
+ OrgIpEntity e = GlobalHelper.getPresentEntity(orgIpRepo, req.getId(), OrgIpEntity.class);
|
|
|
|
+ e.setUpdateBy(req.getUser().getUserId());
|
|
|
|
+ e.setIp(req.getIp());
|
|
|
|
+ e.setRemark(req.getRemark());
|
|
|
|
+ orgIpRepo.save(e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void checkIpExists(OrgIpSave req) {
|
|
|
|
+ OrgIpEntity oi = orgIpRepo.findByRootOrgIdAndOrgIdAndIp(req.getUser().getRootOrgId(), req.getOrgId(),
|
|
|
|
+ req.getIp());
|
|
|
|
+ if (req.getId() == null && oi != null) {
|
|
|
|
+ OrgCacheBean org = CacheHelper.getOrg(req.getOrgId());
|
|
|
|
+ throw new StatusException("学习中心[" + org.getCode() + "] ip[" + req.getIp() + "]已存在");
|
|
|
|
+ }
|
|
|
|
+ if (req.getId() != null && oi != null && !oi.getId().equals(req.getId())) {
|
|
|
|
+ OrgCacheBean org = CacheHelper.getOrg(req.getOrgId());
|
|
|
|
+ throw new StatusException("学习中心[" + org.getCode() + "] ip[" + req.getIp() + "]已存在");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void checkIp(String ip) {
|
|
|
|
+ if (StringUtils.isBlank(ip)) {
|
|
|
|
+ throw new StatusException("ip不能为空");
|
|
|
|
+ }
|
|
|
|
+ String[] ss1 = ip.split("\\.");
|
|
|
|
+ if (ss1.length != 4) {
|
|
|
|
+ throw new StatusException("ip格式错误");
|
|
|
|
+ }
|
|
|
|
+ for (String s : ss1) {
|
|
|
|
+ if (s.indexOf("-") != -1) {
|
|
|
|
+ String[] ss2 = ip.split("-");
|
|
|
|
+ if (ss2.length != 2) {
|
|
|
|
+ throw new StatusException("ip段格式错误");
|
|
|
|
+ }
|
|
|
|
+ for (String s2 : ss2) {
|
|
|
|
+ checkIpNumber(s2);
|
|
|
|
+ }
|
|
|
|
+ if (Integer.valueOf(ss2[0]) >= Integer.valueOf(ss2[1])) {
|
|
|
|
+ throw new StatusException("ip段格式错误,起始数值必须小于截止数值");
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ checkIpNumber(s);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void checkIpNumber(String s) {
|
|
|
|
+ try {
|
|
|
|
+ int val = Integer.valueOf(s);
|
|
|
|
+ if (val < 0 || val > 255) {
|
|
|
|
+ throw new StatusException("ip数值只能是0~255");
|
|
|
|
+ }
|
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
|
+ throw new StatusException("ip只能是整数");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ @Override
|
|
|
|
+ public void delete(List<Long> ids, Long rootOrgId) {
|
|
|
|
+ orgIpRepo.delete(ids, rootOrgId);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String newError(int lineNum, String msg) {
|
|
|
|
+ return "第"+lineNum+"行 "+msg;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String trimAndNullIfBlank(String s) {
|
|
|
|
+ if (StringUtils.isBlank(s)) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ return s.trim();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ @Override
|
|
|
|
+ public List<String> importIp(User user, UserDataRule orgUd, CommonsMultipartFile file) {
|
|
|
|
+ List<String[]> lineList = null;
|
|
|
|
+ try {
|
|
|
|
+ lineList = ExcelReader.readSheetBySax(file.getInputStream(), 1, 4);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new StatusException("Excel 解析失败");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (CollectionUtils.isEmpty(lineList)) {
|
|
|
|
+ throw new StatusException("Excel无内容");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (10001 < lineList.size()) {
|
|
|
|
+ throw new StatusException("数据行数不能超过10000");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<String> failRecords = new ArrayList<>();
|
|
|
|
+
|
|
|
|
+ List<OrgIpSave> list = new ArrayList<>();
|
|
|
|
+ Map<String, Long> orgMap = new HashMap<>();
|
|
|
|
+ for (int i = 0; i < lineList.size(); i++) {
|
|
|
|
+ String[] line = lineList.get(i);
|
|
|
|
+ if (0 == i) {
|
|
|
|
+ if (headerError(line)) {
|
|
|
|
+ throw new StatusException("Excel表头错误");
|
|
|
|
+ }
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ boolean hasError = false;
|
|
|
|
+ StringBuilder msg = new StringBuilder();
|
|
|
|
+
|
|
|
|
+ OrgIpSave info = new OrgIpSave();
|
|
|
|
+ info.setUser(user);
|
|
|
|
+
|
|
|
|
+ String ip = trimAndNullIfBlank(line[0]);
|
|
|
|
+ if (StringUtils.isBlank(ip)) {
|
|
|
|
+ msg.append(" IP/IP段不能为空");
|
|
|
|
+ hasError = true;
|
|
|
|
+ } else if (ip.length() > 100) {
|
|
|
|
+ msg.append(" IP/IP段不能超过100个字符");
|
|
|
|
+ hasError = true;
|
|
|
|
+ } else {
|
|
|
|
+ try {
|
|
|
|
+ checkIp(ip);
|
|
|
|
+ } catch (StatusException e) {
|
|
|
|
+ msg.append(" " + e.getDesc());
|
|
|
|
+ hasError = true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ info.setIp(ip);
|
|
|
|
+
|
|
|
|
+ String code = trimAndNullIfBlank(line[1]);
|
|
|
|
+ if (StringUtils.isBlank(code)) {
|
|
|
|
+ msg.append(" 学习中心代码不能为空");
|
|
|
|
+ hasError = true;
|
|
|
|
+ } else {
|
|
|
|
+ try {
|
|
|
|
+ Long orgId = getOrgId(orgMap, user.getRootOrgId(), code, orgUd);
|
|
|
|
+ info.setOrgId(orgId);
|
|
|
|
+ } catch (StatusException e) {
|
|
|
|
+ msg.append(" " + e.getDesc());
|
|
|
|
+ hasError = true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String remark = trimAndNullIfBlank(line[3]);
|
|
|
|
+ if (remark != null && remark.length() > 200) {
|
|
|
|
+ msg.append(" 备注长度不能超过200");
|
|
|
|
+ hasError = true;
|
|
|
|
+ }
|
|
|
|
+ info.setRemark(remark);
|
|
|
|
+
|
|
|
|
+ if (hasError) {
|
|
|
|
+ failRecords.add(newError(i + 2, msg.toString()));
|
|
|
|
+ } else {
|
|
|
|
+ list.add(info);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (CollectionUtils.isNotEmpty(failRecords)) {
|
|
|
|
+ return failRecords;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ int i = 1;
|
|
|
|
+ for (OrgIpSave cur : list) {
|
|
|
|
+ try {
|
|
|
|
+ saveForImport(cur);
|
|
|
|
+ } catch (StatusException e) {
|
|
|
|
+ failRecords.add(newError(i + 1, e.getDesc()));
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ failRecords.add(newError(i + 1, "系统异常"));
|
|
|
|
+ log.error("IP导入系统异常", e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (CollectionUtils.isNotEmpty(failRecords)) {
|
|
|
|
+ TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
|
|
|
+ }
|
|
|
|
+ return failRecords;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void saveForImport(OrgIpSave req) {
|
|
|
|
+ if (req.getOrgId() == null) {
|
|
|
|
+ throw new StatusException("学习中心不能为空");
|
|
|
|
+ }
|
|
|
|
+ req.setIp(req.getIp().trim());
|
|
|
|
+ if (req.getRemark() != null && req.getRemark().length() > 200) {
|
|
|
|
+ throw new StatusException("备注长度不能超过200");
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ checkIpExists(req);
|
|
|
|
+ } catch (StatusException e) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ OrgIpEntity e = new OrgIpEntity();
|
|
|
|
+ e.setCreationBy(req.getUser().getUserId());
|
|
|
|
+ e.setUpdateBy(req.getUser().getUserId());
|
|
|
|
+ e.setIp(req.getIp());
|
|
|
|
+ e.setRemark(req.getRemark());
|
|
|
|
+ e.setRootOrgId(req.getUser().getRootOrgId());
|
|
|
|
+ orgIpRepo.save(e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private Long getOrgId(Map<String, Long> orgMap, Long rootOrgId, String orgCode, UserDataRule orgUd) {
|
|
|
|
+ Long id = orgMap.get(orgCode);
|
|
|
|
+ if (id != null) {
|
|
|
|
+ return id;
|
|
|
|
+ }
|
|
|
|
+ GetOrgReq req = new GetOrgReq();
|
|
|
|
+ req.setRootOrgId(rootOrgId);
|
|
|
|
+ req.setOrgCode(orgCode);
|
|
|
|
+ GetOrgResp res = orgCloudService.getOrg(req);
|
|
|
|
+ if (res == null || res.getOrg() == null) {
|
|
|
|
+ throw new StatusException("未找到学习中心信息");
|
|
|
|
+ }
|
|
|
|
+ if (orgUd.assertNeedQueryRefIds() && !orgUd.getRefIds().contains(res.getOrg().getId())) {
|
|
|
|
+ throw new StatusException("没有该学习中心权限");
|
|
|
|
+ }
|
|
|
|
+ id = res.getOrg().getId();
|
|
|
|
+ orgMap.put(orgCode, id);
|
|
|
|
+ return id;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public List<OrgIpInfo> listForExport(OrgIpQuery req) {
|
|
|
|
+ if (req.getOrgUd().assertEmptyQueryResult()) {
|
|
|
|
+ return new ArrayList<>();
|
|
|
|
+ }
|
|
|
|
+ Specification<OrgIpEntity> specification = (root, query, cb) -> {
|
|
|
|
+ List<Predicate> predicates = new ArrayList<>();
|
|
|
|
+ predicates.add(cb.equal(root.get("rootOrgId"), req.getRootOrgId()));
|
|
|
|
+
|
|
|
|
+ if (req.getOrgUd().assertNeedQueryRefIds()) {
|
|
|
|
+ predicates.add(root.get("orgId").in(req.getOrgUd().getRefIds()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (req.getOrgId() != null) {
|
|
|
|
+ predicates.add(cb.equal(root.get("orgId"), req.getOrgId()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return cb.and(predicates.toArray(new Predicate[predicates.size()]));
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ List<OrgIpEntity> list = orgIpRepo.findAll(specification, Sort.by(Direction.DESC, "updateTime"));
|
|
|
|
+ if (CollectionUtils.isEmpty(list)) {
|
|
|
|
+ return new ArrayList<>();
|
|
|
|
+ }
|
|
|
|
+ List<OrgIpInfo> ret = new ArrayList<>();
|
|
|
|
+ for (OrgIpEntity e : list) {
|
|
|
|
+ OrgIpInfo info = new OrgIpInfo();
|
|
|
|
+ ret.add(info);
|
|
|
|
+ info.setId(e.getId());
|
|
|
|
+ info.setIp(e.getIp());
|
|
|
|
+ info.setOrgId(e.getOrgId());
|
|
|
|
+ OrgCacheBean org = CacheHelper.getOrg(e.getOrgId());
|
|
|
|
+ info.setOrgCode(org.getCode());
|
|
|
|
+ info.setOrgName(org.getName());
|
|
|
|
+ info.setRemark(e.getRemark());
|
|
|
|
+ info.setUpdateTime(e.getUpdateTime());
|
|
|
|
+ }
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+}
|