package com.taais.biz.service.service.impl;

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

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.taais.biz.domain.ObjectMatch;
import com.taais.biz.domain.bo.ObjectMatchBo;
import com.taais.biz.domain.bo.ObjectTraceMergeBo;
import com.taais.biz.domain.vo.ObjectMatchVo;
import com.taais.biz.mapper.mapper.ObjectMatchMapper;
import com.taais.biz.service.service.IObjectMatchService;
import com.taais.common.core.utils.MapstructUtils;
import com.taais.common.orm.core.page.PageQuery;
import com.taais.common.core.core.page.PageResult;
import com.taais.common.orm.core.service.impl.BaseServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.taais.biz.domain.table.ObjectMatchTableDef.OBJECT_MATCH;
import static com.taais.biz.domain.table.ObjectTraceMergeTableDef.OBJECT_TRACE_MERGE;


/**
 * 异源图像匹配Service业务层处理
 *
 * @author km
 * 2024-10-28
 */
@Service
public class ObjectMatchServiceImpl extends BaseServiceImpl<ObjectMatchMapper, ObjectMatch> implements IObjectMatchService {
    @Resource
    private ObjectMatchMapper objectMatchMapper;

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

    private QueryWrapper buildQueryWrapper(ObjectMatchBo objectMatchBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(OBJECT_MATCH.NAME.like
        (objectMatchBo.getName()));
        queryWrapper.and(OBJECT_MATCH.STATUS.eq
        (objectMatchBo.getStatus()));
        queryWrapper.and(OBJECT_MATCH.PARAMETERS.eq
        (objectMatchBo.getParameters()));
        queryWrapper.and(OBJECT_MATCH.PREPROCESS_PATH.eq
        (objectMatchBo.getPreprocessPath()));
        queryWrapper.and(OBJECT_MATCH.RESULT_PATH.eq
        (objectMatchBo.getResultPath()));
        queryWrapper.and(OBJECT_MATCH.START_TIME.eq
        (objectMatchBo.getStartTime()));
        queryWrapper.and(OBJECT_MATCH.END_TIME.eq
        (objectMatchBo.getEndTime()));
        queryWrapper.and(OBJECT_MATCH.COST_SECOND.eq
        (objectMatchBo.getCostSecond()));
        queryWrapper.and(OBJECT_MATCH.REMARKS.eq
        (objectMatchBo.getRemarks()));

        return queryWrapper;
    }

    /**
     * 查询异源图像匹配
     *
     * @param id 异源图像匹配主键
     * @return 异源图像匹配
     */
    @Override
    public ObjectMatchVo selectById(Long id) {
            return this.getOneAs(query().where(OBJECT_MATCH.ID.eq(id)), ObjectMatchVo.class);

    }

    public ObjectMatchBo getById(Long id) {
        return this.getOneAs(query().where(OBJECT_MATCH.ID.eq(id)), ObjectMatchBo.class);
    }

    /**
     * 查询异源图像匹配列表
     *
     * @param objectMatchBo 异源图像匹配Bo
     * @return 异源图像匹配集合
     */
    @Override
    public List<ObjectMatchVo> selectList(ObjectMatchBo objectMatchBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(objectMatchBo);
            return this.listAs(queryWrapper, ObjectMatchVo.class);
    }

    /**
     * 分页查询异源图像匹配列表
     *
     * @param objectMatchBo 异源图像匹配Bo
     * @return 分页异源图像匹配集合
     */
    @Override
    public PageResult<ObjectMatchVo> selectPage(ObjectMatchBo objectMatchBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(objectMatchBo);
            Page<ObjectMatchVo> page = this.pageAs(PageQuery.build(), queryWrapper, ObjectMatchVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增异源图像匹配
     *
     * @param objectMatchBo 异源图像匹配Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(ObjectMatchBo objectMatchBo) {
    ObjectMatch objectMatch =MapstructUtils.convert(objectMatchBo, ObjectMatch. class);

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

    /**
     * 修改异源图像匹配
     *
     * @param objectMatchBo 异源图像匹配Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(ObjectMatchBo objectMatchBo) {
        ObjectMatch objectMatch =MapstructUtils.convert(objectMatchBo, ObjectMatch. class);
        if (ObjectUtil.isNotNull(objectMatch) && ObjectUtil.isNotNull(objectMatch.getId())){
            boolean updated = this.updateById(objectMatch);
                return updated;
        }
        return false;
    }

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

}
