package com.ruoyi.ems.service.impl;

import com.ruoyi.ems.domain.EmsObjModel;
import com.ruoyi.ems.mapper.EmsObjModelMapper;
import com.ruoyi.ems.service.IEmsObjAbilityService;
import com.ruoyi.ems.service.IEmsObjAttrService;
import com.ruoyi.ems.service.IEmsObjEventService;
import com.ruoyi.ems.service.IEmsObjModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 能源对象属性Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-23
 */
@Service
public class EmsObjModelServiceImpl implements IEmsObjModelService {
    @Autowired
    private EmsObjModelMapper objModelMapper;

    @Autowired
    private IEmsObjAbilityService abilityService;

    @Autowired
    private IEmsObjAttrService attrService;

    @Autowired
    private IEmsObjEventService eventService;

    /**
     * 查询能源对象属性
     *
     * @param id 能源对象属性主键
     * @return 能源对象属性
     */
    @Override
    public EmsObjModel selectObjModelById(Long id) {
        EmsObjModel objModel = objModelMapper.selectObjModelById(id);

        if (null != objModel) {
            objModel.setAttrList(attrService.selectByModelCode(objModel.getModelCode()));
            objModel.setAbilityList(abilityService.selectByModelCode(objModel.getModelCode()));
            objModel.setEventList(eventService.selectByModelCode(objModel.getModelCode()));
        }

        return objModel;
    }

    @Override
    public EmsObjModel selectByModelCode(String code) {
        EmsObjModel objModel = objModelMapper.selectByModelCode(code);

        if (null != objModel) {
            objModel.setAttrList(attrService.selectByModelCode(objModel.getModelCode()));
            objModel.setAbilityList(abilityService.selectByModelCode(objModel.getModelCode()));
            objModel.setEventList(eventService.selectByModelCode(objModel.getModelCode()));
        }

        return objModel;
    }

    /**
     * 查询能源对象属性列表
     *
     * @param objModel 能源对象属性
     * @return 能源对象属性
     */
    @Override
    public List<EmsObjModel> selectObjModelList(EmsObjModel objModel) {
        return objModelMapper.selectObjModelList(objModel);
    }

    /**
     * 新增能源对象属性
     *
     * @param objModel 能源对象属性
     * @return 结果
     */
    @Override
    public int insertObjModel(EmsObjModel objModel) {
        return objModelMapper.insertObjModel(objModel);
    }

    /**
     * 修改能源对象属性
     *
     * @param objModel 能源对象属性
     * @return 结果
     */
    @Override
    public int updateObjModel(EmsObjModel objModel) {
        return objModelMapper.updateObjModel(objModel);
    }

    /**
     * 批量删除能源对象属性
     *
     * @param ids 需要删除的能源对象属性主键
     * @return 结果
     */
    @Override
    public int deleteObjModelByIds(Long[] ids) {
        for (Long id : ids) {
            EmsObjModel objModel = objModelMapper.selectObjModelById(id);

            if (null != objModel) {
                attrService.deleteByModelCode(objModel.getModelCode());
                abilityService.deleteByModelCode(objModel.getModelCode());
                eventService.deleteByModelCode(objModel.getModelCode());
            }
        }

        return objModelMapper.deleteObjModelByIds(ids);
    }

    /**
     * 删除能源对象属性信息
     *
     * @param id 能源对象属性主键
     * @return 结果
     */
    @Override
    public int deleteObjModelById(Long id) {
        EmsObjModel objModel = objModelMapper.selectObjModelById(id);

        if (null != objModel) {
            attrService.deleteByModelCode(objModel.getModelCode());
            abilityService.deleteByModelCode(objModel.getModelCode());
            eventService.deleteByModelCode(objModel.getModelCode());
        }

        return objModelMapper.deleteObjModelById(id);
    }
}
