package com.yolo.keyboard.service.usercalllog;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.yolo.keyboard.controller.admin.usercalllog.vo.*;
import com.yolo.keyboard.dal.dataobject.usercalllog.KeyboardUserCallLogDO;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.pojo.PageParam;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;

import com.yolo.keyboard.dal.mysql.usercalllog.KeyboardUserCallLogMapper;

import static com.yolo.keyboard.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yolo.keyboard.framework.common.util.collection.CollectionUtils.convertList;
import static com.yolo.keyboard.framework.common.util.collection.CollectionUtils.diffList;
import static com.yolo.keyboard.module.infra.enums.ErrorCodeConstants.USER_CALL_LOG_NOT_EXISTS;

/**
 * 用户每次调用日志（用于记录token、模型、耗时、成功率等） Service 实现类
 *
 * @author Ziin
 */
@Service
@Validated
public class KeyboardUserCallLogServiceImpl implements KeyboardUserCallLogService {

    @Resource
    private KeyboardUserCallLogMapper userCallLogMapper;

    @Override
    public Long createUserCallLog(KeyboardUserCallLogSaveReqVO createReqVO) {
        // 插入
        KeyboardUserCallLogDO userCallLog = BeanUtils.toBean(createReqVO, KeyboardUserCallLogDO.class);
        userCallLogMapper.insert(userCallLog);

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

    @Override
    public void updateUserCallLog(KeyboardUserCallLogSaveReqVO updateReqVO) {
        // 校验存在
        validateUserCallLogExists(updateReqVO.getId());
        // 更新
        KeyboardUserCallLogDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardUserCallLogDO.class);
        userCallLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserCallLog(Long id) {
        // 校验存在
        validateUserCallLogExists(id);
        // 删除
        userCallLogMapper.deleteById(id);
    }

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


    private void validateUserCallLogExists(Long id) {
        if (userCallLogMapper.selectById(id) == null) {
            throw exception(USER_CALL_LOG_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardUserCallLogDO getUserCallLog(Long id) {
        return userCallLogMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardUserCallLogDO> getUserCallLogPage(KeyboardUserCallLogPageReqVO pageReqVO) {
        return userCallLogMapper.selectPage(pageReqVO);
    }

}