package com.muyu.service.impl;

import com.muyu.domain.CarFence;
import com.muyu.domain.CarMiddle;
import com.muyu.domain.FenceAndGroupMiddle;
import com.muyu.domain.FenceGroup;
import com.muyu.domain.req.CarFenceAdd;
import com.muyu.mapper.CarFenceServiceMybaitsMapper;
import com.muyu.service.CarFenceServiceMybaits;
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
public class CarFenceServiceMybaitsImpl implements CarFenceServiceMybaits {
    @Autowired
    private CarFenceServiceMybaitsMapper carFenceServiceMybaitsMapper;


    /**
     * 添加多对多围栏组
     * @param fenceGroupId
     * @param carFenceId
     */
    @Override
    public void add(Integer fenceGroupId, List<Integer> carFenceId) {

        for (Integer integer : carFenceId) {
            Integer addMiddle = carFenceServiceMybaitsMapper.addGroup(fenceGroupId,integer);
        }



    }

    /**
     * 添加多对多围栏组
     * @param fenceGroupId
     * @param carFences
     */
    @Override
    public void addFenceGroup(Integer fenceGroupId, List<CarFenceAdd> carFences) {
        for (CarFenceAdd carFence : carFences) {
            Integer id = carFence.getId();
            Integer addMiddle = carFenceServiceMybaitsMapper.addFenceGroup(fenceGroupId,id);
        }
    }

    /**
     * 修改围栏组状态
     * @param groupId
     * @return
     */
    @Override
    public Object updateFenceGroupById(Integer groupId) {
        carFenceServiceMybaitsMapper.updateFenceGroupById(groupId);
        return null;
    }

    /**
     * 启动围栏
     */
    @Override
    public Object activate(Integer groupId) {
        carFenceServiceMybaitsMapper.activate(groupId);
        return null;
    }

    /**
     * 根据围栏组的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;
    }

    /**
     * 根据围栏组和车辆的id添加中间表
     */
    @Override
    public void addFenceGroupAddCarMiddle(Integer id, List<FenceGroup> fenceGroups) {

        //遍历集合
        for (FenceGroup fenceGroup : fenceGroups) {
            //获取围栏组的id
            Integer groupId = fenceGroup.getGroupId();
            carFenceServiceMybaitsMapper.addFenceGroupAddCarMiddle(id,groupId);
        }

    }

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

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