package com.imdroid.sideslope.service;

import com.imdroid.secapi.dto.GnssGroup;
import com.imdroid.secapi.dto.GnssGroupCalc;
import com.imdroid.secapi.dto.GnssGroupCalcMapper;
import com.imdroid.secapi.dto.GnssGroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class GroupParaService {
    @Autowired
    GnssGroupMapper basicGroupMapper;
    @Autowired
    GnssGroupCalcMapper calcGroupMapper;
    private ConcurrentHashMap<Integer, GnssGroup> basicGroupMap;
    private ConcurrentHashMap<Integer, GnssGroupCalc> calcGroupMap;

    public GnssGroup getBasicGroup(Integer groupId){
        if(basicGroupMap==null){
            refreshBasicGroup();
        }
        return basicGroupMap.get(groupId);
    }
    public GnssGroupCalc getCalcGroup(Integer groupId){
        if(calcGroupMap==null){
            refreshCalcGroup();
        }
        return calcGroupMap.get(groupId);
    }
    public void refreshGroup(){
        refreshBasicGroup();
        refreshCalcGroup();
    }
    void refreshBasicGroup(){
        if(basicGroupMap==null){
            basicGroupMap = new ConcurrentHashMap<>();
        }
        basicGroupMap.clear();
        List<GnssGroup> gnssGroupList = basicGroupMapper.selectList(null);
        for(GnssGroup group:gnssGroupList){
            basicGroupMap.put(group.getId(),group);
        }
    }
    void refreshCalcGroup(){
        if(calcGroupMap==null){
            calcGroupMap = new ConcurrentHashMap<>();
        }
        calcGroupMap.clear();
        List<GnssGroupCalc> gnssGroupList = calcGroupMapper.selectList(null);
        for(GnssGroupCalc group:gnssGroupList){
            calcGroupMap.put(group.getId(),group);
        }
    }
}
