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.AlgorithmBizProcess;
import com.taais.common.core.config.TaaisConfig;
import com.taais.common.core.utils.MapstructUtils;
import com.taais.common.core.utils.file.FileUtils;
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.AlgorithmModelMapper;
import com.taais.biz.domain.AlgorithmModel;
import com.taais.biz.domain.bo.AlgorithmModelBo;
import com.taais.biz.domain.vo.AlgorithmModelVo;
import com.taais.biz.service.IAlgorithmModelService;
import static com.taais.biz.domain.table.AlgorithmModelTableDef.ALGORITHM_MODEL;

/**
 * 算法模型配置Service业务层处理
 *
 * @author 0
 * 2024-05-29
 */
@Service
public class AlgorithmModelServiceImpl extends BaseServiceImpl<AlgorithmModelMapper, AlgorithmModel> implements IAlgorithmModelService {
    @Resource
    private AlgorithmModelMapper algorithmModelMapper;

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

    private QueryWrapper buildQueryWrapper(AlgorithmModelBo algorithmModelBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(ALGORITHM_MODEL.ALGORITHM_ID.eq
        (algorithmModelBo.getAlgorithmId()));
        queryWrapper.and(ALGORITHM_MODEL.MODEL_NAME.like
        (algorithmModelBo.getModelName()));
        queryWrapper.and(ALGORITHM_MODEL.SAMPLE_NUMBER.eq
        (algorithmModelBo.getSampleNumber()));
        queryWrapper.and(ALGORITHM_MODEL.CYCLE_EPOCH.eq
        (algorithmModelBo.getCycleEpoch()));
        queryWrapper.and(ALGORITHM_MODEL.REMARKS.eq
        (algorithmModelBo.getRemarks()));

        return queryWrapper;
    }

    /**
     * 查询算法模型配置
     *
     * @param id 算法模型配置主键
     * @return 算法模型配置
     */
    @Override
    public AlgorithmModelVo selectById(Long id) {
            return mapper.getModelById(id);
    }

    /**
     * 查询算法模型配置列表
     *
     * @param algorithmModelBo 算法模型配置Bo
     * @return 算法模型配置集合
     */
    @Override
    public List<AlgorithmModelVo> selectList(AlgorithmModelBo algorithmModelBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(algorithmModelBo);
            return this.listAs(queryWrapper, AlgorithmModelVo.class);
    }

    /**
     * 分页查询算法模型配置列表
     *
     * @param algorithmModelBo 算法模型配置Bo
     * @return 分页算法模型配置集合
     */
    @Override
    public PageResult<AlgorithmModelVo> selectPage(AlgorithmModelBo algorithmModelBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(algorithmModelBo);
            Page<AlgorithmModelVo> page = this.pageAs(PageQuery.build(), queryWrapper, AlgorithmModelVo.class);
        return PageResult.build(page);
    }

    public List<AlgorithmModelVo> getModelByAlgorithmId(Long algorithmId) {
        return algorithmModelMapper.getModelByAlgorithmId(algorithmId);
    }

    /**
     * 新增算法模型配置
     *
     * @param algorithmModelBo 算法模型配置Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(AlgorithmModelBo algorithmModelBo) {
    AlgorithmModel algorithmModel =MapstructUtils.convert(algorithmModelBo, AlgorithmModel. class);

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

    /**
     * 修改算法模型配置
     *
     * @param algorithmModelBo 算法模型配置Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(AlgorithmModelBo algorithmModelBo) {
        AlgorithmModel algorithmModel =MapstructUtils.convert(algorithmModelBo, AlgorithmModel. class);
        if (ObjectUtil.isNotNull(algorithmModel) && ObjectUtil.isNotNull(algorithmModel.getId())){
            boolean updated = this.updateById(algorithmModel);
                return updated;
        }
        return false;
    }

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

    @Override
    public void createModelByBizProcess(AlgorithmBizProcess algorithmBizProcess) {
        AlgorithmModel algorithmModel = new AlgorithmModel();
        algorithmModel.setAlgorithmId(algorithmBizProcess.getAlgorithmId());
        // 获取算法任务名称和算法名称
        String modelName = mapper.getModelNameBySubtaskId(algorithmBizProcess.getSubTaskId(), algorithmBizProcess.getAlgorithmId());
        algorithmModel.setModelName(modelName);
        // todo allen 待添加模型地址
        algorithmModel.setModelAddress("");
        String preprocessPath = algorithmBizProcess.getPreprocessPath();
        Long numberOfFileByFolderPath = FileUtils.getNumberOfFileByFolderPath(TaaisConfig.getProfile() + preprocessPath + BizConstant.IMAGE);
        algorithmModel.setSampleNumber(numberOfFileByFolderPath);
        mapper.insert(algorithmModel);
    }
}
