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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojecttaskattrs.vo.IotProjectTaskAttrsPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojecttaskattrs.vo.IotProjectTaskAttrsSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotprojecttaskattrs.IotProjectTaskAttrsDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotprojecttaskattrs.IotProjectTaskAttrsMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.List;

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

/**
 * 项目任务扩展属性 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotProjectTaskAttrsServiceImpl implements IotProjectTaskAttrsService {

    @Resource
    private IotProjectTaskAttrsMapper iotProjectTaskAttrsMapper;

    @Override
    public Long createIotProjectTaskAttrs(IotProjectTaskAttrsSaveReqVO createReqVO) {
        // 插入
        IotProjectTaskAttrsDO iotProjectTaskAttrs = BeanUtils.toBean(createReqVO, IotProjectTaskAttrsDO.class);
        iotProjectTaskAttrsMapper.insert(iotProjectTaskAttrs);
        // 返回
        return iotProjectTaskAttrs.getId();
    }

    @Override
    public void updateIotProjectTaskAttrs(IotProjectTaskAttrsSaveReqVO updateReqVO) {
        // 校验存在
        validateIotProjectTaskAttrsExists(updateReqVO.getId());
        // 更新
        IotProjectTaskAttrsDO updateObj = BeanUtils.toBean(updateReqVO, IotProjectTaskAttrsDO.class);
        iotProjectTaskAttrsMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotProjectTaskAttrs(Long id) {
        // 校验存在
        validateIotProjectTaskAttrsExists(id);
        // 删除
        iotProjectTaskAttrsMapper.deleteById(id);
    }

    private void validateIotProjectTaskAttrsExists(Long id) {
        if (iotProjectTaskAttrsMapper.selectById(id) == null) {
            throw exception(IOT_PROJECT_TASK_ATTRS_NOT_EXISTS);
        }
    }

    @Override
    public IotProjectTaskAttrsDO getIotProjectTaskAttrs(Long id) {
        return iotProjectTaskAttrsMapper.selectById(id);
    }

    @Override
    public PageResult<IotProjectTaskAttrsDO> getIotProjectTaskAttrsPage(IotProjectTaskAttrsPageReqVO pageReqVO) {
        return iotProjectTaskAttrsMapper.selectPage(pageReqVO);
    }

    @Override
    public List<IotProjectTaskAttrsDO> getIotProjectTaskAttrsList(IotProjectTaskAttrsPageReqVO pageReqVO) {
        return iotProjectTaskAttrsMapper.selectList(pageReqVO);
    }

}