package com.ruoyi.ems.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.ems.domain.EmsFacs;
import com.ruoyi.ems.domain.EmsFacsAttr;
import com.ruoyi.ems.mapper.EmsFacsMapper;
import com.ruoyi.ems.service.IEmsFacsAttrService;
import com.ruoyi.ems.service.IEmsFacsService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 能源设施/系统Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-10
 */
@Service
public class EmsFacsServiceImpl implements IEmsFacsService {
    @Autowired
    private EmsFacsMapper emsFacsMapper;

    @Autowired
    private IEmsFacsAttrService facsAttrService;

    /**
     * 查询能源设施/系统
     *
     * @param id 能源设施/系统主键
     * @return 能源设施/系统
     */
    @Override
    public EmsFacs selectEmsFacsById(Long id) {
        // 查询设施对象
        EmsFacs emsFacs = emsFacsMapper.selectEmsFacsById(id);

        // 查询扩展属性
        if (null != emsFacs) {
            emsFacs.setFacsAttrs(getFacsAttrs(emsFacs));
        }

        return emsFacs;
    }

    /**
     * 查询能源设施/系统列表
     *
     * @param emsFacs 能源设施/系统
     * @return 能源设施/系统
     */
    @Override
    public List<EmsFacs> selectEmsFacsList(EmsFacs emsFacs) {
        // 查询设施对象
        List<EmsFacs> list = emsFacsMapper.selectEmsFacsList(emsFacs);

        // 查询扩展属性
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(facsObj -> {
                facsObj.setFacsAttrs(getFacsAttrs(facsObj));
            });
        }

        return list;
    }

    /**
     * 新增能源设施/系统
     *
     * @param emsFacs 能源设施/系统
     * @return 结果
     */
    @Override
    public int insertEmsFacs(EmsFacs emsFacs) {
        emsFacs.setCreateTime(DateUtils.getNowDate());
        int count = emsFacsMapper.insertEmsFacs(emsFacs);

        if (CollectionUtils.isNotEmpty(emsFacs.getFacsAttrs())) {
            facsAttrService.deleteEmsFacsAttrByFacsCode(emsFacs.getFacsCode());
            facsAttrService.insertEmsFacsAttrBatch(emsFacs.getFacsCode(), emsFacs.getFacsAttrs());
        }
        else {
            facsAttrService.deleteEmsFacsAttrByFacsCode(emsFacs.getFacsCode());
        }

        return count;
    }

    /**
     * 修改能源设施/系统
     *
     * @param emsFacs 能源设施/系统
     * @return 结果
     */
    @Override
    public int updateEmsFacs(EmsFacs emsFacs) {
        emsFacs.setUpdateTime(DateUtils.getNowDate());
        int count = emsFacsMapper.updateEmsFacs(emsFacs);

        if (CollectionUtils.isNotEmpty(emsFacs.getFacsAttrs())) {
            facsAttrService.deleteEmsFacsAttrByFacsCode(emsFacs.getFacsCode());
            facsAttrService.insertEmsFacsAttrBatch(emsFacs.getFacsCode(), emsFacs.getFacsAttrs());
        }
        else {
            facsAttrService.deleteEmsFacsAttrByFacsCode(emsFacs.getFacsCode());
        }

        return count;
    }

    /**
     * 批量删除能源设施/系统
     *
     * @param ids 需要删除的能源设施/系统主键
     * @return 结果
     */
    @Override
    public int deleteEmsFacsByIds(Long[] ids) {
        for (Long id : ids) {
            EmsFacs emsFacs = emsFacsMapper.selectEmsFacsById(id);

            // 查询扩展属性
            if (null != emsFacs) {
                facsAttrService.deleteEmsFacsAttrByFacsCode(emsFacs.getFacsCode());
            }
        }

        return emsFacsMapper.deleteEmsFacsByIds(ids);
    }

    /**
     * 删除能源设施/系统信息
     *
     * @param id 能源设施/系统主键
     * @return 结果
     */
    @Override
    public int deleteEmsFacsById(Long id) {
        EmsFacs emsFacs = emsFacsMapper.selectEmsFacsById(id);

        // 查询扩展属性
        if (null != emsFacs) {
            facsAttrService.deleteEmsFacsAttrByFacsCode(emsFacs.getFacsCode());
        }

        return emsFacsMapper.deleteEmsFacsById(id);
    }

    private List<EmsFacsAttr> getFacsAttrs(EmsFacs emsFacs) {
        EmsFacsAttr param = new EmsFacsAttr(emsFacs.getFacsCode());
        return facsAttrService.selectEmsFacsAttrList(param);
    }
}
