package com.goomood.phm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.goomood.common.utils.StringUtils;
import com.goomood.phm.domain.KGraphEntity;
import com.goomood.phm.mapper.KGraphEntityMapper;
import com.goomood.phm.service.IKGraphEntityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class KGraphEntityServiceImpl  implements IKGraphEntityService {
    @Resource
    private KGraphEntityMapper kGraphEntityMapper;


    /**
     * 构建知识图谱实体关系查询条件
     */
    public static QueryWrapper<KGraphEntity> buildQueryWrapper(KGraphEntity query) {
        QueryWrapper<KGraphEntity> wrapper = new QueryWrapper<>();

        // 基本查询条件
        wrapper.eq(query.getId() != null, "id", query.getId())
                .eq(query.getClauseId() != null, "clause_id", query.getClauseId())
                .like(StringUtils.isNotEmpty(query.getHeadEntity()), "head_entity", query.getHeadEntity())
                .like(StringUtils.isNotEmpty(query.getHeadEntityClass()), "head_entity_class", query.getHeadEntityClass())
                .like(StringUtils.isNotEmpty(query.getRelation()), "relation", query.getRelation())
                .like(StringUtils.isNotEmpty(query.getTailEntity()), "tail_entity", query.getTailEntity())
                .like(StringUtils.isNotEmpty(query.getTailEntityClass()), "tail_entity_class", query.getTailEntityClass())
                .eq(ObjectUtils.isNotEmpty(query.getStatus()), "status", query.getStatus())
                .eq(query.getDelFlag() != null, "del_flag", query.getDelFlag());

        // 时间范围查询（示例：创建时间）
        if (query.getCreateTime() != null) {
            // 假设前端传入的是开始时间
            wrapper.ge("create_time", query.getCreateTime());
        }
        if (query.getUpdateTime() != null) {
            // 假设前端传入的是结束时间
            wrapper.le("update_time", query.getUpdateTime());
        }

        // 排序条件（默认按创建时间降序）
        wrapper.orderByDesc("create_time");

        return wrapper;
    }
    /**
     * 查询知识图谱实体关系
     *
     * @param id 知识图谱实体关系主键
     * @return 知识图谱实体关系
     */
    @Override
    public KGraphEntity selectById(Long id) {
        return kGraphEntityMapper.selectById(id);
    }

    @Override
    public List<KGraphEntity> selectList(KGraphEntity po) {
        return kGraphEntityMapper.selectList(buildQueryWrapper(po));
    }

    @Override
    public List<KGraphEntity> selectListByTaskId(Long taskId) {
        return  kGraphEntityMapper.selectListByTaskId(taskId);
    }
    /**
     * 分页查询知识图谱实体关系列表
     *
     * @param po 知识图谱实体关系Bo
     * @return 分页知识图谱实体关系集合
     */
    @Override
    public Page<KGraphEntity> selectPage(KGraphEntity po) {
        Page<KGraphEntity> kGraphEntityPage = kGraphEntityMapper.selectPage(new Page<>(), buildQueryWrapper(po));
        return kGraphEntityPage;
    }
    /**
     * 新增知识图谱实体关系
     *
     * @param po 知识图谱实体关系Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public void insert(KGraphEntity po) {
         kGraphEntityMapper.insert(po);
    }

    @Override
    public boolean saveBatch(List<KGraphEntity> kGraphEntityList) {
        return false;
    }

    @Override
    public boolean insertWithPk(KGraphEntity po) {
        return false;
    }

    @Override
    public boolean update(KGraphEntity po) {
        return false;
    }

    @Override
    public boolean deleteByIds(Long[] ids) {
        return false;
    }
}
