package com.ruoyi.sim.service.impl;

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

import cn.ele6.catalyzer.ruoyi.vue.custom.Ele6RYBaseService;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.sim.domain.Major;
import com.ruoyi.sim.domain.vo.SelectSysDeptVo;
import com.ruoyi.sim.mapper.MajorMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sim.mapper.AddOnDeptMapper;
import com.ruoyi.sim.domain.AddOnDept;

/**
 * 部门附加Service业务层处理
 *
 * @author tom
 * @date 2024-12-13
 */
@Service
public class AddOnDeptService extends Ele6RYBaseService {
    @Autowired
    private AddOnDeptMapper addOnDeptMapper;

    /**
     * 查询部门附加
     *
     * @param deptId 部门附加主键
     * @return 部门附加
     */
    public AddOnDept selectAddOnDeptByDeptId(Long deptId) {
        return addOnDeptMapper.selectAddOnDeptByDeptId(deptId);
    }

    /**
     * 查询部门附加列表
     *
     * @param addOnDept 部门附加
     * @return 部门附加
     */
    public List<AddOnDept> selectAddOnDeptList(AddOnDept addOnDept) {
        return addOnDeptMapper.selectAddOnDeptList(addOnDept);
    }

    /**
     * 新增部门附加
     *
     * @param addOnDept 部门附加
     * @return 结果
     */
    public int insertAddOnDept(AddOnDept addOnDept) {
        return addOnDeptMapper.insertAddOnDept(addOnDept);
    }

    /**
     * 修改部门附加
     *
     * @param addOnDept 部门附加
     * @return 结果
     */
    public int updateAddOnDept(AddOnDept addOnDept) {
        return addOnDeptMapper.updateAddOnDept(addOnDept);
    }

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

    /**
     * 删除部门附加信息
     *
     * @param deptId 部门附加主键
     * @return 结果
     */
    public int deleteAddOnDeptByDeptId(Long deptId) {
        return addOnDeptMapper.deleteAddOnDeptByDeptId(deptId);
    }

    // -------------------------------- tom add  --------------------------------
    @Autowired
    private MajorMapper majorMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    public static final String ANCESTORS_SEPARATOR = ",";
    public static final int level_5 = 5;

    public Major selectMajorByDeptId(Long deptId) {
        AddOnDept aoD = addOnDeptMapper.selectAddOnDeptByDeptId(deptId);
        if (aoD == null) {
            return null;
        }
        return majorMapper.selectMajorByMajorId(aoD.getMajorId());
    }

    /**
     * 获取所有区队级别部门
     * @return
     */
    public TableDataInfo listLevel5() {
        List<SelectSysDeptVo> list = listByLevel(new SysDept(), level_5);
        return getDataTable(list);
    }

    public List<SelectSysDeptVo> listByLevel(SysDept q, final int level) {
        List<SysDept> list = sysDeptMapper.selectDeptList(q);
        List<SelectSysDeptVo> listVo = new ArrayList<>();
        list.forEach(sd -> {
            if (getLevel(sd) == level) {
                listVo.add(convertVo(sd));
            }
        });
        return listVo;
    }

    public int getLevel(SysDept o) {
        if (o == null) {
            return 0;
        }
        return StringUtils.split(o.getAncestors(), ANCESTORS_SEPARATOR).length;
    }

    public SelectSysDeptVo convertVo(SysDept o) {
        if (o == null) {
            return null;
        }
        SelectSysDeptVo t = new SelectSysDeptVo();
        BeanUtils.copyProperties(o, t);
        StringBuffer s = new StringBuffer();
        for (String idStr : StringUtils.split(o.getAncestors(), ANCESTORS_SEPARATOR)) {
            long id = Long.parseLong(idStr);
            SysDept sd = sysDeptMapper.selectDeptById(id);
            if (sd != null) {
                s.append(sd.getDeptName() + "-");
            }
        }
        s.append(o.getDeptName());
        t.setExpandName(s.toString());
        return t;
    }
}
