package com.its.vms.service;

import com.its.app.utils.Elapsed;
import com.its.vms.dao.mapper.VmsParkMapper;
import com.its.vms.dto.TbVmsParkDto;
import com.its.vms.entity.TbVmsPark;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
@RequiredArgsConstructor
public class VmsParkService {

    private final VmsParkMapper mapper;

    private final ConcurrentHashMap<Long, TbVmsParkDto> dataMap = new ConcurrentHashMap<>();

    public TbVmsParkDto find(Long prkPlceNmbr) {
        return this.dataMap.get(prkPlceNmbr);
    }

    public void loadDb() {
        loadVmsParkInfo();
    }

    public void loadVmsParkInfo() {
        Elapsed elapsed = new Elapsed();
        try {
            List<TbVmsPark> infoList  = this.mapper.selectVmsParkInfo();
            log.info("VmsParkService.loadVmsParkInfo: {} EA", infoList.size());
            infoList.forEach(data -> {
                TbVmsParkDto dto = data.toDto();
                if (dto.getVmsDsplNm() == null || "".equals(dto.getVmsDsplNm())) {
                    dto.setVmsDsplNm(dto.getPrkPlceNm());
                }
                dto.calcCngstStts();
                this.dataMap.put(dto.getPrkPlceNmbr(), dto);
            });
        }
        catch (Exception e) {
            log.error("VmsParkService.loadVmsParkInfo: {}.", e.toString());
        }
        log.info("VmsParkService.loadVmsParkInfo: {} ms.", elapsed.milliSeconds());
    }

}
