/*
*  Copyright 2019-2025 Tz
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.modules.system.domain.BusUser;
import me.zhengjie.modules.system.domain.BusUserCommand;
import me.zhengjie.modules.system.domain.BusUserDevice;
import me.zhengjie.modules.system.domain.dto.BusTreeVo;
import me.zhengjie.modules.system.mapper.BusUserCommandMapper;
import me.zhengjie.modules.system.mapper.BusUserDeviceMapper;
import me.zhengjie.modules.system.service.BusUserDeviceService;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @description 服务实现
* @author tz
* @date 2025-05-23
**/
@Service
@RequiredArgsConstructor
public class BusUserDeviceServiceImpl extends ServiceImpl<BusUserDeviceMapper, BusUserDevice> implements BusUserDeviceService {

    private final BusUserDeviceMapper busUserDeviceMapper;
    private final BusUserCommandMapper busUserCommandMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updUserCommands(BusUser busUser) {
        List<BusTreeVo> commands = busUser.getCommands();
        if (null == commands || commands.isEmpty()) { return; }
        List<BusTreeVo> userDevices = commands.stream().filter(i -> i.getId().contains("d")).collect(Collectors.toList());
        List<BusTreeVo> userCommands = commands.stream().filter(i -> i.getId().contains("c")).collect(Collectors.toList());
        List<BusUserDevice> oldUserDevices = busUserDeviceMapper.selectList(new LambdaQueryWrapper<BusUserDevice>().eq(BusUserDevice::getUserId, busUser.getId()));
        List<BusUserCommand> oldUserCommands = busUserCommandMapper.selectList(new LambdaQueryWrapper<BusUserCommand>().eq(BusUserCommand::getUserId, busUser.getId()));
        if (!oldUserDevices.isEmpty()) {
            List<Long> delIds = oldUserDevices.stream().map(i -> i.getId()).collect(Collectors.toList());
            busUserDeviceMapper.deleteBatchIds(delIds);
        }
        if (!oldUserCommands.isEmpty()) {
            List<Long> delIds = oldUserCommands.stream().map(i -> i.getId()).collect(Collectors.toList());
            busUserCommandMapper.deleteBatchIds(delIds);
        }
        if (!userDevices.isEmpty()) {
            List<Long> deviceIds = userDevices.stream().map(i -> Long.parseLong(i.getId().replace("d", ""))).collect(Collectors.toList());
            busUserDeviceMapper.bindingDevice(busUser.getId(), deviceIds);
        }
        if (!userCommands.isEmpty()) {
            List<Long> commandIds = userCommands.stream().map(i -> Long.parseLong(i.getId().replace("c", ""))).collect(Collectors.toList());
            busUserCommandMapper.bindingCommand(busUser.getId(), commandIds);
        }
    }
}