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

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.iotprojecttasktemplate.vo.IotProjectTaskTemplatePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojecttasktemplate.vo.IotProjectTaskTemplateSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotprojecttasktemplate.IotProjectTaskTemplateDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotprojecttasktemplate.IotProjectTaskTemplateMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

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

/**
 * 项目任务属性模板 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotProjectTaskTemplateServiceImpl implements IotProjectTaskTemplateService {

    @Resource
    private IotProjectTaskTemplateMapper iotProjectTaskTemplateMapper;

    @Override
    public Long createIotProjectTaskTemplate(IotProjectTaskTemplateSaveReqVO createReqVO) {
        // 插入
        IotProjectTaskTemplateDO iotProjectTaskTemplate = BeanUtils.toBean(createReqVO, IotProjectTaskTemplateDO.class);
        iotProjectTaskTemplateMapper.insert(iotProjectTaskTemplate);
        // 返回
        return iotProjectTaskTemplate.getId();
    }

    @Override
    public void updateIotProjectTaskTemplate(IotProjectTaskTemplateSaveReqVO updateReqVO) {
        // 校验存在
        validateIotProjectTaskTemplateExists(updateReqVO.getId());
        // 更新
        IotProjectTaskTemplateDO updateObj = BeanUtils.toBean(updateReqVO, IotProjectTaskTemplateDO.class);
        iotProjectTaskTemplateMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotProjectTaskTemplate(Long id) {
        // 校验存在
        validateIotProjectTaskTemplateExists(id);
        // 删除
        iotProjectTaskTemplateMapper.deleteById(id);
    }

    private void validateIotProjectTaskTemplateExists(Long id) {
        if (iotProjectTaskTemplateMapper.selectById(id) == null) {
            throw exception(IOT_PROJECT_TASK_TEMPLATE_NOT_EXISTS);
        }
    }

    @Override
    public IotProjectTaskTemplateDO getIotProjectTaskTemplate(Long id) {
        return iotProjectTaskTemplateMapper.selectById(id);
    }

    @Override
    public PageResult<IotProjectTaskTemplateDO> getIotProjectTaskTemplatePage(IotProjectTaskTemplatePageReqVO pageReqVO) {
        return iotProjectTaskTemplateMapper.selectPage(pageReqVO);
    }

}