From 657182c7989dc77b69bb3f9adaf6c75b6466ff95 Mon Sep 17 00:00:00 2001
From: kaiyu <404897439@qq.com>
Date: Wed, 09 Jun 2021 10:31:35 +0800
Subject: [PATCH] screen-manage                  编辑账号接口删除分配角色功能,分配角色功能变为单独接口

---
 screen-manage/src/main/java/com/moral/api/service/impl/OrganizationServiceImpl.java |  374 ++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 238 insertions(+), 136 deletions(-)

diff --git a/screen-manage/src/main/java/com/moral/api/service/impl/OrganizationServiceImpl.java b/screen-manage/src/main/java/com/moral/api/service/impl/OrganizationServiceImpl.java
index 705f087..5e40443 100644
--- a/screen-manage/src/main/java/com/moral/api/service/impl/OrganizationServiceImpl.java
+++ b/screen-manage/src/main/java/com/moral/api/service/impl/OrganizationServiceImpl.java
@@ -6,22 +6,25 @@
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.moral.api.config.mybatis.wrapper.NullFilterWrapper;
+import com.moral.api.entity.MonitorPoint;
 import com.moral.api.entity.Organization;
+import com.moral.api.entity.User;
+import com.moral.api.mapper.MonitorPointMapper;
 import com.moral.api.mapper.OrganizationMapper;
+import com.moral.api.mapper.UserMapper;
 import com.moral.api.pojo.dto.organization.OrganizationDTO;
 import com.moral.api.pojo.dto.organization.OrganizationQueryDTO;
-import com.moral.api.pojo.form.organization.OrganizationDeleteForm;
-import com.moral.api.pojo.form.organization.OrganizationInsertForm;
-import com.moral.api.pojo.form.organization.OrganizationQueryForm;
-import com.moral.api.pojo.form.organization.OrganizationUpdateForm;
+import com.moral.api.pojo.dto.organization.OrganizationQueryNamesDTO;
+import com.moral.api.pojo.form.organization.*;
+import com.moral.api.service.MonitorPointService;
 import com.moral.api.service.OrganizationService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.moral.api.service.UserService;
 import com.moral.api.util.LogUtils;
 import com.moral.constant.Constants;
 import com.moral.constant.ResponseCodeEnum;
 import com.moral.util.ConvertUtils;
 import com.moral.util.DateUtils;
-import lombok.Data;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Service;
@@ -32,6 +35,7 @@
 
 import javax.servlet.http.HttpServletRequest;
 import java.util.*;
+import java.util.function.Predicate;
 
 /**
  * <p>
@@ -51,6 +55,15 @@
     @Autowired
     LogUtils logUtils;
 
+    @Autowired
+    UserService userService;
+
+    @Autowired
+    UserMapper userMapper;
+
+    @Autowired
+    MonitorPointService monitorPointService;
+
     Map<String, String> organizationFormMap;
 
     public void setOrganizationFormMap(Map<String, String> organizationFormMap) {
@@ -64,6 +77,8 @@
      * @Author: ���������
      * @Date: 2021/3/22
      */
+
+
     @Override
     @Transactional
     public OrganizationDTO insertOrganization(OrganizationInsertForm organizationInsertForm) {
@@ -82,34 +97,18 @@
             organizationDTO.setMsg(ResponseCodeEnum.ORGANIZATION_EXIST.getMsg());
             return organizationDTO;
         }
-        //���������������������������
-        Integer parentId = organization.getParentId();
-        Organization parentOrganization = new Organization();
-        if (!ObjectUtils.isEmpty(parentId)) {
-            parentOrganization.setId(parentId);
-            parentOrganization.setIsDelete(Constants.NOT_DELETE);
-            queryWrapper.setEntity(parentOrganization);
-            parentOrganization = organizationMapper.selectOne(queryWrapper);
-            if (ObjectUtils.isEmpty(parentOrganization)) {
-                organizationDTO.setCode(ResponseCodeEnum.ORGANIZATION_PARENT_NOT_EXIST.getCode());
-                organizationDTO.setMsg(ResponseCodeEnum.ORGANIZATION_PARENT_NOT_EXIST.getMsg());
-                return organizationDTO;
-            }
-        }
         //������������
         organizationMapper.insert(organization);
 
         //������DTO������
-        organizationDTO.setParentOrganization(parentOrganization);
-        organizationDTO.setOrganization(organization);
         organizationDTO.setCode(ResponseCodeEnum.SUCCESS.getCode());
         organizationDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
 
         //������������������
         HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
         StringBuilder content = new StringBuilder();
