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

import cn.ezhizao.project.business.carrier.domain.BizCarrierCategory;
import cn.ezhizao.project.business.carrier.domain.CarrierCategoryOptions;
import cn.ezhizao.project.business.carrier.mapper.BizCarrierCategoryMapper;
import cn.ezhizao.project.business.carrier.service.IBizCarrierCategoryService;
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 BizCarrierCategoryServiceImpl extends ServiceImpl<BizCarrierCategoryMapper, BizCarrierCategory> implements IBizCarrierCategoryService
{
    @Resource
    private BizCarrierCategoryMapper bizCarrierCategoryMapper;

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

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

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

        return options;
    }


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