package com.nokia.alarm.service;

import java.util.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nokia.alarm.entity.ResultMap;
import com.nokia.alarm.mapper.PgResultMapDao;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class ResultMapService {

    @Autowired
    private PgResultMapDao pgResultMapDao;

    // @Autowired
    // private GpResultMapDao gpResultMapDao;

    public void pgBatchInsert(List<ResultMap> resultMaps, Date date) {
        long start = System.currentTimeMillis();
        List<Object[]> objects = new ArrayList<>();
        for (ResultMap resultMap : resultMaps) {
            objects.add(tObjects(resultMap, date));
        }
        pgResultMapDao.batchInsert(objects);
        log.info("批量插入PG数据库耗时: {}ms", System.currentTimeMillis() - start);
    }

    // public void gpBatchInsert(List<ResultMap> resultMaps, Date date) {
    //     long start = System.currentTimeMillis();
    //     List<Object[]> objects = new ArrayList<>();
    //     for (ResultMap resultMap : resultMaps) {
    //         objects.add(tObjects(resultMap, date));
    //     }
    //     gpResultMapDao.batchInsert(objects);
    //     log.info("批量插入GP数据库耗时: {}ms", System.currentTimeMillis() - start);
    // }

    private Object[] tObjects(ResultMap resultMap, Date date) {
        DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
        String sdate = format2.format(date);
        return new Object[] {
                Timestamp.valueOf(sdate),
                resultMap.getUniqueId(),
                resultMap.getUniqueClearId(),
                resultMap.getEventTime(),
                resultMap.getCancelTime(),
                resultMap.getStatus(),
                resultMap.getProjectStatus(),
                resultMap.getVendorAlarmSerial(),
                resultMap.getAlarmTitle(),
                resultMap.getVendorSeverity(),
                resultMap.getVendorAlarmType(),
                resultMap.getSpecificProblem(),
                resultMap.getSpecificProblemID(),
                resultMap.getEquipmentClass(),
                resultMap.getEquipmentName(),
                resultMap.getLocateNeClass(),
                resultMap.getLocateNeName(),
                resultMap.getProvince(),
                resultMap.getRegion(),
                resultMap.getCity(),
                resultMap.getGrid(),
                resultMap.getArea(),
                resultMap.getVendor(),
                resultMap.getSpecialty(),
                resultMap.getSiteId(),
                resultMap.getRoomId(),
                resultMap.getTowerSiteNo(),
                resultMap.getProEquipmentStatus(),
                resultMap.getProjectName(),
                resultMap.getProjectNo(),
                resultMap.getProjectStartTime(),
                resultMap.getProjectEndTime(),
                resultMap.getWorkSendStatus(),
                resultMap.getWorkStatus(),
                resultMap.getWorkNo(),
                resultMap.getWorkArchivedTime(),
                resultMap.getWorkDutyDep(),
                resultMap.getWorkSendPerson(),
                resultMap.getAlarmLogicClass(),
                resultMap.getAlarmLogicSubClass(),
                resultMap.getStandardAlarmName(),
                resultMap.getAlarmSeverity(),
                resultMap.getStandardAlarmId(),
                resultMap.getEffectOnEquipment(),
                resultMap.getEffectOnBusiness(),
                resultMap.getStandardFlag(),
                resultMap.getNetworkType(),
                resultMap.getEquipmentDN(),
                resultMap.getLocateNEDN(),
                resultMap.getTacCode(),
                resultMap.getTacId(),
                resultMap.getAlarmText()
        };
    }
}
