jiangping
2024-08-13 6765787baa5e0b771d71f865c4deb776367410c4
server/dmvisit_service/src/main/java/com/doumee/service/business/impl/CompanyServiceImpl.java
@@ -2,6 +2,7 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.config.DataSyncConfig;
import com.doumee.core.annotation.excel.ExcelColumn;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
@@ -19,24 +20,25 @@
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.Utils;
import com.doumee.dao.admin.request.UpdateCompanySortDTO;
import com.doumee.dao.admin.response.CompanyDTO;
import com.doumee.dao.business.CompanyMapper;
import com.doumee.dao.business.join.CompanyJoinMapper;
import com.doumee.dao.business.model.Category;
import com.doumee.dao.business.model.Company;
import com.doumee.dao.business.model.Member;
import com.doumee.dao.business.model.*;
import com.doumee.dao.system.dto.UpdateSystemMenuSortDTO;
import com.doumee.dao.system.model.SystemMenu;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.service.business.CompanyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doumee.service.common.tree.CompanyTree;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -55,6 +57,8 @@
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private DataSyncConfig dataSyncConfig;
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
@@ -82,11 +86,10 @@
            if(parent == null || Constants.equalsInteger(parent.getIsdeleted(),Constants.ONE)){
                throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "对不起,父级组织信息不存在~");
            }
            if(StringUtils.isBlank(parent.getHkParentId())){
            if(StringUtils.isBlank(parent.getHkId())){
                throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "对不起,父级组织信息尚未同步下发成功~");
            }
            company.setHkParentId(parent.getHkParentId());
            company.setHkParentId(parent.getHkId());
        }
        company.setCreateDate(new Date());
        company.setCreator(user.getId());
@@ -199,6 +202,7 @@
        if(StringUtils.isBlank(model.getHkId())){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "对不起,组织信息尚未同步下发成功,暂不支持修改,请尝试删除后重新添加~");
        }
        Company company = new Company();
        company.setId(id);
        company.setEditDate(new Date());
@@ -440,24 +444,29 @@
    @Override
    public List<CompanyDTO> findCompanyTreePage(Integer type) {
        //配置组员人员类型来源方式 0自建 1ERP
        String origin = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.ORG_USER_ORIGIN).getCode();
        LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Company::getIsdeleted,Constants.ZERO)
                .eq(Constants.equalsInteger(type,Constants.ONE),Company::getType,type);
        List<Company> companies = companyMapper.selectList(wrapper);
        return getCompanyTree( companies, null,origin);
        return getCompanyTree( companies, null);
    }
    private List<CompanyDTO> getCompanyTree(List<Company> companies, String parentId,String origin){
    private List<CompanyDTO> getCompanyTree(List<Company> companies, String parentId){
        if (CollectionUtils.isEmpty(companies)){
            return null;
        }
        List<Company> collect = companies.stream()
                                        .filter(s -> (StringUtils.equals(origin, Constants.ONE+"")?StringUtils.equals(s.getErpParentId(), parentId):StringUtils.equals(s.getParentId()+"", parentId)))
                                        .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(companies)){
        int  origin =Constants.formatIntegerNum(dataSyncConfig.getOrgUserDataOrigin());
        List<Company> collect = null;
        if(origin == DataSyncConfig.origin.erp){
            collect=  companies.stream().filter(s -> ( StringUtils.defaultString(s.getErpParentId(),"").equals(StringUtils.defaultString(parentId)))).collect(Collectors.toList());
        }else if(origin == DataSyncConfig.origin.hk){
            collect= companies.stream().filter(s -> ( StringUtils.defaultString(s.getHkParentId(),"").equals(StringUtils.defaultString(parentId)))).collect(Collectors.toList());
        }else{
            collect=  companies.stream().filter(s -> (StringUtils.defaultString(s.getParentId()+"","").equals(StringUtils.defaultString(parentId)))).collect(Collectors.toList());
        }
        if (CollectionUtils.isEmpty(collect)){
            return null;
        }
        List<CompanyDTO> companyDTOList = new ArrayList<>();
@@ -465,7 +474,13 @@
            Company company = collect.get(i);
            CompanyDTO companyDTO = new CompanyDTO();
            BeanUtils.copyProperties(company,companyDTO);
            companyDTO.setCompanyDTOList(getCompanyTree(companies,StringUtils.equals(origin, Constants.ONE+"")?company.getErpId(): company.getId()+"",origin));
            String pId = company.getId()+"";
            if(origin == 1){
                pId = company.getErpId();
            }else if(origin == 2){
                pId = company.getHkParentId();
            }
            companyDTO.setCompanyDTOList(getCompanyTree(companies,pId));
            companyDTOList.add(companyDTO);
        }
        return companyDTOList;
@@ -495,4 +510,78 @@
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(),"公司名称已存在");
        }
    }
    @Override
    public List<Company> companyTree(Integer type) {
        MPJLambdaWrapper<Company> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(Company.class);
        queryWrapper.selectAs(Member::getName,Company::getHeadName);
        queryWrapper.selectAs(Member::getPhone,Company::getHeadPhone);
        queryWrapper.leftJoin(Member.class,Member::getId,Company::getHeadId);
        queryWrapper.eq(Company::getIsdeleted,Constants.ZERO);
        queryWrapper.eq(Company::getType,type);
        List<Company> companyList = companyJoinMapper.selectJoinList(Company.class,queryWrapper);
        //配置组员人员类型来源方式 0自建 1ERP
        CompanyTree treeBuild = new CompanyTree(companyList, dataSyncConfig.getOrgUserDataOrigin());
        companyList = treeBuild.buildTree();
        return companyList;
    }
    @Override
    public void updateSort(UpdateCompanySortDTO dto) {
        Company company = companyMapper.selectById(dto.getId());
        if(Objects.isNull(company)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        List<Company> companyPool;
        if (company.getParentId() == null) {
            companyPool = companyMapper.selectList(new QueryWrapper<Company>().lambda()
                    .eq(Company::getIsdeleted,Constants.ZERO)
                    .isNull(Company::getParentId)
                    .orderByAsc(Company::getSortnum)
            );
        } else {
            companyPool = companyMapper.selectList(new QueryWrapper<Company>().lambda()
                    .eq(Company::getIsdeleted,Constants.ZERO)
                    .eq(Company::getParentId,company.getParentId())
                    .orderByAsc(Company::getSortnum)
            );
        }
        int currentMenuIndex = 0;
        for (int i = 0; i < companyPool.size(); i++) {
            if (companyPool.get(i).getId().equals(dto.getId())) {
                currentMenuIndex = i;
                break;
            }
        }
        // 上移
        if ("top".equals(dto.getDirection())) {
            if (currentMenuIndex - 1 < 0) {
                return;
            }
            Company preMenu = companyPool.remove(currentMenuIndex - 1);
            companyPool.add(currentMenuIndex, preMenu);
        }
        // 下移
        else {
            if (currentMenuIndex + 1 > companyPool.size() - 1) {
                return;
            }
            Company nextMenu = companyPool.remove(currentMenuIndex + 1);
            companyPool.add(currentMenuIndex, nextMenu);
        }
        for (int i = 0; i < companyPool.size(); i++) {
            companyPool.get(i).setSortnum(i);
        }
        for (Company updCompany:companyPool) {
            companyMapper.updateById(updCompany);
        }
    }
}