package com.taais.biz.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.ObjectTraceMerge;
import com.taais.biz.domain.bo.ObjectTraceMergeBo;
import com.taais.biz.domain.bo.TargetIdentificationSubtaskDetailsBo;
import com.taais.biz.domain.vo.ObjectTraceMergeVo;
import com.taais.biz.mapper.ObjectTraceMergeMapper;
import com.taais.biz.service.IObjectTraceMergeService;
import com.taais.common.core.utils.MapstructUtils;
import com.taais.common.core.utils.StringUtils;
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.ObjectTraceMergeTableDef.OBJECT_TRACE_MERGE;
import static com.taais.biz.domain.table.TargetIdentificationSubtaskDetailsTableDef.TARGET_IDENTIFICATION_SUBTASK_DETAILS;

/**
 * 多物体融合轨迹识别Service业务层处理
 *
 * @author km
 * 2024-10-16
 */
@Service
public class ObjectTraceMergeServiceImpl extends BaseServiceImpl<ObjectTraceMergeMapper, ObjectTraceMerge> implements IObjectTraceMergeService {
    @Resource
    private ObjectTraceMergeMapper objectTraceMergeMapper;

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

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

        return queryWrapper;
    }

    /**
     * 查询多物体融合轨迹识别
     *
     * @param id 多物体融合轨迹识别主键
     * @return 多物体融合轨迹识别
     */
    @Override
    public ObjectTraceMergeVo selectById(Long id) {
        return this.getOneAs(query().where(OBJECT_TRACE_MERGE.ID.eq(id)), ObjectTraceMergeVo.class);
    }

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

    /**
     * 查询多物体融合轨迹识别列表
     *
     * @param objectTraceMergeBo 多物体融合轨迹识别Bo
     * @return 多物体融合轨迹识别集合
     */
    @Override
    public List<ObjectTraceMergeVo> selectList(ObjectTraceMergeBo objectTraceMergeBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(objectTraceMergeBo);
            return this.listAs(queryWrapper, ObjectTraceMergeVo.class);
    }

    /**
     * 分页查询多物体融合轨迹识别列表
     *
     * @param objectTraceMergeBo 多物体融合轨迹识别Bo
     * @return 分页多物体融合轨迹识别集合
     */
    @Override
    public PageResult<ObjectTraceMergeVo> selectPage(ObjectTraceMergeBo objectTraceMergeBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(objectTraceMergeBo);
            Page<ObjectTraceMergeVo> page = this.pageAs(PageQuery.build(), queryWrapper, ObjectTraceMergeVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增多物体融合轨迹识别
     *
     * @param objectTraceMergeBo 多物体融合轨迹识别Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(ObjectTraceMergeBo objectTraceMergeBo) {
    ObjectTraceMerge objectTraceMerge =MapstructUtils.convert(objectTraceMergeBo, ObjectTraceMerge. class);

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

    /**
     * 修改多物体融合轨迹识别
     *
     * @param objectTraceMergeBo 多物体融合轨迹识别Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(ObjectTraceMergeBo objectTraceMergeBo) {
        ObjectTraceMerge objectTraceMerge =MapstructUtils.convert(objectTraceMergeBo, ObjectTraceMerge. class);
        if (ObjectUtil.isNotNull(objectTraceMerge) && ObjectUtil.isNotNull(objectTraceMerge.getId())){
            boolean updated = this.updateById(objectTraceMerge);
                return updated;
        }
        return false;
    }

    /**
     * 批量删除多物体融合轨迹识别
     *
     * @param ids 需要删除的多物体融合轨迹识别主键集合
     * @return 结果:true 删除成功，false 删除失败
     */
    @Transactional
    @Override
    public boolean deleteByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

}
