package com.sptg.ad.biz.receiver;

import com.sptg.ad.api.entity.DeviceAdvertStatistics;
import com.sptg.ad.biz.service.DeviceAdvertStatisticsService;
import com.sptg.device.api.dto.DeviceDto;
import com.sptg.device.api.stream.DeviceAddReceiverInterface;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *            
 *               
 * description:
 */
@Component
@Slf4j
@RequiredArgsConstructor
@EnableBinding(value = { DeviceAddReceiverInterface.class})
public class DeviceAddReceiver {

    private final DeviceAdvertStatisticsService deviceAdvertStatisticsService;

    @StreamListener(value = DeviceAddReceiverInterface.INPUT)
    public void input(DeviceDto dto){
        String month = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMM"));
        List<DeviceAdvertStatistics> statisticsList = new ArrayList<>(2);
        Arrays.asList(new Integer[]{ 1,2 })
                .forEach(taskType -> {
                    DeviceAdvertStatistics statistics = new DeviceAdvertStatistics();
                    statistics.setTaskType(taskType);
                    statistics.setPublishCount(0L);
                    statistics.setAdCount(0);
                    statistics.setBrowseCount(0L);
                    statistics.setLastMonthBrowse(0);
                    statistics.setThisMonthBrowse(0);
                    statistics.setWeek1Browse(0);
                    statistics.setWeek2Browse(0);
                    statistics.setWeek3Browse(0);
                    statistics.setWeek4Browse(0);
                    statistics.setXqId(dto.getXqId());
                    statistics.setEarlyWarningCount(0);
                    statistics.setOrgPosition(dto.getOrgPosition());
                    statistics.setDeviceId(dto.getId());
                    statistics.setDeviceName(dto.getName());
                    statistics.setDeviceSerialNumber(dto.getSerialNumber());
                    statistics.setDeviceInstallPath(dto.getInstallPath());
                    statistics.setRegionProvince(dto.getRegionProvince());
                    statistics.setRegionCity(dto.getRegionCity());
                    statistics.setRegionArea(dto.getRegionArea());
                    statistics.setCreateTime(month);
                    statisticsList.add(statistics);
                });
        this.deviceAdvertStatisticsService.insertBatch(statisticsList);
    }

}
