package com.yfd.platform.modules.algorithm.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfd.platform.modules.algorithm.domain.AlgorithmClassComponent;
import com.yfd.platform.modules.algorithm.domain.AlgorithmDevice;
import com.yfd.platform.modules.algorithm.mapper.AlgorithmClassComponentMapper;
import com.yfd.platform.modules.algorithm.mapper.AlgorithmDeviceMapper;
import com.yfd.platform.modules.algorithm.service.IAlgorithmClassComponentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhengsl
 * @since 2025-05-13
 */
@Service
public class AlgorithmClassComponentServiceImpl extends ServiceImpl<AlgorithmClassComponentMapper,
        AlgorithmClassComponent> implements IAlgorithmClassComponentService {

    @Resource
    private AlgorithmDeviceMapper algorithmDeviceMapper;

    /**********************************
     * 用途说明: 算法关联主设备部件
     * 参数说明 algorithmId 算法分类id
     * 参数说明 page 分页参数
     * 返回值说明: com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.yfd.platform.modules.algorithm.domain
     * .AlgorithmClassComponent>
     ***********************************/
    @Override
    public Page<AlgorithmClassComponent> getAlgorithmClassComponentPage(String algorithmId, String stationCode,
                                                                        Page<AlgorithmClassComponent> page) {
        LambdaQueryWrapper<AlgorithmClassComponent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AlgorithmClassComponent::getAlgorithmId, algorithmId);
        queryWrapper.eq(AlgorithmClassComponent::getStationCode, stationCode);
        return this.page(page, queryWrapper);
    }

    /**********************************
     * 用途说明: 删除算法关联主设备部件
     * 参数说明 id 算法部件id
     * 返回值说明: boolean
     ***********************************/
    @Override
    public boolean deleteAlgorithmClassComponent(String id) {
        AlgorithmClassComponent algorithmClassComponent = this.getById(id);
        String algorithmId = algorithmClassComponent.getAlgorithmId();
        String componentId = algorithmClassComponent.getComponentId();
        if (StrUtil.isNotBlank(algorithmId) && StrUtil.isNotBlank(componentId)) {
            LambdaQueryWrapper<AlgorithmDevice> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AlgorithmDevice::getAlgorithmId, algorithmId).eq(AlgorithmDevice::getComponentId,
                    componentId);
            algorithmDeviceMapper.delete(queryWrapper);
        }

        return this.removeById(id);
    }

    @Override
    public boolean batchAddAlgorithmClassComponent(List<AlgorithmClassComponent> algorithmClassComponentList) {
        return this.saveOrUpdateBatch(algorithmClassComponentList);
    }
}
