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.KGraphClause;
import org.eco.als.domain.bo.KGraphClauseBo;
import org.eco.als.domain.vo.KGraphClauseVo;
import org.eco.als.mapper.KGraphClauseMapper;
import org.eco.als.service.IKGraphClauseService;
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;

/**
 * 知识图谱分句结果Service业务层处理
 *
 * @author wgk
 * @date 2025-01-03
 */
@Service
@Slf4j
public class KGraphClauseServiceImpl extends BaseServiceImpl<KGraphClauseMapper, KGraphClause> implements IKGraphClauseService {
    @Resource
    private KGraphClauseMapper kGraphClauseMapper;

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

    private QueryWrapper buildQueryWrapper(KGraphClauseBo kGraphClauseBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(KGRAPH_CLAUSE.TASK_ID.eq
            (kGraphClauseBo.getTaskId()));
        queryWrapper.and(KGRAPH_CLAUSE.CLAUSE.eq
            (kGraphClauseBo.getClause()));
        queryWrapper.and(KGRAPH_CLAUSE.STATUS.eq
            (kGraphClauseBo.getStatus()));
        queryWrapper.and(KGRAPH_CLAUSE.REMARKS.eq
            (kGraphClauseBo.getRemarks()));
        queryWrapper.orderBy(KGRAPH_CLAUSE.UPDATE_TIME.desc());
        return queryWrapper;
    }

    /**
     * 查询知识图谱分句结果
     *
     * @param id 知识图谱分句结果主键
     * @return 知识图谱分句结果
     */
    @Override
    public KGraphClauseVo selectById(Long id) {
        return this.getOneAs(query().where(KGRAPH_CLAUSE.ID.eq(id)), KGraphClauseVo.class);

    }


    /**
     * 查询知识图谱分句结果列表
     *
     * @param kGraphClauseBo 知识图谱分句结果Bo
     * @return 知识图谱分句结果集合
     */
    @Override
    public List<KGraphClauseVo> selectList(KGraphClauseBo kGraphClauseBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(kGraphClauseBo);
        return this.listAs(queryWrapper, KGraphClauseVo.class);
    }

    /**
     * 分页查询知识图谱分句结果列表
     *
     * @param kGraphClauseBo 知识图谱分句结果Bo
     * @return 分页知识图谱分句结果集合
     */
    @Override
    public PageResult<KGraphClauseVo> selectPage(KGraphClauseBo kGraphClauseBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(kGraphClauseBo);
        Page<KGraphClauseVo> page = this.pageAs(PageQuery.build(), queryWrapper, KGraphClauseVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增知识图谱分句结果
     *
     * @param kGraphClauseBo 知识图谱分句结果Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(KGraphClauseBo kGraphClauseBo) {
        KGraphClause kGraphClause = MapstructUtils.convert(kGraphClauseBo, KGraphClause.class);
        //使用全局配置的雪花算法主键生成器生成ID值
        return this.save(kGraphClause);
    }

    @Override
    public boolean saveBatch(List<KGraphClauseBo> kGraphClauseBoLists) {
        List<KGraphClause> kGraphClauseList = new ArrayList<>();
        kGraphClauseBoLists.forEach(kGraphClauseBo -> {
            kGraphClauseList.add(MapstructUtils.convert(kGraphClauseBo, KGraphClause.class));
        });
        return this.saveBatch(kGraphClauseList,100);
    }

    /**
     * 新增知识图谱分句结果，前台提供主键值，一般用于导入的场合
     *
     * @param kGraphClauseBo 知识图谱分句结果Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(KGraphClauseBo kGraphClauseBo) {
        KGraphClause kGraphClause = MapstructUtils.convert(kGraphClauseBo, KGraphClause.class);
        //前台传来主键值
        return kGraphClauseMapper.insertWithPk(kGraphClause) > 0;
    }

    /**
     * 修改知识图谱分句结果
     *
     * @param kGraphClauseBo 知识图谱分句结果Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(KGraphClauseBo kGraphClauseBo) {
        KGraphClause kGraphClause = MapstructUtils.convert(kGraphClauseBo, KGraphClause.class);
        if (ObjectUtil.isNotNull(kGraphClause) && ObjectUtil.isNotNull(kGraphClause.getId())) {
            return this.updateById(kGraphClause);
        }
        return false;
    }

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

}
