package cn.iocoder.yudao.module.pms.service.devicegroup;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.pms.controller.admin.devicegroup.IotDeviceGroupDetailPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.devicegroup.IotDeviceGroupDetailSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.devicegroup.IotDeviceGroupDetailDO;
import cn.iocoder.yudao.module.pms.dal.mysql.devicegroup.IotDeviceGroupDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * PMS成套明细 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotDeviceGroupDetailServiceImpl implements IotDeviceGroupDetailService {

    @Resource
    private IotDeviceGroupDetailMapper iotDeviceGroupDetailMapper;

    @Override
    public List<IotDeviceGroupDetailDO> getIotDeviceGroupDetailListByGroupId(Long groupId) {
        return iotDeviceGroupDetailMapper.selectList("group_id", groupId);
    }

    @Override
    public Long createIotDeviceGroupDetail(IotDeviceGroupDetailSaveReqVO createReqVO) {
        // 插入
        IotDeviceGroupDetailDO iotDeviceGroupDetail = BeanUtils.toBean(createReqVO, IotDeviceGroupDetailDO.class);
        iotDeviceGroupDetail.setDeleted(false);
        iotDeviceGroupDetailMapper.insert(iotDeviceGroupDetail);
        // 返回
        return iotDeviceGroupDetail.getId();
    }

    @Override
    public void updateIotDeviceGroupDetail(IotDeviceGroupDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateIotDeviceGroupDetailExists(updateReqVO.getId());
        // 更新
        IotDeviceGroupDetailDO updateObj = BeanUtils.toBean(updateReqVO, IotDeviceGroupDetailDO.class);
        iotDeviceGroupDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotDeviceGroupDetail(Long id) {
        // 校验存在
        validateIotDeviceGroupDetailExists(id);
        // 删除
        iotDeviceGroupDetailMapper.deleteById(id);
    }

    private void validateIotDeviceGroupDetailExists(Long id) {
        if (iotDeviceGroupDetailMapper.selectById(id) == null) {
            throw exception(new ErrorCode(2,"不存在"));
        }
    }

    @Override
    public IotDeviceGroupDetailDO getIotDeviceGroupDetail(Long id) {
        return iotDeviceGroupDetailMapper.selectById(id);
    }

    @Override
    public PageResult<IotDeviceGroupDetailDO> getIotDeviceGroupDetailPage(IotDeviceGroupDetailPageReqVO pageReqVO) {
        return iotDeviceGroupDetailMapper.selectPage(pageReqVO);
    }

}
