package cn.ezhizao.project.business.equipment.service.impl;

import cn.ezhizao.project.business.equipment.domain.BizEquipmentCategory;
import cn.ezhizao.project.business.equipment.domain.CategoryOptions;
import cn.ezhizao.project.business.equipment.mapper.BizEquipmentCategoryMapper;
import cn.ezhizao.project.business.equipment.service.IBizEquipmentCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

/**
 * 设备档案Service业务层处理
 *
 * @author ezhizao
 * @date 2023-11-14
 */
@Service
public class BizEquipmentCategoryServiceImpl extends ServiceImpl<BizEquipmentCategoryMapper, BizEquipmentCategory> implements IBizEquipmentCategoryService
{
    @Resource
    private BizEquipmentCategoryMapper bizEquipmentCategoryMapper;

    /**
     * 物理删除
     * @param id 待删除的id
     * @return 删除结果
     */
    @Override
    public int physicalDelete(Long id){
        return bizEquipmentCategoryMapper.physicalDelete(id);
    }

    /**
     * 返回类别的选项
     * @param list 类别列表
     * @return 选项集合
     */
    @Override
    public List<CategoryOptions> getCategoryOptions(List<BizEquipmentCategory> list) {
        List<CategoryOptions> options = new ArrayList<>();
        // 循环List
        for (BizEquipmentCategory item : list) {
            CategoryOptions option = new CategoryOptions();
            option.setLabel(item.getName());
            option.setValue(item.getId().toString());

            QueryWrapper<BizEquipmentCategory> queryWrapper = new QueryWrapper<>();
            // 获取所有一级类别
            queryWrapper.eq("deleted", 0)
                    .eq("parent_id", item.getId());
            // 获取子类别
            List<BizEquipmentCategory> childrenCategories = bizEquipmentCategoryMapper.selectList(queryWrapper);
            if (childrenCategories.size() > 0) {
                option.setChildren(getCategoryOptions(childrenCategories));
            }
            options.add(option);
        }

        return options;
    }


    /**
     * 递归获取子类别
     */
    @Override
    public void getChildren(List<BizEquipmentCategory> list) {
        // 循环List
        for (BizEquipmentCategory item : list) {
            QueryWrapper<BizEquipmentCategory> queryWrapper = new QueryWrapper<>();
            // 获取所有一级类别
            queryWrapper.eq("deleted", 0)
                    .eq("parent_id", item.getId());
            // 获取子类别
            List<BizEquipmentCategory> children = bizEquipmentCategoryMapper.selectList(queryWrapper);
            if (children.size() > 0) {
                item.setChildren(children);
                getChildren(children);
            }
        }
    }
}
