|
|
@@ -0,0 +1,134 @@
|
|
|
+package com.zhongshu.card.server.core.service.school;
|
|
|
+
|
|
|
+import com.zhongshu.card.client.model.school.AreaModel;
|
|
|
+import com.zhongshu.card.client.model.school.AreaParam;
|
|
|
+import com.zhongshu.card.client.ret.ResultContent;
|
|
|
+import com.zhongshu.card.client.ret.ResultMessage;
|
|
|
+import com.zhongshu.card.client.service.school.AreaService;
|
|
|
+import com.zhongshu.card.client.utils.ITree;
|
|
|
+import com.zhongshu.card.client.utils.TreeUtil;
|
|
|
+import com.zhongshu.card.client.utils.type.DataState;
|
|
|
+import com.zhongshu.card.server.core.dao.school.AreaDao;
|
|
|
+import com.zhongshu.card.server.core.dao.school.CollegeDao;
|
|
|
+import com.zhongshu.card.server.core.domain.school.Area;
|
|
|
+import com.zhongshu.card.server.core.domain.school.College;
|
|
|
+import com.zhongshu.card.server.core.service.base.SuperService;
|
|
|
+import com.zhongshu.card.server.core.util.BeanUtils;
|
|
|
+import com.zhongshu.card.server.core.util.CommonUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.ObjectUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author TRX
|
|
|
+ * @date 2024/6/3
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class AreaServiceImpl extends SuperService implements AreaService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ AreaDao areaDao;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加院级
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResultContent<College> addArea(AreaParam param) {
|
|
|
+ initDefaultUserAndOid(param);
|
|
|
+
|
|
|
+ if (CommonUtil.longIsEmpty(param.getSort())) {
|
|
|
+ param.setSort(1L);
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(param.getParentId())) {
|
|
|
+ param.setParentId(ITree.ROOT_ID);
|
|
|
+ }
|
|
|
+ if (!param.getParentId().equals(ITree.ROOT_ID)) {
|
|
|
+ Area temp = areaDao.findTopById(param.getParentId());
|
|
|
+ if (ObjectUtils.isEmpty(temp)) {
|
|
|
+ return ResultContent.buildFail(String.format("上级数据ID不存在:%s", param.getParentId()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (param.getState() == null) {
|
|
|
+ param.setState(DataState.Enable);
|
|
|
+ }
|
|
|
+ Area entity = null;
|
|
|
+ if (StringUtils.isNotEmpty(param.getId())) {
|
|
|
+ entity = areaDao.findTopById(param.getId());
|
|
|
+ if (ObjectUtils.isEmpty(entity)) {
|
|
|
+ return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(param.getCode())) {
|
|
|
+ entity = areaDao.findTopByCodeAndOid(param.getCode(), param.getOid());
|
|
|
+ if (ObjectUtils.isNotEmpty(entity) && !entity.getId().equals(param.getId())) {
|
|
|
+ return ResultContent.buildFail(String.format("%s code已存在", param.getCode()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (StringUtils.isNotEmpty(param.getCode())) {
|
|
|
+ entity = areaDao.findTopByCodeAndOid(param.getCode(), entity.getOid());
|
|
|
+ if (ObjectUtils.isNotEmpty(entity)) {
|
|
|
+ return ResultContent.buildFail(String.format("%s code已存在", param.getCode()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ entity = new Area();
|
|
|
+ }
|
|
|
+ BeanUtils.copyProperties(param, entity);
|
|
|
+ areaDao.save(entity);
|
|
|
+ return ResultContent.buildSuccess(entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResultContent deleteArea(String id) {
|
|
|
+ String oid = getCurrentOid();
|
|
|
+ Area entity = areaDao.findTopById(id);
|
|
|
+ if (ObjectUtils.isEmpty(entity)) {
|
|
|
+ return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
|
|
|
+ }
|
|
|
+ List<Area> childrens = areaDao.findByParentIdAndOid(id, oid);
|
|
|
+ if (ObjectUtils.isNotEmpty(childrens)) {
|
|
|
+ return ResultContent.buildFail(String.format("%s 有下级数据,不能删除", entity.getName()));
|
|
|
+ }
|
|
|
+ areaDao.delete(entity);
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到所有信息 (树形结构)
|
|
|
+ *
|
|
|
+ * @param oid
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResultContent<List<ITree>> getAreaTree(String oid) {
|
|
|
+ if (StringUtils.isEmpty(oid)) {
|
|
|
+ oid = getCurrentOid();
|
|
|
+ }
|
|
|
+ List<Area> list = areaDao.findByOidOrderBySortAsc(oid);
|
|
|
+ List<ITree> models = list.stream().map(this::toModel).collect(Collectors.toList());
|
|
|
+ List<ITree> iTrees = TreeUtil.buildTree(models, ITree.ROOT_ID);
|
|
|
+ return ResultContent.buildSuccess(iTrees);
|
|
|
+ }
|
|
|
+
|
|
|
+ public AreaModel toModel(Area entity) {
|
|
|
+ AreaModel model = new AreaModel();
|
|
|
+ if (ObjectUtils.isNotEmpty(entity)) {
|
|
|
+ BeanUtils.copyProperties(entity, model);
|
|
|
+ }
|
|
|
+ return model;
|
|
|
+ }
|
|
|
+}
|