-        content.append("������������������").append(organization.getName());
-        logUtils.saveOperationForManage(request, content.toString());
+        content.append("������������������").append(organization.getName() + ";");
+        logUtils.saveOperationForManage(request, content.toString(), Constants.INSERT_OPERATE_TYPE);
         return organizationDTO;
     }
 
@@ -127,34 +126,19 @@
         //form���entity
         Organization organization = organizationUpdateForm.formConvertEntity();
 
-        //������������������������
-        QueryWrapper<Organization> existWrapper = new QueryWrapper<>();
-        Organization existOrganization = new Organization();
-        existOrganization.setId(organization.getId());
-        existOrganization.setIsDelete(Constants.NOT_DELETE);
-        existWrapper.setEntity(existOrganization);
-        existOrganization = organizationMapper.selectOne(existWrapper);
-        if (ObjectUtils.isEmpty(existOrganization)) {
+        //������������������������������������������
+        QueryWrapper<Organization> oldWrapper = new QueryWrapper<>();
+        Organization oldOrganization = new Organization();
+        oldOrganization.setId(organization.getId());
+        oldOrganization.setIsDelete(Constants.NOT_DELETE);
+        oldWrapper.setEntity(oldOrganization);
+        oldOrganization = organizationMapper.selectOne(oldWrapper);
+        if (ObjectUtils.isEmpty(oldOrganization)) {
             organizationDTO.setCode(ResponseCodeEnum.ORGANIZATION_NOT_EXIST.getCode());
             organizationDTO.setMsg(ResponseCodeEnum.ORGANIZATION_NOT_EXIST.getMsg());
             return organizationDTO;
         }
 
-        //������������������������������������������������������
-        Integer parentId = organization.getParentId();
-        Organization parentOrganization = new Organization();
-        if (!ObjectUtils.isEmpty(parentId) && parentId != 0) {
-            QueryWrapper<Organization> existParentWrapper = new QueryWrapper<>();
-            parentOrganization.setId(parentId);
-            parentOrganization.setIsDelete(Constants.NOT_DELETE);
-            existParentWrapper.setEntity(parentOrganization);
-            parentOrganization = organizationMapper.selectOne(existParentWrapper);
-            if (ObjectUtils.isEmpty(parentOrganization)) {
-                organizationDTO.setCode(ResponseCodeEnum.ORGANIZATION_PARENT_NOT_EXIST.getCode());
-                organizationDTO.setMsg(ResponseCodeEnum.ORGANIZATION_PARENT_NOT_EXIST.getMsg());
-                return organizationDTO;
-            }
-        }
 
         //������������
         organizationMapper.updateById(organization);
@@ -163,59 +147,16 @@
         organization = organizationMapper.selectById(organization.getId());
 
         //������DTO������
-        organizationDTO.setParentOrganization(parentOrganization);
-        organizationDTO.setOrganization(organization);
         organizationDTO.setCode(ResponseCodeEnum.SUCCESS.getCode());
         organizationDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
 
         //������������������
-        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
-        StringBuilder content = new StringBuilder();
-        content.append("������������������").append(organization.getName()).append(";");
-        //������������������Map
-        Map<String, Object> newParameters = JSONObject.parseObject(JSON.toJSONString(organizationUpdateForm), Map.class);
-        Map<String, Object> oldParameters = JSONObject.parseObject(JSON.toJSONString(existOrganization), Map.class);
-        Set<String> keys = organizationFormMap.keySet();
-        for (String key : keys) {
-            String value = organizationFormMap.get(key);
-            if ("parentName".equals(key)) {//���������������������������
-                if (organizationUpdateForm.getParentId() != null) {//������������������������������������
-                    String oldParentName = "���";
-                    String newParentName = "���";
-                    if (!existOrganization.getParentId().equals(0)) {
-                        oldParentName = organizationMapper.selectById(existOrganization.getParentId()).getName();
-                    }
-                    if (!organization.getParentId().equals(0)) {
-                        newParentName = organizationMapper.selectById(organization.getParentId()).getName();
-                    }
-                    content.append(value + ":" + oldParentName + "->" + newParentName + ";");
-                }
-            } else if ("expireTime".equals(key)) {//expireTime������������������������
-                if (organizationUpdateForm.getExpireTime() != null) {
-                    Date oldExpireTime = existOrganization.getExpireTime();
-                    Date newExpireTime = organization.getExpireTime();
-                    String oldExpireTimeStr = DateUtils.dateToDateString(oldExpireTime, "yyyy-MM-dd");
-                    String newExpireTimeStr = DateUtils.dateToDateString(newExpireTime, "yyyy-MM-dd");
-                    content.append(value + ":" + oldExpireTimeStr + "->" + newExpireTimeStr + ";");
-                }
-            } else {//������������������
-                if (newParameters.get(key) != null) {
-                    String newValue = "���";
-                    String oldValue = "���";
-                    if (newParameters.get(key) != null && !newParameters.get(key).equals(" ")) {
-                        newValue = String.valueOf(newParameters.get(key));
-                    }
-                    if (oldParameters.get(key) != null && !oldParameters.get(key).equals(" ")) {
-                        oldValue = String.valueOf(oldParameters.get(key));
-                    }
-                    content.append(value + ":" + oldValue + "->" + newValue + ";");
-                }
-            }
-        }
-        logUtils.saveOperationForManage(request, content.toString());
+        insertUpdateLog(organizationUpdateForm, organization, oldOrganization);
 
         return organizationDTO;
     }
