package com.yingpaipay.setting.service.impl;

import org.dromara.common.core.constant.CacheNames;
import org.dromara.common.core.exception.BusinessException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.MessageUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.redis.utils.CacheUtils;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.yingpaipay.setting.domain.bo.KeywordSettingBo;
import com.yingpaipay.setting.domain.vo.KeywordSettingVo;
import com.yingpaipay.setting.domain.KeywordSetting;
import com.yingpaipay.setting.mapper.KeywordSettingMapper;
import com.yingpaipay.setting.service.IKeywordSettingService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 文件夹关键词设置Service业务层处理
 *
 * @author Huanyi
 * @date 2026-01-20
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class KeywordSettingServiceImpl implements IKeywordSettingService {

    private final KeywordSettingMapper baseMapper;

    /**
     * 查询文件夹关键词设置
     *
     * @param id 主键
     * @return 文件夹关键词设置
     */
    @Cacheable(cacheNames = CacheNames.KEYWORD_SETTING, key = "#id")
    @Override
    public KeywordSettingVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询文件夹关键词设置列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 文件夹关键词设置分页列表
     */
    @Override
    public TableDataInfo<KeywordSettingVo> queryPageList(KeywordSettingBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KeywordSetting> lqw = buildQueryWrapper(bo);
        Page<KeywordSettingVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的文件夹关键词设置列表
     *
     * @param bo 查询条件
     * @return 文件夹关键词设置列表
     */
    @Override
    public List<KeywordSettingVo> queryList(KeywordSettingBo bo) {
        LambdaQueryWrapper<KeywordSetting> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KeywordSetting> buildQueryWrapper(KeywordSettingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KeywordSetting> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(KeywordSetting::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), KeywordSetting::getContent, bo.getContent());
        return lqw;
    }

    /**
     * 新增文件夹关键词设置
     *
     * @param bo 文件夹关键词设置
     * @return 是否新增成功
     */
    @CachePut(cacheNames = CacheNames.KEYWORD_SETTING, key = "#bo.id")
    @Override
    public Boolean insertByBo(KeywordSettingBo bo) {
        KeywordSetting add = MapstructUtils.convert(bo, KeywordSetting.class);
        validEntityBeforeSave(add);

        if (baseMapper.selectCount(Wrappers.lambdaQuery(KeywordSetting.class).eq(KeywordSetting::getContent, bo.getContent())) > 0L) {
            throw new BusinessException(MessageUtils.message("setting.keyword.add.exists"));
        }

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改文件夹关键词设置
     *
     * @param bo 文件夹关键词设置
     * @return 是否修改成功
     */
    @CachePut(cacheNames = CacheNames.KEYWORD_SETTING, key = "#bo.id")
    @Override
    public Boolean updateByBo(KeywordSettingBo bo) {
        KeywordSetting update = MapstructUtils.convert(bo, KeywordSetting.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KeywordSetting entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除文件夹关键词设置信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        ids.forEach(id -> CacheUtils.evict(CacheNames.KEYWORD_SETTING, id));
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Cacheable(cacheNames = CacheNames.KEYWORD_SETTING)
    @Override
    public List<KeywordSettingVo> list() {
        return baseMapper.selectVoList();
    }
}
