package com.ruoyi.ems.service.impl;

import com.ruoyi.ems.domain.EmsFacsSubtype;
import com.ruoyi.ems.domain.EmsFacsType;
import com.ruoyi.ems.mapper.EmsFacsTypeMapper;
import com.ruoyi.ems.service.IEmsFacsSubtypeService;
import com.ruoyi.ems.service.IEmsFacsTypeService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 能源设施/系统分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-10
 */
@Service
public class EmsFacsTypeServiceImpl implements IEmsFacsTypeService {
    @Autowired
    private EmsFacsTypeMapper emsFacsTypeMapper;

    @Autowired
    private IEmsFacsSubtypeService facsSubtypeService;

    /**
     * 查询能源设施/系统分类
     *
     * @param id 能源设施/系统分类主键
     * @return 能源设施/系统分类
     */
    @Override
    public EmsFacsType selectFacsTypeById(Long id) {
        // 查询主分类
        EmsFacsType facsType = emsFacsTypeMapper.selectFacsTypeById(id);

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

        return facsType;
    }

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

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

        return list;
    }

    /**
     * 新增能源设施/系统分类
     *
     * @param emsFacsType 能源设施/系统分类
     * @return 结果
     */
    @Override
    public int insertFacsType(EmsFacsType emsFacsType) {
        int count = emsFacsTypeMapper.insertFacsType(emsFacsType);

        if (CollectionUtils.isNotEmpty(emsFacsType.getSubtypeList())) {
            facsSubtypeService.deleteFacsSubtypeByParentCode(emsFacsType.getCode());
            facsSubtypeService.insertFacsSubtypeBatch(emsFacsType.getSubtypeList());
        }
        else {
            facsSubtypeService.deleteFacsSubtypeByParentCode(emsFacsType.getCode());
        }

        return count;
    }

    /**
     * 修改能源设施/系统分类
     *
     * @param emsFacsType 能源设施/系统分类
     * @return 结果
     */
    @Override
    public int updateFacsType(EmsFacsType emsFacsType) {
        int count = emsFacsTypeMapper.updateFacsType(emsFacsType);

        if (CollectionUtils.isNotEmpty(emsFacsType.getSubtypeList())) {
            facsSubtypeService.deleteFacsSubtypeByParentCode(emsFacsType.getCode());
            facsSubtypeService.insertFacsSubtypeBatch(emsFacsType.getSubtypeList());
        }
        else {
            facsSubtypeService.deleteFacsSubtypeByParentCode(emsFacsType.getCode());
        }

        return count;
    }

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

            // 查询扩展属性
            if (null != facsType) {
                facsSubtypeService.deleteFacsSubtypeByParentCode(facsType.getCode());
            }
        }

        return emsFacsTypeMapper.deleteFacsTypeByIds(ids);
    }

    /**
     * 删除能源设施/系统分类信息
     *
     * @param id 能源设施/系统分类主键
     * @return 结果
     */
    @Override
    public int deleteFacsTypeById(Long id) {
        EmsFacsType facsType = emsFacsTypeMapper.selectFacsTypeById(id);

        // 查询扩展属性
        if (null != facsType) {
            facsSubtypeService.deleteFacsSubtypeByParentCode(facsType.getCode());
        }

        return emsFacsTypeMapper.deleteFacsTypeById(id);
    }

    private List<EmsFacsSubtype> getFacsSubtypes(EmsFacsType emsFacsType) {
        EmsFacsSubtype param = new EmsFacsSubtype();
        param.setParentCode(emsFacsType.getCode());

        return facsSubtypeService.selectFacsSubtypeList(param);
    }
}
