package com.cico.nest.lcdp.boot.core.qxsd.application.service.impl;

import com.cico.nest.lcdp.boot.core.qxsd.application.service.EventTypeService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import com.cico.nest.lcdp.framework.common.pojo.PageResult;
import com.cico.nest.lcdp.framework.common.pojo.PageParam;
import com.cico.nest.lcdp.framework.common.util.object.BeanUtils;

import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.EventTypeMapper;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.eventType.*;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.EventTypeDO;
import static com.cico.nest.lcdp.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
import static com.cico.nest.lcdp.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 事件类型映射 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class EventTypeServiceImpl implements EventTypeService {

    @Resource
    private EventTypeMapper eventTypeMapper;

    @Override
    public Integer createEventType(EventTypeSaveReqVO createReqVO) {
        // 插入
        EventTypeDO eventType = BeanUtils.toBean(createReqVO, EventTypeDO.class);
        eventTypeMapper.insert(eventType);
        // 返回
        return eventType.getId();
    }

    @Override
    public void updateEventType(EventTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateEventTypeExists(updateReqVO.getId());
        // 更新
        EventTypeDO updateObj = BeanUtils.toBean(updateReqVO, EventTypeDO.class);
        eventTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteEventType(Integer id) {
        // 校验存在
        validateEventTypeExists(id);
        // 删除
        eventTypeMapper.deleteById(id);
    }

    private void validateEventTypeExists(Integer id) {
        if (eventTypeMapper.selectById(id) == null) {
            throw exception(INTERNAL_SERVER_ERROR.getCode(), "事件类型不存在");
        }
    }

    @Override
    public EventTypeDO getEventType(Integer id) {
        return eventTypeMapper.selectById(id);
    }

    @Override
    public PageResult<EventTypeDO> getEventTypePage(EventTypePageReqVO pageReqVO) {
        return eventTypeMapper.selectPage(pageReqVO);
    }

}
