package org.dromara.service.impl;

import cn.hutool.core.codec.Base64;
import org.dromara.common.core.utils.MapstructUtils;
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 org.springframework.stereotype.Service;
import org.dromara.domain.bo.TblRulesNodeBo;
import org.dromara.domain.vo.TblRulesNodeVo;
import org.dromara.domain.TblRulesNode;
import org.dromara.mapper.TblRulesNodeMapper;
import org.dromara.service.ITblRulesNodeService;

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

/**
 * 规则节点Service业务层处理
 *
 * @author Lion Li
 * @date 2024-08-15
 */
@RequiredArgsConstructor
@Service
public class TblRulesNodeServiceImpl implements ITblRulesNodeService {

    private final TblRulesNodeMapper baseMapper;

    /**
     * 查询规则节点
     *
     * @param id 主键
     * @return 规则节点
     */
    @Override
    public TblRulesNodeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询规则节点列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 规则节点分页列表
     */
    @Override
    public TableDataInfo<TblRulesNodeVo> queryPageList(TblRulesNodeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TblRulesNode> lqw = buildQueryWrapper(bo);
        Page<TblRulesNodeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的规则节点列表
     *
     * @param bo 查询条件
     * @return 规则节点列表
     */
    @Override
    public List<TblRulesNodeVo> queryList(TblRulesNodeBo bo) {
        LambdaQueryWrapper<TblRulesNode> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TblRulesNode> buildQueryWrapper(TblRulesNodeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblRulesNode> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), TblRulesNode::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getNodedata()), TblRulesNode::getNodedata, bo.getNodedata());
        lqw.eq(StringUtils.isNotBlank(bo.getNodeui()), TblRulesNode::getNodeui, bo.getNodeui());
        lqw.eq(StringUtils.isNotBlank(bo.getNodeconfigui()), TblRulesNode::getNodeconfigui, bo.getNodeconfigui());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), TblRulesNode::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getCategory()), TblRulesNode::getCategory, bo.getCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getExt1()), TblRulesNode::getExt1, bo.getExt1());
        lqw.eq(StringUtils.isNotBlank(bo.getExt2()), TblRulesNode::getExt2, bo.getExt2());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TblRulesNode::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增规则节点
     *
     * @param bo 规则节点
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TblRulesNodeBo bo) {
        TblRulesNode add = MapstructUtils.convert(bo, TblRulesNode.class);
        validEntityBeforeSave(add);
        if(add.getNodeconfigui()!=null){
            add.setNodeconfigui(Base64.decodeStr(add.getNodeconfigui()));
        }
        if(add.getNodeui()!=null){
            add.setNodeui(Base64.decodeStr(add.getNodeui()));
        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改规则节点
     *
     * @param bo 规则节点
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TblRulesNodeBo bo) {
        TblRulesNode update = MapstructUtils.convert(bo, TblRulesNode.class);
        validEntityBeforeSave(update);
        if(update.getNodeconfigui()!=null){
            update.setNodeconfigui(Base64.decodeStr(update.getNodeconfigui()));
        }
        if(update.getNodeui()!=null){
            update.setNodeui(Base64.decodeStr(update.getNodeui()));
        }
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除规则节点信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
