package com.xhpc.activity.service.impl;

import com.xhpc.activity.domain.XhpcWorkDeptDomain;
import com.xhpc.activity.domain.XhpcWorkUserDomain;
import com.xhpc.activity.mapper.XhpcWorkDeptMapper;
import com.xhpc.activity.service.WorkDeptService;
import com.xhpc.activity.service.WorkUserService;
import com.xhpc.common.core.exception.CustomException;
import com.xhpc.common.core.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class WorkDeptServiceImpl implements WorkDeptService {

    @Resource
    XhpcWorkDeptMapper deptMapper;

    @Resource
    WorkUserService workUserService;

    @Override
    public List<XhpcWorkDeptDomain> getPage(Map<String, Object> params){
        return deptMapper.selectListByParams(params);
    }

    @Override
    public List<Map<String, Object>> getList(Map<String, Object> params){
        List<Map<String, Object>> deptList = deptMapper.selectMapListByParams(params);
        for (Map<String, Object> dept: deptList){
            params.put("parentDeptId", dept.get("id").toString().replaceAll("d_", ""));
            List<Map<String,Object>> childDeptList = deptMapper.selectMapListByParams(params);
            dept.put("children", childDeptList);
        }
        return deptList;
    }

    @Override
    public List<Map<String, Object>> getTree(Map<String, Object> params){
        List<Map<String, Object>> deptList = deptMapper.selectMapListByParams(params);
        for (Map<String, Object> dept: deptList){
            params.put("parentDeptId", dept.get("id").toString().replaceAll("d_", ""));
            List<Map<String,Object>> childDeptList = deptMapper.selectMapListByParams(params);
            if(childDeptList.size() > 0){
                dept.put("children", childDeptList);
            }
            for (Map<String, Object> childMap: childDeptList){
                params.put("parentDeptId", childMap.get("id").toString().replaceAll("d_", ""));
                List<Map<String,Object>> childDeptList1 = deptMapper.selectMapListByParams(params);
                if(childDeptList1.size() > 0){
                    childMap.put("children", childDeptList1);
                }

            }
        }
        return deptList;
    }

    @Override
    public XhpcWorkDeptDomain getDomainByPk(Long deptId){
        return deptMapper.selectByPrimaryKey(deptId);
    }

    @Override
    public Boolean insertDomain(XhpcWorkDeptDomain domain){

        XhpcWorkDeptDomain deptDomain = deptMapper.selectDomainByNameAndTenant(domain.getDeptName(), domain.getParentDeptId(), domain.getTenantId());
        if(StringUtils.isNotNull(deptDomain)){
            throw new CustomException("部门已存在");
        }

        if(domain.getParentDeptId() != null){
            XhpcWorkDeptDomain parentDeptDomain = deptMapper.selectByPrimaryKey(domain.getParentDeptId());
            if(parentDeptDomain != null && !parentDeptDomain.getType().equals(domain.getType())){
                throw new CustomException("部门类型与上级部门类型不一致");
            }
        }

        return deptMapper.insert(domain) > 0;
    }

    @Override
    public Boolean updateDomain(XhpcWorkDeptDomain domain){
        XhpcWorkDeptDomain deptDomain = deptMapper.selectByPrimaryKey(domain.getWorkDeptId());
        if(!deptDomain.getType().equals(domain.getType())){
            List<XhpcWorkUserDomain> userDomainList = workUserService.getUserListByDeptId(deptDomain.getWorkDeptId());
            if(userDomainList.size()> 0){
                throw new CustomException("部门已存在人员");
            }
        }
        return deptMapper.updateByPrimaryKey(domain) > 0;
    }

    @Override
    public Boolean updateDomainStatus(XhpcWorkDeptDomain domain){
        return deptMapper.updateStatusByPrimaryKey(domain) > 0;
    }

    @Override
    public Boolean deleteDomain(Long deptId){
        List<XhpcWorkDeptDomain> domainList = deptMapper.selectListByParentDeptId(deptId);
        if (domainList.size() > 0){
            throw new CustomException("存在下级部门");
        }

        return deptMapper.deleteLogicByPrimaryKey(deptId) > 0;
    }
}
