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

import cn.iocoder.yudao.module.pms.controller.admin.app.vo.IotAppPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.app.vo.IotAppSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.app.IotAppDO;
import cn.iocoder.yudao.module.pms.dal.mysql.app.IotAppMapper;
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.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_APP_NOT_EXISTS;

/**
 * app版本更新 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotAppServiceImpl implements IotAppService {

    @Resource
    private IotAppMapper iotAppMapper;

    @Override
    public IotAppDO getNew() {
        IotAppDO iotAppDO = iotAppMapper.selectNew();
        return iotAppDO;
    }

    @Override
    public Long createIotApp(IotAppSaveReqVO createReqVO) {
        // 插入
        IotAppDO iotApp = BeanUtils.toBean(createReqVO, IotAppDO.class);
        iotAppMapper.insert(iotApp);
        // 返回
        return iotApp.getId();
    }

    @Override
    public void updateIotApp(IotAppSaveReqVO updateReqVO) {
        // 校验存在
        validateIotAppExists(updateReqVO.getId());
        // 更新
        IotAppDO updateObj = BeanUtils.toBean(updateReqVO, IotAppDO.class);
        iotAppMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotApp(Long id) {
        // 校验存在
        validateIotAppExists(id);
        // 删除
        iotAppMapper.deleteById(id);
    }

    private void validateIotAppExists(Long id) {
        if (iotAppMapper.selectById(id) == null) {
            throw exception(IOT_APP_NOT_EXISTS);
        }
    }

    @Override
    public IotAppDO getIotApp(Long id) {
        return iotAppMapper.selectById(id);
    }

    @Override
    public PageResult<IotAppDO> getIotAppPage(IotAppPageReqVO pageReqVO) {
        return iotAppMapper.selectPage(pageReqVO);
    }

}
