/**
 * 20241029 ©WANGKANG 1686617586@qq.com
 */


package com.taais.biz.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
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.AlgorithmConfigTrackMapper;
import com.taais.biz.domain.AlgorithmConfigTrack;
import com.taais.biz.domain.bo.AlgorithmConfigTrackBo;
import com.taais.biz.domain.vo.AlgorithmConfigTrackVo;
import com.taais.biz.service.IAlgorithmConfigTrackService;

import static com.taais.biz.domain.table.AlgorithmConfigTrackTableDef.ALGORITHM_CONFIG_TRACK;

/**
 * 算法配置Service业务层处理
 *
 * @author wangkang
 * 2024-09-26
 */
@Service
public class AlgorithmConfigTrackServiceImpl extends BaseServiceImpl<AlgorithmConfigTrackMapper, AlgorithmConfigTrack> implements IAlgorithmConfigTrackService {
    @Resource
    private AlgorithmConfigTrackMapper algorithmConfigTrackMapper;

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

    private QueryWrapper buildQueryWrapper(AlgorithmConfigTrackBo algorithmConfigTrackBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(ALGORITHM_CONFIG_TRACK.TYPE.eq(algorithmConfigTrackBo.getType()));
        queryWrapper.and(ALGORITHM_CONFIG_TRACK.PARENT_ID.eq(algorithmConfigTrackBo.getParentId()));
        queryWrapper.and(ALGORITHM_CONFIG_TRACK.SUBSYSTEM.eq(algorithmConfigTrackBo.getSubsystem()));
        queryWrapper.and(ALGORITHM_CONFIG_TRACK.ALGORITHM_NAME.like(algorithmConfigTrackBo.getAlgorithmName()));
        queryWrapper.and(ALGORITHM_CONFIG_TRACK.ALGORITHM_ADDRESS.eq(algorithmConfigTrackBo.getAlgorithmAddress()));
        queryWrapper.and(ALGORITHM_CONFIG_TRACK.PARAMETER_CONFIG.eq(algorithmConfigTrackBo.getParameterConfig()));
        queryWrapper.and(ALGORITHM_CONFIG_TRACK.REMARKS.eq(algorithmConfigTrackBo.getRemarks()));
        queryWrapper.and(ALGORITHM_CONFIG_TRACK.SYSTEM.eq(algorithmConfigTrackBo.getSystem()));

        return queryWrapper;
    }

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

    }

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

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

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

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

    /**
     * 修改算法配置
     *
     * @param algorithmConfigTrackBo 算法配置Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(AlgorithmConfigTrackBo algorithmConfigTrackBo) {
        AlgorithmConfigTrack algorithmConfigTrack = MapstructUtils.convert(algorithmConfigTrackBo, AlgorithmConfigTrack.class);
        if (ObjectUtil.isNotNull(algorithmConfigTrack) && ObjectUtil.isNotNull(algorithmConfigTrack.getId())) {
            boolean updated = this.updateById(algorithmConfigTrack);
            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<Object> getEnums() {
        algorithmConfigTrackMapper.selectAll();

        List<Object> result = new ArrayList<>();

        for (AlgorithmConfigTrack algorithmConfigTrack : algorithmConfigTrackMapper.selectAll()) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("label", algorithmConfigTrack.getAlgorithmName());
            map.put("value", algorithmConfigTrack.getId());
            map.put("type", algorithmConfigTrack.getType());
            map.put("subsystem", algorithmConfigTrack.getSubsystem());

            result.add(map);
        }

        return result;
    }

}
