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.AlgorithmConfigMapper;
import com.taais.biz.domain.AlgorithmConfig;
import com.taais.biz.domain.bo.AlgorithmConfigBo;
import com.taais.biz.domain.vo.AlgorithmConfigVo;
import com.taais.biz.service.IAlgorithmConfigService;
import static com.taais.biz.domain.table.AlgorithmConfigTableDef.ALGORITHM_CONFIG;

/**
 * 算法配置Service业务层处理
 *
 * @author 0
 * 2024-05-28
 */
@Service
public class AlgorithmConfigServiceImpl extends BaseServiceImpl<AlgorithmConfigMapper, AlgorithmConfig> implements IAlgorithmConfigService {
    @Resource
    private AlgorithmConfigMapper algorithmConfigMapper;

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

    private QueryWrapper buildQueryWrapper(AlgorithmConfigBo algorithmConfigBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(ALGORITHM_CONFIG.TYPE.eq
        (algorithmConfigBo.getType()));
        queryWrapper.and(ALGORITHM_CONFIG.SUBSYSTEM.eq
        (algorithmConfigBo.getSubsystem()));
        queryWrapper.and(ALGORITHM_CONFIG.ALGORITHM_NAME.like
        (algorithmConfigBo.getAlgorithmName()));
        queryWrapper.and(ALGORITHM_CONFIG.ALGORITHM_ADDRESS.eq
        (algorithmConfigBo.getAlgorithmAddress()));

        return queryWrapper;
    }

    /**
     * 查询算法配置
     *
     * @param id 算法配置主键
     * @return 算法配置
     */
    @Override
    public AlgorithmConfigVo selectById(Long id) {
            return this.getOneAs(query().where(ALGORITHM_CONFIG.ID.eq(id)), AlgorithmConfigVo.class);

    }

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

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

    /**
     * 新增算法配置
     *
     * @param algorithmConfigBo 算法配置Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(AlgorithmConfigBo algorithmConfigBo) {
    AlgorithmConfig algorithmConfig =MapstructUtils.convert(algorithmConfigBo, AlgorithmConfig. class);

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

    /**
     * 修改算法配置
     *
     * @param algorithmConfigBo 算法配置Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(AlgorithmConfigBo algorithmConfigBo) {
        AlgorithmConfig algorithmConfig =MapstructUtils.convert(algorithmConfigBo, AlgorithmConfig. class);
        if (ObjectUtil.isNotNull(algorithmConfig) && ObjectUtil.isNotNull(algorithmConfig.getId())){
            boolean updated = this.updateById(algorithmConfig);
                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 getOption() {
        return mapper.getOption();
    }

}
