package com.ruoyi.hezhi.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hezhi.domain.TbCareerClass;
import com.ruoyi.hezhi.mapper.TbCareerClassMapper;
import com.ruoyi.hezhi.service.ITbCareerClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 职业分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-10
 */
@Service
public class TbCareerClassServiceImpl implements ITbCareerClassService
{
    @Autowired
    private TbCareerClassMapper tbCareerClassMapper;

    /**
     * 查询职业分类
     *
     * @param careerId 职业分类主键
     * @return 职业分类
     */
    @Override
    public TbCareerClass selectTbCareerClassByCareerId(Long careerId)
    {
        return tbCareerClassMapper.selectTbCareerClassByCareerId(careerId);
    }

    /**
     * 查询职业分类列表
     *
     * @param tbCareerClass 职业分类
     * @return 职业分类
     */
    @Override
    public List<TbCareerClass> selectTbCareerClassList(TbCareerClass tbCareerClass)
    {
        return tbCareerClassMapper.selectTbCareerClassList(tbCareerClass);
    }

    /**
     * 新增职业分类
     *
     * @param tbCareerClass 职业分类
     * @return 结果
     */
    @Override
    public int insertTbCareerClass(TbCareerClass tbCareerClass)
    {
        tbCareerClass.setCreateTime(DateUtils.getNowDate());
        return tbCareerClassMapper.insertTbCareerClass(tbCareerClass);
    }

    /**
     * 修改职业分类
     *
     * @param tbCareerClass 职业分类
     * @return 结果
     */
    @Override
    public int updateTbCareerClass(TbCareerClass tbCareerClass)
    {
        tbCareerClass.setUpdateTime(DateUtils.getNowDate());
        return tbCareerClassMapper.updateTbCareerClass(tbCareerClass);
    }

    /**
     * 批量删除职业分类
     *
     * @param careerIds 需要删除的职业分类主键
     * @return 结果
     */
    @Override
    public int deleteTbCareerClassByCareerIds(Long[] careerIds)
    {
        return tbCareerClassMapper.deleteTbCareerClassByCareerIds(careerIds);
    }

    /**
     * 删除职业分类信息
     *
     * @param careerId 职业分类主键
     * @return 结果
     */
    @Override
    public int deleteTbCareerClassByCareerId(Long careerId)
    {
        return tbCareerClassMapper.deleteTbCareerClassByCareerId(careerId);
    }

    /**
     * 查询职业分类列表
     *
     * @return 职业分类集合
     */
    @Override
    public List<TbCareerClass> selectTbCareerClassTreeList() {
        TbCareerClass tbCareerClass = new TbCareerClass();
        tbCareerClass.setStatus(0);
        tbCareerClass.setDelFlag(0);
        List<TbCareerClass> list = tbCareerClassMapper.selectTbCareerClassList(tbCareerClass);
        return this.toTree(list, 0L);
    }

    /**
     * 方式一：使用两层循环实现
     * @param treeList
     * @return
     */
    @Override
    public List<TbCareerClass> toTree(List<TbCareerClass> treeList, Long pid) {
        List<TbCareerClass> retList = new ArrayList<TbCareerClass>();
        for (TbCareerClass parent : treeList) {
            if (pid.equals(parent.getPid())) {
                retList.add(parent);
            }
            for (TbCareerClass childOne : treeList) {
                if (childOne.getPid().equals(parent.getCareerId())) {
                    if (parent.getChildrenList() == null) {
                        parent.setChildrenList(new ArrayList<TbCareerClass>());
                    }
                    parent.getChildrenList().add(childOne);
                }
            }
        }
        return retList;
    }

    /**
     * 职业分类列表
     *
     * @param params 参数
     * @return 职业分类集合列表
     */
    @Override
    public List<Map<String, Object>> selectTbCareerClassMapList(Map<String, Object> params) {
        return tbCareerClassMapper.selectTbCareerClassMapList(params);
    }
}
