package com.its.wthr.process;

import com.its.app.AppUtils;
import com.its.app.utils.Elapsed;
import com.its.wthr.mapper.AtmpMapper;
import com.its.wthr.mapper.FrcsMapper;
import com.its.wthr.vo.AtmpPltnSttsVo;
import com.its.wthr.vo.FrcsZoneSttsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DbmsJobProcess extends AbstractDbmsJobProcess {

    private FrcsMapper frcsMapper;
    private AtmpMapper atmpMapper;

    @Override
    protected void postConstruct() {
        this.frcsMapper = (FrcsMapper) AppUtils.getBean(FrcsMapper.class);
        this.atmpMapper = (AtmpMapper) AppUtils.getBean(AtmpMapper.class);
    }

    @Override
    public void process(DbmsJobData data) {

        Elapsed elapsed = new Elapsed();

        int count = 0;
        String sType = "Unknown";
        int type = data.getType();
        //log.info("DbmsJobProcess.process: start. type: {}", data.getType());
//        int ii = 0;
        try {
            switch(type) {
                case DbmsJobType.DATA_TYPE_ATMP:
                    sType = "ATMP";
                    if (data.getData() == null) {
                        log.warn("DbmsJobProcess.process: ATMP data is NULL");
                        break;
                    }
                    List<AtmpPltnSttsVo> atmpPltnSttsList =  ((List<AtmpPltnSttsVo>)data.getData()).stream().collect(Collectors.toList());
                    count = atmpPltnSttsList.size();
//                    for (ii = 0; ii < count; ii++) {
//                        // java.util.ConcurrentModificationException
//                        this.atmpMapper.updateAtmpPltnStts(atmpPltnSttsList.get(ii));
//                    }
                    for (AtmpPltnSttsVo vo : atmpPltnSttsList) {
                        this.atmpMapper.updateAtmpPltnStts(vo);
                    }
                    atmpPltnSttsList.clear();
                    break;
                case DbmsJobType.DATA_TYPE_FRCS:
                    sType = "FRCS";
                    if (data.getData() == null) {
                        log.warn("DbmsJobProcess.process: FRCS data is NULL");
                        break;
                    }
                    List<FrcsZoneSttsVo> frcsZoneSttsList = ((List<FrcsZoneSttsVo>)data.getData()).stream().collect(Collectors.toList());
                    count = frcsZoneSttsList.size();
//                    for (ii = 0; ii < count; ii++) {
//                        // java.util.ConcurrentModificationException
//                        this.frcsMapper.updateFrcsZoneStts(frcsZoneSttsList.get(ii));
//                    }
                    for (FrcsZoneSttsVo vo : frcsZoneSttsList) {
                        this.frcsMapper.updateFrcsZoneStts(vo);
                    }
                    frcsZoneSttsList.clear();
                    break;
                default:
                    log.error("DbmsJobProcess.process: Unknown Data Type: {}", type);
                    break;
            }
        } catch (Exception e) {
            log.error("DbmsJobProcess.process: Exception: {}, {}, {} EA. {}", type, sType, count, e.toString());
        }
        log.info("DbmsJobProcess.process: ..end: {}, {} EA, {} ms. {}", sType, count, elapsed.milliSeconds(), Thread.currentThread().getName());
    }

}
