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.AlgorithmSubtaskDetailMapper;
import com.taais.biz.domain.AlgorithmSubtaskDetail;
import com.taais.biz.domain.bo.AlgorithmSubtaskDetailBo;
import com.taais.biz.domain.vo.AlgorithmSubtaskDetailVo;
import com.taais.biz.service.IAlgorithmSubtaskDetailService;
import static com.taais.biz.domain.table.AlgorithmSubtaskDetailTableDef.ALGORITHM_SUBTASK_DETAIL;

/**
 * 算法子任务详情Service业务层处理
 *
 * @author 0
 * 2024-05-29
 */
@Service
public class AlgorithmSubtaskDetailServiceImpl extends BaseServiceImpl<AlgorithmSubtaskDetailMapper, AlgorithmSubtaskDetail> implements IAlgorithmSubtaskDetailService {
    @Resource
    private AlgorithmSubtaskDetailMapper algorithmSubtaskDetailMapper;

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

    private QueryWrapper buildQueryWrapper(AlgorithmSubtaskDetailBo algorithmSubtaskDetailBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(ALGORITHM_SUBTASK_DETAIL.NAME.like
        (algorithmSubtaskDetailBo.getName()));
        queryWrapper.and(ALGORITHM_SUBTASK_DETAIL.STATUS.eq
        (algorithmSubtaskDetailBo.getStatus()));

        return queryWrapper;
    }

    /**
     * 查询算法子任务详情
     *
     * @param id 算法子任务详情主键
     * @return 算法子任务详情
     */
    @Override
    public AlgorithmSubtaskDetailVo selectById(Long id) {
            return this.getOneAs(query().where(ALGORITHM_SUBTASK_DETAIL.ID.eq(id)), AlgorithmSubtaskDetailVo.class);

    }

    /**
     * 查询算法子任务详情列表
     *
     * @param algorithmSubtaskDetailBo 算法子任务详情Bo
     * @return 算法子任务详情集合
     */
    @Override
    public List<AlgorithmSubtaskDetailVo> selectList(AlgorithmSubtaskDetailBo algorithmSubtaskDetailBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(algorithmSubtaskDetailBo);
            return this.listAs(queryWrapper, AlgorithmSubtaskDetailVo.class);
    }

    /**
     * 分页查询算法子任务详情列表
     *
     * @param algorithmSubtaskDetailBo 算法子任务详情Bo
     * @return 分页算法子任务详情集合
     */
    @Override
    public PageResult<AlgorithmSubtaskDetailVo> selectPage(AlgorithmSubtaskDetailBo algorithmSubtaskDetailBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(algorithmSubtaskDetailBo);
            Page<AlgorithmSubtaskDetailVo> page = this.pageAs(PageQuery.build(), queryWrapper, AlgorithmSubtaskDetailVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增算法子任务详情
     *
     * @param algorithmSubtaskDetailBo 算法子任务详情Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(AlgorithmSubtaskDetailBo algorithmSubtaskDetailBo) {
    AlgorithmSubtaskDetail algorithmSubtaskDetail =MapstructUtils.convert(algorithmSubtaskDetailBo, AlgorithmSubtaskDetail. class);

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

    /**
     * 修改算法子任务详情
     *
     * @param algorithmSubtaskDetailBo 算法子任务详情Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(AlgorithmSubtaskDetailBo algorithmSubtaskDetailBo) {
        AlgorithmSubtaskDetail algorithmSubtaskDetail =MapstructUtils.convert(algorithmSubtaskDetailBo, AlgorithmSubtaskDetail. class);
        if (ObjectUtil.isNotNull(algorithmSubtaskDetail) && ObjectUtil.isNotNull(algorithmSubtaskDetail.getId())){
            boolean updated = this.updateById(algorithmSubtaskDetail);
                return updated;
        }
        return false;
    }

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

}
