package com.ruoyi.ems.service.impl;

import com.ruoyi.ems.domain.FacsCategory;
import com.ruoyi.ems.domain.FacsSubCategory;
import com.ruoyi.ems.mapper.FacsCategoryMapper;
import com.ruoyi.ems.service.IFacsCategoryService;
import com.ruoyi.ems.service.IFacsSubCategoryService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 能源设施/系统分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-10
 */
@Service
public class FacsCategoryServiceImpl implements IFacsCategoryService {
    @Autowired
    private FacsCategoryMapper categoryMapper;

    @Autowired
    private IFacsSubCategoryService subCategoryService;

    /**
     * 查询能源设施/系统分类
     *
     * @param id 能源设施/系统分类主键
     * @return 能源设施/系统分类
     */
    @Override
    public FacsCategory selectCategoryById(Long id) {
        // 查询主分类
        FacsCategory category = categoryMapper.selectCategoryById(id);

        // 查询子分类
        if (null != category) {
            category.setSubtypeList(getFacsSubtypes(category));
        }

        return category;
    }

    @Override
    public FacsCategory selectCategoryByCode(String code) {
        // 查询主分类
        FacsCategory category = categoryMapper.selectCategoryByCode(code);

        // 查询子分类
        if (null != category) {
            category.setSubtypeList(getFacsSubtypes(category));
        }

        return category;
    }

    @Override
    public List<FacsCategory> selectCategoryList() {
        // 查询主分类
        List<FacsCategory> list = categoryMapper.selectCategoryAll();

        // 查询子分类
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(category -> {
                category.setSubtypeList(getFacsSubtypes(category));
            });
        }

        return list;
    }

    /**
     * 查询能源设施/系统分类列表
     *
     * @param param 能源设施/系统分类
     * @return 能源设施/系统分类
     */
    @Override
    public List<FacsCategory> selectCategoryList(FacsCategory param) {
        // 查询主分类
        List<FacsCategory> list = categoryMapper.selectCategoryList(param);

        // 查询子分类
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(category -> {
                category.setSubtypeList(getFacsSubtypes(category));
            });
        }

        return list;
    }

    /**
     * 新增能源设施/系统分类
     *
     * @param category 能源设施/系统分类
     * @return 结果
     */
    @Override
    public int insertCategory(FacsCategory category) {
        int count = categoryMapper.insertCategory(category);

        if (CollectionUtils.isNotEmpty(category.getSubtypeList())) {
            subCategoryService.deleteSubCategoryByParentCode(category.getCode());
            subCategoryService.insertSubCategoryBatch(category.getSubtypeList());
        }
        else {
            subCategoryService.deleteSubCategoryByParentCode(category.getCode());
        }

        return count;
    }

    /**
     * 修改能源设施/系统分类
     *
     * @param category 能源设施/系统分类
     * @return 结果
     */
    @Override
    public int updateCategory(FacsCategory category) {
        int count = categoryMapper.updateCategory(category);

        if (CollectionUtils.isNotEmpty(category.getSubtypeList())) {
            subCategoryService.deleteSubCategoryByParentCode(category.getCode());
            subCategoryService.insertSubCategoryBatch(category.getSubtypeList());
        }
        else {
            subCategoryService.deleteSubCategoryByParentCode(category.getCode());
        }

        return count;
    }

    /**
     * 批量删除能源设施/系统分类
     *
     * @param ids 需要删除的能源设施/系统分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByIds(Long[] ids) {
        for (Long id : ids) {
            FacsCategory category = categoryMapper.selectCategoryById(id);

            // 查询扩展属性
            if (null != category) {
                subCategoryService.deleteSubCategoryByParentCode(category.getCode());
            }
        }

        return categoryMapper.deleteCategoryByIds(ids);
    }

    /**
     * 删除能源设施/系统分类信息
     *
     * @param id 能源设施/系统分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryById(Long id) {
        FacsCategory category = categoryMapper.selectCategoryById(id);

        // 查询扩展属性
        if (null != category) {
            subCategoryService.deleteSubCategoryByParentCode(category.getCode());
        }

        return categoryMapper.deleteCategoryById(id);
    }

    private List<FacsSubCategory> getFacsSubtypes(FacsCategory category) {
        FacsSubCategory param = new FacsSubCategory();
        param.setParentCode(category.getCode());

        return subCategoryService.selectSubCategoryList(param);
    }
}
