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.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 com.taais.biz.mapper.AlgorithmDataSetMapper;
import com.taais.biz.domain.AlgorithmDataSet;
import com.taais.biz.domain.bo.AlgorithmDataSetBo;
import com.taais.biz.domain.vo.AlgorithmDataSetVo;
import com.taais.biz.service.IAlgorithmDataSetService;
import static com.taais.biz.domain.table.AlgorithmDataSetTableDef.ALGORITHM_DATA_SET;

/**
 * 算法数据集合Service业务层处理
 *
 * @author km
 * 2024-06-14
 */
@Service
public class AlgorithmDataSetServiceImpl extends BaseServiceImpl<AlgorithmDataSetMapper, AlgorithmDataSet> implements IAlgorithmDataSetService {
    @Resource
    private AlgorithmDataSetMapper algorithmDataSetMapper;

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

    private QueryWrapper buildQueryWrapper(AlgorithmDataSetBo algorithmDataSetBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(ALGORITHM_DATA_SET.SUB_TASK_ID.eq
        (algorithmDataSetBo.getSubTaskId()));
        queryWrapper.and(ALGORITHM_DATA_SET.NAME.like
        (algorithmDataSetBo.getName()));
        queryWrapper.and(ALGORITHM_DATA_SET.TYPE.eq
        (algorithmDataSetBo.getType()));
        queryWrapper.and(ALGORITHM_DATA_SET.STATUS.eq
        (algorithmDataSetBo.getStatus()));

        return queryWrapper;
    }

    /**
     * 查询算法数据集合
     *
     * @param id 算法数据集合主键
     * @return 算法数据集合
     */
    @Override
    public AlgorithmDataSetVo selectById(Long id) {
            return this.getOneAs(query().where(ALGORITHM_DATA_SET.ID.eq(id)), AlgorithmDataSetVo.class);

    }

    /**
     * 查询算法数据集合列表
     *
     * @param algorithmDataSetBo 算法数据集合Bo
     * @return 算法数据集合集合
     */
    @Override
    public List<AlgorithmDataSetVo> selectList(AlgorithmDataSetBo algorithmDataSetBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(algorithmDataSetBo);
            return this.listAs(queryWrapper, AlgorithmDataSetVo.class);
    }

    /**
     * 分页查询算法数据集合列表
     *
     * @param algorithmDataSetBo 算法数据集合Bo
     * @return 分页算法数据集合集合
     */
    @Override
    public PageResult<AlgorithmDataSetVo> selectPage(AlgorithmDataSetBo algorithmDataSetBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(algorithmDataSetBo);
            Page<AlgorithmDataSetVo> page = this.pageAs(PageQuery.build(), queryWrapper, AlgorithmDataSetVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增算法数据集合
     *
     * @param algorithmDataSetBo 算法数据集合Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(AlgorithmDataSetBo algorithmDataSetBo) {
    AlgorithmDataSet algorithmDataSet =MapstructUtils.convert(algorithmDataSetBo, AlgorithmDataSet. class);

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

    /**
     * 修改算法数据集合
     *
     * @param algorithmDataSetBo 算法数据集合Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(AlgorithmDataSetBo algorithmDataSetBo) {
        AlgorithmDataSet algorithmDataSet =MapstructUtils.convert(algorithmDataSetBo, AlgorithmDataSet. class);
        if (ObjectUtil.isNotNull(algorithmDataSet) && ObjectUtil.isNotNull(algorithmDataSet.getId())){
            boolean updated = this.updateById(algorithmDataSet);
                return updated;
        }
        return false;
    }

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

}
