package com.cy.guoyan.admin.module.ai.service.chatapp;

import com.cy.guoyan.admin.module.ai.controller.admin.chatapp.vo.ChatAppPageReqVO;
import com.cy.guoyan.admin.module.ai.controller.admin.chatapp.vo.ChatAppSaveReqVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import com.cy.guoyan.admin.module.ai.dal.dataobject.chatapp.ChatAppDO;
import com.cy.guoyan.admin.framework.common.pojo.PageResult;
import com.cy.guoyan.admin.framework.common.util.object.BeanUtils;
import com.cy.guoyan.admin.module.ai.dal.mysql.chatapp.ChatAppMapper;
import static com.cy.guoyan.admin.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cy.guoyan.admin.module.system.enums.ErrorCodeConstants.*;

/**
 * AI 应用 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
@Slf4j
public class ChatAppServiceImpl implements ChatAppService {

    @Resource
    private ChatAppMapper chatAppMapper;

    @Override
    public Long createChatApp(ChatAppSaveReqVO createReqVO) {
        // 插入
        ChatAppDO chatApp = BeanUtils.toBean(createReqVO, ChatAppDO.class);
        chatAppMapper.insert(chatApp);
        // 返回
        return chatApp.getId();
    }

    @Override
    public void updateChatApp(ChatAppSaveReqVO updateReqVO) {
        // 校验存在
        validateChatAppExists(updateReqVO.getId());
        // 更新
        ChatAppDO updateObj = BeanUtils.toBean(updateReqVO, ChatAppDO.class);
        chatAppMapper.updateById(updateObj);
    }

    @Override
    public void deleteChatApp(Long id) {
        // 校验存在
        validateChatAppExists(id);
        // 删除
        chatAppMapper.deleteById(id);
    }

    private void validateChatAppExists(Long id) {
        if (chatAppMapper.selectById(id) == null) {
            throw exception(CHAT_APP_NOT_EXISTS);
        }
    }

    @Override
    public ChatAppDO getChatApp(Long id) {
        return chatAppMapper.selectById(id);
    }

    @Override
    public PageResult<ChatAppDO> getChatAppPage(ChatAppPageReqVO pageReqVO) {
        return chatAppMapper.selectPage(pageReqVO);
    }

}