|  |  | 
 |  |  | 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.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.CompareFieldUtils; | 
 |  |  | import com.moral.api.util.LogUtils; | 
 |  |  | import com.moral.constant.Constants; | 
 |  |  | import com.moral.constant.ResponseCodeEnum; | 
 |  |  | 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.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; | 
 |  |  |  | 
 |  |  | 
 |  |  |  * @since 2021-04-06 | 
 |  |  |  */ | 
 |  |  | @Service | 
 |  |  | @ConfigurationProperties(prefix = "log-aspect") | 
 |  |  | public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements OrganizationService { | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  | 
 |  |  |     @Autowired | 
 |  |  |     UserMapper userMapper; | 
 |  |  |  | 
 |  |  |     Map<String, String> organizationFormMap; | 
 |  |  |  | 
 |  |  |     public void setOrganizationFormMap(Map<String, String> organizationFormMap) { | 
 |  |  |         this.organizationFormMap = organizationFormMap; | 
 |  |  |     } | 
 |  |  |     @Autowired | 
 |  |  |     MonitorPointService monitorPointService; | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * @Description: 添加客户组织 | 
 |  |  | 
 |  |  |      * @Author: 陈凯裕 | 
 |  |  |      * @Date: 2021/3/22 | 
 |  |  |      */ | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     @Transactional | 
 |  |  |     public OrganizationDTO insertOrganization(OrganizationInsertForm organizationInsertForm) { | 
 |  |  | 
 |  |  |         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(), Constants.INSERT_OPERATE_TYPE); | 
 |  |  |         insertLog(organization); | 
 |  |  |         return organizationDTO; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | 
 |  |  |         organizationDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
 |  |  |  | 
 |  |  |         //操作插入日志 | 
 |  |  |         insertUpdateLog(organizationUpdateForm, organization, oldOrganization); | 
 |  |  |         updateLog(oldOrganization,organization); | 
 |  |  |  | 
 |  |  |         return 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); | 
 |  |  | 
 |  |  |             return dto; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         //逻辑删除组织 | 
 |  |  |         UpdateWrapper deleteWrapper = new UpdateWrapper(); | 
 |  |  |         deleteWrapper.eq("id", id); | 
 |  |  |         deleteWrapper.set("is_delete", Constants.DELETE); | 
 |  |  |         organizationMapper.update(null, deleteWrapper); | 
 |  |  |  | 
 |  |  |         //删除组织账号 | 
 |  |  |         userService.deleteUserByOrganizationId(id); | 
 |  |  |  | 
 |  |  |         //判断是否含有子组织 | 
 |  |  |         List<Organization> children = getAllChildrenOrganization(existOrganization.getId()); | 
 |  |  |         if (!ObjectUtils.isEmpty(children)) {//如果含有子组织 | 
 |  |  |         if (!ObjectUtils.isEmpty(children)) { | 
 |  |  |             //判断是否删除所有子组织,如果不删除子组织则所有子组织全部变为无父组织,孙子组织不变 | 
 |  |  |             UpdateWrapper updateWrapper = new UpdateWrapper(); | 
 |  |  |             if (form.getDeleteChildren().equals(Constants.DELETE_CHILDREN_ORG)) {//删除所有子组织 | 
 |  |  | 
 |  |  |                 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.deleteUserByOrganizationId(value)); | 
 |  |  |                 childrenId.forEach(value->userService.deleteUsersByOrganizationId(value)); | 
 |  |  |  | 
 |  |  |             } else {//不删除 | 
 |  |  |                 //提取所有直属子组织id | 
 |  |  | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         //逻辑删除组织 | 
 |  |  |         UpdateWrapper deleteWrapper = new UpdateWrapper(); | 
 |  |  |         deleteWrapper.eq("id", id); | 
 |  |  |         deleteWrapper.set("is_delete", Constants.DELETE); | 
 |  |  |         organizationMapper.update(null, deleteWrapper); | 
 |  |  |  | 
 |  |  |         //删除组织账号 | 
 |  |  |         userService.deleteUsersByOrganizationId(id); | 
 |  |  |  | 
 |  |  |         //封装返回结果 | 
 |  |  |         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("以及所有子组织;"); | 
 |  |  |         else | 
 |  |  |             content.append(";"); | 
 |  |  |         logUtils.saveOperationForManage(request, content.toString(), Constants.DELETE_OPERATE_TYPE); | 
 |  |  |         logUtils.saveOperationForManage(content.toString(), Constants.DELETE_OPERATE_TYPE); | 
 |  |  |  | 
 |  |  |         return dto; | 
 |  |  |     } | 
 |  |  | 
 |  |  |         queryWrapper.eq("is_delete", Constants.NOT_DELETE); | 
 |  |  |         //查询所有组织 | 
 |  |  |         List<Organization> organizations = organizationMapper.selectList(queryWrapper); | 
 |  |  |         //判断form是否含有id,如果有则对所有组织进行过滤 | 
 |  |  |         //判断form是否含有id,如果有则对其所有子组织进行过滤 | 
 |  |  |         if(!ObjectUtils.isEmpty(id)){ | 
 |  |  |             List<Organization> children = getAllChildrenOrganization(id); | 
 |  |  |             List<Integer> thisAndchildrenIds = new ArrayList<>();//该id以及其所有子组织的id集合 | 
 |  |  | 
 |  |  |         return dto; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * @Description: 将更新操作插入日志 | 
 |  |  |      * @Param: [updateForm, newOrganization, oldOrganization] | 
 |  |  |      * @return: void | 
 |  |  |      * @Author: 陈凯裕 | 
 |  |  |      * @Date: 2021/4/8 | 
 |  |  |      */ | 
 |  |  |     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: 根据父组织获取所有子组织 | 
 |  |  | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |     * @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); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     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")){ | 
 |  |  |                 //父组织id转换为名称 | 
 |  |  |                 String oldData = result.getOldData(); | 
 |  |  |                 String newData = result.getNewData(); | 
 |  |  |  | 
 |  |  |                 if (!oldData.equals("0")) | 
 |  |  |                     oldData = organizationMapper.selectById(Integer.parseInt(oldData)).getName(); | 
 |  |  |                 else | 
 |  |  |                     oldData = "null"; | 
 |  |  |  | 
 |  |  |                 if (!newData.equals("0")) | 
 |  |  |                     newData = organizationMapper.selectById(Integer.parseInt(newData)).getName(); | 
 |  |  |                 else | 
 |  |  |                     newData = "null"; | 
 |  |  |  | 
 |  |  |                 result.setNewData(newData); | 
 |  |  |                 result.setOldData(oldData); | 
 |  |  |                 result.setFieldAnnoName("父组织"); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             //过期时间格式转换 | 
 |  |  |             if(result.getFieldName().equals("expireTime")){ | 
 |  |  |                 //DateToString的时间格式改为yyyy-MM-dd | 
 |  |  |                 String oldData = result.getOldData(); | 
 |  |  |                 String newData = result.getNewData(); | 
 |  |  |  | 
 |  |  |                 if(oldData!=null){ | 
 |  |  |                     Date oldDate = DateUtils.dateStringToDate(oldData); | 
 |  |  |                     oldData = DateUtils.dateToDateString(oldDate, "yyyy-MM-dd"); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 if(newData!=null){ | 
 |  |  |                     Date newDate = DateUtils.dateStringToDate(newData); | 
 |  |  |                     newData = DateUtils.dateToDateString(newDate, "yyyy-MM-dd"); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 result.setNewData(newData); | 
 |  |  |                 result.setOldData(oldData); | 
 |  |  |                 result.setFieldAnnoName("过期时间"); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         String content = CompareFieldUtils.resultsConvertContent(results, "修改了组织;组织名称:"+oldOrganization.getName()); | 
 |  |  |         logUtils.saveOperationForManage(content, Constants.UPDATE_OPERATE_TYPE); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  | } |