+
+
 
     /**
      * @Description: ������������������
@@ -230,7 +171,16 @@
         OrganizationDTO dto = new OrganizationDTO();
         //������
         Integer id = form.getOrganizationId();
-        //������������������������
+
+        //������������������������������
+        List<MonitorPoint> monitorPoints = monitorPointService.getMonitorPointsByOrganizationId(id);
+        if(!ObjectUtils.isEmpty(monitorPoints)){
+            dto.setCode(ResponseCodeEnum.ORGANIZATION_EXIST_MONITORPOINT.getCode());
+            dto.setMsg(ResponseCodeEnum.ORGANIZATION_EXIST_MONITORPOINT.getMsg());
+            return dto;
+        }
+
+        //������������������������������������������
         Organization existOrganization = new Organization();
         existOrganization.setIsDelete(Constants.NOT_DELETE);
         existOrganization.setId(id);
@@ -242,35 +192,69 @@
             dto.setMsg(ResponseCodeEnum.ORGANIZATION_NOT_EXIST.getMsg());
             return dto;
         }
+
+        //���������������������������
+        List<Organization> children = getAllChildrenOrganization(existOrganization.getId());
+        if (!ObjectUtils.isEmpty(children)) {
+            //���������������������������������������������������������������������������������������������������������������������������
+            UpdateWrapper updateWrapper = new UpdateWrapper();
+            if (form.getDeleteChildren().equals(Constants.DELETE_CHILDREN_ORG)) {//���������������������
+                //������������������������id���������������
+                List<Integer> childrenId = new ArrayList<>();
+                children.forEach(value -> {
+                    childrenId.add(value.getId());
+                });
+                //���������������������������������������������������������������������������������
+                for (Integer childId : childrenId) {
+                    List<MonitorPoint> childMonitorPoints = monitorPointService.getMonitorPointsByOrganizationId(childId);
+                    if(!ObjectUtils.isEmpty(childMonitorPoints)){
+                        dto.setCode(ResponseCodeEnum.CHILDREN_ORGANIZATION_EXIST_MONITORPOINT.getCode());
+                        dto.setMsg(ResponseCodeEnum.CHILDREN_ORGANIZATION_EXIST_MONITORPOINT.getMsg());
+                        return dto;
+                    }
+                }
+                //������������
+                updateWrapper.in("id", childrenId);
+                updateWrapper.set("is_delete", Constants.DELETE);
+                organizationMapper.update(null, updateWrapper);
+                //���������������������������
+                childrenId.forEach(value->userService.deleteUsersByOrganizationId(value));
+
+            } else {//���������
+                //���������������������������id
+                List<Integer> childrenId = new ArrayList<>();
+                children.forEach(value -> {
+                    if (value.getParentId().equals(id))
+                        childrenId.add(value.getId());
+                });
+                //������������
+                updateWrapper.in("id", childrenId);
+                updateWrapper.set("parent_id", 0);
+                organizationMapper.update(null, updateWrapper);
+            }
+        }
+
         //������������������
         UpdateWrapper deleteWrapper = new UpdateWrapper();
         deleteWrapper.eq("id", id);
         deleteWrapper.set("is_delete", Constants.DELETE);
         organizationMapper.update(null, deleteWrapper);
-        //������������������������������������������������������������������parentId���0
-        if (form.getDeleteChildren().equals(Constants.DELETE_CHILDREN_ORG)) {
-            UpdateWrapper deleteChildrenWrapper = new UpdateWrapper();
-            deleteChildrenWrapper.eq("parent_id", id);
-            deleteChildrenWrapper.set("is_delete", Constants.DELETE);
-            organizationMapper.update(null, deleteChildrenWrapper);
-        } else {
-            UpdateWrapper updateChildrenWrapper = new UpdateWrapper();
-            updateChildrenWrapper.eq("parent_id", id);
-            updateChildrenWrapper.set("parent_id", 0);
-            organizationMapper.update(null, updateChildrenWrapper);
-        }
+
+        //������������������
+        userService.deleteUsersByOrganizationId(id);
 
         //������������������
-        dto.setOrganization(existOrganization);
         dto.setCode(ResponseCodeEnum.SUCCESS.getCode());
         dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
         //������������������
         HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
         StringBuilder content = new StringBuilder();
         content.append("������������������").append(existOrganization.getName());
-        if(form.getDeleteChildren().equals(Constants.DELETE_CHILDREN_ORG))
-            content.append("���������������������");
-        logUtils.saveOperationForManage(request, content.toString());
+        if (form.getDeleteChildren().equals(Constants.DELETE_CHILDREN_ORG))
+            content.append("���������������������;");
+        else
+            content.append(";");
+        logUtils.saveOperationForManage(request, content.toString(), Constants.DELETE_OPERATE_TYPE);
 
         return dto;
     }
@@ -339,14 +323,22 @@
         Page<Organization> resultPage = organizationMapper.selectPage(page, queryWrapper);
         List<Organization> organizations = resultPage.getRecords();
         List<OrganizationDTO> organizationDTOS = new ArrayList<>();
-        //������������������������������������������organization���DTO���
-        for (Organization child : organizations) {
+        //���������������������������������admin������������������organization���DTO���
+        for (Organization organization : organizations) {
             OrganizationDTO resultDto = new OrganizationDTO();
-            Organization parent = organizationMapper.selectById(child.getParentId());//���������������
-            //���������������������
-            changeAddressByOrganization(child);
-            resultDto.setOrganization(child);
+            //���������������
+            Organization parent = organizationMapper.selectById(organization.getParentId());
+            resultDto.setOrganization(organization);
             resultDto.setParentOrganization(parent);
+            //������admin������
+            QueryWrapper userWrapper = new QueryWrapper();
+            User adminUser = new User();
+            adminUser.setIsAdmin(true);
+            adminUser.setOrganizationId(organization.getId());
+            adminUser.setIsDelete(Constants.NOT_DELETE);
+            userWrapper.setEntity(adminUser);
+            adminUser = userMapper.selectOne(userWrapper);
+            resultDto.setAdminUser(adminUser);
             organizationDTOS.add(resultDto);
         }
 
@@ -354,35 +346,145 @@
         dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
         dto.setOrganizationDTOS(organizationDTOS);
         dto.setCurrent(page.getCurrent());
-        dto.setPage(page.getPages());
+        dto.setPages(page.getPages());
         dto.setSize(page.getSize());
         dto.setTotal(page.getTotal());
         return dto;
     }
 
     /**
-     * @Description: ���organization���address���������provinceName cityName areaName������������
-     * @Param: [organization]
+    * @Description: ������form���id���������������������������������
+     *              ������id������������������������id������id������������������������������������
+            * @Param: [from]
+            * @return: com.moral.api.pojo.dto.organization.OrganizationQueryNamesDTO
+            * @Author: ���������
+            * @Date: 2021/5/7
+            */
+    @Override
+    public OrganizationQueryNamesDTO queryNames(OrganizationQueryNamesForm form) {
+        OrganizationQueryNamesDTO dto = new OrganizationQueryNamesDTO();
+        //������
+        Integer id = form.getId();
+        //������������������
+        QueryWrapper<Organization> queryWrapper = new QueryWrapper();
+        queryWrapper.eq("is_delete", Constants.NOT_DELETE);
+        //������������������
+        List<Organization> organizations = organizationMapper.selectList(queryWrapper);
+        //������form������������id,���������������������������������������������
+        if(!ObjectUtils.isEmpty(id)){
+            List<Organization> children = getAllChildrenOrganization(id);
+            List<Integer> thisAndchildrenIds = new ArrayList<>();//���id���������������������������id������
+            thisAndchildrenIds.add(id);
+            for (Organization child : children) {
+                thisAndchildrenIds.add(child.getId());
+            }
+            organizations.removeIf(new Predicate<Organization>() {//������
+                @Override
+                public boolean test(Organization organization) {
+                    if(thisAndchildrenIds.contains(organization.getId()))
+                        return true;
+                    return false;
+                }
+            });
+        }
+        //������������������
+        dto.setCode(ResponseCodeEnum.SUCCESS.getCode());
+        dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
+        dto.setOrganizations(organizations);
+        return dto;
+    }
+
+
+    /**
+     * @Description: ���������������������������
+     * @Param: [updateForm, newOrganization, oldOrganization]
      * @return: void
      * @Author: ���������
-     * @Date: 2021/4/2
+     * @Date: 2021/4/8
      */
