package com.fs.hospital.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.company.domain.CompanyDept;
import com.fs.company.domain.CompanyDeptTreeSelect;
import com.fs.company.domain.CompanyRole;
import com.fs.company.mapper.CompanyRoleMapper;
import com.fs.hospital.domain.HospitalDeptTreeSelect;
import com.fs.hospital.domain.HospitalRole;
import com.fs.hospital.mapper.HospitalRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.hospital.mapper.HospitalDeptMapper;
import com.fs.hospital.domain.HospitalDept;
import com.fs.hospital.service.IHospitalDeptService;

/**
 * 部门Service业务层处理
 *
 * @author fs
 * @date 2024-02-26
 */
@Service
public class HospitalDeptServiceImpl implements IHospitalDeptService
{
    @Autowired
    private HospitalDeptMapper hospitalDeptMapper;
    @Autowired
    private HospitalRoleMapper roleMapper;
    /**
     * 查询部门
     *
     * @param deptId 部门主键
     * @return 部门
     */
    @Override
    public HospitalDept selectHospitalDeptByDeptId(Long deptId)
    {
        return hospitalDeptMapper.selectHospitalDeptByDeptId(deptId);
    }

    /**
     * 查询部门列表
     *
     * @param hospitalDept 部门
     * @return 部门
     */
    @Override
    public List<HospitalDept> selectHospitalDeptList(HospitalDept hospitalDept)
    {
        return hospitalDeptMapper.selectHospitalDeptList(hospitalDept);
    }

    /**
     * 新增部门
     *
     * @param hospitalDept 部门
     * @return 结果
     */
    @Override
    public int insertHospitalDept(HospitalDept hospitalDept)
    {
        hospitalDept.setCreateTime(DateUtils.getNowDate());
        return hospitalDeptMapper.insertHospitalDept(hospitalDept);
    }

    /**
     * 修改部门
     *
     * @param hospitalDept 部门
     * @return 结果
     */
    @Override
    public int updateHospitalDept(HospitalDept hospitalDept)
    {
        hospitalDept.setUpdateTime(DateUtils.getNowDate());
        return hospitalDeptMapper.updateHospitalDept(hospitalDept);
    }

    /**
     * 批量删除部门
     *
     * @param deptIds 需要删除的部门主键
     * @return 结果
     */
    @Override
    public int deleteHospitalDeptByDeptIds(Long[] deptIds)
    {
        return hospitalDeptMapper.deleteHospitalDeptByDeptIds(deptIds);
    }

    /**
     * 删除部门信息
     *
     * @param deptId 部门主键
     * @return 结果
     */
    @Override
    public int deleteHospitalDeptByDeptId(Long deptId)
    {
        return hospitalDeptMapper.deleteHospitalDeptByDeptId(deptId);
    }

    @Override
    public List<HospitalDeptTreeSelect> buildDeptTreeSelect(List<HospitalDept> depts) {
        List<HospitalDept> deptTrees = buildDeptTree(depts);
        return deptTrees.stream().map(HospitalDeptTreeSelect::new).collect(Collectors.toList());
    }
    @Override
    public List<HospitalDept> buildDeptTree(List<HospitalDept> depts) {
        List<HospitalDept> returnList = new ArrayList<HospitalDept>();
        List<Long> tempList = new ArrayList<Long>();
        for (HospitalDept dept : depts)
        {
            tempList.add(dept.getDeptId());
        }
        for (Iterator<HospitalDept> iterator = depts.iterator(); iterator.hasNext();)
        {
            HospitalDept dept = (HospitalDept) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId()))
            {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = depts;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<HospitalDept> list, HospitalDept t)
    {
        // 得到子节点列表
        List<HospitalDept> childList = getChildList(list, t);
        t.setChildren(childList);
        for (HospitalDept tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<HospitalDept> getChildList(List<HospitalDept> list, HospitalDept t)
    {
        List<HospitalDept> tlist = new ArrayList<HospitalDept>();
        Iterator<HospitalDept> it = list.iterator();
        while (it.hasNext())
        {
            HospitalDept n = (HospitalDept) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<HospitalDept> list, HospitalDept t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }


    public List<Integer> selectDeptListByRoleId(Long roleId) {
        return hospitalDeptMapper.selectDeptListByRoleId(roleId, true);
    }

    @Override
    public int checkDeptNameUnique(HospitalDept dept) {
        return hospitalDeptMapper.checkDeptNameUnique(dept.getHospitalId(),dept.getDeptName());
    }

    @Override
    public int selectNormalChildrenDeptById(Long deptId) {
        return hospitalDeptMapper.selectNormalChildrenDeptById(deptId);
    }

    @Override
    public int checkDeptExistUser(Long deptId) {
        int result = hospitalDeptMapper.checkDeptExistUser(deptId);
        return result;
    }

    @Override
    public int hasChildByDeptId(Long deptId) {
        int result = hospitalDeptMapper.hasChildByDeptId(deptId);
        return result;
    }
}
