package com.taais.biz.service.impl;

import java.util.ArrayList;
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.TargetIdentificationSubtaskDetailsMapper;
import com.taais.biz.domain.TargetIdentificationSubtaskDetails;
import com.taais.biz.domain.bo.TargetIdentificationSubtaskDetailsBo;
import com.taais.biz.domain.vo.TargetIdentificationSubtaskDetailsVo;
import com.taais.biz.service.ITargetIdentificationSubtaskDetailsService;
import static com.taais.biz.domain.table.TargetIdentificationSubtaskDetailsTableDef.TARGET_IDENTIFICATION_SUBTASK_DETAILS;

/**
 * 目标识别子任务Service业务层处理
 *
 * @author 0
 * 2024-08-17
 */
@Service
public class TargetIdentificationSubtaskDetailsServiceImpl extends BaseServiceImpl<TargetIdentificationSubtaskDetailsMapper, TargetIdentificationSubtaskDetails> implements ITargetIdentificationSubtaskDetailsService {
    @Resource
    private TargetIdentificationSubtaskDetailsMapper targetIdentificationSubtaskDetailsMapper;


    public List<TargetIdentificationSubtaskDetailsBo> getTargetIdentificationSubtaskDetailsListBySubtaskId(Long subtaskId) {
        return targetIdentificationSubtaskDetailsMapper.getTargetIdentificationSubtaskDetailsListBySubtaskId(subtaskId);
    }

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

    private QueryWrapper buildQueryWrapper(TargetIdentificationSubtaskDetailsBo targetIdentificationSubtaskDetailsBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(TARGET_IDENTIFICATION_SUBTASK_DETAILS.NAME.like
        (targetIdentificationSubtaskDetailsBo.getName()));
        queryWrapper.and(TARGET_IDENTIFICATION_SUBTASK_DETAILS.SUBTASK_ID.eq
            (targetIdentificationSubtaskDetailsBo.getSubtaskId()));
        queryWrapper.and(TARGET_IDENTIFICATION_SUBTASK_DETAILS.STATUS.eq
        (targetIdentificationSubtaskDetailsBo.getStatus()));
        queryWrapper.and(TARGET_IDENTIFICATION_SUBTASK_DETAILS.ALGORITHM_ID.eq
        (targetIdentificationSubtaskDetailsBo.getAlgorithmId()));
        queryWrapper.and(TARGET_IDENTIFICATION_SUBTASK_DETAILS.DATA_BATCH_NUMS.eq
        (targetIdentificationSubtaskDetailsBo.getDataBatchNums()));
        queryWrapper.and(TARGET_IDENTIFICATION_SUBTASK_DETAILS.PARAMETERS.eq
        (targetIdentificationSubtaskDetailsBo.getParameters()));
        queryWrapper.and(TARGET_IDENTIFICATION_SUBTASK_DETAILS.PREPROCESS_PATH.eq
        (targetIdentificationSubtaskDetailsBo.getPreprocessPath()));
        queryWrapper.and(TARGET_IDENTIFICATION_SUBTASK_DETAILS.RESULT_PATH.eq
        (targetIdentificationSubtaskDetailsBo.getResultPath()));
        queryWrapper.and(TARGET_IDENTIFICATION_SUBTASK_DETAILS.INDEX.eq
        (targetIdentificationSubtaskDetailsBo.getIndex()));
        queryWrapper.and(TARGET_IDENTIFICATION_SUBTASK_DETAILS.START_TIME.eq
        (targetIdentificationSubtaskDetailsBo.getStartTime()));
        queryWrapper.and(TARGET_IDENTIFICATION_SUBTASK_DETAILS.END_TIME.eq
        (targetIdentificationSubtaskDetailsBo.getEndTime()));
        queryWrapper.and(TARGET_IDENTIFICATION_SUBTASK_DETAILS.COST_SECOND.eq
        (targetIdentificationSubtaskDetailsBo.getCostSecond()));
        queryWrapper.and(TARGET_IDENTIFICATION_SUBTASK_DETAILS.LOG.eq
        (targetIdentificationSubtaskDetailsBo.getLog()));
        queryWrapper.and(TARGET_IDENTIFICATION_SUBTASK_DETAILS.REMARKS.eq
        (targetIdentificationSubtaskDetailsBo.getRemarks()));
        return queryWrapper;
    }