-    public void changeAddressByOrganization(Organization organization) {
-        String provinceName = organization.getProvinceName();
-        String cityName = organization.getCityName();
-        String areaName = organization.getAreaName();
-        String address = organization.getAddress();
-
-        StringBuilder newAddress = new StringBuilder();
-        if (provinceName != null)
-            newAddress.append(provinceName);
-        if (cityName != null)
-            newAddress.append(cityName);
-        if (areaName != null)
-            newAddress.append(areaName);
-        if (address != null)
-            newAddress.append(address);
-
-        organization.setAddress(newAddress.toString());
+    private void insertUpdateLog(OrganizationUpdateForm updateForm, Organization newOrganization, Organization oldOrganization) {
+        //������������������
+        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
+        StringBuilder content = new StringBuilder();
+        content.append("������������������").append(oldOrganization.getName()).append(";");
+        //������������Map,���������������������������������
+        Map<String, Object> newParameters = JSONObject.parseObject(JSON.toJSONString(updateForm), Map.class);
+        Map<String, Object> oldParameters = JSONObject.parseObject(JSON.toJSONString(oldOrganization), Map.class);
+        //������������������������Map���������������������������
+        Set<String> keys = organizationFormMap.keySet();
+        for (String key : keys) {
+            String value = organizationFormMap.get(key);//���������������������
+            if ("parentName".equals(key)) {//���������������������������
+                if (updateForm.getParentId() != null) {//������������������������������������
+                    String oldParentName = "���";
+                    String newParentName = "���";
+                    if (!oldOrganization.getParentId().equals(0)) {
+                        oldParentName = organizationMapper.selectById(oldOrganization.getParentId()).getName();
+                    }
+                    if (!newOrganization.getParentId().equals(0)) {
+                        newParentName = organizationMapper.selectById(newOrganization.getParentId()).getName();
+                    }
+                    content.append(value + ":" + oldParentName + "->" + newParentName + ";");
+                }
+            } else if ("expireTime".equals(key)) {//expireTime������������������������
+                if (updateForm.getExpireTime() != null) {
+                    Date oldExpireTime = oldOrganization.getExpireTime();
+                    Date newExpireTime = newOrganization.getExpireTime();
+                    String oldExpireTimeStr = DateUtils.dateToDateString(oldExpireTime, "yyyy-MM-dd");
+                    String newExpireTimeStr = DateUtils.dateToDateString(newExpireTime, "yyyy-MM-dd");
+                    content.append(value + ":" + oldExpireTimeStr + "->" + newExpireTimeStr + ";");
+                }
+            } else {//������������������
+                if (newParameters.get(key) != null) {
+                    String newValue = "���";
+                    String oldValue = "���";
+                    if (newParameters.get(key) != null && !newParameters.get(key).equals(" ")) {
+                        newValue = String.valueOf(newParameters.get(key));
+                    }
+                    if (oldParameters.get(key) != null && !oldParameters.get(key).equals(" ")) {
+                        oldValue = String.valueOf(oldParameters.get(key));
+                    }
+                    content.append(value + ":" + oldValue + "->" + newValue + ";");
+                }
+            }
+        }
+        logUtils.saveOperationForManage(request, content.toString(), Constants.UPDATE_OPERATE_TYPE);
     }
+
+    /**
+     * @Description: ������������������������������������
+     * @Param: []
+     * @return: java.util.List<com.moral.api.entity.Organization>
+     * @Author: ���������
+     * @Date: 2021/4/14
+     */
+    private List<Organization> getAllChildrenOrganization(Integer parentId) {
+        List<Organization> children = new ArrayList<>();
+        recursionQueryChildren(parentId, children);
+        return children;
+    }
+
+    /**
+     * @Description: ���������������������������������������������������children���
+     * @Param: [parent, children]
+     * @return: void
+     * @Author: ���������
+     * @Date: 2021/4/14
+     */
+    private void recursionQueryChildren(Integer parentId, List<Organization> children) {
+        QueryWrapper<Organization> queryWrapper = new QueryWrapper();
+        queryWrapper.eq("is_delete", Constants.NOT_DELETE);
+        queryWrapper.eq("parent_id", parentId);
+        List<Organization> organizations = organizationMapper.selectList(queryWrapper);
+        if (!ObjectUtils.isEmpty(organizations)) {
+            children.addAll(organizations);
+            for (Organization organization : organizations) {
+                recursionQueryChildren(organization.getId(), children);
+            }
+        } else {
+            return;
+        }
+    }
+
+
 }

--
Gitblit v1.8.0