package org.dromara.inspection.service.impl;

import org.dromara.common.core.domain.R;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.inspection.domain.bo.OpsInspectionNodeBo;
import org.dromara.inspection.domain.vo.OpsInspectionNodeVo;
import org.dromara.inspection.domain.OpsInspectionNode;
import org.dromara.inspection.mapper.OpsInspectionNodeMapper;
import org.dromara.inspection.service.IOpsInspectionNodeService;

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

/**
 * 运维-巡检-通用节点Service业务层处理
 *
 * @author LionLi
 * @date 2025-09-23
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class OpsInspectionNodeServiceImpl implements IOpsInspectionNodeService {

    private final OpsInspectionNodeMapper baseMapper;

    /**
     * 查询运维-巡检-通用节点
     *
     * @param id 主键
     * @return 运维-巡检-通用节点
     */
    @Override
    public OpsInspectionNodeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询运维-巡检-通用节点列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 运维-巡检-通用节点分页列表
     */
    @Override
    public TableDataInfo<OpsInspectionNodeVo> queryPageList(OpsInspectionNodeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OpsInspectionNode> lqw = buildQueryWrapper(bo);
        Page<OpsInspectionNodeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的运维-巡检-通用节点列表
     *
     * @param bo 查询条件
     * @return 运维-巡检-通用节点列表
     */
    @Override
    public List<OpsInspectionNodeVo> queryList(OpsInspectionNodeBo bo) {
        LambdaQueryWrapper<OpsInspectionNode> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OpsInspectionNode> buildQueryWrapper(OpsInspectionNodeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OpsInspectionNode> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(OpsInspectionNode::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getModule()), OpsInspectionNode::getModule, bo.getModule());
        lqw.eq(bo.getOrderId() != null, OpsInspectionNode::getOrderId, bo.getOrderId());
        lqw.eq(bo.getCode() != null, OpsInspectionNode::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), OpsInspectionNode::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增运维-巡检-通用节点
     *
     * @param bo 运维-巡检-通用节点
     * @return 是否新增成功
     */
    @Override
    public R<String> insertByBo(List<OpsInspectionNodeBo> bo) {
        List<String> ids = new ArrayList<>();
        if (bo != null) {
            for (OpsInspectionNodeBo nodeBo : bo) {
                OpsInspectionNode add = MapstructUtils.convert(nodeBo, OpsInspectionNode.class);
                validEntityBeforeSave(add);
                int insert = baseMapper.insert(add);
                if (insert > 0){
                    if (add != null) {
                        ids.add(add.getId().toString());
                    }
                }
            }
//            log.info("{}", StringUtils.join(ids, ","));
            return R.ok(StringUtils.join(ids, ","));
        }else{
            return R.fail();
        }
    }

    /**
     * 修改运维-巡检-通用节点
     *
     * @param bo 运维-巡检-通用节点
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(List<OpsInspectionNodeBo> bo) {
        List<OpsInspectionNode> nodes = new ArrayList<>();
        for (OpsInspectionNodeBo nodeBo : bo) {
            OpsInspectionNode update = MapstructUtils.convert(nodeBo, OpsInspectionNode.class);
            validEntityBeforeSave(update);
            nodes.add(update);
        }
        return baseMapper.updateBatchById(nodes);
    }

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

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



}