    /**
     * 查询目标识别子任务
     *
     * @param id 目标识别子任务主键
     * @return 目标识别子任务
     */
    @Override
    public TargetIdentificationSubtaskDetailsVo selectById(Long id) {
            return this.getOneAs(query().where(TARGET_IDENTIFICATION_SUBTASK_DETAILS.ID.eq(id)), TargetIdentificationSubtaskDetailsVo.class);

    }

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

    /**
     * 查询目标识别子任务列表
     *
     * @param targetIdentificationSubtaskDetailsBo 目标识别子任务Bo
     * @return 目标识别子任务集合
     */
    @Override
    public List<TargetIdentificationSubtaskDetailsVo> selectList(TargetIdentificationSubtaskDetailsBo targetIdentificationSubtaskDetailsBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(targetIdentificationSubtaskDetailsBo);
            return this.listAs(queryWrapper, TargetIdentificationSubtaskDetailsVo.class);
    }

    /**
     * 分页查询目标识别子任务列表
     *
     * @param targetIdentificationSubtaskDetailsBo 目标识别子任务Bo
     * @return 分页目标识别子任务集合
     */
    @Override
    public PageResult<TargetIdentificationSubtaskDetailsVo> selectPage(TargetIdentificationSubtaskDetailsBo targetIdentificationSubtaskDetailsBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(targetIdentificationSubtaskDetailsBo);
            Page<TargetIdentificationSubtaskDetailsVo> page = this.pageAs(PageQuery.build(), queryWrapper, TargetIdentificationSubtaskDetailsVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增目标识别子任务
     *
     * @param targetIdentificationSubtaskDetailsBo 目标识别子任务Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(TargetIdentificationSubtaskDetailsBo targetIdentificationSubtaskDetailsBo) {
        TargetIdentificationSubtaskDetails targetIdentificationSubtaskDetails =MapstructUtils.convert(targetIdentificationSubtaskDetailsBo, TargetIdentificationSubtaskDetails. class);
        targetIdentificationSubtaskDetails.setSubtaskId(targetIdentificationSubtaskDetailsBo.getSubtaskId());
        targetIdentificationSubtaskDetails.setType(targetIdentificationSubtaskDetailsBo.getType());
        targetIdentificationSubtaskDetails.setIndex(targetIdentificationSubtaskDetailsBo.getIndex());
        System.out.println("convert subdetail: " + targetIdentificationSubtaskDetails);
        System.out.println("convert before subdetail: " + targetIdentificationSubtaskDetailsBo);
        return this.save(targetIdentificationSubtaskDetails);//使用全局配置的雪花算法主键生成器生成ID值
    }

    /**
     * 修改目标识别子任务
     *
     * @param targetIdentificationSubtaskDetailsBo 目标识别子任务Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(TargetIdentificationSubtaskDetailsBo targetIdentificationSubtaskDetailsBo) {
        TargetIdentificationSubtaskDetails targetIdentificationSubtaskDetails =MapstructUtils.convert(targetIdentificationSubtaskDetailsBo, TargetIdentificationSubtaskDetails. class);
        System.out.println("convert subdetail: " + targetIdentificationSubtaskDetails);
        if (ObjectUtil.isNotNull(targetIdentificationSubtaskDetails) && ObjectUtil.isNotNull(targetIdentificationSubtaskDetails.getId())){
            boolean updated = this.updateById(targetIdentificationSubtaskDetails);
                return updated;
        }
        return false;
    }

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

    @Override
    public List<TargetIdentificationSubtaskDetailsBo> getBySubtaskId(Long trainingTaskId) {
        return targetIdentificationSubtaskDetailsMapper.getTargetIdentificationSubtaskDetailsListBySubtaskId(trainingTaskId);
    }

    public Long getAvailableTask() {
        List<Long> list = null;
        list = targetIdentificationSubtaskDetailsMapper.getByTaskType("训练");
        if (!list.isEmpty()) {
            return list.get(0);
        }
        list = targetIdentificationSubtaskDetailsMapper.getByTaskType("验证");
        if (!list.isEmpty()) {
            return list.get(0);
        }
        list = targetIdentificationSubtaskDetailsMapper.getByTaskType("测试");
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }
}
