package org.eco.als.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.eco.als.domain.KGraphEntity;
import org.eco.als.domain.bo.KGraphEntityBo;
import org.eco.als.domain.vo.KGraphEntityVo;
import org.eco.als.mapper.KGraphEntityMapper;
import org.eco.als.service.IKGraphEntityService;
import org.eco.common.core.core.page.PageResult;
import org.eco.common.core.utils.MapstructUtils;
import org.eco.common.orm.core.page.PageQuery;
import org.eco.common.orm.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.eco.als.domain.table.KGraphClauseTableDef.KGRAPH_CLAUSE;
import static org.eco.als.domain.table.KGraphEntityTableDef.KGRAPH_ENTITY;

/**
 * 知识图谱实体关系Service业务层处理
 *
 * @author wgk
 * @date 2025-01-03
 */
@Service
@Slf4j
public class KGraphEntityServiceImpl extends BaseServiceImpl<KGraphEntityMapper, KGraphEntity> implements IKGraphEntityService {
    @Resource
    private KGraphEntityMapper kGraphEntityMapper;

    @Override
    public QueryWrapper query() {
        return super.query().from(KGRAPH_ENTITY);
    }

    private QueryWrapper buildQueryWrapper(KGraphEntityBo kGraphEntityBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(KGRAPH_ENTITY.CLAUSE_ID.eq
            (kGraphEntityBo.getClauseId()));
        queryWrapper.and(KGRAPH_ENTITY.HEAD_ENTITY.eq
            (kGraphEntityBo.getHeadEntity()));
        queryWrapper.and(KGRAPH_ENTITY.HEAD_ENTITY_CLASS.eq
            (kGraphEntityBo.getHeadEntityClass()));
        queryWrapper.and(KGRAPH_ENTITY.RELATION.eq
            (kGraphEntityBo.getRelation()));
        queryWrapper.and(KGRAPH_ENTITY.TAIL_ENTITY.eq
            (kGraphEntityBo.getTailEntity()));
        queryWrapper.and(KGRAPH_ENTITY.TAIL_ENTITY_CLASS.eq
            (kGraphEntityBo.getTailEntityClass()));
        queryWrapper.and(KGRAPH_ENTITY.STATUS.eq
            (kGraphEntityBo.getStatus()));
        queryWrapper.and(KGRAPH_ENTITY.REMARKS.eq
            (kGraphEntityBo.getRemarks()));
        queryWrapper.orderBy(KGRAPH_ENTITY.UPDATE_TIME.desc());

        return queryWrapper;
    }

    /**
     * 查询知识图谱实体关系
     *
     * @param id 知识图谱实体关系主键
     * @return 知识图谱实体关系
     */
    @Override
    public KGraphEntityVo selectById(Long id) {
        return this.getOneAs(query().where(KGRAPH_ENTITY.ID.eq(id)), KGraphEntityVo.class);

    }


    /**
     * 查询知识图谱实体关系列表
     *
     * @param kGraphEntityBo 知识图谱实体关系Bo
     * @return 知识图谱实体关系集合
     */
    @Override
    public List<KGraphEntityVo> selectList(KGraphEntityBo kGraphEntityBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(kGraphEntityBo);
        return this.listAs(queryWrapper, KGraphEntityVo.class);
    }

    @Override
    public List<KGraphEntityVo> selectListByTaskId(Long taskId) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.select(
                KGRAPH_ENTITY.HEAD_ENTITY,
                KGRAPH_ENTITY.HEAD_ENTITY_CLASS,
                KGRAPH_ENTITY.RELATION,
                KGRAPH_ENTITY.TAIL_ENTITY,
                KGRAPH_ENTITY.TAIL_ENTITY_CLASS)
            .from(KGRAPH_ENTITY).join(KGRAPH_CLAUSE).on(KGRAPH_ENTITY.CLAUSE_ID.eq(KGRAPH_CLAUSE.ID))
            .where(KGRAPH_CLAUSE.TASK_ID.eq(taskId)).and(KGRAPH_CLAUSE.STATUS.eq(1));
        return this.listAs(queryWrapper, KGraphEntityVo.class);
    }

    /**
     * 分页查询知识图谱实体关系列表
     *
     * @param kGraphEntityBo 知识图谱实体关系Bo
     * @return 分页知识图谱实体关系集合
     */
    @Override
    public PageResult<KGraphEntityVo> selectPage(KGraphEntityBo kGraphEntityBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(kGraphEntityBo);
        Page<KGraphEntityVo> page = this.pageAs(PageQuery.build(), queryWrapper, KGraphEntityVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增知识图谱实体关系
     *
     * @param kGraphEntityBo 知识图谱实体关系Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(KGraphEntityBo kGraphEntityBo) {
        KGraphEntity kGraphEntity = MapstructUtils.convert(kGraphEntityBo, KGraphEntity.class);

        return this.save(kGraphEntity);//使用全局配置的雪花算法主键生成器生成ID值
    }

    @Override
    public boolean saveBatch(List<KGraphEntityBo> kGraphEntityBoList) {
        List<KGraphEntity> kGraphEntityList = new ArrayList<>();
        kGraphEntityBoList.forEach(kGraphEntityBo ->
            kGraphEntityList.add(MapstructUtils.convert(kGraphEntityBo, KGraphEntity.class))
        );
        return this.saveBatch(kGraphEntityList, 100);
    }

    /**
     * 新增知识图谱实体关系，前台提供主键值，一般用于导入的场合
     *
     * @param kGraphEntityBo 知识图谱实体关系Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(KGraphEntityBo kGraphEntityBo) {
        KGraphEntity kGraphEntity = MapstructUtils.convert(kGraphEntityBo, KGraphEntity.class);


        return kGraphEntityMapper.insertWithPk(kGraphEntity) > 0;//前台传来主键值
    }

    /**
     * 修改知识图谱实体关系
     *
     * @param kGraphEntityBo 知识图谱实体关系Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(KGraphEntityBo kGraphEntityBo) {
        KGraphEntity kGraphEntity = MapstructUtils.convert(kGraphEntityBo, KGraphEntity.class);
        if (ObjectUtil.isNotNull(kGraphEntity) && ObjectUtil.isNotNull(kGraphEntity.getId())) {
            return this.updateById(kGraphEntity);
        }
        return false;
    }

    /**
     * 批量删除知识图谱实体关系
     *
     * @param ids 需要删除的知识图谱实体关系主键集合
     * @return 结果:true 删除成功，false 删除失败
     */
    @Transactional
    @Override
    public boolean deleteByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

}
