package com.yolo.keyboard.service.aicompanion;

import com.yolo.keyboard.controller.admin.aicompanion.vo.KeyboardAiCompanionPageReqVO;
import com.yolo.keyboard.controller.admin.aicompanion.vo.KeyboardAiCompanionSaveReqVO;
import com.yolo.keyboard.dal.dataobject.aicompanion.KeyboardAiCompanionDO;
import com.yolo.keyboard.dal.mysql.aicompanion.KeyboardAiCompanionMapper;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static com.yolo.keyboard.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yolo.keyboard.module.infra.enums.ErrorCodeConstants.AI_COMPANION_NOT_EXISTS;

/**
 * AI陪聊角色表，用于定义恋爱/陪伴型虚拟角色的基础信息与人设 Service 实现类
 *
 * @author ziin
 */
@Service
@Validated
public class KeyboardAiCompanionServiceImpl implements KeyboardAiCompanionService {

    @Resource
    private KeyboardAiCompanionMapper aiCompanionMapper;

    @Override
    public Long createAiCompanion(KeyboardAiCompanionSaveReqVO createReqVO) {
        // 插入
        KeyboardAiCompanionDO aiCompanion = BeanUtils.toBean(createReqVO, KeyboardAiCompanionDO.class);
        aiCompanionMapper.insert(aiCompanion);

        // 返回
        return aiCompanion.getId();
    }

    @Override
    public void updateAiCompanion(KeyboardAiCompanionSaveReqVO updateReqVO) {
        // 校验存在
        validateAiCompanionExists(updateReqVO.getId());
        // 更新
        KeyboardAiCompanionDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardAiCompanionDO.class);
        aiCompanionMapper.updateById(updateObj);
    }

    @Override
    public void deleteAiCompanion(Long id) {
        // 校验存在
        validateAiCompanionExists(id);
        // 删除
        aiCompanionMapper.deleteById(id);
    }

    @Override
        public void deleteAiCompanionListByIds(List<Long> ids) {
        // 删除
        aiCompanionMapper.deleteByIds(ids);
        }


    private void validateAiCompanionExists(Long id) {
        if (aiCompanionMapper.selectById(id) == null) {
            throw exception(AI_COMPANION_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardAiCompanionDO getAiCompanion(Long id) {
        return aiCompanionMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardAiCompanionDO> getAiCompanionPage(KeyboardAiCompanionPageReqVO pageReqVO) {
        return aiCompanionMapper.selectPage(pageReqVO);
    }

}