package com.yolo.keyboard.service.tag;

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.tag.vo.*;
import com.yolo.keyboard.dal.dataobject.tag.KeyboardTagDO;
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.tag.KeyboardTagMapper;

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.TAG_NOT_EXISTS;

/**
 * 人设标签 Service 实现类
 *
 * @author ziin
 */
@Service
@Validated
public class KeyboardTagServiceImpl implements KeyboardTagService {

    @Resource
    private KeyboardTagMapper tagMapper;

    @Override
    public Integer createTag(KeyboardTagSaveReqVO createReqVO) {
        // 插入
        KeyboardTagDO tag = BeanUtils.toBean(createReqVO, KeyboardTagDO.class);
        tagMapper.insert(tag);

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

    @Override
    public void updateTag(KeyboardTagSaveReqVO updateReqVO) {
        // 校验存在
        validateTagExists(updateReqVO.getId());
        // 更新
        KeyboardTagDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardTagDO.class);
        tagMapper.updateById(updateObj);
    }

    @Override
    public void deleteTag(Integer id) {
        // 校验存在
        validateTagExists(id);
        // 删除
        tagMapper.deleteById(id);
    }

    @Override
        public void deleteTagListByIds(List<Integer> ids) {
        // 删除
        tagMapper.deleteByIds(ids);
        }


    private void validateTagExists(Integer id) {
        if (tagMapper.selectById(id) == null) {
            throw exception(TAG_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardTagDO getTag(Integer id) {
        return tagMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardTagDO> getTagPage(KeyboardTagPageReqVO pageReqVO) {
        return tagMapper.selectPage(pageReqVO);
    }

}