| | |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.moral.api.config.mybatis.wrapper.NullFilterWrapper; |
| | | import com.moral.api.entity.Organization; |
| | | import com.moral.api.entity.User; |
| | | 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.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; |
| | |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.*; |
| | | import java.util.function.Predicate; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @Autowired |
| | | LogUtils logUtils; |
| | | |
| | | @Autowired |
| | | UserService userService; |
| | | |
| | | @Autowired |
| | | UserMapper userMapper; |
| | | |
| | | Map<String, String> organizationFormMap; |
| | | |
| | | public void setOrganizationFormMap(Map<String, String> organizationFormMap) { |
| | |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/22 |
| | | */ |
| | | |
| | | |
| | | @Override |
| | | @Transactional |
| | | public OrganizationDTO insertOrganization(OrganizationInsertForm organizationInsertForm) { |
| | |
| | | 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; |
| | | } |
| | | |
| | |
| | | //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); |
| | |
| | | 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: 删除客户组织 |
| | |
| | | OrganizationDTO dto = new OrganizationDTO(); |
| | | //取参 |
| | | Integer id = form.getOrganizationId(); |
| | | //查询组织是否存在 |
| | | |
| | | //查询要删除的组织用于插入日志 |
| | | Organization existOrganization = new Organization(); |
| | | existOrganization.setIsDelete(Constants.NOT_DELETE); |
| | | existOrganization.setId(id); |
| | |
| | | dto.setMsg(ResponseCodeEnum.ORGANIZATION_NOT_EXIST.getMsg()); |
| | | return dto; |
| | | } |
| | | |
| | | //逻辑删除组织 |
| | | 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); |
| | | |
| | | //判断是否含有子组织 |
| | | 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()); |
| | | }); |
| | | //进行删除 |
| | | 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); |
| | | } |
| | | } |
| | | |
| | | //封装返回结果 |
| | | 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; |
| | | } |
| | |
| | | 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); |
| | | } |
| | | |
| | |
| | | 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; |
| | | } |
| | | } |
| | | |
| | | |
| | | } |