package com.imdroid.sideslope.sal;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imdroid.secapi.dto.GnssDevice;
import com.imdroid.secapi.dto.GnssDeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 描述
 *
 * @author LiGang
 */
@Component(value = "db")
public class DbDeviceServiceImpl implements DeviceService {

    @Autowired
    private GnssDeviceMapper gnssDeviceRepository;

    @Override
    public Device findByDeviceId(String deviceId) {
        GnssDevice gnssDevice = gnssDeviceRepository.queryByDeviceId(deviceId);
        if (gnssDevice == null) {
            return null;
        }
        Device device = new Device();
        device.setTenantId(gnssDevice.getTenantid());
        device.setDeviceId(gnssDevice.getDeviceid());
        device.setDeviceType(gnssDevice.getDevicetype());
        device.setParentId(gnssDevice.getParentid());
        device.setName(gnssDevice.getName());
        device.setProjectId(gnssDevice.getProject_id());
        device.setCalcGroupId(gnssDevice.getCalc_group_id());
        device.setOpMode(gnssDevice.getOpmode());
        device.setFwdId(gnssDevice.getFwd_group_id());
        device.setIPose(gnssDevice.getIpose());
        device.setIPosn(gnssDevice.getIposn());
        device.setIPosd(gnssDevice.getIposd());
        return device;
    }

    @Override
    public List<Device> findByParentId(String parentId) {
        QueryWrapper<GnssDevice> query = new QueryWrapper();
        query.eq("parentid", parentId);
        List<GnssDevice> gnssDeviceList = gnssDeviceRepository.selectList(query);
        List<Device> deviceList = new ArrayList<>(gnssDeviceList.size());
        for (GnssDevice gnssDevice : gnssDeviceList) {
            Device device = new Device();
            device.setDeviceId(gnssDevice.getDeviceid());
            device.setDeviceType(gnssDevice.getDevicetype());
            device.setParentId(gnssDevice.getParentid());
            device.setName(gnssDevice.getName());
            device.setProjectId(gnssDevice.getProject_id());
            device.setCalcGroupId(gnssDevice.getCalc_group_id());
            device.setOpMode(gnssDevice.getOpmode());
            device.setFwdId(gnssDevice.getFwd_group_id());
            device.setIPose(gnssDevice.getIpose());
            device.setIPosn(gnssDevice.getIposn());
            device.setIPosd(gnssDevice.getIposd());
            deviceList.add(device);
        }
        return deviceList;
    }

}
