package com.yolo.keyboard.service.tag;

import cn.hutool.core.collection.CollUtil;
import com.yolo.keyboard.dal.dataobject.tag.TagI18nDO;
import com.yolo.keyboard.dal.mysql.tag.TagI18nMapper;
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_I18N_NOT_EXISTS;
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;

    @Resource
    private TagI18nMapper tagI18nMapper;

    @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);
    }

    // ==================== 子表（人设标签国际化） ====================

    @Override
    public PageResult<TagI18nDO> getTagI18nPage(PageParam pageReqVO, Integer tagId) {
        return tagI18nMapper.selectPage(pageReqVO, tagId);
    }

    @Override
    public Long createTagI18n(TagI18nDO tagI18n) {
        tagI18nMapper.insert(tagI18n);
        return tagI18n.getId();
    }

    @Override
    public void updateTagI18n(TagI18nDO tagI18n) {
        // 校验存在
        validateTagI18nExists(tagI18n.getId());
        // 更新
        tagI18nMapper.updateById(tagI18n);
    }

    @Override
    public void deleteTagI18n(Long id) {
        // 删除
        tagI18nMapper.deleteById(id);
    }

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

    @Override
    public TagI18nDO getTagI18n(Long id) {
        return tagI18nMapper.selectById(id);
    }

    private void validateTagI18nExists(Long id) {
        if (tagI18nMapper.selectById(id) == null) {
            throw exception(TAG_I18N_NOT_EXISTS);
        }
    }

    private void deleteTagI18nByTagId(Integer tagId) {
        tagI18nMapper.deleteByTagId(tagId);
    }

    private void deleteTagI18nByTagIds(List<Integer> tagIds) {
        tagI18nMapper.deleteByTagIds(tagIds);
    }
}