package com.supervision.web.peopleGateManage.Service.impl;

import com.supervision.web.peopleGateManage.mapper.PeopleDeviceMapper;
import com.supervision.web.peopleGateManage.Service.PeopleDeviceService;
import com.supervision.web.peopleGateManage.enity.PeopleDevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PeopleDeviceServiceImpl implements PeopleDeviceService {

    @Autowired
    private PeopleDeviceMapper peopleDeviceMapper;

    @Override
    public void bindPersonToDevice(String personId, Long deviceId) {
        PeopleDevice pd = new PeopleDevice();
        pd.setPersonId(personId);
        pd.setDeviceId(deviceId);
        pd.setCreateTime(LocalDateTime.now());
        pd.setUpdateTime(LocalDateTime.now());
        peopleDeviceMapper.insert(pd);
    }

    @Override
    public void bindPersonToDevices(String personId, List<Long> deviceIds) {
        for (Long deviceId : deviceIds) {
            bindPersonToDevice(personId, deviceId);
        }
    }

    @Override
    public void unbindPersonFromDevice(String personId, Long deviceId) {
        peopleDeviceMapper.delete(personId, deviceId);
    }

    @Override
    public List<Long> getDevicesByPerson(String personId) {
        return peopleDeviceMapper.selectDevicesByPerson(personId);
    }

    @Override
    public List<String> getPersonsByDevice(Long deviceId) {
        return peopleDeviceMapper.selectPersonsByDevice(deviceId);
    }


    public void addRelation(String personId, Long deviceId) {
        PeopleDevice relation = new PeopleDevice();
        relation.setPersonId(personId);
        relation.setDeviceId(deviceId);
        relation.setCreateTime(LocalDateTime.now());
        relation.setUpdateTime(LocalDateTime.now());
        peopleDeviceMapper.insertRelation(relation);
    }


}
