package com.kym.service.impl;

import com.kym.entity.RechargeConfigStationGroup;
import com.kym.mapper.RechargeConfigStationGroupMapper;
import com.kym.service.RechargeConfigStationGroupService;
import com.kym.service.mybatisplus.MyBaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 充值配置分组-站点关联服务实现
 *
 * @author skyline
 * @since 2026-05-26
 */
@Service
public class RechargeConfigStationGroupServiceImpl
        extends MyBaseServiceImpl<RechargeConfigStationGroupMapper, RechargeConfigStationGroup>
        implements RechargeConfigStationGroupService {

    @Override
    public Long getGroupIdByStationId(String stationId) {
        var record = lambdaQuery().eq(RechargeConfigStationGroup::getStationId, stationId).one();
        return record != null ? record.getGroupId() : null;
    }

    @Override
    public List<String> listStationIdsByGroupId(Long groupId) {
        return lambdaQuery()
                .eq(RechargeConfigStationGroup::getGroupId, groupId)
                .list()
                .stream()
                .map(RechargeConfigStationGroup::getStationId)
                .toList();
    }

    @Override
    public void assignStationToGroup(String stationId, Long groupId) {
        // 一个站点只能属于一个分组，先删除旧关联
        lambdaUpdate()
                .eq(RechargeConfigStationGroup::getStationId, stationId)
                .remove();
        var record = new RechargeConfigStationGroup();
        record.setStationId(stationId);
        record.setGroupId(groupId);
        save(record);
    }

    @Override
    public void removeStationFromGroup(String stationId, Long groupId) {
        lambdaUpdate()
                .eq(RechargeConfigStationGroup::getStationId, stationId)
                .eq(RechargeConfigStationGroup::getGroupId, groupId)
                .remove();
    }
}
