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

import cn.ezhizao.project.business.domain.BizInspectionCategory;
import cn.ezhizao.project.business.domain.InspectionCategoryOptions;
import cn.ezhizao.project.business.mapper.BizInspectionCategoryMapper;
import cn.ezhizao.project.business.mapper.BizInspectionInstructionMapper;
import cn.ezhizao.project.business.service.IBizInspectionCategoryService;
import cn.ezhizao.project.business.service.IBizInspectionInstructionService;
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.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 检查指导书类别Service业务层处理
 *
 * @author zhuangdezheng
 * @date 2024-04-22
 */
@Service
public class BizInspectionCategoryServiceImpl extends ServiceImpl<BizInspectionCategoryMapper, BizInspectionCategory> implements IBizInspectionCategoryService {
    @Resource
    private BizInspectionCategoryMapper bizInspectionCategoryMapper;

    /**
     * 物理删除
     *
     * @return 删除结果
     */
    @Override
    public int physicalDelete(BizInspectionCategory bizInspectionCategory) {
        return bizInspectionCategoryMapper.physicalDelete(bizInspectionCategory);
    }


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

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

        return options;
    }

    /**
     * 递归获取子类别
     */
    @Override
    public List<BizInspectionCategory> recursionList(List<BizInspectionCategory> list, long parentId) {
        List<BizInspectionCategory> resultList = list.stream()
                .filter(item -> item.getParentId().equals(parentId))
                .sorted(Comparator.comparing(BizInspectionCategory::getSort))
                .collect(Collectors.toList());

        for (BizInspectionCategory item : resultList) {
            QueryWrapper<BizInspectionCategory> queryWrapper = new QueryWrapper<>();
            // 获取所有一级类别
            queryWrapper.eq("parent_id", item.getId());
            // 获取子类别
            List<BizInspectionCategory> children = recursionList(list, item.getId());
            if (!children.isEmpty()) {
                item.setChildren(children);
            }
        }

        return resultList;
    }
}
