|  |  | 
 |  |  | package com.moral.api.service.impl; | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson.JSON; | 
 |  |  | import com.alibaba.fastjson.JSONObject; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
 |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
 |  |  | 
 |  |  | 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.pojo.CompareFieldResult; | 
 |  |  | import com.moral.util.ConvertUtils; | 
 |  |  | import com.moral.util.DateUtils; | 
 |  |  |  | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.boot.context.properties.ConfigurationProperties; | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  | import org.springframework.transaction.annotation.Transactional; | 
 |  |  | import org.springframework.util.ObjectUtils; | 
 |  |  | import org.springframework.web.context.request.RequestContextHolder; | 
 |  |  | import org.springframework.web.context.request.ServletRequestAttributes; | 
 |  |  |  | 
 |  |  | import javax.servlet.http.HttpServletRequest; | 
 |  |  | import java.text.ParseException; | 
 |  |  | import java.text.SimpleDateFormat; | 
 |  |  | import java.util.*; | 
 |  |  | import java.util.function.Predicate; | 
 |  |  |  | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     OrganizationMapper organizationMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     LogUtils logUtils; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     UserService userService; | 
 |  |  | 
 |  |  |         organizationDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
 |  |  |  | 
 |  |  |         //操作插入日志 | 
 |  |  |         updateLog(oldOrganization,organization); | 
 |  |  |         updateLog(oldOrganization, organization); | 
 |  |  |  | 
 |  |  |         return organizationDTO; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  | 
 |  |  |  | 
 |  |  |         //判断组织是否含有站点 | 
 |  |  |         List<MonitorPoint> monitorPoints = monitorPointService.getMonitorPointsByOrganizationId(id); | 
 |  |  |         if(!ObjectUtils.isEmpty(monitorPoints)){ | 
 |  |  |         if (!ObjectUtils.isEmpty(monitorPoints)) { | 
 |  |  |             dto.setCode(ResponseCodeEnum.ORGANIZATION_EXIST_MONITORPOINT.getCode()); | 
 |  |  |             dto.setMsg(ResponseCodeEnum.ORGANIZATION_EXIST_MONITORPOINT.getMsg()); | 
 |  |  |             return dto; | 
 |  |  | 
 |  |  |                 //判断子组织是否含有站点,有一个子组织含有站点则不能删除 | 
 |  |  |                 for (Integer childId : childrenId) { | 
 |  |  |                     List<MonitorPoint> childMonitorPoints = monitorPointService.getMonitorPointsByOrganizationId(childId); | 
 |  |  |                     if(!ObjectUtils.isEmpty(childMonitorPoints)){ | 
 |  |  |                     if (!ObjectUtils.isEmpty(childMonitorPoints)) { | 
 |  |  |                         dto.setCode(ResponseCodeEnum.CHILDREN_ORGANIZATION_EXIST_MONITORPOINT.getCode()); | 
 |  |  |                         dto.setMsg(ResponseCodeEnum.CHILDREN_ORGANIZATION_EXIST_MONITORPOINT.getMsg()); | 
 |  |  |                         return dto; | 
 |  |  | 
 |  |  |                 updateWrapper.set("is_delete", Constants.DELETE); | 
 |  |  |                 organizationMapper.update(null, updateWrapper); | 
 |  |  |                 //删除所有子组织账号 | 
 |  |  |                 childrenId.forEach(value->userService.deleteUsersByOrganizationId(value)); | 
 |  |  |                 childrenId.forEach(value -> userService.deleteUsersByOrganizationId(value)); | 
 |  |  |  | 
 |  |  |             } else {//不删除 | 
 |  |  |                 //提取所有直属子组织id | 
 |  |  | 
 |  |  |             content.append("以及所有子组织;"); | 
 |  |  |         else | 
 |  |  |             content.append(";"); | 
 |  |  |         logUtils.saveOperationForManage(content.toString(), Constants.DELETE_OPERATE_TYPE); | 
 |  |  |         LogUtils.saveOperationForManage(content.toString(), Constants.DELETE_OPERATE_TYPE); | 
 |  |  |  | 
 |  |  |         return dto; | 
 |  |  |     } | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |     * @Description: 如果form中id为空,则查询所有的组织 | 
 |  |  |      *              如果id不为空,则查询该id和该id所有子组织之外的所有组织 | 
 |  |  |             * @Param: [from] | 
 |  |  |             * @return: com.moral.api.pojo.dto.organization.OrganizationQueryNamesDTO | 
 |  |  |             * @Author: 陈凯裕 | 
 |  |  |             * @Date: 2021/5/7 | 
 |  |  |             */ | 
 |  |  |      * @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(); | 
 |  |  | 
 |  |  |         //查询所有组织 | 
 |  |  |         List<Organization> organizations = organizationMapper.selectList(queryWrapper); | 
 |  |  |         //判断form是否含有id,如果有则对其所有子组织进行过滤 | 
 |  |  |         if(!ObjectUtils.isEmpty(id)){ | 
 |  |  |         if (!ObjectUtils.isEmpty(id)) { | 
 |  |  |             List<Organization> children = getAllChildrenOrganization(id); | 
 |  |  |             List<Integer> thisAndchildrenIds = new ArrayList<>();//该id以及其所有子组织的id集合 | 
 |  |  |             thisAndchildrenIds.add(id); | 
 |  |  | 
 |  |  |             organizations.removeIf(new Predicate<Organization>() {//过滤 | 
 |  |  |                 @Override | 
 |  |  |                 public boolean test(Organization organization) { | 
 |  |  |                     if(thisAndchildrenIds.contains(organization.getId())) | 
 |  |  |                     if (thisAndchildrenIds.contains(organization.getId())) | 
 |  |  |                         return true; | 
 |  |  |                     return false; | 
 |  |  |                 } | 
 |  |  | 
 |  |  |      * @Author: 陈凯裕 | 
 |  |  |      * @Date: 2021/4/14 | 
 |  |  |      */ | 
 |  |  |     private List<Organization> getAllChildrenOrganization(Integer parentId) { | 
 |  |  |     @Override | 
 |  |  |     public List<Organization> getAllChildrenOrganization(Integer parentId) { | 
 |  |  |         List<Organization> children = new ArrayList<>(); | 
 |  |  |         recursionQueryChildren(parentId, children); | 
 |  |  |         return children; | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |     * @Description: 插入操作插入日志 | 
 |  |  |             * @Param: [organization] | 
 |  |  |             * @return: void | 
 |  |  |             * @Author: 陈凯裕 | 
 |  |  |             * @Date: 2021/8/25 | 
 |  |  |             */ | 
 |  |  |     private void insertLog(Organization organization){ | 
 |  |  |      * @Description: 插入操作插入日志 | 
 |  |  |      * @Param: [organization] | 
 |  |  |      * @return: void | 
 |  |  |      * @Author: 陈凯裕 | 
 |  |  |      * @Date: 2021/8/25 | 
 |  |  |      */ | 
 |  |  |     private void insertLog(Organization organization) { | 
 |  |  |         StringBuilder content = new StringBuilder(); | 
 |  |  |         content.append("添加了组织:").append(organization.getName() + ";"); | 
 |  |  |         logUtils.saveOperationForManage(content.toString(), Constants.INSERT_OPERATE_TYPE); | 
 |  |  |         LogUtils.saveOperationForManage(content.toString(), Constants.INSERT_OPERATE_TYPE); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public void updateLog(Organization oldOrganization,Organization newOrganization){ | 
 |  |  |     public void updateLog(Organization oldOrganization, Organization newOrganization) { | 
 |  |  |         List<CompareFieldResult> results = CompareFieldUtils.compare(Organization.class, oldOrganization, newOrganization); | 
 |  |  |         for (CompareFieldResult result : results) { | 
 |  |  |             //改变父组织特殊处理 | 
 |  |  |             if(result.getFieldName().equals("parentId")){ | 
 |  |  |             if (result.getFieldName().equals("parentId")) { | 
 |  |  |                 //父组织id转换为名称 | 
 |  |  |                 String oldData = result.getOldData(); | 
 |  |  |                 String newData = result.getNewData(); | 
 |  |  | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             //过期时间格式转换 | 
 |  |  |             if(result.getFieldName().equals("expireTime")){ | 
 |  |  |             if (result.getFieldName().equals("expireTime")) { | 
 |  |  |                 //DateToString的时间格式改为yyyy-MM-dd | 
 |  |  |                 String oldData = result.getOldData(); | 
 |  |  |                 String newData = result.getNewData(); | 
 |  |  |  | 
 |  |  |                 if(oldData!=null){ | 
 |  |  |                 if (oldData != null) { | 
 |  |  |                     Date oldDate = DateUtils.dateStringToDate(oldData); | 
 |  |  |                     oldData = DateUtils.dateToDateString(oldDate, "yyyy-MM-dd"); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 if(newData!=null){ | 
 |  |  |                 if (newData != null) { | 
 |  |  |                     Date newDate = DateUtils.dateStringToDate(newData); | 
 |  |  |                     newData = DateUtils.dateToDateString(newDate, "yyyy-MM-dd"); | 
 |  |  |                 } | 
 |  |  | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         String content = CompareFieldUtils.resultsConvertContent(results, "修改了组织;组织名称:"+oldOrganization.getName()); | 
 |  |  |         logUtils.saveOperationForManage(content, Constants.UPDATE_OPERATE_TYPE); | 
 |  |  |         String content = CompareFieldUtils.resultsConvertContent(results, "修改了组织;组织名称:" + oldOrganization.getName()); | 
 |  |  |         LogUtils.saveOperationForManage(content, Constants.UPDATE_OPERATE_TYPE); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  | } |