DepInfoServiceImpl.java 5.39 KB
package com.chinagas.modules.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinagas.api.system.domain.DepInfo;
import com.chinagas.api.system.domain.SysDept;
import com.chinagas.common.core.constants.UserConstants;
import com.chinagas.common.core.utils.StringUtils;
import com.chinagas.modules.system.mapper.DepInfoMapper;
import com.chinagas.modules.system.service.IDepInfoService;
import com.chinagas.modules.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 组织机构表 服务实现类
 * </p>
 *
 * @author lidwd
 * @since 2022-10-14
 */
@Service
public class DepInfoServiceImpl extends ServiceImpl<DepInfoMapper, DepInfo> implements IDepInfoService {

    @Autowired
    private DepInfoMapper depInfoMapper;

    @Autowired
    private ISysDeptService deptService;

//    @Transactional(rollbackFor = Exception.class)
//    @Override
//    public int insertDepInfo(DepInfo depInfo) {
//        QueryWrapper<DepInfo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("dept_id",depInfo.getDeptId());
//        DepInfo depInfo_old = depInfoMapper.selectOne(queryWrapper);
//        if(StringUtils.isNotNull(depInfo_old)){
//            queryWrapper.clear();
//            queryWrapper.eq("dept_id",depInfo_old.getDeptId());
//            depInfo.setLstupdtDate(new Date());
//            copyToSysDept(depInfo,BusinessType.UPDATE);
//            return depInfoMapper.update(depInfo,queryWrapper);
//        }
//        copyToSysDept(depInfo,BusinessType.INSERT);
//        return depInfoMapper.insertDepInfo(depInfo);
//    }

    @Override
//    @Transactional(rollbackFor = Exception.class)
    public int insertDepInfoBach(List<DepInfo> depList){
        int rows = 0;
        SysDept sysDept = null;
        List<SysDept> deptList = new ArrayList<>();
        for(DepInfo depInfo:depList){
            sysDept = new SysDept();
            sysDept.setDeptId(Long.parseLong(depInfo.getDeptId()));
            if(StringUtils.isNotNull(depInfo.getDepName())){
                sysDept.setDeptName(depInfo.getDepName());
            }else {
                sysDept.setDeptName("");
            }
            if(StringUtils.isNotEmpty(depInfo.getCgDeptParent())){
                sysDept.setParentId(Long.parseLong(depInfo.getCgDeptParent()));
            }
            sysDept.setAncestors(getAncestors(depInfo.getDeptId(), depList));
            sysDept.setDeptPath(depInfo.getCgDeptPath());

            sysDept.setCreateBy("定时任务同步");
            if(StringUtils.equals(depInfo.getDeptId(),"100000")){
                sysDept.setParentId(0L);
            }
            if("A".equals(depInfo.getEffStatus())){
                sysDept.setStatus(UserConstants.DEPT_NORMAL);
            }else{
                sysDept.setStatus(UserConstants.DEPT_DISABLE);
            }
            if(StringUtils.isNotEmpty(depInfo.getCgDeptSort())){
                sysDept.setOrderNum(Integer.parseInt(depInfo.getCgDeptSort()));
            }
            sysDept.setIsCompany(depInfo.getCgIsCompany());
            rows += deptService.insertOrUpdateDept(sysDept);
//            deptList.add(sysDept);
//            if (deptList.size()%100==0){
//                rows += deptService.insertOrUpdateDeptBatch(deptList);
//                deptList.clear();
//            }
        }
//        rows += deptService.insertOrUpdateDeptBatch(deptList);
        return rows;
    }

    /**
     * 根据机构ID,获取该部门所有上级机构
     * @param deptId
     * @param depList
     * @return
     */
    public String getAncestors(String deptId, List<DepInfo> depList){
        if (StringUtils.isEmpty(deptId)) {//异常数据
            return "";
        }
        if (StringUtils.equals(deptId,"100000")) {
            return "100000";
        }else {
            String ancestors = deptId;
            for(DepInfo depInfo:depList){
                if(StringUtils.equals(depInfo.getDeptId(), deptId)) {
                    ancestors = getAncestors(depInfo.getCgDeptParent(), depList) + "," + ancestors;
                }
            }
            return  ancestors;
        }
    }

//    private int copyToSysDept(DepInfo depInfo, BusinessType opreation){
//        int rows = 0;
//        SysDept sysDept = new SysDept();
//        sysDept.setDeptId(Long.parseLong(depInfo.getDeptId()));
//        if(StringUtils.isNotNull(depInfo.getDepName())){
//            sysDept.setDeptName(depInfo.getDepName());
//        }else {
//            sysDept.setDeptName("");
//        }
//        sysDept.setParentId(Long.parseLong(depInfo.getCgDeptParent()));
//        sysDept.setDeptPath(depInfo.getCgDeptPath());
//        sysDept.setCreateBy("定时任务同步");
//        if("A".equals(depInfo.getEffStatus())){
//            sysDept.setStatus(UserConstants.DEPT_NORMAL);
//        }else{
//            sysDept.setStatus(UserConstants.DEPT_DISABLE);
//        }
//        if(StringUtils.isNotEmpty(depInfo.getCgDeptSort())){
//            sysDept.setOrderNum(Integer.parseInt(depInfo.getCgDeptSort()));
//        }
//        if(BusinessType.INSERT.equals(opreation)){
//            rows += deptService.insertDept(sysDept);
//        }
//        if (BusinessType.UPDATE.equals(opreation)){
//            rows += deptService.updateDept(sysDept);
//        }
//        return rows;
//    }

}