package cn.iocoder.yudao.module.iot.service.recipedevicerecord;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.iot.controller.admin.recipedevicerecord.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.recipedevicerecord.RecipeDeviceRecordDO;
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 cn.iocoder.yudao.module.iot.dal.mysql.recipedevicerecord.RecipeDeviceRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*;

/**
 * 设备点位采集值记录 Service 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class RecipeDeviceRecordServiceImpl implements RecipeDeviceRecordService {

    @Resource
    private RecipeDeviceRecordMapper recipeDeviceRecordMapper;

    @Override
    public Long createRecipeDeviceRecord(RecipeDeviceRecordSaveReqVO createReqVO) {

        // 插入
        RecipeDeviceRecordDO recipeDeviceRecord = BeanUtils.toBean(createReqVO, RecipeDeviceRecordDO.class);
        recipeDeviceRecordMapper.insert(recipeDeviceRecord);
        // 返回
        return recipeDeviceRecord.getId();
    }

    @Override
    public void updateRecipeDeviceRecord(RecipeDeviceRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateRecipeDeviceRecordExists(updateReqVO.getId());
        // 更新
        RecipeDeviceRecordDO updateObj = BeanUtils.toBean(updateReqVO, RecipeDeviceRecordDO.class);
        recipeDeviceRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteRecipeDeviceRecord(Long id) {
        // 校验存在
        validateRecipeDeviceRecordExists(id);
        // 删除
        recipeDeviceRecordMapper.deleteById(id);
    }

    private void validateRecipeDeviceRecordExists(Long id) {
        if (recipeDeviceRecordMapper.selectById(id) == null) {
            throw exception(RECIPE_DEVICE_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public RecipeDeviceRecordDO getRecipeDeviceRecord(Long id) {
        return recipeDeviceRecordMapper.selectById(id);
    }

    @Override
    public PageResult<RecipeDeviceRecordDO> getRecipeDeviceRecordPage(RecipeDeviceRecordPageReqVO pageReqVO) {
        return recipeDeviceRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RecipeDeviceRecordDO> getListByRecipeId(Long recipeId) {
        return recipeDeviceRecordMapper.selectList(Wrappers.<RecipeDeviceRecordDO>lambdaQuery().eq(RecipeDeviceRecordDO::getRecipeId, recipeId));

    }

    @Override
    public void deleteByIds(List<RecipeDeviceRecordDO> recipeDeviceRecordDOS) {
        recipeDeviceRecordMapper.deleteByIds(recipeDeviceRecordDOS);

    }

}