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.constant.BizConstant;
import com.taais.biz.domain.bo.AlgorithmTaskConfigurationParam;
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.AlgorithmTaskConfigurationMapper;
import com.taais.biz.domain.AlgorithmTaskConfiguration;
import com.taais.biz.domain.bo.AlgorithmTaskConfigurationBo;
import com.taais.biz.domain.vo.AlgorithmTaskConfigurationVo;
import com.taais.biz.service.IAlgorithmTaskConfigurationService;
import static com.taais.biz.domain.table.AlgorithmTaskConfigurationTableDef.ALGORITHM_TASK_CONFIGURATION;

/**
 * 算法任务Service业务层处理
 *
 * @author 0
 * 2024-08-23
 */
@Service
public class AlgorithmTaskConfigurationServiceImpl extends BaseServiceImpl<AlgorithmTaskConfigurationMapper, AlgorithmTaskConfiguration> implements IAlgorithmTaskConfigurationService {
    @Resource
    private AlgorithmTaskConfigurationMapper algorithmTaskConfigurationMapper;

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

    private QueryWrapper buildQueryWrapper(AlgorithmTaskConfigurationBo algorithmTaskConfigurationBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(ALGORITHM_TASK_CONFIGURATION.NAME.like
        (algorithmTaskConfigurationBo.getName()));
        queryWrapper.and(ALGORITHM_TASK_CONFIGURATION.TRAIN_URL.eq
        (algorithmTaskConfigurationBo.getTrainUrl()));
        queryWrapper.and(ALGORITHM_TASK_CONFIGURATION.TRAIN_PARAMS.eq
        (algorithmTaskConfigurationBo.getTrainParams()));
        queryWrapper.and(ALGORITHM_TASK_CONFIGURATION.VERIFY_URL.eq
        (algorithmTaskConfigurationBo.getVerifyUrl()));
        queryWrapper.and(ALGORITHM_TASK_CONFIGURATION.VERIFY_PARAMS.eq
        (algorithmTaskConfigurationBo.getVerifyParams()));
        queryWrapper.and(ALGORITHM_TASK_CONFIGURATION.TEST_URL.eq
        (algorithmTaskConfigurationBo.getTestUrl()));
        queryWrapper.and(ALGORITHM_TASK_CONFIGURATION.TEST_PARAMS.eq
        (algorithmTaskConfigurationBo.getTestParams()));

        return queryWrapper;
    }

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

    }

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

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

    /**
     * 新增算法任务
     *
     * @param algorithmTaskConfigurationBo 算法任务Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(AlgorithmTaskConfigurationBo algorithmTaskConfigurationBo) {
    AlgorithmTaskConfiguration algorithmTaskConfiguration =MapstructUtils.convert(algorithmTaskConfigurationBo, AlgorithmTaskConfiguration. class);

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

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

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

    @Override
    public boolean insertExistedModel(AlgorithmTaskConfigurationParam param) {
        //int res = algorithmTaskConfigurationMapper.insertExistedModel(param);
        AlgorithmTaskConfiguration bo = new AlgorithmTaskConfiguration();
        bo.setName(param.getModelName());
        bo.setTestUrl(param.getTestUrl());
        bo.setTestParams(param.getTestParams());
        bo.setTrainUrl(param.getTrainUrl());
        bo.setTrainParams(param.getTrainParams());
        bo.setVerifyParams(param.getValParams());
        bo.setVerifyUrl(param.getValUrl());
        bo.setRemark(BizConstant.REMARK_PREFIX + param.getModelAddress());
        return this.save(bo);
    }
}
