|
@@ -7,6 +7,7 @@ import cn.com.qmth.examcloud.core.oe.admin.dao.IllegallyTypeRepo;
|
|
|
import cn.com.qmth.examcloud.core.oe.admin.dao.entity.IllegallyTypeEntity;
|
|
|
import cn.com.qmth.examcloud.core.oe.admin.service.IllegallyTypeService;
|
|
|
import cn.com.qmth.examcloud.core.oe.admin.service.bean.IllegallyTypeInfo;
|
|
|
+import cn.com.qmth.examcloud.support.enums.DataCategory;
|
|
|
import cn.com.qmth.examcloud.web.helpers.GlobalHelper;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.google.common.collect.Maps;
|
|
@@ -24,176 +25,210 @@ import java.util.Map;
|
|
|
@Service
|
|
|
public class IllegallyTypeServiceImpl implements IllegallyTypeService {
|
|
|
|
|
|
- @Autowired
|
|
|
- IllegallyTypeRepo illegallyTypeRepo;
|
|
|
-
|
|
|
- private static final String[] EXCEL_HEADER = new String[]{"违纪类型名称", "违纪类型代码"};
|
|
|
-
|
|
|
- @Override
|
|
|
- public IllegallyTypeEntity saveIllegallyType(IllegallyTypeInfo info) {
|
|
|
- Long id = info.getId();
|
|
|
- String code = info.getCode();
|
|
|
- Boolean enable = info.getEnable();
|
|
|
- Long rootOrgId = info.getRootOrgId();
|
|
|
- String name = info.getName();
|
|
|
-
|
|
|
- if (null == rootOrgId) {
|
|
|
- throw new StatusException("620001", "rootOrgId is null");
|
|
|
- }
|
|
|
- IllegallyTypeEntity entity = null;
|
|
|
-
|
|
|
- if (null != id) {
|
|
|
- entity = GlobalHelper.getEntity(illegallyTypeRepo, id, IllegallyTypeEntity.class);
|
|
|
- if (null == entity) {
|
|
|
- throw new StatusException("620001", "id is wrong");
|
|
|
- }
|
|
|
- if (!entity.getRootOrgId().equals(rootOrgId)) {
|
|
|
- throw new StatusException("620001", "rootOrgId or id is worng");
|
|
|
- }
|
|
|
- } else if (StringUtils.isNotBlank(code)) {
|
|
|
- entity = illegallyTypeRepo.findByRootOrgIdAndCode(rootOrgId, code);
|
|
|
- if (null == entity) {
|
|
|
- if (StringUtils.isBlank(name)) {
|
|
|
- throw new StatusException("620001", "name is blank");
|
|
|
- }
|
|
|
- entity = new IllegallyTypeEntity();
|
|
|
- entity.setCode(code);
|
|
|
- entity.setName(name);
|
|
|
- entity.setRootOrgId(rootOrgId);
|
|
|
- entity.setEnable(true);
|
|
|
- }
|
|
|
- } else {
|
|
|
- throw new StatusException("620001", "id and code can not be all null");
|
|
|
- }
|
|
|
-
|
|
|
- if (null != enable) {
|
|
|
- entity.setEnable(enable);
|
|
|
- }
|
|
|
- if (StringUtils.isNotBlank(name)) {
|
|
|
- entity.setName(name);
|
|
|
- }
|
|
|
-
|
|
|
- IllegallyTypeEntity saved = illegallyTypeRepo.save(entity);
|
|
|
-
|
|
|
- return saved;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public List<Map<String, Object>> importIllegallyType(Long rootOrgId, File file) {
|
|
|
- List<String[]> lineList = null;
|
|
|
- try {
|
|
|
- lineList = ExcelReader.readSheetBySax(PathUtil.getCanonicalPath(file), 1, 2);
|
|
|
- } catch (Exception e) {
|
|
|
- throw new StatusException("100110", "Excel 解析失败");
|
|
|
- }
|
|
|
-
|
|
|
- if (CollectionUtils.isEmpty(lineList)) {
|
|
|
- throw new StatusException("100111", "Excel无内容");
|
|
|
- }
|
|
|
-
|
|
|
- if (10001 < lineList.size()) {
|
|
|
- throw new StatusException("100112", "数据行数不能超过10000");
|
|
|
- }
|
|
|
-
|
|
|
- List<Map<String, Object>> failRecords = Collections
|
|
|
- .synchronizedList(new ArrayList<Map<String, Object>>());
|
|
|
-
|
|
|
- List<IllegallyTypeEntity> illegallyTypeList = Lists.newArrayList();
|
|
|
-
|
|
|
- for (int i = 0; i < lineList.size(); i++) {
|
|
|
- String[] line = lineList.get(i);
|
|
|
- if (0 == i) {
|
|
|
- if (headerError(line)) {
|
|
|
- throw new StatusException("100111", "Excel表头错误");
|
|
|
- }
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- boolean hasError = false;
|
|
|
- StringBuilder msg = new StringBuilder();
|
|
|
-
|
|
|
- IllegallyTypeEntity illegallyType = new IllegallyTypeEntity();
|
|
|
- illegallyType.setRootOrgId(rootOrgId);
|
|
|
- illegallyType.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;
|
|
|
- }
|
|
|
- illegallyType.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;
|
|
|
- }
|
|
|
-
|
|
|
- illegallyType.setCode(code);
|
|
|
-
|
|
|
- if (hasError) {
|
|
|
- failRecords.add(newError(i + 1, msg.toString()));
|
|
|
- } else {
|
|
|
- illegallyTypeList.add(illegallyType);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- if (CollectionUtils.isNotEmpty(failRecords)) {
|
|
|
- return failRecords;
|
|
|
- }
|
|
|
-
|
|
|
- for (IllegallyTypeEntity cur : illegallyTypeList) {
|
|
|
- IllegallyTypeEntity query = illegallyTypeRepo.findByRootOrgIdAndCode(cur.getRootOrgId(),
|
|
|
- cur.getCode());
|
|
|
-
|
|
|
- if (null != query) {
|
|
|
- query.setName(cur.getName());
|
|
|
- illegallyTypeRepo.save(query);
|
|
|
- } else {
|
|
|
- illegallyTypeRepo.save(cur);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return failRecords;
|
|
|
- }
|
|
|
-
|
|
|
- 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;
|
|
|
- }
|
|
|
+ @Autowired
|
|
|
+ IllegallyTypeRepo illegallyTypeRepo;
|
|
|
+
|
|
|
+ private static final String[] EXCEL_HEADER = new String[]{"违纪类型名称", "违纪类型代码"};
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IllegallyTypeEntity saveIllegallyType(IllegallyTypeInfo info) {
|
|
|
+ Long id = info.getId();
|
|
|
+ String code = info.getCode();
|
|
|
+ Boolean enable = info.getEnable();
|
|
|
+ Long rootOrgId = info.getRootOrgId();
|
|
|
+ String name = info.getName();
|
|
|
+ Integer sortNo = info.getSortNo();
|
|
|
+ String dataCategory = info.getDataCategory();
|
|
|
+
|
|
|
+ if (null == rootOrgId) {
|
|
|
+ throw new StatusException("620001", "rootOrgId is null");
|
|
|
+ }
|
|
|
+ IllegallyTypeEntity entity = null;
|
|
|
+
|
|
|
+ if (null != id) {
|
|
|
+ entity = GlobalHelper.getEntity(illegallyTypeRepo, id, IllegallyTypeEntity.class);
|
|
|
+ if (null == entity) {
|
|
|
+ throw new StatusException("620001", "id is wrong");
|
|
|
+ }
|
|
|
+ if (!entity.getRootOrgId().equals(rootOrgId)) {
|
|
|
+ throw new StatusException("620001", "rootOrgId or id is worng");
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if (StringUtils.isNotBlank(code)) {
|
|
|
+ entity = illegallyTypeRepo.findByRootOrgIdAndCode(rootOrgId, code);
|
|
|
+ if (null == entity) {
|
|
|
+ if (StringUtils.isBlank(name)) {
|
|
|
+ throw new StatusException("620001", "name is blank");
|
|
|
+ }
|
|
|
+ entity = new IllegallyTypeEntity();
|
|
|
+
|
|
|
+ entity.setName(name);
|
|
|
+ entity.setRootOrgId(rootOrgId);
|
|
|
+ entity.setEnable(true);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new StatusException("620001", "id and code can not be all null");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (null != enable) {
|
|
|
+ entity.setEnable(enable);
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(name)) {
|
|
|
+ entity.setName(name);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (null != sortNo) {
|
|
|
+ entity.setSortNo(sortNo);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isNotEmpty(dataCategory)) {
|
|
|
+ entity.setDataCategory(DataCategory.valueOf(dataCategory));
|
|
|
+ }
|
|
|
+
|
|
|
+ IllegallyTypeEntity saved = illegallyTypeRepo.save(entity);
|
|
|
+
|
|
|
+ return saved;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Map<String, Object>> importIllegallyType(Long rootOrgId, File file) {
|
|
|
+ List<String[]> lineList = null;
|
|
|
+ try {
|
|
|
+ lineList = ExcelReader.readSheetBySax(PathUtil.getCanonicalPath(file), 1, 2);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new StatusException("100110", "Excel 解析失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (CollectionUtils.isEmpty(lineList)) {
|
|
|
+ throw new StatusException("100111", "Excel无内容");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (10001 < lineList.size()) {
|
|
|
+ throw new StatusException("100112", "数据行数不能超过10000");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Map<String, Object>> failRecords = Collections
|
|
|
+ .synchronizedList(new ArrayList<Map<String, Object>>());
|
|
|
+
|
|
|
+ List<IllegallyTypeEntity> illegallyTypeList = Lists.newArrayList();
|
|
|
+
|
|
|
+ for (int i = 0; i < lineList.size(); i++) {
|
|
|
+ String[] line = lineList.get(i);
|
|
|
+ if (0 == i) {
|
|
|
+ if (headerError(line)) {
|
|
|
+ throw new StatusException("100111", "Excel表头错误");
|
|
|
+ }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ boolean hasError = false;
|
|
|
+ StringBuilder msg = new StringBuilder();
|
|
|
+
|
|
|
+ IllegallyTypeEntity illegallyType = new IllegallyTypeEntity();
|
|
|
+ illegallyType.setRootOrgId(rootOrgId);
|
|
|
+ illegallyType.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;
|
|
|
+ }
|
|
|
+ illegallyType.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;
|
|
|
+ }
|
|
|
+
|
|
|
+ illegallyType.setCode(code);
|
|
|
+
|
|
|
+ //校验当前编码是否为系统编码
|
|
|
+ IllegallyTypeEntity existSysData = illegallyTypeRepo.findByRootOrgIdAndCodeAndDataCategory(
|
|
|
+ rootOrgId, code, DataCategory.SYSTEM.name());
|
|
|
+ if (null != existSysData) {
|
|
|
+ msg.append(" 违纪类型编码不允许存在数据类型为系统的数据");
|
|
|
+ hasError = true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //排序号
|
|
|
+ String sortNo = trimAndNullIfBlank(line[2]);
|
|
|
+ if (StringUtils.isBlank(code)) {
|
|
|
+ msg.append(" 排序号不能为空");
|
|
|
+ hasError = true;
|
|
|
+ } else if (StringUtils.isNumeric(sortNo)) {
|
|
|
+ msg.append(" 排序号必须为正整数的数字");
|
|
|
+ hasError = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ illegallyType.setSortNo(Integer.valueOf(sortNo));
|
|
|
+
|
|
|
+ if (hasError) {
|
|
|
+ failRecords.add(newError(i + 1, msg.toString()));
|
|
|
+ } else {
|
|
|
+ illegallyTypeList.add(illegallyType);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (CollectionUtils.isNotEmpty(failRecords)) {
|
|
|
+ return failRecords;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (IllegallyTypeEntity cur : illegallyTypeList) {
|
|
|
+ IllegallyTypeEntity existData = illegallyTypeRepo.findByRootOrgIdAndCode(cur.getRootOrgId(),
|
|
|
+ cur.getCode());
|
|
|
+
|
|
|
+ if (null != existData) {
|
|
|
+ existData.setName(cur.getName());
|
|
|
+ existData.setSortNo(cur.getSortNo());
|
|
|
+ illegallyTypeRepo.save(existData);
|
|
|
+ } else {
|
|
|
+ illegallyTypeRepo.save(cur);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return failRecords;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 方法注释
|
|
|
+ *
|
|
|
+ * @param header
|
|
|
+ * @author WANGWEI
|
|
|
+ */
|
|
|
+ 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;
|
|
|
+ }
|
|
|
|
|
|
}
|