package com.muyu.enterpise.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.domain.CarFence;
import com.muyu.domain.CarMiddle;
import com.muyu.domain.CarAndGroupMiddle;
import com.muyu.domain.FenceGroup;
import com.muyu.domain.req.CarFenceAdd;

import com.muyu.enterpise.mapper.CarFenceServiceMybaitsMapper;
import com.muyu.enterpise.service.CarFenceServiceMybaits;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 围栏组服务实现类
 *  * @Author：yan
 *  * @Package：com.muyu.car.service.impl
 *  * @Project：plues
 *  * @name：CarFenceServiceMybaitsImpl
 *  * @Date：2024/9/22 19:24
 */
@Service
@Log4j2
public class CarFenceServiceMybaitsImpl extends ServiceImpl<CarFenceServiceMybaitsMapper,CarMiddle> implements CarFenceServiceMybaits {
    @Autowired
    private CarFenceServiceMybaitsMapper carFenceServiceMybaitsMapper;




    /**
     * 添加多对多围栏组
     *
     * @param fenceGroupId
     * @param carFences
     * @return
     */
    @Override
    public boolean saveBatch(List<CarFenceAdd> carFences, Integer fenceGroupId) {
        List<CarMiddle> list = carFences.stream().map(carFenceAdd -> {
            CarMiddle carMiddle = new CarMiddle();
            carMiddle.setCarFenceId(carFenceAdd.getId());
            carMiddle.setCarGroupId(fenceGroupId);
            return carMiddle;
        }).toList();
        boolean b = this.saveBatch(list);
        return b;
    }


    /**
     * 根据围栏组的id查询绑定的围栏的中间表
     */
    @Override
    public List<CarFence> BoundFenceGroup(Integer groupId) {
        //根据id获取中间表的数据
        List<CarMiddle> list = carFenceServiceMybaitsMapper.BoundFenceGroup(groupId);
        ArrayList<CarFence> carFences = new ArrayList<>();
        for (CarMiddle carMiddle : list) {
            //获取中间表中的围栏id
            Integer carFenceId = carMiddle.getCarFenceId();
            //查询围栏组绑定的围栏列表
            carFences.add(carFenceServiceMybaitsMapper.selectBoundCarFenceList(carFenceId));
        }
        return carFences;
    }





    /**
     * 获取绑定的围栏组
     * @param carId
     */
    @Override
    public List<FenceGroup> selectBoundGFenceGroup(Integer carId) {
        /*根据id查询围栏组的id*/
        List<CarAndGroupMiddle> list =  carFenceServiceMybaitsMapper.selectBoundGFenceGroup(carId);
        ArrayList<FenceGroup> fenceGroups = new ArrayList<>();

        /*循环*/
        for (CarAndGroupMiddle fenceAndGroupMiddle : list) {
            /**
             * 获取围栏组的id
             */
            Long carGroupId = fenceAndGroupMiddle.getGroupId();
            //获取围栏组的数据 并 存入list
            fenceGroups.add(carFenceServiceMybaitsMapper.selectGroup(carGroupId));
        }
        return fenceGroups;
    }




}
