| | |
| | | import com.moral.api.mapper.OrganizationMapper; |
| | | 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.OrganizationQueryNameDTO; |
| | | 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.util.LogUtils; |
| | |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.*; |
| | | import java.util.function.Predicate; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/22 |
| | | */ |
| | | |
| | | |
| | | @Override |
| | | @Transactional |
| | | public OrganizationDTO insertOrganization(OrganizationInsertForm organizationInsertForm) { |
| | |
| | | OrganizationDTO dto = new OrganizationDTO(); |
| | | //取参 |
| | | Integer id = form.getOrganizationId(); |
| | | |
| | | //查询组织是否存在 |
| | | Organization existOrganization = new Organization(); |
| | | existOrganization.setIsDelete(Constants.NOT_DELETE); |
| | |
| | | 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); |
| | | |
| | | //判断是否含有子组织 |
| | | List<Organization> children = getAllChildrenOrganization(existOrganization); |
| | | 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); |
| | | } 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); |
| | | } |
| | | } |
| | | |
| | | //封装返回结果 |
| | |
| | | //操作插入日志 |
| | | HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append("删除了组织:").append(existOrganization.getName()+";"); |
| | | 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); |
| | | |
| | | return dto; |
| | |
| | | for (Organization child : organizations) { |
| | | OrganizationDTO resultDto = new OrganizationDTO(); |
| | | Organization parent = organizationMapper.selectById(child.getParentId());//查找父组织 |
| | | //拼接地址字符串 |
| | | changeAddressByOrganization(child); |
| | | resultDto.setOrganization(child); |
| | | resultDto.setParentOrganization(parent); |
| | | organizationDTOS.add(resultDto); |
| | |
| | | return dto; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @Description: 将organization的address字段与provinceName cityName areaName进行拼接 |
| | | * @Param: [organization] |
| | | * @return: void |
| | | * @Description: 查询所有组织的名称和Id,不做分页 |
| | | * @Param: [organizationQueryNameForm] |
| | | * @return: com.moral.api.pojo.dto.organization.OrganizationQueryNameDTO |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/4/2 |
| | | * @Date: 2021/4/13 |
| | | */ |
| | | private void changeAddressByOrganization(Organization organization) { |
| | | String provinceName = organization.getProvinceName(); |
| | | String cityName = organization.getCityName(); |
| | | String areaName = organization.getAreaName(); |
| | | String address = organization.getAddress(); |
| | | @Override |
| | | public OrganizationQueryNameDTO queryOrganizationNames(OrganizationQueryNameForm form) { |
| | | OrganizationQueryNameDTO dto = new OrganizationQueryNameDTO(); |
| | | //取参 |
| | | String name = form.getName(); |
| | | //构造查询条件 |
| | | NullFilterWrapper<Organization> queryWrapper = new NullFilterWrapper(); |
| | | queryWrapper.like("name", name); |
| | | queryWrapper.eq("is_delete", Constants.NOT_DELETE); |
| | | //查询 |
| | | List<Organization> organizations = organizationMapper.selectList(queryWrapper); |
| | | |
| | | 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()); |
| | | //封装返回对象 |
| | | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); |
| | | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); |
| | | dto.setOrganizations(organizations); |
| | | return dto; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @Description: 将更新操作插入日志 |
| | |
| | | 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(Organization parent) { |
| | | List<Organization> children = new ArrayList<>(); |
| | | recursionQueryChildren(parent, children); |
| | | return children; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 通过父组织查询下面所有的子组织放到children中 |
| | | * @Param: [parent, children] |
| | | * @return: void |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/4/14 |
| | | */ |
| | | private void recursionQueryChildren(Organization parent, List<Organization> children) { |
| | | Integer parentId = parent.getId(); |
| | | 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, children); |
| | | } |
| | | } else { |
| | | return; |
| | | } |
| | | } |
| | | |
| | | |
| | | } |